forked from gorilla/websocket
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from philipatl/tls-proxy
Accept https proxy fixes #10
- Loading branch information
Showing
5 changed files
with
131 additions
and
5 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,52 @@ | ||
//go:build go1.15 | ||
// +build go1.15 | ||
|
||
package websocket | ||
|
||
import ( | ||
"crypto/tls" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestHttpsProxy(t *testing.T) { | ||
|
||
sTLS := newTLSServer(t) | ||
defer sTLS.Close() | ||
s := newServer(t) | ||
defer s.Close() | ||
|
||
surlTLS, _ := url.Parse(sTLS.Server.URL) | ||
|
||
cstDialer := cstDialer // make local copy for modification on next line. | ||
cstDialer.Proxy = http.ProxyURL(surlTLS) | ||
|
||
connect := false | ||
origHandler := sTLS.Server.Config.Handler | ||
|
||
// Capture the request Host header. | ||
sTLS.Server.Config.Handler = http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == "CONNECT" { | ||
connect = true | ||
w.WriteHeader(http.StatusOK) | ||
return | ||
} | ||
|
||
if !connect { | ||
t.Log("connect not received") | ||
http.Error(w, "connect not received", http.StatusMethodNotAllowed) | ||
return | ||
} | ||
origHandler.ServeHTTP(w, r) | ||
}) | ||
|
||
cstDialer.TLSClientConfig = &tls.Config{RootCAs: rootCAs(t, sTLS.Server)} | ||
ws, _, err := cstDialer.Dial(s.URL, nil) | ||
if err != nil { | ||
t.Fatalf("Dial: %v", err) | ||
} | ||
defer ws.Close() | ||
sendRecv(t, ws) | ||
} |
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,37 @@ | ||
//go:build go1.15 | ||
// +build go1.15 | ||
|
||
package websocket | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"net" | ||
"net/url" | ||
) | ||
|
||
func registerDialerHttps() { | ||
proxy_RegisterDialerType("https", func(proxyURL *url.URL, forwardDialer proxy_Dialer) (proxy_Dialer, error) { | ||
fwd := forwardDialer.Dial | ||
if dialerEx, ok := forwardDialer.(proxyDialerEx); !ok || !dialerEx.UsesTLS() { | ||
tlsDialer := &tls.Dialer{ | ||
Config: &tls.Config{}, | ||
NetDialer: &net.Dialer{}, | ||
} | ||
fwd = tlsDialer.Dial | ||
} | ||
return &httpProxyDialer{proxyURL: proxyURL, forwardDial: fwd, usesTLS: true}, nil | ||
}) | ||
} | ||
|
||
func modifyProxyDialer(ctx context.Context, d *Dialer, proxyURL *url.URL, proxyDialer *netDialerFunc) { | ||
if proxyURL.Scheme == "https" { | ||
proxyDialer.usesTLS = true | ||
proxyDialer.fn = func(network, addr string) (net.Conn, error) { | ||
t := tls.Dialer{} | ||
t.Config = d.TLSClientConfig | ||
t.NetDialer = &net.Dialer{} | ||
return t.DialContext(ctx, network, addr) | ||
} | ||
} | ||
} |
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,15 @@ | ||
//go:build !go1.15 | ||
// +build !go1.15 | ||
|
||
package websocket | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
) | ||
|
||
func registerDialerHttps() { | ||
} | ||
|
||
func modifyProxyDialer(ctx context.Context, d *Dialer, proxyURL *url.URL, proxyDialer *netDialerFunc) { | ||
} |