|
| 1 | +package dialer |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "crypto/tls" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "net" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + "strings" |
| 13 | + |
| 14 | + xproxy "golang.org/x/net/proxy" |
| 15 | + |
| 16 | + "github.com/SenseUnit/dumbproxy/tlsutil" |
| 17 | +) |
| 18 | + |
| 19 | +type OptimisticHTTPProxyDialer struct { |
| 20 | + address string |
| 21 | + tlsConfig *tls.Config |
| 22 | + userinfo *url.Userinfo |
| 23 | + next Dialer |
| 24 | +} |
| 25 | + |
| 26 | +func NewOptimisticHTTPProxyDialer(address string, tlsConfig *tls.Config, userinfo *url.Userinfo, next LegacyDialer) *OptimisticHTTPProxyDialer { |
| 27 | + return &OptimisticHTTPProxyDialer{ |
| 28 | + address: address, |
| 29 | + tlsConfig: tlsConfig, |
| 30 | + next: MaybeWrapWithContextDialer(next), |
| 31 | + userinfo: userinfo, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func OptimisticHTTPProxyDialerFromURL(u *url.URL, next xproxy.Dialer) (xproxy.Dialer, error) { |
| 36 | + host := u.Hostname() |
| 37 | + port := u.Port() |
| 38 | + |
| 39 | + var tlsConfig *tls.Config |
| 40 | + var err error |
| 41 | + switch strings.ToLower(u.Scheme) { |
| 42 | + case "http+optimistic": |
| 43 | + if port == "" { |
| 44 | + port = "80" |
| 45 | + } |
| 46 | + case "https+optimistic": |
| 47 | + if port == "" { |
| 48 | + port = "443" |
| 49 | + } |
| 50 | + tlsConfig, err = tlsutil.TLSConfigFromURL(u) |
| 51 | + if err != nil { |
| 52 | + return nil, fmt.Errorf("TLS configuration failed: %w", err) |
| 53 | + } |
| 54 | + default: |
| 55 | + return nil, errors.New("unsupported proxy type") |
| 56 | + } |
| 57 | + |
| 58 | + address := net.JoinHostPort(host, port) |
| 59 | + |
| 60 | + return NewOptimisticHTTPProxyDialer(address, tlsConfig, u.User, next), nil |
| 61 | +} |
| 62 | + |
| 63 | +func (d *OptimisticHTTPProxyDialer) Dial(network, address string) (net.Conn, error) { |
| 64 | + return d.DialContext(context.Background(), network, address) |
| 65 | +} |
| 66 | + |
| 67 | +func (d *OptimisticHTTPProxyDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) { |
| 68 | + switch network { |
| 69 | + case "tcp", "tcp4", "tcp6": |
| 70 | + default: |
| 71 | + return nil, errors.New("only \"tcp\" network is supported") |
| 72 | + } |
| 73 | + conn, err := d.next.DialContext(ctx, "tcp", d.address) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("proxy dialer is unable to make connection: %w", err) |
| 76 | + } |
| 77 | + if d.tlsConfig != nil { |
| 78 | + conn = tls.Client(conn, d.tlsConfig) |
| 79 | + } |
| 80 | + |
| 81 | + return &futureH1ProxiedConn{ |
| 82 | + Conn: conn, |
| 83 | + address: address, |
| 84 | + userinfo: d.userinfo, |
| 85 | + }, nil |
| 86 | +} |
| 87 | + |
| 88 | +type futureH1ProxiedConn struct { |
| 89 | + net.Conn |
| 90 | + address string |
| 91 | + userinfo *url.Userinfo |
| 92 | + rDone bool |
| 93 | + wDone bool |
| 94 | + rErr error |
| 95 | + wErr error |
| 96 | +} |
| 97 | + |
| 98 | +func (c *futureH1ProxiedConn) Write(b []byte) (n int, err error) { |
| 99 | + if c.wErr != nil { |
| 100 | + return 0, c.wErr |
| 101 | + } |
| 102 | + if !c.wDone { |
| 103 | + buf := new(bytes.Buffer) |
| 104 | + fmt.Fprintf(buf, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n", c.address, c.address) |
| 105 | + if c.userinfo != nil { |
| 106 | + fmt.Fprintf(buf, "Proxy-Authorization: %s\r\n", basicAuthHeader(c.userinfo)) |
| 107 | + } |
| 108 | + fmt.Fprintf(buf, "User-Agent: dumbproxy\r\n\r\n") |
| 109 | + prologueBytes := buf.Len() |
| 110 | + buf.Write(b) |
| 111 | + n, err := c.Conn.Write(buf.Bytes()) |
| 112 | + if err != nil { |
| 113 | + c.wErr = err |
| 114 | + } |
| 115 | + c.wDone = true |
| 116 | + c.address = "" |
| 117 | + c.userinfo = nil |
| 118 | + return max(0, n-prologueBytes), err |
| 119 | + } |
| 120 | + return c.Conn.Write(b) |
| 121 | +} |
| 122 | + |
| 123 | +func (c *futureH1ProxiedConn) Read(b []byte) (n int, err error) { |
| 124 | + if c.rErr != nil { |
| 125 | + return 0, c.rErr |
| 126 | + } |
| 127 | + if !c.rDone { |
| 128 | + resp, err := readResponse(c.Conn) |
| 129 | + if err != nil { |
| 130 | + c.rErr = fmt.Errorf("reading proxy response failed: %w", err) |
| 131 | + return 0, c.rErr |
| 132 | + } |
| 133 | + if resp.StatusCode != http.StatusOK { |
| 134 | + c.rErr = fmt.Errorf("bad status code from proxy: %d", resp.StatusCode) |
| 135 | + return 0, c.rErr |
| 136 | + } |
| 137 | + c.rDone = true |
| 138 | + } |
| 139 | + return c.Conn.Read(b) |
| 140 | +} |
0 commit comments