|
| 1 | +package dialer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + "slices" |
| 13 | + "strings" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/SenseUnit/dumbproxy/tlsutil" |
| 17 | + "golang.org/x/net/http2" |
| 18 | + xproxy "golang.org/x/net/proxy" |
| 19 | +) |
| 20 | + |
| 21 | +type H2ProxyDialer struct { |
| 22 | + address string |
| 23 | + tlsConfig *tls.Config |
| 24 | + userinfo *url.Userinfo |
| 25 | + next Dialer |
| 26 | + t *http2.Transport |
| 27 | +} |
| 28 | + |
| 29 | +func H2ProxyDialerFromURL(u *url.URL, next xproxy.Dialer) (xproxy.Dialer, error) { |
| 30 | + host := u.Hostname() |
| 31 | + port := u.Port() |
| 32 | + |
| 33 | + var ( |
| 34 | + tlsConfig *tls.Config |
| 35 | + err error |
| 36 | + h2c bool |
| 37 | + ) |
| 38 | + switch strings.ToLower(u.Scheme) { |
| 39 | + case "h2c": |
| 40 | + if port == "" { |
| 41 | + port = "80" |
| 42 | + } |
| 43 | + h2c = true |
| 44 | + case "h2": |
| 45 | + if port == "" { |
| 46 | + port = "443" |
| 47 | + } |
| 48 | + tlsConfig, err = tlsutil.TLSConfigFromURL(u) |
| 49 | + if !slices.Contains(tlsConfig.NextProtos, "h2") { |
| 50 | + tlsConfig.NextProtos = append([]string{"h2"}, tlsConfig.NextProtos...) |
| 51 | + } |
| 52 | + if err != nil { |
| 53 | + return nil, fmt.Errorf("TLS configuration failed: %w", err) |
| 54 | + } |
| 55 | + default: |
| 56 | + return nil, errors.New("unsupported proxy type") |
| 57 | + } |
| 58 | + |
| 59 | + address := net.JoinHostPort(host, port) |
| 60 | + t := &http2.Transport{ |
| 61 | + AllowHTTP: h2c, |
| 62 | + TLSClientConfig: tlsConfig, |
| 63 | + } |
| 64 | + nextDialer := MaybeWrapWithContextDialer(next) |
| 65 | + if h2c { |
| 66 | + t.DialTLSContext = func(ctx context.Context, network, _ string, _ *tls.Config) (net.Conn, error) { |
| 67 | + return nextDialer.DialContext(ctx, network, address) |
| 68 | + } |
| 69 | + } else { |
| 70 | + t.DialTLSContext = func(ctx context.Context, network, _ string, _ *tls.Config) (net.Conn, error) { |
| 71 | + conn, err := nextDialer.DialContext(ctx, network, address) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + conn = tls.Client(conn, tlsConfig) |
| 76 | + return conn, nil |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return &H2ProxyDialer{ |
| 81 | + address: address, |
| 82 | + tlsConfig: tlsConfig, |
| 83 | + userinfo: u.User, |
| 84 | + next: MaybeWrapWithContextDialer(next), |
| 85 | + t: t, |
| 86 | + }, nil |
| 87 | +} |
| 88 | + |
| 89 | +func (d *H2ProxyDialer) Dial(network, address string) (net.Conn, error) { |
| 90 | + return d.DialContext(context.Background(), network, address) |
| 91 | +} |
| 92 | + |
| 93 | +func (d *H2ProxyDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) { |
| 94 | + h2c := d.tlsConfig == nil |
| 95 | + scheme := "https" |
| 96 | + if h2c { |
| 97 | + scheme = "http" |
| 98 | + } |
| 99 | + pr, pw := io.Pipe() |
| 100 | + connCtx, connCl := context.WithCancel(ctx) |
| 101 | + req := (&http.Request{ |
| 102 | + Method: "CONNECT", |
| 103 | + URL: &url.URL{ |
| 104 | + Scheme: scheme, |
| 105 | + Host: address, |
| 106 | + }, |
| 107 | + Header: http.Header{ |
| 108 | + "User-Agent": []string{"dumbproxy"}, |
| 109 | + }, |
| 110 | + Body: pr, |
| 111 | + Host: address, |
| 112 | + }).WithContext(connCtx) |
| 113 | + if d.userinfo != nil { |
| 114 | + req.Header.Set("Proxy-Authorization", basicAuthHeader(d.userinfo)) |
| 115 | + } |
| 116 | + resp, err := d.t.RoundTrip(req) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + if resp.StatusCode != http.StatusOK { |
| 121 | + resp.Body.Close() |
| 122 | + pw.Close() |
| 123 | + return nil, errors.New(resp.Status) |
| 124 | + } |
| 125 | + return &h2Conn{ |
| 126 | + r: resp.Body, |
| 127 | + w: pw, |
| 128 | + cl: connCl, |
| 129 | + }, nil |
| 130 | +} |
| 131 | + |
| 132 | +type h2Conn struct { |
| 133 | + r io.ReadCloser |
| 134 | + w io.WriteCloser |
| 135 | + cl func() |
| 136 | +} |
| 137 | + |
| 138 | +func (c *h2Conn) Read(b []byte) (n int, err error) { |
| 139 | + return c.r.Read(b) |
| 140 | +} |
| 141 | + |
| 142 | +func (c *h2Conn) Write(b []byte) (n int, err error) { |
| 143 | + return c.w.Write(b) |
| 144 | +} |
| 145 | + |
| 146 | +func (c *h2Conn) Close() (err error) { |
| 147 | + defer c.cl() |
| 148 | + return errors.Join(c.w.Close(), c.r.Close()) |
| 149 | +} |
| 150 | + |
| 151 | +func (c *h2Conn) LocalAddr() net.Addr { |
| 152 | + return &net.TCPAddr{IP: net.IPv4zero, Port: 0} |
| 153 | +} |
| 154 | + |
| 155 | +func (c *h2Conn) RemoteAddr() net.Addr { |
| 156 | + return &net.TCPAddr{IP: net.IPv4zero, Port: 0} |
| 157 | +} |
| 158 | + |
| 159 | +func (c *h2Conn) SetDeadline(t time.Time) error { |
| 160 | + return &net.OpError{Op: "set", Net: "h2", Source: nil, Addr: nil, Err: errors.New("deadline not supported")} |
| 161 | +} |
| 162 | + |
| 163 | +func (c *h2Conn) SetReadDeadline(t time.Time) error { |
| 164 | + return &net.OpError{Op: "set", Net: "h2", Source: nil, Addr: nil, Err: errors.New("deadline not supported")} |
| 165 | +} |
| 166 | + |
| 167 | +func (c *h2Conn) SetWriteDeadline(t time.Time) error { |
| 168 | + return &net.OpError{Op: "set", Net: "h2", Source: nil, Addr: nil, Err: errors.New("deadline not supported")} |
| 169 | +} |
| 170 | + |
| 171 | +func (c *h2Conn) CloseWrite() error { |
| 172 | + return c.w.Close() |
| 173 | +} |
0 commit comments