Example #1
0
func (receiver *httpReceiver) handleTunnel(con net.Conn, host string, port int, isdomain bool) {
	con.Write(httpOk)
	ci, flag, err := client.NewRemoteServer(receiver.cfg).ChooseServer()
	if err != nil {
		logger.Errorf("create server client error %s", err)
		return
	}
	if flag {
		cli := ci.(*client.SSLClient)
		defer func() {
			cli.Conn.Close()
			con.Close()
		}()
		var err = receiver.handleSocks5(host, port, isdomain, &cli.Conn)
		if err != nil {
			logger.Errorf("Http--->handle error message %s\n", err)
			return
		}
		pipe(&cli.Conn, con)
	}
	cli := ci.(*client.Client)
	defer func() {
		cli.Conn.Close()
	}()
	err = receiver.handleSocks5(host, port, isdomain, cli.Conn)
	if err != nil {
		logger.Errorf("Http--->handle error message %s\n", err)
		return
	}
	pipe(cli.Conn, con)
}
Example #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)
}
Example #3
0
func (receiver *httpReceiver) handleHTTPTunnel(rw http.ResponseWriter, req *http.Request, domain string, port int, isdomain bool) {
	ci, flag, err := client.NewRemoteServer(receiver.cfg).ChooseServer()
	if err != nil {
		logger.Errorf("Http--->create server client error %s", err)
		return
	}
	if flag {
		cli := ci.(*client.SSLClient)
		defer func() {
			cli.Conn.Close()
		}()
		var err = receiver.handleSocks5(domain, port, isdomain, &cli.Conn)
		if err != nil {
			logger.Errorf("Http--->handle error message %s\n", err)
			return
		}
		if err = req.Write(&cli.Conn); err != nil {
			logger.Errorf("Http->write data error %s\n", err)
			return
		}
		rsp, err := http.ReadResponse(bufio.NewReader(&cli.Conn), nil)
		if err != nil {
			logger.Errorf("Http->read response error %s\n", err)
			return
		}
		defer rsp.Body.Close()
		//clearHeaders(rw.Header())
		//copyHeaders(rw.Header(), rsp.Header)
		clearAndCopy(rw.Header(), rsp.Header)
		if _, err = io.Copy(rw, rsp.Body); err != nil {
			logger.Errorf("http->copy response error %s\n", err)
			return
		}
		return
	}
	cli := ci.(*client.Client)
	defer func() {
		cli.Conn.Close()
	}()
	err = receiver.handleSocks5(domain, port, isdomain, cli.Conn)
	if err != nil {
		logger.Errorf("Http--->handle error message %s\n", err)
		return
	}
	if err = req.Write(cli.Conn); err != nil {
		logger.Errorf("Http->write data error %s\n", err)
		return
	}
	rsp, err := http.ReadResponse(bufio.NewReader(cli.Conn), nil)
	if err != nil {
		logger.Errorf("Http->read response error %s\n", err)
		return
	}
	defer rsp.Body.Close()
	//clearHeaders(rw.Header())
	//copyHeaders(rw.Header(), rsp.Header)
	clearAndCopy(rw.Header(), rsp.Header)
	rw.WriteHeader(rsp.StatusCode)
	if _, err = io.Copy(rw, rsp.Body); err != nil {
		logger.Errorf("http->copy response error %s\n", err)
		return
	}
}