From 585090981921633b44f12f70e13d00ec2307f2f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9C=9F=E4=BA=BA?= Date: Fri, 31 Mar 2017 17:48:20 +0800 Subject: [PATCH 1/2] style: consistent code style --- shadowaead/cipher.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/shadowaead/cipher.go b/shadowaead/cipher.go index 952d5b27..19410df9 100644 --- a/shadowaead/cipher.go +++ b/shadowaead/cipher.go @@ -46,8 +46,7 @@ func (a *metaCipher) SaltSize() int { func (a *metaCipher) Encrypter(salt []byte) (cipher.AEAD, error) { subkey := make([]byte, a.KeySize()) hkdfSHA1(a.psk, salt, []byte("ss-subkey"), subkey) - aead, err := a.makeAEAD(subkey) - return aead, err + return a.makeAEAD(subkey) } func (a *metaCipher) Decrypter(salt []byte) (cipher.AEAD, error) { subkey := make([]byte, a.KeySize()) From 9dfb3e55b573a2380bbc6b6db0229c37649feb84 Mon Sep 17 00:00:00 2001 From: Riobard Date: Sun, 9 Apr 2017 17:29:06 +0800 Subject: [PATCH 2/2] Use TCP KeepAlive to detect dead peers and maintain NAT entries. --- tcp.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tcp.go b/tcp.go index 32292216..bd603d28 100644 --- a/tcp.go +++ b/tcp.go @@ -43,6 +43,7 @@ func tcpLocal(addr, server string, ciph core.StreamConnCipher, getAddr func(net. go func() { defer c.Close() + c.(*net.TCPConn).SetKeepAlive(true) tgt, err := getAddr(c) if err != nil { @@ -50,12 +51,14 @@ func tcpLocal(addr, server string, ciph core.StreamConnCipher, getAddr func(net. return } - rc, err := core.Dial("tcp", server, ciph) + rc, err := net.Dial("tcp", server) if err != nil { logf("failed to connect to server %v: %v", server, err) return } defer rc.Close() + rc.(*net.TCPConn).SetKeepAlive(true) + rc = ciph.StreamConn(rc) if _, err = rc.Write(tgt); err != nil { logf("failed to send target address: %v", err) @@ -76,7 +79,7 @@ func tcpLocal(addr, server string, ciph core.StreamConnCipher, getAddr func(net. // Listen on addr for incoming connections. func tcpRemote(addr string, ciph core.StreamConnCipher) { - l, err := core.Listen("tcp", addr, ciph) + l, err := net.Listen("tcp", addr) if err != nil { logf("failed to listen on %s: %v", addr, err) return @@ -92,6 +95,8 @@ func tcpRemote(addr string, ciph core.StreamConnCipher) { go func() { defer c.Close() + c.(*net.TCPConn).SetKeepAlive(true) + c = ciph.StreamConn(c) tgt, err := socks.ReadAddr(c) if err != nil { @@ -105,6 +110,7 @@ func tcpRemote(addr string, ciph core.StreamConnCipher) { return } defer rc.Close() + rc.(*net.TCPConn).SetKeepAlive(true) logf("proxy %s <-> %s", c.RemoteAddr(), tgt) _, _, err = relay(c, rc)