Skip to content

Commit

Permalink
Allow options in SetProxy and SetProxyAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed May 20, 2024
1 parent d900d6f commit 81f8f07
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,23 +256,23 @@ func NewClient(deviceStore *store.Device, log waLog.Logger) *Client {
// SetProxyAddress is a helper method that parses a URL string and calls SetProxy or SetSOCKSProxy based on the URL scheme.
//
// Returns an error if url.Parse fails to parse the given address.
func (cli *Client) SetProxyAddress(addr string) error {
func (cli *Client) SetProxyAddress(addr string, opts ...SetProxyOptions) error {
if addr == "" {
cli.SetProxy(nil)
cli.SetProxy(nil, opts...)
return nil
}
parsed, err := url.Parse(addr)
if err != nil {
return err
}
if parsed.Scheme == "http" || parsed.Scheme == "https" {
cli.SetProxy(http.ProxyURL(parsed))
cli.SetProxy(http.ProxyURL(parsed), opts...)
} else if parsed.Scheme == "socks5" {
px, err := proxy.FromURL(parsed, proxy.Direct)
if err != nil {
return err
}
cli.SetSOCKSProxy(px)
cli.SetSOCKSProxy(px, opts...)
} else {
return fmt.Errorf("unsupported proxy scheme %q", parsed.Scheme)
}
Expand Down Expand Up @@ -301,13 +301,21 @@ type Proxy = func(*http.Request) (*url.URL, error)
// return mediaProxyURL, nil
// }
// })
func (cli *Client) SetProxy(proxy Proxy) {
cli.proxy = proxy
cli.socksProxy = nil
transport := cli.http.Transport.(*http.Transport)
transport.Proxy = proxy
transport.Dial = nil
transport.DialContext = nil
func (cli *Client) SetProxy(proxy Proxy, opts ...SetProxyOptions) {
var opt SetProxyOptions
if len(opts) > 0 {
opt = opts[0]
}
if !opt.NoWebsocket {
cli.proxy = proxy
cli.socksProxy = nil
}
if !opt.NoMedia {
transport := cli.http.Transport.(*http.Transport)
transport.Proxy = proxy
transport.Dial = nil
transport.DialContext = nil
}
}

type SetProxyOptions struct {
Expand Down

0 comments on commit 81f8f07

Please sign in to comment.