Skip to content
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

fix missing context ID in datagrams #58

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"context"
"errors"
"fmt"
"io"
"log"
"net"
Expand Down Expand Up @@ -77,16 +78,26 @@
}
return 0, nil, os.ErrDeadlineExceeded
}
contextID, n, err := quicvarint.Parse(data)
if err != nil {
return 0, nil, fmt.Errorf("masque: malformed datagram: %w", err)

Check warning on line 83 in conn.go

View check run for this annotation

Codecov / codecov/patch

conn.go#L83

Added line #L83 was not covered by tests
}
if contextID != 0 {
// Drop this datagram. We currently only support proxying of UDP payloads.
goto start

Check warning on line 87 in conn.go

View check run for this annotation

Codecov / codecov/patch

conn.go#L87

Added line #L87 was not covered by tests
}
// If b is too small, additional bytes are discarded.
// This mirrors the behavior of large UDP datagrams received on a UDP socket (on Linux).
n = copy(b, data)
return n, c.remoteAddr, nil
return copy(b, data[n:]), c.remoteAddr, nil
}

// WriteTo sends a UDP datagram to the target.
// The net.Addr parameter is ignored.
func (c *proxiedConn) WriteTo(p []byte, _ net.Addr) (n int, err error) {
return len(p), c.str.SendDatagram(p)
data := make([]byte, 0, len(contextIDZero)+len(p))
data = append(data, contextIDZero...)
data = append(data, p...)
return len(p), c.str.SendDatagram(data)
}

func (c *proxiedConn) Close() error {
Expand Down
17 changes: 15 additions & 2 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
uriTemplateTargetPort = "target_port"
)

var contextIDZero = quicvarint.Append([]byte{}, 0)

type proxyEntry struct {
str http3.Stream
conn *net.UDPConn
Expand Down Expand Up @@ -126,7 +128,15 @@
if err != nil {
return err
}
if _, err := conn.Write(data); err != nil {
contextID, n, err := quicvarint.Parse(data)
if err != nil {
return err

Check warning on line 133 in proxy.go

View check run for this annotation

Codecov / codecov/patch

proxy.go#L133

Added line #L133 was not covered by tests
}
if contextID != 0 {
// Drop this datagram. We currently only support proxying of UDP payloads.
continue
}
if _, err := conn.Write(data[n:]); err != nil {
return err
}
}
Expand All @@ -139,7 +149,10 @@
if err != nil {
return err
}
if err := str.SendDatagram(b[:n]); err != nil {
data := make([]byte, 0, len(contextIDZero)+n)
data = append(data, contextIDZero...)
data = append(data, b[:n]...)
if err := str.SendDatagram(data); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestProxyCloseProxiedConn(t *testing.T) {
done := make(chan struct{})
str := NewMockStream(gomock.NewController(t))
str.EXPECT().ReceiveDatagram(gomock.Any()).DoAndReturn(func(context.Context) ([]byte, error) {
return []byte("foo"), nil
return append(contextIDZero, []byte("foo")...), nil
})
// This datagram is received after the connection is closed.
// We expect that it won't get sent on.
Expand Down