-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
015dbaf
commit 66e60ea
Showing
7 changed files
with
186 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/url" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/btccom/connectproxy" | ||
"golang.org/x/net/proxy" | ||
) | ||
|
||
type Dialer interface { | ||
// Dial connects to the given address. | ||
Dial(network, addr string) (net.Conn, error) | ||
} | ||
|
||
func GetProxyURLFromEnv() (url string) { | ||
url = os.Getenv("ALL_PROXY") | ||
if len(url) < 1 { | ||
url = os.Getenv("all_proxy") | ||
} | ||
if len(url) < 1 { | ||
url = os.Getenv("HTTPS_PROXY") | ||
} | ||
if len(url) < 1 { | ||
url = os.Getenv("https_proxy") | ||
} | ||
if len(url) < 1 { | ||
url = os.Getenv("HTTP_PROXY") | ||
} | ||
if len(url) < 1 { | ||
url = os.Getenv("http_proxy") | ||
} | ||
return | ||
} | ||
|
||
func RegularProxyURL(url string) string { | ||
if len(url) < 1 { | ||
return url | ||
} | ||
url = strings.TrimSpace(url) | ||
pos := strings.Index(url, "://") | ||
|
||
var protocol, address string | ||
if pos < 0 { | ||
protocol = "http" | ||
address = url | ||
} else { | ||
protocol = strings.ToLower(url[:pos]) | ||
address = url[pos+3:] | ||
} | ||
|
||
switch protocol { | ||
case "": | ||
protocol = "http" | ||
case "socks4": | ||
fallthrough | ||
case "socks4a": | ||
fallthrough | ||
case "socks5": | ||
protocol = "socks" | ||
} | ||
return fmt.Sprintf("%s://%s", protocol, address) | ||
} | ||
|
||
func GetProxyDialer(proxyURL string, timeout time.Duration, insecureSkipVerify bool) (dailer Dialer, err error) { | ||
proxyURL = RegularProxyURL(proxyURL) | ||
u, err := url.Parse(proxyURL) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if u.Scheme == "socks" { | ||
var auth proxy.Auth | ||
auth.User = u.User.Username() | ||
auth.Password, _ = u.User.Password() | ||
dailer, err = proxy.SOCKS5("tcp", u.Host, &auth, &net.Dialer{ | ||
Timeout: timeout, | ||
}) | ||
return | ||
} | ||
|
||
if u.Scheme == "http" || u.Scheme == "https" { | ||
dailer, err = connectproxy.NewWithConfig( | ||
u, | ||
&net.Dialer{ | ||
Timeout: timeout, | ||
}, | ||
&connectproxy.Config{ | ||
InsecureSkipVerify: insecureSkipVerify, | ||
DialTimeout: timeout, | ||
}, | ||
) | ||
return | ||
} | ||
|
||
if len(proxyURL) > 0 { | ||
err = fmt.Errorf("unknown proxy scheme '%s'", u.Scheme) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
github.com/bits-and-blooms/bitset v1.2.1 h1:M+/hrU9xlMp7t4TyTDQW97d3tRPVuKFC6zBEK16QnXY= | ||
github.com/bits-and-blooms/bitset v1.2.1/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA= | ||
github.com/btccom/connectproxy v0.0.0-20200725203833-3582e84f0c9b h1:ADw0dUto49/UxuLGZ+TyD8luQoKO2NUWFP6Y41htmRM= | ||
github.com/btccom/connectproxy v0.0.0-20200725203833-3582e84f0c9b/go.mod h1:KqKyCk7zguFVUJUad6BqC+zE6L8Yqd0tzr1uK9TAFlU= | ||
github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ= | ||
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4= | ||
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9 h1:0qxwC5n+ttVOINCBeRHO0nq9X7uy8SDsPoi5OaCdIEI= | ||
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |