Example #1
0
func (conn *ForwardConnection) initForwardConn(proxyAddr string, isHttps bool) error {
	if !strings.Contains(proxyAddr, ":") {
		proxyAddr = proxyAddr + ":80"
	}

	if nil != conn.forward_conn && conn.proxyAddr == proxyAddr {
		if !util.IsDeadConnection(conn.forward_conn) {
			return nil
		}
	}
	conn.Close()
	var err error
	conn.conn_url, err = url.Parse(conn.manager.target)
	if nil != err {
		return err
	}

	addr := conn.conn_url.Host
	lookup_trusted_dns := false
	if !conn.manager.overProxy {
		if isHttps || (conn.manager.inject_crlf) {
			lookup_trusted_dns = true
		}
	}

	if conn.use_sys_dns {
		lookup_trusted_dns = false
	}

	isSocks := strings.HasPrefix(strings.ToLower(conn.conn_url.Scheme), "socks")
	if !isSocks {
		conn.forward_conn, err = conn.dialRemote(addr, lookup_trusted_dns)
	} else {
		proxy := &socks.Proxy{Addr: conn.conn_url.Host}
		if nil != conn.conn_url.User {
			proxy.Username = conn.conn_url.User.Username()
			proxy.Password, _ = conn.conn_url.User.Password()
		}
		conn.forward_conn, err = proxy.Dial("tcp", proxyAddr)
	}
	if nil != err {
		log.Printf("Failed to dial address:%s for reason:%s\n", addr, err.Error())
		conn.Close()
		return err
	} else {
		conn.proxyAddr = proxyAddr
	}
	if nil == conn.forwardChan {
		conn.forwardChan = make(chan int, 2)
	}
	conn.buf_forward_conn = bufio.NewReader(conn.forward_conn)
	conn.closed = false
	return nil
}
Example #2
0
func (conn *ForwardConnection) IsClosed() bool {
	if nil != conn.forward_conn && !conn.closed {
		return util.IsDeadConnection(conn.forward_conn)
	}
	return true
}