Golang Request.WriteProxy Examples

Golang Request.WriteProxy - 9 examples found. These are the top rated real world Golang examples of net/http.Request.WriteProxy extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func (s *HttpServer) forwardRequest(req *http.Request) {
	last := s.Base.Chain.lastNode
	if last == nil {
		return
	}
	cc, err := s.Base.Chain.GetConn()
	if err != nil {
		glog.V(LWARNING).Infof("[http] %s -> %s : %s", s.conn.RemoteAddr(), last.Addr, err)

		b := []byte("HTTP/1.1 503 Service unavailable\r\n" +
			"Proxy-Agent: gost/" + Version + "\r\n\r\n")
		glog.V(LDEBUG).Infof("[http] %s <- %s\n%s", s.conn.RemoteAddr(), last.Addr, string(b))
		s.conn.Write(b)
		return
	}
	defer cc.Close()

	if last.User != nil {
		req.Header.Set("Proxy-Authorization",
			"Basic "+base64.StdEncoding.EncodeToString([]byte(last.User.String())))
	}

	cc.SetWriteDeadline(time.Now().Add(WriteTimeout))
	if err = req.WriteProxy(cc); err != nil {
		glog.V(LWARNING).Infof("[http] %s -> %s : %s", s.conn.RemoteAddr(), req.Host, err)
		return
	}
	cc.SetWriteDeadline(time.Time{})

	glog.V(LINFO).Infof("[http] %s <-> %s", s.conn.RemoteAddr(), req.Host)
	s.Base.transport(s.conn, cc)
	glog.V(LINFO).Infof("[http] %s >-< %s", s.conn.RemoteAddr(), req.Host)
	return
}
Example #2
0
func doRequest(req *http.Request) (*http.Response, error) {
	if proxyURL != nil {
		c, err := dial(proxyURL.Host)
		if err != nil {
			log.Println(err)
			return nil, err
		}
		defer c.Close()

		setBasicAuth(req)
		if err := req.WriteProxy(c); err != nil {
			log.Println(err)
			return nil, err
		}
		/*
			b, err := ioutil.ReadAll(c)
			if err != nil {
				log.Println(err)
				return nil, err
			}
		*/
		return http.ReadResponse(bufio.NewReader(c), req)
	}

	return http.DefaultClient.Do(req)
}
Example #3
0
func (handler *SogouProxyHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	proxyConn := mustDialSogou(handler)

	timestamp := fmt.Sprintf("%08x", time.Now().Unix())
	request.Header.Add("X-Sogou-Timestamp", timestamp)
	tag := fmt.Sprintf("%08x", sogouTagHash(timestamp+request.Host+"SogouExplorerProxy"))
	request.Header.Add("X-Sogou-Tag", tag)
	request.Header.Add("X-Sogou-Auth", "58C41A7C258CAB58167E110BB5DEF7AF/4.1.3.8107/md5")

	request.WriteProxy(proxyConn)

	hj, ok := writer.(http.Hijacker)
	if !ok {
		log.Println("ERROR: ", "webserver doesn't support hijacking", http.StatusInternalServerError)
		http.Error(writer, "webserver doesn't support hijacking", http.StatusInternalServerError)
		return
	}
	clientConn, _, err := hj.Hijack()
	if err != nil {
		log.Println("ERROR: ", err, http.StatusInternalServerError)
		http.Error(writer, err.Error(), http.StatusInternalServerError)
		return
	}

	proxyBufReader := bufio.NewReader(proxyConn)
	response, err := http.ReadResponse(proxyBufReader, request)
	if err != nil {
		http.Error(writer, err.Error(), http.StatusInternalServerError)
		return
	}
	if request.Method == "CONNECT" {
		response.Body.Close()
	} else {
		defer response.Body.Close()
	}

	log.Printf("%s %s %s\n<- %s\n", request.RemoteAddr, request.Method, request.RequestURI, response.Status)
	response.Write(clientConn)

	if request.Method == "CONNECT" && response.StatusCode == http.StatusOK {
		go copyAndClose(proxyConn, clientConn)
		go copyAndClose(clientConn, proxyBufReader)
	} else {
		clientConn.Close()
		proxyConn.Close()
	}
}
Example #4
0
func (h *ProxyHTTPHandler) doConnect(w http.ResponseWriter, r *http.Request) {
	var (
		clientConn net.Conn
		serverConn net.Conn
		err        error
	)
	hj, ok := w.(http.Hijacker)
	if !ok {
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
	clientConn, _, err = hj.Hijack()
	if err != nil {
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
	removeProxyHeaders(r)
	pacConn, err := h.pac.PacConn(r.URL)
	if err != nil {
		http.Error(w, "", http.StatusBadGateway)
		return
	}
	if pacConn != nil {
		serverConn, err = pacConn.Dial()
		if err != nil {
			http.Error(w, "", http.StatusBadGateway)
			return
		}
		defer serverConn.Close()
		r.WriteProxy(serverConn)
	} else {
		serverConn, err = net.Dial("tcp", r.URL.Host)
		if err != nil {
			http.Error(w, "", http.StatusBadGateway)
			return
		}
		defer serverConn.Close()
		clientConn.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
	}
	defer clientConn.Close()
	defer serverConn.Close()
	go io.Copy(clientConn, serverConn)
	io.Copy(serverConn, clientConn)
}
Example #5
0
func (conn *ForwardConnection) writeHttpRequest(req *http.Request) error {
	var err error
	index := 0
	for {
		if conn.manager.overProxy {
			err = req.WriteProxy(conn.forward_conn)
		} else {
			err = req.Write(conn.forward_conn)
		}

		if nil != err {
			log.Printf("Resend request since error:%v occured.\n", err)
			conn.Close()
			conn.initForwardConn(req.Host, strings.EqualFold(req.Method, "Connect"))
		} else {
			return nil
		}
		index++
		if index == 2 {
			return err
		}
	}
	return nil
}
Example #6
0
func (client *Client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	r.Header.Add("Proxy-Authenticate", client.psw)
	c, err := net.Dial("tcp", client.remote_addr)
	if err != nil {
		//should not print
		log.Println(GetColorError(err))
		return
	}
	defer c.Close()
	if tc, ok := c.(*net.TCPConn); ok {
		if tc.SetReadBuffer(4096*32) != nil {
			log.Println(GetColorError(err))
			return
		}
		if tc.SetKeepAlive(true) != nil {
			log.Println(GetColorError(err))
			return
		}
		if tc.SetKeepAlivePeriod(1*time.Minute) != nil {
			log.Println(GetColorError(err))
			return
		}
	}
	conn := NewRecord(tls.Client(c, &tls.Config{InsecureSkipVerify: true}), client.lock, &client.upload, &client.download)
	//Send the requset to own proxy server
	read := bufio.NewReader(conn)
	if err = r.WriteProxy(conn); err != nil {
		log.Println(GetColorError(err))
		atomic.AddUint64(&client.errCounter, 1)
		return
	}
	if r.Method == "CONNECT" {
		//send the https request
		resp, err := http.ReadResponse(read, r)
		if err != nil {
			log.Println(GetColorError(err))
			return
		}
		defer resp.Body.Close()
		if resp.StatusCode != 200 {
			return
		}
		hij, ok := w.(http.Hijacker)
		if !ok {
			log.Println("httpserver does not support hijacking")
			return
		}
		proxyClient, _, e := hij.Hijack()
		if e != nil {
			log.Println("Cannot hijack connection " + e.Error())
			return
		}
		//write the 200 ok to the client
		proxyClient.Write([]byte("HTTP/1.1 200 OK\r\n\r\n"))
		//connect keep the connect alive
		go copyAndClose(proxyClient, read)
		copyAndClose(conn, proxyClient)
		return
	}
	//read the response
	resp, err := http.ReadResponse(read, r)
	if err != nil {
		log.Println(GetColorError(err))
		atomic.AddUint64(&client.errCounter, 1)
		return
	}
	defer resp.Body.Close()
	copyHeaders(w.Header(), resp.Header)
	w.WriteHeader(resp.StatusCode)
	io.Copy(w, resp.Body)
}