Skip to content

Commit

Permalink
fix pool issue&#40 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
若尘(樱の泪) committed Feb 10, 2019
1 parent 03b156e commit 96c4b42
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 59 deletions.
2 changes: 1 addition & 1 deletion common/ciphers/ssaead/cipher_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func GetAEADPacketCiphers(method string) func(string, net.PacketConn) (net.Packe
PacketConn: packCon,
IAEADCipher: c,
key: evpBytesToKey(password, c.KeySize()),
buf: pool.GetUdpBuf(),
buf: pool.GetBuf(),
}
return ap, nil
}
Expand Down
4 changes: 2 additions & 2 deletions common/ciphers/ssstream/cipher_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ func (c *streamPacket) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
return n, addr, err
}

pool.GetUdpBuf()
pool.GetBuf()
decryptr.XORKeyStream(b[ivLen:], b[ivLen:ivLen+n])
copy(b, b[ivLen:])
return n - ivLen, addr, err
}

func (c *streamPacket) Close() error {
pool.PutUdpBuf(c.buf)
pool.PutBuf(c.buf)
return c.PacketConn.Close()
}
2 changes: 1 addition & 1 deletion common/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package pool

import "sync"

const BufferSize = 4108
const BufferSize = 4096

var (
poolMap map[int]*sync.Pool
Expand Down
25 changes: 0 additions & 25 deletions common/pool/udp_buffer_pool.go

This file was deleted.

58 changes: 28 additions & 30 deletions proxy/server/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ import (

type mode int

const (
remoteServer mode = iota
relayClient
socksClient
)

var (
logging *log.Logging
// ShadowsocksServerList Global map for store shadowsocks proxy
Expand Down Expand Up @@ -268,8 +262,18 @@ func relayTCP(left, right net.Conn) (int64, int64, error) {
Err error
}
ch := make(chan res)
defer func() {
if e := recover(); e != nil {
log.Error("panic in timedCopy: %v", e)
}
}()

go func() {
defer func() {
if e := recover(); e != nil {
log.Error("panic in timedCopy: %v", e)
}
}()
n, err := io.Copy(right, left)
right.SetDeadline(time.Now()) // wake up the other goroutine blocking on right
left.SetDeadline(time.Now()) // wake up the other goroutine blocking on left
Expand Down Expand Up @@ -332,8 +336,8 @@ func (s *ShadowsocksProxy) startUDP() error {
}

nm := newNATmap(s.ConnectTimeout)
buf := pool.GetUdpBuf()
defer pool.PutUdpBuf(buf)
buf := pool.GetBuf()
defer pool.PutBuf(buf)

// logging.Info("listening UDP on %s", addr)

Expand Down Expand Up @@ -384,7 +388,7 @@ func (s *ShadowsocksProxy) startUDP() error {
continue
}

nm.Add(raddr, server, pc, remoteServer)
nm.Add(raddr, server, pc)
}

_, err = pc.WriteTo(payload, tgtUDPAddr) // accept only UDPAddr despite the signature
Expand Down Expand Up @@ -457,7 +461,6 @@ func (m *natmap) Get(key string) net.PacketConn {
func (m *natmap) Set(key string, pc net.PacketConn) {
m.Lock()
defer m.Unlock()

m.m[key] = pc
}

Expand All @@ -473,21 +476,25 @@ func (m *natmap) Del(key string) net.PacketConn {
return nil
}

func (m *natmap) Add(peer net.Addr, dst, src net.PacketConn, role mode) {
func (m *natmap) Add(peer net.Addr, dst, src net.PacketConn) {
m.Set(peer.String(), src)

go func() {
timedCopy(dst, peer, src, m.timeout, role)
timedCopy(dst, peer, src, m.timeout)
if pc := m.Del(peer.String()); pc != nil {
pc.Close()
}
}()
}

// copy from src to dst at target with read timeout
func timedCopy(dst net.PacketConn, target net.Addr, src net.PacketConn, timeout time.Duration, role mode) error {
buf := pool.GetUdpBuf()
defer pool.PutUdpBuf(buf)
func timedCopy(dst net.PacketConn, target net.Addr, src net.PacketConn, timeout time.Duration) error {
buf := pool.GetBuf()
defer pool.PutBuf(buf)
defer func() {
if e := recover(); e != nil {
log.Error("panic in timedCopy: %v", e)
}
}()

for {
src.SetReadDeadline(time.Now().Add(timeout))
Expand All @@ -496,20 +503,11 @@ func timedCopy(dst net.PacketConn, target net.Addr, src net.PacketConn, timeout
return errors.Cause(err)
}

switch role {
case remoteServer: // server -> client: add original packet source
srcAddr := socks.ParseAddr(raddr.String())
srcAddrByte := srcAddr.Raw
copy(buf[len(srcAddrByte):], buf[:n])
copy(buf, srcAddrByte)
_, err = dst.WriteTo(buf[:len(srcAddrByte)+n], target)
case relayClient: // client -> user: strip original packet source
srcAddr := socks.SplitAddr(buf[:n])
srcAddrByte := srcAddr.Raw
_, err = dst.WriteTo(buf[len(srcAddrByte):n], target)
case socksClient: // client -> socks5 program: just set RSV and FRAG = 0
_, err = dst.WriteTo(append([]byte{0, 0, 0}, buf[:n]...), target)
}
srcAddr := socks.ParseAddr(raddr.String())
srcAddrByte := srcAddr.Raw
copy(buf[len(srcAddrByte):], buf[:n])
copy(buf, srcAddrByte)
_, err = dst.WriteTo(buf[:len(srcAddrByte)+n], target)

if err != nil {
return errors.Cause(err)
Expand Down

0 comments on commit 96c4b42

Please sign in to comment.