func handleConnection(conn *ss.Conn) { var host string atomic.AddInt32(&connCnt, 1) if connCnt-nextLogConnCnt >= 0 { // XXX There's no xadd in the atomic package, so it's difficult to log // the message only once with low cost. Also note nextLogConnCnt maybe // added twice for current peak connection number level. log.Printf("Number of client connections reaches %d\n", nextLogConnCnt) nextLogConnCnt += logCntDelta } // function arguments are always evaluated, so surround debug statement // with if statement if debug { debug.Printf("new client %s->%s\n", conn.RemoteAddr().String(), conn.LocalAddr()) } defer func() { if debug { debug.Printf("closing pipe %s<->%s\n", conn.RemoteAddr(), host) } atomic.AddInt32(&connCnt, -1) conn.Close() }() host, extra, err := getRequest(conn) if err != nil { log.Println("error getting request:", err) return } debug.Println("connecting", host) remote, err := net.Dial("tcp", host) if err != nil { if ne, ok := err.(*net.OpError); ok && (ne.Err == syscall.EMFILE || ne.Err == syscall.ENFILE) { // log too many open file error // EMFILE is process reaches open file limits, ENFILE is system limit log.Println("dial error:", err) } else { debug.Println("error connecting to:", host, err) } return } defer remote.Close() // write extra bytes read from if extra != nil { // 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 } } 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 return }
func handleConnection(conn *ss.Conn) { if debug { // function arguments are always evaluated, so surround debug // statement with if statement debug.Printf("socks connect from %s\n", conn.RemoteAddr().String()) } defer conn.Close() host, extra, err := getRequest(conn) if err != nil { log.Println("error getting request:", err) return } debug.Println("connecting", host) remote, err := net.Dial("tcp", host) if err != nil { if ne, ok := err.(*net.OpError); ok && (ne.Err == syscall.EMFILE || ne.Err == syscall.ENFILE) { // log too many open file error // EMFILE is process reaches open file limits, ENFILE is system limit log.Println("dial error:", err) } else { debug.Println("error connecting to:", host, err) } return } defer remote.Close() // write extra bytes read from if extra != nil { 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) 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 }
func handleConnection(conn net.Conn) { if debug { debug.Printf("socks connect from %s\n", conn.RemoteAddr().String()) } defer conn.Close() var err error = nil if err = handShake(conn); err != nil { log.Println("socks handshake:", err) return } rawaddr, addr, err := getRequest(conn) if err != nil { log.Println("error getting request:", err) return } // Sending connection established message immediately to client. // This some round trip time for creating socks connection with the client. // But if connection failed, the client will get connection reset error. _, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43}) if err != nil { debug.Println("send connection confirmation:", err) return } remote, err := createServerConn(rawaddr, addr) if err != nil { if len(servers.srvenc) > 1 { log.Println("Failed connect to all avaiable shadowsocks server") } return } defer remote.Close() 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") }