Beispiel #1
0
func handleConnection(conn net.Conn, server string) {
	log.Printf("socks connect from %s\n", conn.RemoteAddr().String())
	b := make([]byte, 262)
	var err error = nil
	var hasError = false
	for {
		var _ int
		buf := make([]byte, 4096)
		_, err = conn.Read(b)
		if err != nil {
			hasError = true
			break
		}
		conn.Write([]byte{0x05, 0x00})
		_, err = conn.Read(buf)
		mode := buf[1]
		if mode != 1 {
			hasError = true
			log.Println("mode != 1")
			break
		}
		var addr string
		addrType := buf[3]
		var addrToSend []byte
		if addrType == 1 {
			var addrIp net.IP = make(net.IP, 4)
			copy(addrIp, buf[4:8])
			addr = addrIp.String()
			addrToSend = buf[3:10]
		} else if addrType == 3 {
			addrLen := buf[4]
			addr = string(buf[5 : 5+addrLen])
			addrToSend = buf[3 : 5+addrLen+2]
		} else {
			hasError = true
			log.Println("unsurpported addr type")
			break
		}
		log.Println("connecting ", addr)
		conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43})

		remote, err := shadowsocks.DialWithAddrBuf(addrToSend, server)
		if err != nil {
			hasError = true
			break
		}
		c := make(chan int, 2)
		go shadowsocks.Pipe(conn, remote, c)
		go shadowsocks.Pipe(remote, conn, c)
		<-c // close the other connection whenever one connection is closed
		log.Println("closing")
		err = conn.Close()
		err1 := remote.Close()
		if err == nil {
			err = err1
		}
		break
	}
	if err != nil || hasError {
		if err != nil {
			log.Println("error ", err)
		}
		err = conn.Close()
		if err != nil {
			log.Println("close:", err)
		}
		return
	}

}
Beispiel #2
0
func handleConnection(conn shadowsocks.Conn) {
	log.Printf("socks connect from %s\n", conn.RemoteAddr().String())
	var err error = nil
	var hasError = false
	for {
		var _ int
		buf := make([]byte, 1)
		_, err = conn.Read(buf)
		if err != nil {
			hasError = true
			break
		}
		addrType := buf[0]
		var addr string
		var port int16
		if addrType == 1 {
			buf = make([]byte, 6)
			_, err = conn.Read(buf)
			if err != nil {
				hasError = true
				break
			}
			var addrIp net.IP = make(net.IP, 4)
			copy(addrIp, buf[0:4])
			addr = addrIp.String()
			sb := bytes.NewBuffer(buf[4:6])
			binary.Read(sb, binary.BigEndian, &port)
		} else if addrType == 3 {
			_, err = conn.Read(buf)
			if err != nil {
				hasError = true
				break
			}
			addrLen := buf[0]
			buf = make([]byte, addrLen+2)
			_, err = conn.Read(buf)
			if err != nil {
				hasError = true
				break
			}
			sb := bytes.NewBuffer(buf[0:addrLen])
			addr = sb.String()
			sb = bytes.NewBuffer(buf[addrLen : addrLen+2])
			binary.Read(sb, binary.BigEndian, &port)
		} else {
			hasError = true
			log.Println("unsurpported addr type")
			break
		}
		log.Println("connecting ", addr)
		var remote net.Conn
		remote, err = net.Dial("tcp", fmt.Sprintf("%s:%d", addr, port))
		if err != nil {
			hasError = true
			break
		}
		if err != nil {
			hasError = true
			break
		}
		c := make(chan int, 2)
		go shadowsocks.Pipe(conn, remote, c)
		go shadowsocks.Pipe(remote, conn, c)
		<-c // close the other connection whenever one connection is closed
		log.Println("closing")
		err = conn.Close()
		err1 := remote.Close()
		if err == nil {
			err = err1
		}
		break
	}
	if err != nil || hasError {
		if err != nil {
			log.Println("error ", err)
		}
		err = conn.Close()
		if err != nil {
			log.Println("close:", err)
		}
		return
	}

}