Skip to content

Commit

Permalink
Use TCP KeepAlive to detect dead peers and maintain NAT entries.
Browse files Browse the repository at this point in the history
  • Loading branch information
riobard committed Apr 9, 2017
1 parent 5850909 commit 9dfb3e5
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,22 @@ 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 {
logf("failed to get target address: %v", err)
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)
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 9dfb3e5

Please sign in to comment.