Skip to content

Support http proxy #227

@BigSully

Description

@BigSully

I'm currently running the http proxy locally with the following implementation, which takes care of both plain http request and the http CONNECT. I'm wondering whether the http proxy can be supported. If it's okay, I can make a pull request.

in main.go

flag.StringVar(&flags.HttpProxy, "http-proxy", "", "(client-only) http CONNECT listen address")

if flags.HttpProxy != "" {
	go httpLocal(flags.HttpProxy, addr, ciph.StreamConn)
}

in tcp.go

func httpLocal(addr, server string, shadow func(net.Conn) net.Conn) {
	logf("http proxy %s <-> %s", addr, server)
	tcpLocal(addr, server, shadow, func(c net.Conn) (socks.Addr, error) { return httpproxy.Handshake(c) })
}

httpproxy.go

package httpproxy

import (
	"bufio"
	"bytes"
	"io"
	"net/http"
	"strings"

	"github.com/shadowsocks/go-shadowsocks2/socks"
)

func Handshake(rw io.ReadWriter) (socks.Addr, error) {
	var buf bytes.Buffer
	req, err := http.ReadRequest(bufio.NewReader(io.TeeReader(rw, &buf))) // TeeReader keeps a copy of data read in case it's a plain http req
	if err != nil {
		return nil, err
	}

	/**
	 * for plain http://
	 *
	 * GET /ip HTTP/1.1
	 * Host: httpbin.org
	 * User-Agent: curl/7.79.1
	 * Accept: &#42;/&#42;
	 * Proxy-Connection: Keep-Alive
	 * content-length: 0
	 */
	if req.Method != "CONNECT" {
		target := req.Host
		if !strings.Contains(target, ":") {
			target = target + ":80"
		}
		addr := socks.ParseAddr(target)

		data := buf.Bytes()

		nAddr := len(addr)
		nData := len(data)
		addrWithData := make([]byte, nAddr+nData)
		copy(addrWithData[:nAddr], addr)
		copy(addrWithData[nAddr:], data)

		return addrWithData, nil
	}

	/**
	 * for tcp/ssl/tls, including https://
	 *
	 * CONNECT streamline.t-mobile.com:22 HTTP/1.1
	 * User-Agent: curl/7.79.1
	 */
	rw.Write([]byte("HTTP/1.1 200 Connection Established\r\n" +
		"Proxy-agent: Golang-Proxy\r\n" +
		"\r\n"))
	target := req.RequestURI
	addr := socks.ParseAddr(target)

	return addr, nil
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions