-
Notifications
You must be signed in to change notification settings - Fork 166
feat(httpconnect): add NewH2ProxyTransport for pure H2 CONNECT tunneling #589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fortuna
wants to merge
9
commits into
main
Choose a base branch
from
feat/h2-proxy-transport
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1c8035c
feat(httpconnect): add NewH2ProxyTransport for pure HTTP/2 CONNECT
fortuna 6e55067
test(httpconnect): add H2-TLS test for NewH2ProxyTransport with multi…
fortuna 23f73dc
refactor(httpconnect): improve test readability
fortuna 8e00814
refactor(httpproxy): extend connect handler to support H2/H3 streams
fortuna 38d616e
docs(httpconnect): add package overview and Caddy testing guide; rena…
fortuna 8bb54ef
feat(configurl): add httpconnect, h2connect, h3connect transport types
fortuna 87355e1
refactor(httpproxy): remove configurl dependency; inject StreamDialer…
fortuna 651adcf
fix: correct doc link and test comment
fortuna 9d03edf
docs(httpconnect): add NewH2ProxyTransport to ConnectClient transport…
fortuna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| // Copyright 2025 The Outline Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package configurl | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net" | ||
| "net/url" | ||
| "strings" | ||
|
|
||
| "golang.getoutline.org/sdk/transport" | ||
| "golang.getoutline.org/sdk/transport/tls" | ||
| "golang.getoutline.org/sdk/x/httpconnect" | ||
| ) | ||
|
|
||
| // parseConnectOptions parses query parameters from a hierarchical config URL | ||
| // (e.g. h2connect://host:port?sni=example.com&plain=true) into TransportOptions. | ||
| // | ||
| // Supported parameters: | ||
| // - sni: TLS server name for SNI. | ||
| // - certname: name to validate against the server certificate. | ||
| // - plain: if "true", use cleartext (no TLS). Only meaningful for h2connect (h2c). | ||
| func parseConnectOptions(configURL url.URL) ([]httpconnect.TransportOption, error) { | ||
| values, err := url.ParseQuery(configURL.RawQuery) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var opts []httpconnect.TransportOption | ||
| var tlsOpts []tls.ClientOption | ||
| for key, vals := range values { | ||
| switch strings.ToLower(key) { | ||
| case "sni": | ||
| if len(vals) != 1 { | ||
| return nil, fmt.Errorf("sni option must have one value, found %v", len(vals)) | ||
| } | ||
| tlsOpts = append(tlsOpts, tls.WithSNI(vals[0])) | ||
| case "certname": | ||
| if len(vals) != 1 { | ||
| return nil, fmt.Errorf("certname option must have one value, found %v", len(vals)) | ||
| } | ||
| tlsOpts = append(tlsOpts, tls.WithCertVerifier(&tls.StandardCertVerifier{CertificateName: vals[0]})) | ||
| case "plain": | ||
| if len(vals) != 1 { | ||
| return nil, fmt.Errorf("plain option must have one value, found %v", len(vals)) | ||
| } | ||
| if vals[0] == "true" { | ||
| opts = append(opts, httpconnect.WithPlainHTTP()) | ||
| } | ||
| default: | ||
| return nil, fmt.Errorf("unsupported option %v", key) | ||
| } | ||
| } | ||
| if len(tlsOpts) > 0 { | ||
| opts = append(opts, httpconnect.WithTLSOptions(tlsOpts...)) | ||
| } | ||
| return opts, nil | ||
| } | ||
|
|
||
| // registerHTTPConnectStreamDialer registers an HTTP CONNECT proxy transport (H1.1, or H2 via ALPN). | ||
| // | ||
| // Config format: httpconnect://host:port[?sni=SNI][&certname=CERTNAME] | ||
| // | ||
| // The base dialer (from the previous element in the pipe chain) is used to establish | ||
| // the TCP connection to the proxy. TLS is negotiated by the transport itself. | ||
| // When H2 is negotiated via ALPN, CONNECT streams are multiplexed over the single TCP connection. | ||
| func registerHTTPConnectStreamDialer(r TypeRegistry[transport.StreamDialer], typeID string, newSD BuildFunc[transport.StreamDialer]) { | ||
| r.RegisterType(typeID, func(ctx context.Context, config *Config) (transport.StreamDialer, error) { | ||
| sd, err := newSD(ctx, config.BaseConfig) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| opts, err := parseConnectOptions(config.URL) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| tr, err := httpconnect.NewHTTPProxyTransport(sd, config.URL.Host, opts...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return httpconnect.NewConnectClient(tr) | ||
| }) | ||
| } | ||
|
|
||
| // registerH2ConnectStreamDialer registers a pure HTTP/2 CONNECT proxy transport. | ||
| // | ||
| // Config format: h2connect://host:port[?sni=SNI][&certname=CERTNAME] | ||
| // | ||
| // Unlike httpconnect, all CONNECT streams are multiplexed over a single TCP connection | ||
| // to the proxy. The base dialer is used to establish that connection. | ||
| func registerH2ConnectStreamDialer(r TypeRegistry[transport.StreamDialer], typeID string, newSD BuildFunc[transport.StreamDialer]) { | ||
| r.RegisterType(typeID, func(ctx context.Context, config *Config) (transport.StreamDialer, error) { | ||
| sd, err := newSD(ctx, config.BaseConfig) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| opts, err := parseConnectOptions(config.URL) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| tr, err := httpconnect.NewH2ProxyTransport(sd, config.URL.Host, opts...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return httpconnect.NewConnectClient(tr) | ||
| }) | ||
| } | ||
|
|
||
| // registerH3ConnectStreamDialer registers an HTTP/3 CONNECT proxy transport over QUIC. | ||
| // | ||
| // Config format: h3connect://host:port[?sni=SNI][&certname=CERTNAME] | ||
| // | ||
| // A UDP socket is created internally and shared across all CONNECT streams (QUIC multiplexing). | ||
| // The base stream dialer is not used; QUIC always runs over a fresh UDP connection. | ||
| func registerH3ConnectStreamDialer(r TypeRegistry[transport.StreamDialer], typeID string) { | ||
| r.RegisterType(typeID, func(ctx context.Context, config *Config) (transport.StreamDialer, error) { | ||
| opts, err := parseConnectOptions(config.URL) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| udpConn, err := net.ListenPacket("udp", ":0") | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create UDP socket: %w", err) | ||
| } | ||
| tr, err := httpconnect.NewH3ProxyTransport(udpConn, config.URL.Host, opts...) | ||
| if err != nil { | ||
| udpConn.Close() | ||
| return nil, err | ||
| } | ||
| return httpconnect.NewConnectClient(tr) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // Copyright 2025 The Outline Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package configurl_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "golang.getoutline.org/sdk/transport" | ||
| "golang.getoutline.org/sdk/x/configurl" | ||
| "golang.getoutline.org/sdk/x/httpproxy" | ||
| "golang.org/x/net/http2" | ||
| ) | ||
|
|
||
| // Test_H2Connect_H2C tests the h2connect configurl type using h2c (cleartext HTTP/2). | ||
| // It starts a local h2c proxy, builds a stream dialer via "h2connect://host:port?plain=true", | ||
| // and verifies that an HTTP request is tunneled through to a target server. | ||
| func Test_H2Connect_H2C(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tcpDialer := &transport.TCPDialer{} | ||
|
|
||
| // Start an h2c proxy server (plain HTTP/2 without TLS). | ||
| ln, err := net.Listen("tcp", "127.0.0.1:0") | ||
| require.NoError(t, err) | ||
| t.Cleanup(func() { ln.Close() }) | ||
|
|
||
| h2srv := &http2.Server{} | ||
| handler := httpproxy.NewConnectHandler(tcpDialer) | ||
| go func() { | ||
| for { | ||
| conn, err := ln.Accept() | ||
| if err != nil { | ||
| return | ||
| } | ||
| go h2srv.ServeConn(conn, &http2.ServeConnOpts{Handler: handler}) | ||
| } | ||
| }() | ||
|
|
||
| // Build a dialer using the configurl h2connect type. | ||
| providers := configurl.NewDefaultProviders() | ||
| dialer, err := providers.NewStreamDialer(context.Background(), | ||
| fmt.Sprintf("h2connect://%s?plain=true", ln.Addr().String()), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| // Start a target server that returns a JSON response. | ||
| type Response struct { | ||
| Message string `json:"message"` | ||
| } | ||
| want := Response{Message: "hello"} | ||
| targetSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.Header().Set("Content-Type", "application/json") | ||
| json.NewEncoder(w).Encode(want) | ||
| })) | ||
| t.Cleanup(targetSrv.Close) | ||
|
|
||
| // Make an HTTP request through the tunnel. | ||
| hc := &http.Client{ | ||
| Transport: &http.Transport{ | ||
| DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) { | ||
| return dialer.DialStream(ctx, addr) | ||
| }, | ||
| }, | ||
| } | ||
| resp, err := hc.Get(targetSrv.URL) | ||
| require.NoError(t, err) | ||
| defer resp.Body.Close() | ||
|
|
||
| require.Equal(t, http.StatusOK, resp.StatusCode) | ||
| var got Response | ||
| require.NoError(t, json.NewDecoder(resp.Body).Decode(&got)) | ||
| require.Equal(t, want, got) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.