Skip to content
This repository was archived by the owner on Apr 9, 2020. It is now read-only.

Commit 964fa27

Browse files
committed
Better handling UDP packets with OTA.
1 parent 5ba846e commit 964fa27

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

cmd/shadowsocks-server/server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,9 @@ func runUDP(port, password string, auth bool) {
360360
}
361361
SecurePacketConn := ss.NewSecurePacketConn(conn, cipher.Copy(), auth)
362362
for {
363-
ss.ReadAndHandleUDPReq(SecurePacketConn)
363+
if err := ss.ReadAndHandleUDPReq(SecurePacketConn); err != nil {
364+
debug.Println(err)
365+
}
364366
}
365367
}
366368

shadowsocks/udp.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func (c *SecurePacketConn) Close() error {
3737
}
3838

3939
func (c *SecurePacketConn) ReadFrom(b []byte) (n int, src net.Addr, err error) {
40+
ota := false
4041
cipher := c.Copy()
4142
buf := make([]byte, 4096)
4243
n, src, err = c.PacketConn.ReadFrom(buf)
@@ -61,12 +62,20 @@ func (c *SecurePacketConn) ReadFrom(b []byte) (n int, src net.Addr, err error) {
6162

6263
cipher.decrypt(b[0:], buf[c.info.ivLen:n])
6364
n -= c.info.ivLen
64-
if c.ota {
65+
if b[idType]&OneTimeAuthMask > 0 {
66+
ota = true
67+
}
68+
69+
if c.ota && !ota {
70+
return 0, src, errPacketOtaFailed
71+
}
72+
73+
if ota {
6574
key := cipher.key
6675
actualHmacSha1Buf := HmacSha1(append(iv, key...), b[:n-lenHmacSha1])
6776
if !bytes.Equal(b[n-lenHmacSha1:n], actualHmacSha1Buf) {
6877
Debug.Printf("verify one time auth failed, iv=%v key=%v data=%v", iv, key, b)
69-
return 0, nil, errPacketOtaFailed
78+
return 0, src, errPacketOtaFailed
7079
}
7180
n -= lenHmacSha1
7281
}
@@ -94,6 +103,9 @@ func (c *SecurePacketConn) WriteTo(b []byte, dst net.Addr) (n int, err error) {
94103

95104
cipher.encrypt(cipherData[len(iv):], b)
96105
n, err = c.PacketConn.WriteTo(cipherData, dst)
106+
if c.ota {
107+
n -= lenHmacSha1
108+
}
97109
return
98110
}
99111

@@ -135,5 +147,6 @@ func (c *SecurePacketConn) ForceOTAWriteTo(b []byte, dst net.Addr) (n int, err e
135147

136148
cipher.encrypt(cipherData[len(iv):], b)
137149
n, err = c.PacketConn.WriteTo(cipherData, dst)
150+
n -= lenHmacSha1
138151
return
139152
}

shadowsocks/udprelay.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,6 @@ func handleUDPConnection(handle *SecurePacketConn, n int, src net.Addr, receive
176176
if addrType&OneTimeAuthMask > 0 {
177177
ota = true
178178
}
179-
if handle.IsOta() && !ota {
180-
Debug.Println("[udp]incoming connection dropped, due to ota enforcement")
181-
return
182-
}
183179
compatiblemode := !handle.IsOta() && ota
184180

185181
switch addrType & AddrMask {
@@ -258,11 +254,12 @@ func handleUDPConnection(handle *SecurePacketConn, n int, src net.Addr, receive
258254
return
259255
}
260256

261-
func ReadAndHandleUDPReq(c *SecurePacketConn) {
257+
func ReadAndHandleUDPReq(c *SecurePacketConn) error {
262258
buf := leakyBuf.Get()
263259
n, src, err := c.ReadFrom(buf[0:])
264260
if err != nil {
265-
return
261+
return err
266262
}
267263
go handleUDPConnection(c, n, src, buf)
264+
return nil
268265
}

0 commit comments

Comments
 (0)