Skip to content

Commit

Permalink
Add client connection number in server debug msg.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Dec 31, 2012
1 parent 8c7663c commit 4295a7c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
22 changes: 17 additions & 5 deletions cmd/shadowsocks-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,24 @@ func getRequest(conn *ss.Conn) (host string, extra []byte, err error) {
return
}

var connCnt int32

func handleConnection(conn *ss.Conn) {
var host string
if debug {
// function arguments are always evaluated, so surround debug
// statement with if statement
debug.Printf("socks connect from %s\n", conn.RemoteAddr().String())
debug.Printf("new client %s->%s\n", conn.RemoteAddr().String(), conn.LocalAddr())
atomic.AddInt32(&connCnt, 1)
defer func() {
atomic.AddInt32(&connCnt, -1)
debug.Printf("closing pipe %s<->%s\n", conn.RemoteAddr(), host)
conn.Close()
debug.Printf("%d concurrent client connections\n", connCnt)
}()
} else {
defer conn.Close()
}
defer conn.Close()

host, extra, err := getRequest(conn)
if err != nil {
Expand All @@ -107,18 +118,19 @@ func handleConnection(conn *ss.Conn) {
defer remote.Close()
// write extra bytes read from
if extra != nil {
debug.Println("getRequest read extra data, writing to remote, len", len(extra))
// debug.Println("getRequest read extra data, writing to remote, len", len(extra))
if _, err = remote.Write(extra); err != nil {
debug.Println("write request extra error:", err)
return
}
}
debug.Println("piping", host)
if debug {
debug.Printf("piping %s<->%s", conn.RemoteAddr(), host)
}
c := make(chan byte, 2)
go ss.Pipe(conn, remote, c)
go ss.Pipe(remote, conn, c)
<-c // close the other connection whenever one connection is closed
debug.Println("closing", host)
return
}

Expand Down
12 changes: 8 additions & 4 deletions shadowsocks/pipe.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package shadowsocks

import (
"io"
// "io"
"net"
"time"
)
Expand Down Expand Up @@ -31,9 +31,13 @@ func Pipe(src, dst net.Conn, end chan byte) {
}
}
if err != nil {
if err != io.EOF {
Debug.Println("read:", err)
}
// always "use of closed network connection", but no easy way to
// identify this specific error. So just leave the error along
/*
if bool(Debug) && err != io.EOF {
Debug.Println("read:", err)
}
*/
break
}
}
Expand Down

0 comments on commit 4295a7c

Please sign in to comment.