Esempio n. 1
0
func (receiver *socksReceiver) handleConnection(conn net.Conn) {
	data, host, err := receiver.request(conn)
	if err != nil {
		logger.Errorf("Server:---->cannot find host %s\n", err)
		return
	}
	cli := client.NewClient(host)
	if cli == nil {
		logger.Error("handle connection error,cannot create client.")
		return
	}
	defer func() {
		//fixe bug when cli is nil
		if cli != nil {
			cli.Close()
		}
		conn.Close()
	}()
	if _, err := cli.Conn.Write(data); err != nil {
		logger.Error("Server:----->write data error")
		return
	}

	c.SetTimeout(cli.Conn.SetReadDeadline, receiver.cfg.Timeout)
	c.SetTimeout(conn.SetReadDeadline, receiver.cfg.Timeout)
	//for comment
	go io.Copy(cli.Conn, conn)
	io.Copy(conn, cli.Conn)

}
Esempio n. 2
0
func (receiver *receiver) handleConnection(conn net.Conn) {
	var err error
	if err = receiver.handshake(conn); err != nil {
		logger.Errorf("Socks5--->handshake error %s\n", err)
		return
	}

	//set default timeout
	cfg.SetTimeout(conn.SetReadDeadline, receiver.config.Timeout)
	//conn.SetReadDeadline(time.Now().Add(time.Duration(s.config.Timeout) * time.Second))

	rawAddr, host, domian, err := receiver.request(conn)
	if err != nil {
		logger.Errorf("Socks5--->get host failed %s\n", err)
		return
	}
	_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43})
	if err != nil {
		logger.Errorf("Socks5--->error send data to client %s\n", err)
		return
	}
	//need connect to remote server or not
	if domian && !cfg.IsInWhiteList(strings.Split(host, ":")[0]) {
		result := cfg.ParserDomain(strings.Split(host, ":")[0])
		if result {
			handleChina(conn, host)
			return
		}
	} else if !domian {
		result := cfg.ParserIP(strings.Split(host, ":")[0])
		if result {
			handleChina(conn, host)
			return
		}
	}
	ci, flag, err := client.NewRemoteServer(receiver.config).ChooseServer()
	if err != nil {
		logger.Errorf("Socks5--->connect to server failed %s", err)
		return
	}
	if flag {
		cli := ci.(*client.SSLClient)
		cfg.SetTimeout(cli.Conn.SetReadDeadline, receiver.config.Timeout)
		defer func() {
			cli.Conn.Close()
			conn.Close()
		}()
		if _, err = cli.Conn.Write(rawAddr); err != nil {
			logger.Errorf("Socks5--->handle error message when write data %s \n", err)
			return
		}
		pipe(&cli.Conn, conn)
		return
	}
	cli := ci.(*client.Client)
	cfg.SetTimeout(cli.Conn.SetReadDeadline, receiver.config.Timeout)
	//cli.Conn.SetReadDeadline(time.Now().Add(time.Duration(s.config.Timeout) * time.Second))
	defer func() {
		cli.Conn.Close()
		conn.Close()
	}()
	if _, err = cli.Conn.Write(rawAddr); err != nil {
		logger.Errorf("Socks5--->handle error message when write data %s \n", err)
		return
	}
	pipe(cli.Conn, conn)
}