tss/proxy/proxy.go

56 lines
1.3 KiB
Go
Raw Normal View History

2022-04-29 13:38:49 -04:00
package proxy
import (
"fmt"
"net/http"
"net/url"
"golang.org/x/net/proxy"
)
2022-05-01 23:28:54 -04:00
func GetTransport(proxy string) (http.RoundTripper, error) {
2022-05-01 19:13:27 -04:00
if proxy == "" {
2022-05-01 23:28:54 -04:00
return http.DefaultTransport, nil
2022-05-01 19:13:27 -04:00
}
2022-04-29 13:38:49 -04:00
proxyUrl, err := url.Parse(proxy)
if err != nil {
return nil, fmt.Errorf("failed to parse proxyUrl, url:%s, err: %w", proxy, err)
}
if proxyUrl.Host == "unix" && proxyUrl.Scheme == "socks5" &&
len(proxyUrl.Path) > 1 /* Path cannot be empty or just / */ {
return unixSocks5Proxy(proxyUrl.Path)
}
return proxyHttp(proxyUrl)
}
2022-05-01 23:28:54 -04:00
func unixSocks5Proxy(path string) (http.RoundTripper, error) {
2022-04-29 13:38:49 -04:00
// TODO: Auth?
dialer, err := proxy.SOCKS5("unix", path, nil /*auth*/, nil)
if err != nil {
return nil, fmt.Errorf("failed to make socks proxy, path: %s, err: %w", path, err)
}
ctxDialer, ok := dialer.(proxy.ContextDialer)
if !ok {
2022-05-01 19:13:27 -04:00
panic("proxy.SOCKS5 did not return a ContextDialer")
2022-04-29 13:38:49 -04:00
}
2022-05-01 19:13:27 -04:00
trans := defaultTransport()
trans.DialContext = ctxDialer.DialContext
trans.Proxy = nil
2022-05-01 23:28:54 -04:00
return trans, nil
2022-04-29 13:38:49 -04:00
}
2022-05-01 23:28:54 -04:00
func proxyHttp(proxyUrl *url.URL) (http.RoundTripper, error) {
2022-05-01 19:13:27 -04:00
trans := defaultTransport()
trans.Proxy = http.ProxyURL(proxyUrl)
2022-05-01 23:28:54 -04:00
return trans, nil
2022-05-01 19:13:27 -04:00
}
func defaultTransport() *http.Transport {
transPtr, ok := http.DefaultTransport.(*http.Transport)
if !ok {
panic("http.DefaultTransport is not a *http.Transport")
}
trans := transPtr.Clone()
return trans
2022-04-29 13:38:49 -04:00
}