From 0f5eae5daef7e7cb2240977a71a46b8115775f1e Mon Sep 17 00:00:00 2001 From: xvzc Date: Sun, 16 Jan 2022 01:44:00 +0900 Subject: [PATCH] fix recursive redirect issue --- net/conn.go | 6 ++++++ packet/http.go | 45 ++++++++++++++++++++++++++++++++++++--------- proxy/proxy.go | 4 ---- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/net/conn.go b/net/conn.go index 24e3fd9..1058bd2 100644 --- a/net/conn.go +++ b/net/conn.go @@ -72,6 +72,10 @@ func (conn *Conn) ReadBytes() ([]byte, error) { } func (lConn *Conn) HandleHttp(p packet.HttpPacket) { + p.Tidy() + + log.Debug("[HTTP] request: \n\n" + string(p.Raw())) + ip, err := doh.Lookup(p.Domain()) if err != nil { log.Debug("[HTTP] Error looking up for domain: ", err) @@ -99,6 +103,8 @@ func (lConn *Conn) HandleHttp(p packet.HttpPacket) { } func (lConn *Conn) HandleHttps(p packet.HttpPacket) { + log.Debug("[HTTPS] request: \n\n" + string(p.Raw())) + ip, err := doh.Lookup(p.Domain()) if err != nil { log.Debug("[HTTPS] Error looking up for domain: ", p.Domain(), " ", err) diff --git a/packet/http.go b/packet/http.go index 1556d59..3158932 100644 --- a/packet/http.go +++ b/packet/http.go @@ -44,16 +44,18 @@ type HttpPacket struct { raw []byte method string domain string + path string version string } func NewHttpPacket(raw []byte) HttpPacket { - method, domain, version := parse(raw) + method, domain, path, version := parse(raw) return HttpPacket{ raw: raw, method: method, domain: domain, + path: path, version: version, } } @@ -64,9 +66,11 @@ func (p *HttpPacket) Raw() []byte { func (p *HttpPacket) Method() string { return p.method } + func (p *HttpPacket) Domain() string { return p.domain } + func (p *HttpPacket) Version() string { return p.version } @@ -83,10 +87,13 @@ func (p *HttpPacket) IsConnectMethod() bool { return p.Method() == "CONNECT" } -func (p *HttpPacket) RemoveProxyHeader() { +func (p *HttpPacket) Tidy() { s := string(p.raw) lines := strings.Split(s, "\n") + + lines[0] = p.method + " " + p.path + " " + p.version + for i := 0; i < len(lines); i++ { if strings.HasPrefix(lines[i], "Proxy-Connection") { lines[i] = "" @@ -105,24 +112,44 @@ func (p *HttpPacket) RemoveProxyHeader() { p.raw = []byte(result) } -func parse(raw []byte) (string, string, string) { +func parse(raw []byte) (string, string, string, string) { var firstLine string for i := 0; i < len(raw); i++ { if (raw)[i] == '\n' { firstLine = string((raw)[:i]) + break } } tokens := strings.Split(firstLine, " ") method := strings.TrimSpace(tokens[0]) - domain := strings.TrimSpace(tokens[1]) + url := strings.TrimSpace(tokens[1]) version := strings.TrimSpace(tokens[2]) - domain = strings.Replace(domain, "http://", "", 1) - domain = strings.Replace(domain, "https://", "", 1) - domain = strings.Split(domain, ":")[0] - domain = strings.Split(domain, "/")[0] + url = strings.Replace(url, "http://", "", 1) + url = strings.Replace(url, "https://", "", 1) - return method, domain, version + domain := url + for i := 0; i < len(url); i++ { + if url[i] == ':' { + domain = url[:i] + break + } + + if url[i] == '/' { + domain = url[:i] + break + } + } + + path := "/" + for i := 0; i < len(url); i++ { + if url[i] == '/' { + path = url[i:] + break + } + } + + return method, domain, path, version } diff --git a/proxy/proxy.go b/proxy/proxy.go index 8883901..9274dbe 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -54,10 +54,6 @@ func (p *Proxy) Start() { return } - pkt.RemoveProxyHeader() - - log.Debug("New request: \n\n" + string(pkt.Raw())) - if pkt.IsConnectMethod() { log.Debug("[HTTPS] Start") go conn.HandleHttps(pkt)