From 729008083eb9aaa5249d74576b62f21cbc5773ea Mon Sep 17 00:00:00 2001 From: xvzc Date: Mon, 9 May 2022 10:57:34 +0900 Subject: [PATCH 1/2] update packet parsing --- net/conn.go | 4 +-- packet/http.go | 70 +++++++++++++++++--------------------------------- 2 files changed, 26 insertions(+), 48 deletions(-) diff --git a/net/conn.go b/net/conn.go index 388c8e0..9f9d149 100644 --- a/net/conn.go +++ b/net/conn.go @@ -108,7 +108,7 @@ func (lConn *Conn) HandleHttp(p *packet.HttpPacket) { // Create connection to server var port = ":80" if p.Port() != "" { - port = p.Port() + port = ":" + p.Port() } rConn, err := Dial("tcp", ip + port) @@ -154,7 +154,7 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) { // Create a connection to the requested server var port = ":443" if p.Port() != "" { - port = p.Port() + port = ":" + p.Port() } rConn, err := Dial("tcp", ip + port) diff --git a/packet/http.go b/packet/http.go index d9c86d2..65e53b3 100644 --- a/packet/http.go +++ b/packet/http.go @@ -1,7 +1,9 @@ package packet import ( - "errors" + "bufio" + "net" + "net/http" "strings" ) @@ -122,58 +124,34 @@ func (p *HttpPacket) Tidy() { } func (p *HttpPacket )parse() error { - var firstLine string - for i := 0; i < len(p.raw); i++ { - if (p.raw)[i] == '\r' { - firstLine = string(p.raw[:i]) - break - } - } - - tokens := strings.Split(firstLine, " ") - - if (len(tokens) < 3) { - return errors.New("Error parsing http request") + reader := bufio.NewReader(strings.NewReader(string(p.raw))) + request, err := http.ReadRequest(reader) + if err != nil { + return err } - p.method = tokens[0] - url := tokens[1] - p.version = tokens[2] - - if strings.HasPrefix(url, "http://") { - url = strings.Replace(url, "http://", "", 1) + p.domain, p.port, err = net.SplitHostPort(request.Host) + if err != nil { + p.domain = request.Host + p.port = "" } - if strings.HasPrefix(url, "https://") { - url = strings.Replace(url, "https://", "", 1) + p.method = request.Method + p.version = request.Proto + p.path = request.URL.Path + + if request.URL.RawQuery != "" { + p.path += "?" + request.URL.RawQuery } - domain := "" - port := "" - for i := 0; i < len(url); i++ { - if url[i] == ':' { - domain = url[:i] - port = url[i:] - break - } + if request.URL.RawFragment != "" { + p.path += "#" + request.URL.RawFragment + } + if p.path == "" { + p.path = "/" + } - if url[i] == '/' { - domain = url[:i] - break - } - } - p.domain = domain - p.port = port - - path := "/" - for i := 0; i < len(url); i++ { - if url[i] == '/' { - path = url[i:] - break - } - } - - p.path = path + request.Body.Close() return nil } From 8b6bd3d650b1e8cd7e753b3cde767c7ca640eaf6 Mon Sep 17 00:00:00 2001 From: xvzc Date: Mon, 9 May 2022 13:44:00 +0900 Subject: [PATCH 2/2] set keep-alive to false --- proxy/proxy.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index 97ce557..530a10a 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -43,8 +43,7 @@ func (p *Proxy) Start() { log.Fatal("Error accepting connection: ", err) continue } - // conn.SetDeadLine(time.Now().Add(3 * time.Second)) - // conn.SetKeepAlive(false) + conn.SetKeepAlive(false) go func() { b, err := conn.ReadBytes()