Skip to content

Commit

Permalink
don't embed a http3.Server in the Proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jun 30, 2024
1 parent 763636e commit e73e70b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
17 changes: 8 additions & 9 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ func main() {
tlsConf := http3.ConfigureTLSConfig(&tls.Config{
Certificates: []tls.Certificate{cert},
})
proxy := masque.Proxy{
Template: template,
Server: http3.Server{
Addr: bind,
TLSConfig: tlsConf,
EnableDatagrams: true,
Logger: slog.Default(),
},
server := http3.Server{
Addr: bind,
TLSConfig: tlsConf,
EnableDatagrams: true,
Logger: slog.Default(),
}
defer server.Close()
proxy := masque.Proxy{Template: template}
// parse the template to extract the path for the HTTP handler
u, err := url.Parse(templateStr)
if err != nil {
Expand All @@ -60,7 +59,7 @@ func main() {
}
w.WriteHeader(http.StatusOK)
})
if err := proxy.ListenAndServe(); err != nil {
if err := server.ListenAndServe(); err != nil {
log.Fatalf("failed to run proxy: %v", err)
}
}
38 changes: 20 additions & 18 deletions connect-udp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,20 @@ func TestProxying(t *testing.T) {
template := uritemplate.MustNew(fmt.Sprintf("https://localhost:%d/masque?h={target_host}&p={target_port}", conn.LocalAddr().(*net.UDPAddr).Port))

mux := http.NewServeMux()
server := masque.Proxy{
Server: http3.Server{
TLSConfig: tlsConf,
QUICConfig: &quic.Config{EnableDatagrams: true},
EnableDatagrams: true,
Handler: mux,
},
server := http3.Server{
TLSConfig: tlsConf,
QUICConfig: &quic.Config{EnableDatagrams: true},
EnableDatagrams: true,
Handler: mux,
}
defer server.Close()
proxy := masque.Proxy{
Template: template,
Allow: func(context.Context, *net.UDPAddr) bool { return true },
}
defer server.Close()
defer proxy.Close()
mux.HandleFunc("/masque", func(w http.ResponseWriter, r *http.Request) {
if err := server.Upgrade(w, r); err != nil {
if err := proxy.Upgrade(w, r); err != nil {
t.Log("Upgrade failed:", err)
return
}
Expand Down Expand Up @@ -104,18 +105,19 @@ func TestProxyShutdown(t *testing.T) {
template := uritemplate.MustNew(fmt.Sprintf("https://localhost:%d/masque?h={target_host}&p={target_port}", conn.LocalAddr().(*net.UDPAddr).Port))

mux := http.NewServeMux()
server := masque.Proxy{
Server: http3.Server{
TLSConfig: tlsConf,
QUICConfig: &quic.Config{EnableDatagrams: true},
EnableDatagrams: true,
Handler: mux,
},
server := http3.Server{
TLSConfig: tlsConf,
QUICConfig: &quic.Config{EnableDatagrams: true},
EnableDatagrams: true,
Handler: mux,
}
defer server.Close()
proxy := masque.Proxy{
Template: template,
Allow: func(context.Context, *net.UDPAddr) bool { return true },
}
mux.HandleFunc("/masque", func(w http.ResponseWriter, r *http.Request) {
if err := server.Upgrade(w, r); err != nil {
if err := proxy.Upgrade(w, r); err != nil {
t.Log("Upgrade failed:", err)
return
}
Expand Down Expand Up @@ -143,7 +145,7 @@ func TestProxyShutdown(t *testing.T) {
require.Equal(t, []byte("foobar"), b[:n])

// Close the server and expect the proxied connection to unblock ReadFrom and WriteTo.
server.Close()
proxy.Close()
_, _, err = proxiedConn.ReadFrom(b)
require.Error(t, err)
var errored bool
Expand Down
5 changes: 1 addition & 4 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ type proxyEntry struct {
}

type Proxy struct {
http3.Server

// Template is the URI template that clients will use to configure this UDP proxy.
Template *uritemplate.Template

Expand Down Expand Up @@ -211,7 +209,6 @@ func (s *Proxy) proxyConnReceive(conn *net.UDPConn, str http3.Stream) error {

func (s *Proxy) Close() error {
s.closed.Store(true)
err := s.Server.Close()
s.mx.Lock()
for entry := range s.conns {
entry.str.CancelRead(quic.StreamErrorCode(http3.ErrCodeNoError))
Expand All @@ -221,5 +218,5 @@ func (s *Proxy) Close() error {
s.conns = nil
s.mx.Unlock()
s.refCount.Wait()
return err
return nil
}
4 changes: 0 additions & 4 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ var _ http3.HTTPStreamer = &http3ResponseWriter{}
func (s *http3ResponseWriter) HTTPStream() http3.Stream { return s.str }

func TestUpgradeFailures(t *testing.T) {
mux := http.NewServeMux()
s := Proxy{
Server: http3.Server{Handler: mux},
Template: uritemplate.MustNew("https://localhost:1234/masque?h={target_host}&p={target_port}"),
}

Expand Down Expand Up @@ -113,7 +111,6 @@ func TestProxyCloseProxiedConn(t *testing.T) {
require.NoError(t, err)

s := Proxy{
Server: http3.Server{Handler: http.NewServeMux()},
Template: uritemplate.MustNew("https://localhost:1234/masque?h={target_host}&p={target_port}"),
}
req := newRequest(fmt.Sprintf("https://localhost:1234/masque?h=localhost&p=%d", remoteServerConn.LocalAddr().(*net.UDPAddr).Port))
Expand Down Expand Up @@ -163,7 +160,6 @@ func TestProxyDialFailure(t *testing.T) {
var dialedAddr *net.UDPAddr
testErr := errors.New("test error")
s := Proxy{
Server: http3.Server{Handler: http.NewServeMux()},
Template: uritemplate.MustNew("https://localhost:1234/masque?h={target_host}&p={target_port}"),
DialTarget: func(_ context.Context, addr *net.UDPAddr) (*net.UDPConn, error) {
dialedAddr = addr
Expand Down

0 comments on commit e73e70b

Please sign in to comment.