Skip to content

Commit

Permalink
Only set timeout for read from shadowsocks client.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Jan 29, 2013
1 parent 3f66636 commit 86e5ba2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cmd/shadowsocks-local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ func handleConnection(conn net.Conn) {
}
}()

ss.Pipe(conn, remote)
go ss.PipeThenClose(conn, remote, ss.NO_TIMEOUT)
ss.PipeThenClose(remote, conn, ss.NO_TIMEOUT)
closed = true
debug.Println("closed connection to", addr)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/shadowsocks-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ func handleConnection(conn *ss.Conn) {
if debug {
debug.Printf("piping %s<->%s", conn.RemoteAddr(), host)
}
ss.Pipe(conn, remote)
go ss.PipeThenClose(conn, remote, ss.SET_TIMEOUT)
ss.PipeThenClose(remote, conn, ss.NO_TIMEOUT)
closed = true
return
}
Expand Down
19 changes: 10 additions & 9 deletions shadowsocks/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@ import (
"time"
)

const (
NO_TIMEOUT = iota
SET_TIMEOUT
)

func SetReadTimeout(c net.Conn) {
if readTimeout != 0 {
c.SetReadDeadline(time.Now().Add(readTimeout))
}
}

// Pipe copies data between c1 and c2. Closes c1 and c2 when done.
func Pipe(c1, c2 net.Conn) {
go pipeClose(c1, c2)
pipeClose(c2, c1)
}

// pipeClose copies data from src to dst. Closes dst when done.
func pipeClose(src, dst net.Conn) {
// PipeThenClose copies data from src to dst, closes dst when done.
func PipeThenClose(src, dst net.Conn, timeoutOpt int) {
defer dst.Close()
buf := make([]byte, 4096)
for {
SetReadTimeout(src)
if timeoutOpt == SET_TIMEOUT {
SetReadTimeout(src)
}
n, err := src.Read(buf)
// read may return EOF with n > 0
// should always process n > 0 bytes before handling error
Expand Down

0 comments on commit 86e5ba2

Please sign in to comment.