Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support http proxy #227

Open
BigSully opened this issue Jan 18, 2022 · 3 comments
Open

Support http proxy #227

BigSully opened this issue Jan 18, 2022 · 3 comments

Comments

@BigSully
Copy link

BigSully commented Jan 18, 2022

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
}
@JimLee1996
Copy link

awesome bro

@slrem
Copy link

slrem commented Jul 20, 2022

great!!

@donghaoahahahah

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants