|
| 1 | +package proxify |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/base64" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + |
| 11 | + rbtransport "github.com/projectdiscovery/roundrobin/transport" |
| 12 | + "golang.org/x/net/proxy" |
| 13 | +) |
| 14 | + |
| 15 | +type httpProxyDialer struct { |
| 16 | + proxyURL *url.URL |
| 17 | + forward proxy.Dialer |
| 18 | +} |
| 19 | + |
| 20 | +// Dial connects to the address using the HTTP proxy. |
| 21 | +func (d *httpProxyDialer) Dial(_, addr string) (net.Conn, error) { |
| 22 | + conn, err := d.forward.Dial("tcp", d.proxyURL.Host) |
| 23 | + if err != nil { |
| 24 | + return nil, err |
| 25 | + } |
| 26 | + |
| 27 | + connectReq := &http.Request{ |
| 28 | + Method: "CONNECT", |
| 29 | + URL: &url.URL{Opaque: addr}, |
| 30 | + Host: addr, |
| 31 | + Header: make(http.Header), |
| 32 | + } |
| 33 | + if d.proxyURL.User != nil { |
| 34 | + encodedUserinfo := base64.StdEncoding.EncodeToString([]byte(d.proxyURL.User.String())) |
| 35 | + connectReq.Header.Set("Proxy-Authorization", "Basic "+encodedUserinfo) |
| 36 | + } |
| 37 | + |
| 38 | + if err := connectReq.Write(conn); err != nil { |
| 39 | + _ = conn.Close() |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + |
| 43 | + br := bufio.NewReader(conn) |
| 44 | + resp, err := http.ReadResponse(br, connectReq) |
| 45 | + if err != nil { |
| 46 | + _ = conn.Close() |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + if resp.StatusCode != http.StatusOK { |
| 50 | + _ = conn.Close() |
| 51 | + return nil, fmt.Errorf("unexpected response from proxy: %s", resp.Status) |
| 52 | + } |
| 53 | + |
| 54 | + return conn, nil |
| 55 | +} |
| 56 | + |
| 57 | +type httpProxyRoundRobinDialer struct { |
| 58 | + proxyDialers map[string]httpProxyDialer |
| 59 | + transport *rbtransport.RoundTransport |
| 60 | +} |
| 61 | + |
| 62 | +// Dial connects to the address on the named network via one of the HTTP proxies using round-robin scheduling. |
| 63 | +func (d *httpProxyRoundRobinDialer) Dial(network, addr string) (net.Conn, error) { |
| 64 | + nextProxyURL := d.transport.Next() |
| 65 | + dialer, ok := d.proxyDialers[nextProxyURL] |
| 66 | + if !ok { |
| 67 | + return nil, fmt.Errorf("no matching proxy dialer found") |
| 68 | + } |
| 69 | + return dialer.Dial(network, addr) |
| 70 | +} |
| 71 | + |
| 72 | +func newHTTPProxyRoundRobinDialer(upstreamProxies []string) (proxy.Dialer, error) { |
| 73 | + if len(upstreamProxies) == 0 { |
| 74 | + return nil, fmt.Errorf("proxy URLs cannot be empty") |
| 75 | + } |
| 76 | + |
| 77 | + proxyURLs := make([]*url.URL, 0, len(upstreamProxies)) |
| 78 | + dialers := make(map[string]httpProxyDialer) |
| 79 | + for _, proxyAddr := range upstreamProxies { |
| 80 | + proxyURL, err := url.Parse(proxyAddr) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + proxyURLs = append(proxyURLs, proxyURL) |
| 85 | + dialer := httpProxyDialer{proxyURL: proxyURL, forward: proxy.Direct} |
| 86 | + dialers[proxyURL.String()] = dialer |
| 87 | + } |
| 88 | + |
| 89 | + robin, err := rbtransport.NewWithOptions(1, toStringSlice(proxyURLs)...) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + return &httpProxyRoundRobinDialer{proxyDialers: dialers, transport: robin}, nil |
| 95 | +} |
| 96 | + |
| 97 | +type socks5ProxyRoundRobinDialer struct { |
| 98 | + proxyDialers map[string]proxy.Dialer |
| 99 | + robin *rbtransport.RoundTransport |
| 100 | +} |
| 101 | + |
| 102 | +// Dial connects to the address on the named network via one of the SOCKS5 proxies using round-robin scheduling. |
| 103 | +func (d *socks5ProxyRoundRobinDialer) Dial(network, addr string) (net.Conn, error) { |
| 104 | + nextProxyURL := d.robin.Next() |
| 105 | + dialer, ok := d.proxyDialers[nextProxyURL] |
| 106 | + if !ok { |
| 107 | + return nil, fmt.Errorf("no matching proxy dialer found") |
| 108 | + } |
| 109 | + return dialer.Dial(network, addr) |
| 110 | +} |
| 111 | + |
| 112 | +func newSOCKS5ProxyRoundRobinDialer(upstreamProxies []string) (proxy.Dialer, error) { |
| 113 | + if len(upstreamProxies) == 0 { |
| 114 | + return nil, fmt.Errorf("proxy URLs cannot be empty") |
| 115 | + } |
| 116 | + |
| 117 | + proxyURLs := make([]*url.URL, 0, len(upstreamProxies)) |
| 118 | + dialers := make(map[string]proxy.Dialer) |
| 119 | + for _, proxyAddr := range upstreamProxies { |
| 120 | + proxyURL, err := url.Parse(proxyAddr) |
| 121 | + if err != nil { |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + proxyURLs = append(proxyURLs, proxyURL) |
| 125 | + var auth *proxy.Auth |
| 126 | + if proxyURL.User != nil { |
| 127 | + password, _ := proxyURL.User.Password() |
| 128 | + auth = &proxy.Auth{ |
| 129 | + User: proxyURL.User.Username(), |
| 130 | + Password: password, |
| 131 | + } |
| 132 | + } |
| 133 | + dialer, err := proxy.SOCKS5("tcp", proxyURL.Host, auth, proxy.Direct) |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + dialers[proxyAddr] = dialer |
| 138 | + } |
| 139 | + |
| 140 | + robin, err := rbtransport.NewWithOptions(1, toStringSlice(proxyURLs)...) |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + |
| 145 | + return &socks5ProxyRoundRobinDialer{proxyDialers: dialers, robin: robin}, nil |
| 146 | +} |
| 147 | + |
| 148 | +func toStringSlice(urls []*url.URL) []string { |
| 149 | + s := make([]string, len(urls)) |
| 150 | + for i, u := range urls { |
| 151 | + s[i] = u.String() |
| 152 | + } |
| 153 | + return s |
| 154 | +} |
0 commit comments