Skip to content

Commit

Permalink
proxy: support socks5h scheme in proxy URL
Browse files Browse the repository at this point in the history
Environment variable 'ALL_PROXY=socks5h://example.com' is commonly
used to specify a SOCKS5 proxy server.
In curl, 'socks5' means the host name will be resolved locally,
and 'socks5h' means the host name will be resolved by the server.

Go SOCKS5 client always uses the server to resolve host names.
So this change just added socks5h as a supported URL scheme.

Fixes golang/go#13454

Change-Id: I06d2b07f66cd0923c114dba4df0f884b39e58bc0
Reviewed-on: https://go-review.googlesource.com/c/156517
Reviewed-by: Brad Fitzpatrick <[email protected]>
Run-TryBot: Brad Fitzpatrick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
crvv authored and bradfitz committed Jan 8, 2019
1 parent be88a9a commit 395948e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 7 additions & 2 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,13 @@ func FromURL(u *url.URL, forward Dialer) (Dialer, error) {
}

switch u.Scheme {
case "socks5":
return SOCKS5("tcp", u.Host, auth, forward)
case "socks5", "socks5h":
addr := u.Hostname()
port := u.Port()
if port == "" {
port = "1080"
}
return SOCKS5("tcp", net.JoinHostPort(addr, port), auth, forward)
}

// If the scheme doesn't match any of the built-in schemes, see if it
Expand Down
2 changes: 2 additions & 0 deletions proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"testing"

"golang.org/x/net/internal/socks"
"golang.org/x/net/internal/sockstest"
)

Expand Down Expand Up @@ -53,6 +54,7 @@ func TestFromEnvironment(t *testing.T) {
{allProxyEnv: "127.0.0.1:8080", noProxyEnv: "localhost, 127.0.0.1", wantTypeOf: direct{}},
{allProxyEnv: "ftp://example.com:8000", noProxyEnv: "localhost, 127.0.0.1", wantTypeOf: direct{}},
{allProxyEnv: "socks5://example.com:8080", noProxyEnv: "localhost, 127.0.0.1", wantTypeOf: &PerHost{}},
{allProxyEnv: "socks5h://example.com", wantTypeOf: &socks.Dialer{}},
{allProxyEnv: "irc://example.com:8000", wantTypeOf: dummyDialer{}},
{noProxyEnv: "localhost, 127.0.0.1", wantTypeOf: direct{}},
{wantTypeOf: direct{}},
Expand Down

0 comments on commit 395948e

Please sign in to comment.