go-shadowsocks2 icon indicating copy to clipboard operation
go-shadowsocks2 copied to clipboard

Support http proxy

Open BigSully opened this issue 3 years ago • 3 comments

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
}

BigSully avatar Jan 18 '22 12:01 BigSully

awesome bro

JimLee1996 avatar Jan 18 '22 12:01 JimLee1996

great!!

slrem avatar Jul 20 '22 03:07 slrem

这是来自QQ邮箱的假期自动回复邮件。   您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。

donghaoahahahah avatar Oct 11 '22 07:10 donghaoahahahah