Skip to content

Commit

Permalink
Fix: listener tcp keepalive & reuse net.BufferedConn (#1987)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamingchan authored Feb 23, 2022
1 parent 03e4b5d commit 132a6a6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
7 changes: 6 additions & 1 deletion listener/http/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ func HandleConn(c net.Conn, in chan<- C.ConnContext, cache *cache.Cache) {
client := newClient(c.RemoteAddr(), in)
defer client.CloseIdleConnections()

conn := N.NewBufferedConn(c)
var conn *N.BufferedConn
if bufConn, ok := c.(*N.BufferedConn); ok {
conn = bufConn
} else {
conn = N.NewBufferedConn(c)
}

keepAlive := true
trusted := cache == nil // disable authenticate if cache is nil
Expand Down
2 changes: 2 additions & 0 deletions listener/mixed/mixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
}

func handleConn(conn net.Conn, in chan<- C.ConnContext, cache *cache.Cache) {
conn.(*net.TCPConn).SetKeepAlive(true)

bufConn := N.NewBufferedConn(conn)
head, err := bufConn.Peek(1)
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions listener/socks/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
}

func handleSocks(conn net.Conn, in chan<- C.ConnContext) {
conn.(*net.TCPConn).SetKeepAlive(true)
bufConn := N.NewBufferedConn(conn)
head, err := bufConn.Peek(1)
if err != nil {
Expand All @@ -84,9 +85,6 @@ func HandleSocks4(conn net.Conn, in chan<- C.ConnContext) {
conn.Close()
return
}
if c, ok := conn.(*net.TCPConn); ok {
c.SetKeepAlive(true)
}
in <- inbound.NewSocket(socks5.ParseAddr(addr), conn, C.SOCKS4)
}

Expand All @@ -96,9 +94,6 @@ func HandleSocks5(conn net.Conn, in chan<- C.ConnContext) {
conn.Close()
return
}
if c, ok := conn.(*net.TCPConn); ok {
c.SetKeepAlive(true)
}
if command == socks5.CmdUDPAssociate {
defer conn.Close()
io.Copy(io.Discard, conn)
Expand Down

0 comments on commit 132a6a6

Please sign in to comment.