Skip to content

Commit

Permalink
Add -t for local, always set timeout for data transfer.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Apr 26, 2015
1 parent 67ddbe4 commit 7aed8e8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
7 changes: 5 additions & 2 deletions cmd/shadowsocks-local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func handShake(conn net.Conn) (err error) {
buf := make([]byte, 258)

var n int
ss.SetReadTimeout(conn)
// make sure we get the nmethod field
if n, err = io.ReadAtLeast(conn, buf, idNmethod+1); err != nil {
return
Expand Down Expand Up @@ -92,6 +93,7 @@ func getRequest(conn net.Conn) (rawaddr []byte, host string, err error) {
// refer to getRequest in server.go for why set buffer size to 263
buf := make([]byte, 263)
var n int
ss.SetReadTimeout(conn)
// read till we get possible domain length field
if n, err = io.ReadAtLeast(conn, buf, idDmLen+1); err != nil {
return
Expand Down Expand Up @@ -314,8 +316,8 @@ func handleConnection(conn net.Conn) {
}
}()

go ss.PipeThenClose(conn, remote, ss.NO_TIMEOUT)
ss.PipeThenClose(remote, conn, ss.NO_TIMEOUT)
go ss.PipeThenClose(conn, remote)
ss.PipeThenClose(remote, conn)
closed = true
debug.Println("closed connection to", addr)
}
Expand Down Expand Up @@ -354,6 +356,7 @@ func main() {
flag.StringVar(&cmdLocal, "b", "", "local address, listen only to this address if specified")
flag.StringVar(&cmdConfig.Password, "k", "", "password")
flag.IntVar(&cmdConfig.ServerPort, "p", 0, "server port")
flag.IntVar(&cmdConfig.Timeout, "t", 300, "timeout in seconds")
flag.IntVar(&cmdConfig.LocalPort, "l", 0, "local socks5 proxy port")
flag.StringVar(&cmdConfig.Method, "m", "", "encryption method, default: aes-256-cfb")
flag.BoolVar((*bool)(&debug), "d", false, "print debug message")
Expand Down
9 changes: 3 additions & 6 deletions cmd/shadowsocks-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (

var debug ss.DebugLog

const dnsGoroutineNum = 64

func getRequest(conn *ss.Conn) (host string, extra []byte, err error) {
const (
idType = 0 // address type index
Expand Down Expand Up @@ -62,7 +60,6 @@ func getRequest(conn *ss.Conn) (host string, extra []byte, err error) {
}

if n < reqLen { // rare case
ss.SetReadTimeout(conn)
if _, err = io.ReadFull(conn, buf[n:reqLen]); err != nil {
return
}
Expand Down Expand Up @@ -154,8 +151,8 @@ func handleConnection(conn *ss.Conn) {
if debug {
debug.Printf("piping %s<->%s", conn.RemoteAddr(), host)
}
go ss.PipeThenClose(conn, remote, ss.SET_TIMEOUT)
ss.PipeThenClose(remote, conn, ss.NO_TIMEOUT)
go ss.PipeThenClose(conn, remote)
ss.PipeThenClose(remote, conn)
closed = true
return
}
Expand Down Expand Up @@ -321,7 +318,7 @@ func main() {
flag.StringVar(&configFile, "c", "config.json", "specify config file")
flag.StringVar(&cmdConfig.Password, "k", "", "password")
flag.IntVar(&cmdConfig.ServerPort, "p", 0, "server port")
flag.IntVar(&cmdConfig.Timeout, "t", 60, "connection timeout (in seconds)")
flag.IntVar(&cmdConfig.Timeout, "t", 300, "timeout in seconds")
flag.StringVar(&cmdConfig.Method, "m", "", "encryption method, default: aes-256-cfb")
flag.IntVar(&core, "core", 0, "maximum number of CPU cores to use, default is determinied by Go runtime")
flag.BoolVar((*bool)(&debug), "d", false, "print debug message")
Expand Down
3 changes: 3 additions & 0 deletions shadowsocks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,7 @@ func UpdateConfig(old, new *Config) {
if old.Method == "table" {
old.Method = ""
}

old.Timeout = new.Timeout
readTimeout = time.Duration(old.Timeout) * time.Second
}
11 changes: 2 additions & 9 deletions shadowsocks/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,19 @@ import (
"time"
)

const (
NO_TIMEOUT = iota
SET_TIMEOUT
)

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

// PipeThenClose copies data from src to dst, closes dst when done.
func PipeThenClose(src, dst net.Conn, timeoutOpt int) {
func PipeThenClose(src, dst net.Conn) {
defer dst.Close()
buf := leakyBuf.Get()
defer leakyBuf.Put(buf)
for {
if timeoutOpt == SET_TIMEOUT {
SetReadTimeout(src)
}
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 7aed8e8

Please sign in to comment.