Skip to content

Commit 5c4d5ee

Browse files
committed
Merge branch 'master' into sbruens/caddy-ws
2 parents d8093d5 + ff61c9f commit 5c4d5ee

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

service/udp.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ func (h *packetHandler) SetTargetIPValidator(targetIPValidator onet.TargetIPVali
130130
// Listen on addr for encrypted packets and basically do UDP NAT.
131131
// We take the ciphers as a pointer because it gets replaced on config updates.
132132
func (h *packetHandler) Handle(clientConn net.PacketConn) {
133-
var running sync.WaitGroup
134-
135-
nm := newNATmap(h.natTimeout, h.m, &running, h.logger)
133+
nm := newNATmap(h.natTimeout, h.m, h.logger)
136134
defer nm.Close()
137135
cipherBuf := make([]byte, serverUDPBufferSize)
138136
textBuf := make([]byte, serverUDPBufferSize)
@@ -143,7 +141,6 @@ func (h *packetHandler) Handle(clientConn net.PacketConn) {
143141
break
144142
}
145143

146-
keyID := ""
147144
var proxyTargetBytes int
148145
var targetConn *natconn
149146

@@ -171,7 +168,7 @@ func (h *packetHandler) Handle(clientConn net.PacketConn) {
171168
var textData []byte
172169
var cryptoKey *shadowsocks.EncryptionKey
173170
unpackStart := time.Now()
174-
textData, keyID, cryptoKey, err = findAccessKeyUDP(ip, textBuf, cipherData, h.ciphers, h.logger)
171+
textData, keyID, cryptoKey, err := findAccessKeyUDP(ip, textBuf, cipherData, h.ciphers, h.logger)
175172
timeToCipher := time.Since(unpackStart)
176173
h.ssm.AddCipherSearch(err == nil, timeToCipher)
177174

@@ -199,9 +196,6 @@ func (h *packetHandler) Handle(clientConn net.PacketConn) {
199196
return onet.NewConnectionError("ERR_CIPHER", "Failed to unpack data from client", err)
200197
}
201198

202-
// The key ID is known with confidence once decryption succeeds.
203-
keyID = targetConn.keyID
204-
205199
var onetErr *onet.ConnectionError
206200
if payload, tgtUDPAddr, onetErr = h.validatePacket(textData); onetErr != nil {
207201
return onetErr
@@ -256,7 +250,6 @@ func isDNS(addr net.Addr) bool {
256250
type natconn struct {
257251
net.PacketConn
258252
cryptoKey *shadowsocks.EncryptionKey
259-
keyID string
260253
metrics UDPConnMetrics
261254
// NAT timeout to apply for non-DNS packets.
262255
defaultTimeout time.Duration
@@ -320,11 +313,10 @@ type natmap struct {
320313
logger *slog.Logger
321314
timeout time.Duration
322315
metrics UDPMetrics
323-
running *sync.WaitGroup
324316
}
325317

326-
func newNATmap(timeout time.Duration, sm UDPMetrics, running *sync.WaitGroup, l *slog.Logger) *natmap {
327-
m := &natmap{logger: l, metrics: sm, running: running}
318+
func newNATmap(timeout time.Duration, sm UDPMetrics, l *slog.Logger) *natmap {
319+
m := &natmap{logger: l, metrics: sm}
328320
m.keyConn = make(map[string]*natconn)
329321
m.timeout = timeout
330322
return m
@@ -336,11 +328,10 @@ func (m *natmap) Get(key string) *natconn {
336328
return m.keyConn[key]
337329
}
338330

339-
func (m *natmap) set(key string, pc net.PacketConn, cryptoKey *shadowsocks.EncryptionKey, keyID string, connMetrics UDPConnMetrics) *natconn {
331+
func (m *natmap) set(key string, pc net.PacketConn, cryptoKey *shadowsocks.EncryptionKey, connMetrics UDPConnMetrics) *natconn {
340332
entry := &natconn{
341333
PacketConn: pc,
342334
cryptoKey: cryptoKey,
343-
keyID: keyID,
344335
metrics: connMetrics,
345336
defaultTimeout: m.timeout,
346337
}
@@ -366,16 +357,14 @@ func (m *natmap) del(key string) net.PacketConn {
366357

367358
func (m *natmap) Add(clientAddr net.Addr, clientConn net.PacketConn, cryptoKey *shadowsocks.EncryptionKey, targetConn net.PacketConn, keyID string) *natconn {
368359
connMetrics := m.metrics.AddUDPNatEntry(clientAddr, keyID)
369-
entry := m.set(clientAddr.String(), targetConn, cryptoKey, keyID, connMetrics)
360+
entry := m.set(clientAddr.String(), targetConn, cryptoKey, connMetrics)
370361

371-
m.running.Add(1)
372362
go func() {
373-
timedCopy(clientAddr, clientConn, entry, keyID, m.logger)
363+
timedCopy(clientAddr, clientConn, entry, m.logger)
374364
connMetrics.RemoveNatEntry()
375365
if pc := m.del(clientAddr.String()); pc != nil {
376366
pc.Close()
377367
}
378-
m.running.Done()
379368
}()
380369
return entry
381370
}
@@ -399,7 +388,7 @@ func (m *natmap) Close() error {
399388
var maxAddrLen int = len(socks.ParseAddr("[2001:db8::1]:12345"))
400389

401390
// copy from target to client until read timeout
402-
func timedCopy(clientAddr net.Addr, clientConn net.PacketConn, targetConn *natconn, keyID string, l *slog.Logger) {
391+
func timedCopy(clientAddr net.Addr, clientConn net.PacketConn, targetConn *natconn, l *slog.Logger) {
403392
// pkt is used for in-place encryption of downstream UDP packets, with the layout
404393
// [padding?][salt][address][body][tag][extra]
405394
// Padding is only used if the address is IPv4.

service/udp_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"errors"
2020
"net"
2121
"net/netip"
22-
"sync"
2322
"testing"
2423
"time"
2524

@@ -207,14 +206,14 @@ func assertAlmostEqual(t *testing.T, a, b time.Time) {
207206
}
208207

209208
func TestNATEmpty(t *testing.T) {
210-
nat := newNATmap(timeout, &natTestMetrics{}, &sync.WaitGroup{}, noopLogger())
209+
nat := newNATmap(timeout, &natTestMetrics{}, noopLogger())
211210
if nat.Get("foo") != nil {
212211
t.Error("Expected nil value from empty NAT map")
213212
}
214213
}
215214

216215
func setupNAT() (*fakePacketConn, *fakePacketConn, *natconn) {
217-
nat := newNATmap(timeout, &natTestMetrics{}, &sync.WaitGroup{}, noopLogger())
216+
nat := newNATmap(timeout, &natTestMetrics{}, noopLogger())
218217
clientConn := makePacketConn()
219218
targetConn := makePacketConn()
220219
nat.Add(&clientAddr, clientConn, natCryptoKey, targetConn, "key id")

0 commit comments

Comments
 (0)