From 2cb7cde1d0a21363c7cdbee2aecdb2ed212c0543 Mon Sep 17 00:00:00 2001 From: Chen Yufei Date: Fri, 14 Dec 2012 10:02:05 +0800 Subject: [PATCH] Set request buffer size to 260. Testing shows that usually the first read will get just the request, no more extra data. --- cmd/shadowsocks-server/server.go | 5 +++-- shadowsocks/config.go | 2 +- shadowsocks/pipe.go | 12 ++++++------ 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/cmd/shadowsocks-server/server.go b/cmd/shadowsocks-server/server.go index be35bd7f..a18404ee 100644 --- a/cmd/shadowsocks-server/server.go +++ b/cmd/shadowsocks-server/server.go @@ -20,13 +20,14 @@ func getRequest(conn *ss.Conn) (host string, extra []byte, err error) { // buf size should at least have the same size with the largest possible // request size (when addrType is 3, domain name has at most 256 bytes) // 1(addrType) + 1(lenByte) + 256(max length address) + 2(port) - buf := make([]byte, 4096, 4096) // use 4096 to read more if possible - cur := 0 // current location in buf + buf := make([]byte, 260, 260) + cur := 0 // current location in buf // first read the complete request, may read extra bytes var n int for { // hopefully, we should only need one read to get the complete request + // this read normally will read just the request, no extra data if n, err = conn.Read(buf[cur:]); err != nil { // debug.Println("read request error:", err) return diff --git a/shadowsocks/config.go b/shadowsocks/config.go index f37c11fb..7d22f924 100644 --- a/shadowsocks/config.go +++ b/shadowsocks/config.go @@ -26,7 +26,7 @@ type Config struct { func ParseConfig(path string) *Config { file, err := os.Open(path) // For read access. if err != nil { - log.Fatalf("error opening config file %s: %v\n", file, err) + log.Fatal("error opening config file:", err) } defer file.Close() data, err := ioutil.ReadAll(file) diff --git a/shadowsocks/pipe.go b/shadowsocks/pipe.go index befdd381..b2ca8f51 100644 --- a/shadowsocks/pipe.go +++ b/shadowsocks/pipe.go @@ -12,16 +12,16 @@ func Pipe(src, dst net.Conn, end chan byte) { // introducing unnecessary overhead. buf := make([]byte, 4096) for { - num, err := src.Read(buf) - // read may return EOF with num > 0 - // should always process num > 0 bytes before handling error - if num > 0 { - if _, err = dst.Write(buf[0:num]); err != nil { + n, err := src.Read(buf) + // read may return EOF with n > 0 + // should always process n > 0 bytes before handling error + if n > 0 { + if _, err = dst.Write(buf[0:n]); err != nil { Debug.Println("write:", err) break } } - if num == 0 { // num == 0 should associate with EOF + if n == 0 { // n == 0 should associate with EOF break } if err != nil {