mirror of https://github.com/gorilla/websocket.git
Fix socks5
This commit is contained in:
parent
76d8f8c503
commit
4480c3df51
29
client.go
29
client.go
|
@ -283,7 +283,27 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getDefaultDialerFunc := func() (netDialerFunc, error) {
|
||||||
|
dialer, err := proxy.FromURL(proxyURL, netDial)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if d, ok := dialer.(proxy.ContextDialer); ok {
|
||||||
|
return d.DialContext, nil
|
||||||
|
} else {
|
||||||
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
|
return dialer.Dial(network, addr)
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
|
case proxyURL.Scheme == "socks5":
|
||||||
|
netDial, err = getDefaultDialerFunc()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
case proxyURL == nil:
|
case proxyURL == nil:
|
||||||
// Do nothing. Not using a proxy.
|
// Do nothing. Not using a proxy.
|
||||||
case u.Scheme == "http":
|
case u.Scheme == "http":
|
||||||
|
@ -296,17 +316,10 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
|
||||||
case u.Scheme == "https":
|
case u.Scheme == "https":
|
||||||
netDial = (&httpsProxyDialer{proxyURL: proxyURL, forwardDial: netDial}).DialContext
|
netDial = (&httpsProxyDialer{proxyURL: proxyURL, forwardDial: netDial}).DialContext
|
||||||
default:
|
default:
|
||||||
dialer, err := proxy.FromURL(proxyURL, netDial)
|
netDial, err = getDefaultDialerFunc()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
if d, ok := dialer.(proxy.ContextDialer); ok {
|
|
||||||
netDial = d.DialContext
|
|
||||||
} else {
|
|
||||||
netDial = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
||||||
return dialer.Dial(network, addr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -173,24 +173,19 @@ func TestProxyDial(t *testing.T) {
|
||||||
cstDialer := cstDialer // make local copy for modification on next line.
|
cstDialer := cstDialer // make local copy for modification on next line.
|
||||||
cstDialer.Proxy = http.ProxyURL(surl)
|
cstDialer.Proxy = http.ProxyURL(surl)
|
||||||
|
|
||||||
connect := false
|
|
||||||
origHandler := s.Server.Config.Handler
|
origHandler := s.Server.Config.Handler
|
||||||
|
|
||||||
// Capture the request Host header.
|
// Capture the request Host header.
|
||||||
s.Server.Config.Handler = http.HandlerFunc(
|
s.Server.Config.Handler = http.HandlerFunc(
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == http.MethodConnect {
|
if r.Method == http.MethodConnect {
|
||||||
connect = true
|
// HTTPS_PROXY comes here.
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !connect {
|
// HTTP_PROXY comes here.
|
||||||
t.Log("connect not received")
|
|
||||||
http.Error(w, "connect not received", http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
origHandler.ServeHTTP(w, r)
|
origHandler.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
})
|
})
|
||||||
|
|
||||||
ws, _, err := cstDialer.Dial(s.URL, nil)
|
ws, _, err := cstDialer.Dial(s.URL, nil)
|
||||||
|
@ -211,7 +206,6 @@ func TestProxyAuthorizationDial(t *testing.T) {
|
||||||
cstDialer := cstDialer // make local copy for modification on next line.
|
cstDialer := cstDialer // make local copy for modification on next line.
|
||||||
cstDialer.Proxy = http.ProxyURL(surl)
|
cstDialer.Proxy = http.ProxyURL(surl)
|
||||||
|
|
||||||
connect := false
|
|
||||||
origHandler := s.Server.Config.Handler
|
origHandler := s.Server.Config.Handler
|
||||||
|
|
||||||
// Capture the request Host header.
|
// Capture the request Host header.
|
||||||
|
@ -219,17 +213,22 @@ func TestProxyAuthorizationDial(t *testing.T) {
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
proxyAuth := r.Header.Get("Proxy-Authorization")
|
proxyAuth := r.Header.Get("Proxy-Authorization")
|
||||||
expectedProxyAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte("username:password"))
|
expectedProxyAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte("username:password"))
|
||||||
if r.Method == http.MethodConnect && proxyAuth == expectedProxyAuth {
|
if proxyAuth != expectedProxyAuth {
|
||||||
connect = true
|
msg := fmt.Sprintf("expected proxy authorization is %q, but %q is given", expectedProxyAuth, proxyAuth)
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
|
t.Log(msg)
|
||||||
|
http.Error(
|
||||||
|
w,
|
||||||
|
msg,
|
||||||
|
http.StatusProxyAuthRequired,
|
||||||
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !connect {
|
if r.Method == http.MethodConnect {
|
||||||
t.Log("connect with proxy authorization not received")
|
w.WriteHeader(http.StatusOK)
|
||||||
http.Error(w, "connect with proxy authorization not received", http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
origHandler.ServeHTTP(w, r)
|
origHandler.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue