Skip to content

Commit

Permalink
update socks5 udp
Browse files Browse the repository at this point in the history
  • Loading branch information
刘河 committed Dec 2, 2019
1 parent 8d27a17 commit f1cb451
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
7 changes: 6 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,12 @@ func (s *TRPClient) handleUdp(serverConn net.Conn) {
buf := bytes.Buffer{}
dgram := common.NewUDPDatagram(common.NewUDPHeader(0, 0, common.ToSocksAddr(raddr)), b[:n])
dgram.Write(&buf)
if _, err := serverConn.Write(buf.Bytes()); err != nil {
b, err := conn.GetLenBytes(buf.Bytes())
if err != nil {
logs.Warn("get len bytes error", err.Error())
continue
}
if _, err := serverConn.Write(b); err != nil {
logs.Error("write data to remote error", err.Error())
return
}
Expand Down
2 changes: 1 addition & 1 deletion lib/common/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

const PoolSize = 64 * 1024
const PoolSizeSmall = 100
const PoolSizeUdp = 1472
const PoolSizeUdp = 1472 + 200
const PoolSizeCopy = 32 << 10
const PoolSizeBuffer = 4096
const PoolSizeWindow = PoolSizeBuffer - 2 - 4 - 4 - 1
Expand Down
9 changes: 7 additions & 2 deletions server/proxy/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,21 @@ func (s *Sock5ModeServer) handleUDP(c net.Conn) {
}()

go func() {
var l int32
b := common.BufPoolUdp.Get().([]byte)
defer common.BufPoolUdp.Put(b)
defer c.Close()
for {
n, err := target.Read(b)
if err := binary.Read(target, binary.LittleEndian, &l); err != nil || l >= common.PoolSizeUdp || l <= 0 {
logs.Warn("read len bytes error", err.Error())
return
}
binary.Read(target, binary.LittleEndian, b[:l])
if err != nil {
logs.Warn("read data form client error", err.Error())
return
}
if _, err := reply.WriteTo(b[:n], clientAddr); err != nil {
if _, err := reply.WriteTo(b[:l], clientAddr); err != nil {
logs.Warn("write data to user ", err.Error())
return
}
Expand Down

0 comments on commit f1cb451

Please sign in to comment.