Skip to content

Commit 807b820

Browse files
committed
dynamic address length calculation in shadowsocks udp encoding
1 parent 1d6cecf commit 807b820

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

proxy/shadowsocks/protocol.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,24 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload []byte) (*buf.Buff
187187
account := user.Account.(*MemoryAccount)
188188

189189
ivLen := account.Cipher.IVSize()
190-
// Calculate required buffer size: IV + max address length (1+255+2) + payload + AEAD overhead (16)
191-
neededSize := ivLen + 258 + int32(len(payload)) + 16
190+
// Calculate required buffer size: IV + address length + payload + AEAD overhead (16)
191+
192+
var addrPortLen int32
193+
switch request.Address.Family() {
194+
case net.AddressFamilyDomain:
195+
if protocol.IsDomainTooLong(request.Address.Domain()) {
196+
return nil, newError("Super long domain is not supported: ", request.Address.Domain())
197+
}
198+
addrPortLen = 1 + 1 + int32(len(request.Address.Domain())) + 2
199+
case net.AddressFamilyIPv4:
200+
addrPortLen = 1 + 4 + 2
201+
case net.AddressFamilyIPv6:
202+
addrPortLen = 1 + 16 + 2
203+
default:
204+
panic("Unknown address type.")
205+
}
206+
207+
neededSize := ivLen + addrPortLen + int32(len(payload)) + 16
192208

193209
var buffer *buf.Buffer
194210
if neededSize > buf.Size {

0 commit comments

Comments
 (0)