-
Notifications
You must be signed in to change notification settings - Fork 95
Demo: Feat/proxy connection settings #389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 commits
b3bc57b
f066472
a5a3e01
60a3700
d7bd0cc
37536d8
c5821fd
7a63e7f
80f55b4
f674b36
ff41d86
05a6fb4
93b8cab
e74c421
5226bf8
a212d77
4e0a004
3903ce7
bd7c116
03de983
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,15 +2,19 @@ package client | |||||||
|
|
||||||||
| import ( | ||||||||
| "context" | ||||||||
| "crypto/tls" | ||||||||
| "errors" | ||||||||
| "net" | ||||||||
| "net/http" | ||||||||
| "net/url" | ||||||||
| "strings" | ||||||||
| "sync" | ||||||||
| "sync/atomic" | ||||||||
| "time" | ||||||||
|
|
||||||||
| "github.com/cenkalti/backoff/v4" | ||||||||
| "github.com/gorilla/websocket" | ||||||||
| dialer "github.com/michel-laterman/proxy-connect-dialer-go" | ||||||||
|
|
||||||||
| "github.com/open-telemetry/opamp-go/client/internal" | ||||||||
| "github.com/open-telemetry/opamp-go/client/types" | ||||||||
|
|
@@ -81,6 +85,12 @@ func (c *wsClient) Start(ctx context.Context, settings types.StartSettings) erro | |||||||
| // Prepare connection settings. | ||||||||
| c.dialer = *websocket.DefaultDialer | ||||||||
|
|
||||||||
| if settings.ProxyURL != "" { | ||||||||
| if err := c.useProxy(settings.ProxyURL, settings.ProxyHeaders, settings.TLSConfig); err != nil { | ||||||||
| return err | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| var err error | ||||||||
| c.url, err = url.Parse(settings.OpAMPServerURL) | ||||||||
| if err != nil { | ||||||||
|
|
@@ -419,3 +429,52 @@ func (c *wsClient) runUntilStopped(ctx context.Context) { | |||||||
| c.runOneCycle(ctx) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // useProxy sets the websocket dialer to use the passed proxy URL. | ||||||||
| // If the proxy has no schema http is used. | ||||||||
| // This method is not thread safe and must be called before c.dialer is used. | ||||||||
| func (c *wsClient) useProxy(proxy string, headers http.Header, cfg *tls.Config) error { | ||||||||
| proxyURL, err := url.Parse(proxy) | ||||||||
| if err != nil || proxyURL.Scheme == "" || proxyURL.Host == "" { // error or bad URL - try to use http as scheme to resolve | ||||||||
| proxyURL, err = url.Parse("http://" + proxy) | ||||||||
| if err != nil { | ||||||||
| return err | ||||||||
| } | ||||||||
| } | ||||||||
| if proxyURL.Hostname() == "" { | ||||||||
| return url.InvalidHostError(proxy) | ||||||||
| } | ||||||||
|
|
||||||||
| // Clear previous settings | ||||||||
| c.dialer.Proxy = nil | ||||||||
| c.dialer.NetDialContext = nil | ||||||||
| c.dialer.NetDialTLSContext = nil | ||||||||
|
|
||||||||
| switch strings.ToLower(proxyURL.Scheme) { | ||||||||
| case "http": | ||||||||
| // FIXME: dialer.NetDialContext is currently used as a work around instead of setting dialer.Proxy as gorilla/websockets does not have 1st class support for setting proxy connect headers | ||||||||
| // Once https://github.com/gorilla/websocket/issues/479 is complete, we should use dialer.Proxy, and dialer.ProxyConnectHeader | ||||||||
| if len(headers) > 0 { | ||||||||
| dialer, err := dialer.NewProxyConnectDialer(proxyURL, &net.Dialer{}, dialer.WithProxyConnectHeaders(headers)) | ||||||||
| if err != nil { | ||||||||
| return err | ||||||||
| } | ||||||||
| c.dialer.NetDialContext = dialer.DialContext | ||||||||
| return nil | ||||||||
| } | ||||||||
| c.dialer.Proxy = http.ProxyURL(proxyURL) // No connect headers, use a regular proxy | ||||||||
| case "https": | ||||||||
| if len(headers) > 0 { | ||||||||
| dialer, err := dialer.NewProxyConnectDialer(proxyURL, &net.Dialer{}, dialer.WithTLS(cfg), dialer.WithProxyConnectHeaders(headers)) | ||||||||
| if err != nil { | ||||||||
| return err | ||||||||
| } | ||||||||
| c.dialer.NetDialTLSContext = dialer.DialContext | ||||||||
| return nil | ||||||||
| } | ||||||||
| c.dialer.Proxy = http.ProxyURL(proxyURL) // No connect headers, use a regular proxy | ||||||||
|
Comment on lines
+461
to
+482
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a little messy (as we mix setting the dialer.Proxy and dialer.NetDial*Context methods); but it's much easer to do at this point in time then it is to replace the entire websockets library. If gorilla/websocket#988 is ever merged and released we can clean this up. I've tested this locally using tinyproxy. Steps to test:
ProxyURL: "http://localhost:8888", // tinyproxy's default address
ProxyHeaders: http.Header{"test-proxy-key": []string{"test-val"}},
|
||||||||
| default: // catches socks5 | ||||||||
| c.dialer.Proxy = http.ProxyURL(proxyURL) | ||||||||
| } | ||||||||
| return nil | ||||||||
| } | ||||||||
Uh oh!
There was an error while loading. Please reload this page.