Beispiel #1
0
func handleConnection(conn net.Conn) {
	if debug {
		debug.Printf("socks connect from %s\n", conn.RemoteAddr().String())
	}
	closed := false
	defer func() {
		if !closed {
			conn.Close()
		}
	}()

	buf := make([]byte, 4096)
	var n int
	var err error
	if n, err = conn.Read(buf); err != nil {
		return
	}

	var rawaddr []byte
	var addr string
	var toWrite []byte
	//log.Println(string(buf[:n]))
	if buf[0] == 'C' || buf[0] == 'G' {
		rawaddr, addr, toWrite, err = getHttpRequest(buf[:n])
		if err != nil {
			log.Println("error getting http request:", err)
			return
		}
		_, err = conn.Write([]byte("HTTP/1.1 200 Connection Established"))
		if err != nil {
			debug.Println("send connection confirmation:", err)
			return
		}
	} else {
		if err = handSocksShakeByte(buf[:n], conn); err != nil {
			log.Println("socks handshake:", err)
			return
		}
		if n, err = conn.Read(buf); err != nil {
			log.Println("read:", err)
			return
		}
		rawaddr, addr, err = getSocksRequest(buf[:n])
		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 {
		log.Println(err)
		if len(servers.srvCipher) > 1 {
			log.Println("Failed connect to all avaiable shadowsocks server")
		}
		return
	}
	if len(toWrite) > 0 {
		_, err = remote.Write(toWrite)
		if err != nil {
			log.Println("http write req:", err)
			return
		}
	}
	defer func() {
		if !closed {
			remote.Close()
		}
	}()

	go ss.PipeThenClose(conn, remote)
	ss.PipeThenClose(remote, conn)
	closed = true
	debug.Println("closed connection to", addr)
}
Beispiel #2
0
func handleConnection(conn *ss.Conn) {
	var host string

	connCnt++ // this maybe not accurate, but should be enough
	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())
	}
	closed := false
	defer func() {
		if debug {
			debug.Printf("closed pipe %s<->%s\n", conn.RemoteAddr(), host)
		}
		connCnt--
		if !closed {
			conn.Close()
		}
	}()

	host, extra, err := getRequest(conn)
	if err != nil {
		log.Println("error getting request", conn.RemoteAddr(), conn.LocalAddr(), err)
		return
	}
	debug.Println("connecting", host)
	remote, err := net.Dial("tcp", host)
	//remote, err := pipe.Dial(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 {
			log.Println("error connecting to:", host, err)
		}
		return
	}
	defer func() {
		if !closed {
			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)
	}
	go ss.PipeThenClose(conn, remote)
	ss.PipeThenClose(remote, conn)
	closed = true
	return
}