Skip to content

Commit 3b3a025

Browse files
authored
Include headroom calculations (#106)
* add space in buffer to prevent overflow * add upstream to conns
1 parent 21a1481 commit 3b3a025

File tree

10 files changed

+107
-65
lines changed

10 files changed

+107
-65
lines changed

go.mod

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ require (
5454
github.com/oschwald/geoip2-golang v1.9.0 // indirect
5555
github.com/oschwald/maxminddb-golang v1.13.1 // indirect
5656
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
57-
github.com/ulikunitz/xz v0.5.10 // indirect
57+
github.com/ulikunitz/xz v0.5.11 // indirect
5858
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
5959
go.opentelemetry.io/proto/otlp v1.7.1 // indirect
6060
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
@@ -100,7 +100,7 @@ require (
100100
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
101101
github.com/dblohm7/wingoes v0.0.0-20240119213807-a09d6be7affa // indirect
102102
github.com/digitalocean/go-smbios v0.0.0-20180907143718-390a4f403a8e // indirect
103-
github.com/dustin/go-humanize v1.0.0 // indirect
103+
github.com/dustin/go-humanize v1.0.1 // indirect
104104
github.com/edsrzf/mmap-go v1.1.0 // indirect
105105
github.com/fsnotify/fsnotify v1.7.0 // indirect
106106
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
@@ -156,23 +156,22 @@ require (
156156
github.com/mitchellh/go-ps v1.0.0 // indirect
157157
github.com/mschoch/smat v0.2.0 // indirect
158158
github.com/pierrec/lz4/v4 v4.1.21 // indirect
159-
github.com/pion/datachannel v1.5.2 // indirect
159+
github.com/pion/datachannel v1.5.5 // indirect
160160
github.com/pion/dtls/v2 v2.2.7 // indirect
161-
github.com/pion/ice/v2 v2.2.6 // indirect
162-
github.com/pion/interceptor v0.1.11 // indirect
161+
github.com/pion/ice/v2 v2.3.5 // indirect
162+
github.com/pion/interceptor v0.1.17 // indirect
163163
github.com/pion/logging v0.2.2 // indirect
164-
github.com/pion/mdns v0.0.5 // indirect
164+
github.com/pion/mdns v0.0.7 // indirect
165165
github.com/pion/randutil v0.1.0 // indirect
166-
github.com/pion/rtcp v1.2.9 // indirect
166+
github.com/pion/rtcp v1.2.10 // indirect
167167
github.com/pion/rtp v1.7.13 // indirect
168168
github.com/pion/sctp v1.8.8 // indirect
169-
github.com/pion/sdp/v3 v3.0.5 // indirect
170-
github.com/pion/srtp/v2 v2.0.9 // indirect
169+
github.com/pion/sdp/v3 v3.0.6 // indirect
170+
github.com/pion/srtp/v2 v2.0.15 // indirect
171171
github.com/pion/stun v0.6.1 // indirect
172-
github.com/pion/transport v0.13.1 // indirect
173172
github.com/pion/transport/v2 v2.2.3 // indirect
174-
github.com/pion/turn/v2 v2.0.8 // indirect
175-
github.com/pion/webrtc/v3 v3.1.42 // indirect
173+
github.com/pion/turn/v2 v2.1.0 // indirect
174+
github.com/pion/webrtc/v3 v3.2.6 // indirect
176175
github.com/pkg/errors v0.9.1 // indirect
177176
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
178177
github.com/prometheus-community/pro-bing v0.4.0 // indirect

go.sum

Lines changed: 51 additions & 47 deletions
Large diffs are not rendered by default.

tracker/clientcontext/injector.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ func (c *writeConn) sendInfo(conn net.Conn) error {
166166
return nil
167167
}
168168

169+
func (c *writeConn) Upstream() any {
170+
return c.Conn
171+
}
172+
169173
type writePacketConn struct {
170174
N.PacketConn
171175
metadata adapter.InboundContext
@@ -246,3 +250,7 @@ func (c *writePacketConn) sendInfo(conn net.PacketConn) error {
246250
}
247251
return nil
248252
}
253+
254+
func (c *writePacketConn) Upstream() any {
255+
return c.PacketConn
256+
}

tracker/clientcontext/manager.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ func (c *readConn) readInfo() (*ClientInfo, error) {
174174
return &info, nil
175175
}
176176

177+
func (c *readConn) Upstream() any {
178+
return c.Conn
179+
}
180+
177181
type readPacketConn struct {
178182
N.PacketConn
179183
mgr *Manager
@@ -214,11 +218,22 @@ func (c *readPacketConn) readInfo() (*ClientInfo, error) {
214218
// CRITICAL: Use a new buffer for the response to ensure we have enough headroom
215219
// for the packet headers (e.g. VMess). Reusing the old buffer with Reset()
216220
// discards the headroom and causes 'buffer overflow' panics.
221+
// advance buffer start, and reserve end, to leave room for headers/trailers on various protocols
217222
respBuffer := buf.NewPacket()
218223
defer respBuffer.Release()
224+
225+
headroom := N.CalculateFrontHeadroom(c)
226+
rearHeadroom := N.CalculateRearHeadroom(c)
227+
respBuffer.Advance(headroom)
228+
respBuffer.Reserve(rearHeadroom)
229+
219230
respBuffer.WriteString("OK")
220231
if err := c.WritePacket(respBuffer, destination); err != nil {
221232
return nil, fmt.Errorf("writing OK response: %w", err)
222233
}
223234
return &info, nil
224235
}
236+
237+
func (c *readPacketConn) Upstream() any {
238+
return c.PacketConn
239+
}

tracker/datacap/conn.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,7 @@ func (c *Conn) GetStatus() (*DataCapStatus, error) {
241241
func (c *Conn) GetBytesConsumed() int64 {
242242
return c.bytesSent.Load() + c.bytesReceived.Load()
243243
}
244+
245+
func (c *Conn) Upstream() any {
246+
return c.Conn
247+
}

tracker/datacap/packet_conn.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,7 @@ func (c *PacketConn) GetStatus() (*DataCapStatus, error) {
238238
func (c *PacketConn) GetBytesConsumed() int64 {
239239
return c.bytesSent.Load() + c.bytesReceived.Load()
240240
}
241+
242+
func (c *PacketConn) Upstream() any {
243+
return c.PacketConn
244+
}

tracker/metrics/conn.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ func (c *Conn) Close() error {
5555
metrics.conns.Add(context.Background(), -1, metric.WithAttributes(c.attributes...))
5656
return c.Conn.Close()
5757
}
58+
59+
func (c *Conn) Upstream() any {
60+
return c.Conn
61+
}

tracker/metrics/metrics.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ import (
1818
)
1919

2020
type metricsManager struct {
21-
meter metric.Meter
22-
ProxyIO metric.Int64Counter
23-
Connections metric.Int64Counter
24-
conns metric.Int64UpDownCounter
25-
duration metric.Int64Histogram
21+
meter metric.Meter
22+
ProxyIO metric.Int64Counter
23+
Connections metric.Int64Counter
24+
conns metric.Int64UpDownCounter
25+
duration metric.Int64Histogram
26+
2627
countryLookup geo.CountryLookup
2728
}
2829

tracker/metrics/packet_conn.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ func (c *PacketConn) Close() error {
6060
metrics.conns.Add(context.Background(), -1, metric.WithAttributes(c.attributes...))
6161
return c.PacketConn.Close()
6262
}
63+
64+
func (c *PacketConn) Upstream() any {
65+
return c.PacketConn
66+
}

tracker/metrics/tracker_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ func TestTracker(t *testing.T) {
8282
if results["receive"] != int64(serverReceive) {
8383
t.Errorf("receive bytes did not match, got %d, want %d", results["receive"], serverReceive)
8484
}
85-
8685
}
8786

8887
func extractCountersByAttribute(rm metricdata.ResourceMetrics, name string) map[string]int64 {

0 commit comments

Comments
 (0)