improve proxy
This commit is contained in:
		@@ -1,7 +1,9 @@
 | 
			
		||||
package proxy
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"net"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
 | 
			
		||||
@@ -20,12 +22,28 @@ func GetTransport(proxy string) (http.RoundTripper, error) {
 | 
			
		||||
		len(proxyUrl.Path) > 1 /* Path cannot be empty or just / */ {
 | 
			
		||||
		return unixSocks5Proxy(proxyUrl.Path)
 | 
			
		||||
	}
 | 
			
		||||
	return proxyHttp(proxyUrl)
 | 
			
		||||
	return proxyTcp(proxyUrl)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type forwardDialer struct {
 | 
			
		||||
	dialContext func(ctx context.Context, network, address string) (net.Conn, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (d *forwardDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
 | 
			
		||||
	return d.dialContext(ctx, network, address)
 | 
			
		||||
}
 | 
			
		||||
func (d *forwardDialer) Dial(network, address string) (net.Conn, error) {
 | 
			
		||||
	panic("Dial should not be called")
 | 
			
		||||
	//default Dial is nil
 | 
			
		||||
	//return defaultTransport().Dial(network, address)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func unixSocks5Proxy(path string) (http.RoundTripper, error) {
 | 
			
		||||
	// TODO: Auth?
 | 
			
		||||
	dialer, err := proxy.SOCKS5("unix", path, nil /*auth*/, nil)
 | 
			
		||||
	trans := defaultTransport().Clone()
 | 
			
		||||
	if trans.DialContext == nil {
 | 
			
		||||
		panic("DefaultTransport has nil DialContext")
 | 
			
		||||
	}
 | 
			
		||||
	dialer, err := proxy.SOCKS5("unix", path, nil /*auth*/, &forwardDialer{trans.DialContext})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("failed to make socks proxy, path: %s, err: %w", path, err)
 | 
			
		||||
	}
 | 
			
		||||
@@ -33,14 +51,13 @@ func unixSocks5Proxy(path string) (http.RoundTripper, error) {
 | 
			
		||||
	if !ok {
 | 
			
		||||
		panic("proxy.SOCKS5 did not return a ContextDialer")
 | 
			
		||||
	}
 | 
			
		||||
	trans := defaultTransport()
 | 
			
		||||
	trans.DialContext = ctxDialer.DialContext
 | 
			
		||||
	trans.Proxy = nil
 | 
			
		||||
	return trans, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func proxyHttp(proxyUrl *url.URL) (http.RoundTripper, error) {
 | 
			
		||||
	trans := defaultTransport()
 | 
			
		||||
func proxyTcp(proxyUrl *url.URL) (http.RoundTripper, error) {
 | 
			
		||||
	trans := defaultTransport().Clone()
 | 
			
		||||
	trans.Proxy = http.ProxyURL(proxyUrl)
 | 
			
		||||
	return trans, nil
 | 
			
		||||
}
 | 
			
		||||
@@ -50,6 +67,5 @@ func defaultTransport() *http.Transport {
 | 
			
		||||
	if !ok {
 | 
			
		||||
		panic("http.DefaultTransport is not a *http.Transport")
 | 
			
		||||
	}
 | 
			
		||||
	trans := transPtr.Clone()
 | 
			
		||||
	return trans
 | 
			
		||||
	return transPtr
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user