From 9d9e17f3d24eeb3feb864a86abff190920e0c920 Mon Sep 17 00:00:00 2001 From: xvzc Date: Sat, 5 Mar 2022 11:51:17 +0900 Subject: [PATCH] update port parsing, and request order --- net/conn.go | 30 ++++++++++++++++++++---------- packet/http.go | 20 ++++++++++++++++---- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/net/conn.go b/net/conn.go index f2cb504..f256bff 100644 --- a/net/conn.go +++ b/net/conn.go @@ -94,7 +94,12 @@ func (lConn *Conn) HandleHttp(p packet.HttpPacket) { log.Debug("[DOH] Found ", ip, " with ", p.Domain()) // Create connection to server - rConn, err := Dial("tcp", ip+":80") + var port = ":80" + if p.Port() != "" { + port = p.Port() + } + + rConn, err := Dial("tcp", ip + port) if err != nil { log.Debug("[HTTPS] ", err) return @@ -102,6 +107,9 @@ func (lConn *Conn) HandleHttp(p packet.HttpPacket) { log.Debug("[HTTP] Connected to ", p.Domain()) + go lConn.Serve(rConn, "[HTTP]", "localhost", p.Domain()) + go rConn.Serve(lConn, "[HTTP]", p.Domain(), "localhost") + _, err = rConn.Write(p.Raw()) if err != nil { log.Debug("[HTTP] Error sending request to ", p.Domain(), err) @@ -109,10 +117,6 @@ func (lConn *Conn) HandleHttp(p packet.HttpPacket) { } log.Debug("[HTTP] Sent a request to ", p.Domain()) - - go rConn.Serve(lConn, "[HTTP]", p.Domain(), "localhost") - lConn.Serve(rConn, "[HTTP]", "localhost", p.Domain()) - } func (lConn *Conn) HandleHttps(p packet.HttpPacket) { @@ -127,7 +131,12 @@ func (lConn *Conn) HandleHttps(p packet.HttpPacket) { log.Debug("[DOH] Found ", ip, " with ", p.Domain()) // Create a connection to the requested server - rConn, err := Dial("tcp", ip+":443") + var port = ":443" + if p.Port() != "" { + port = p.Port() + } + + rConn, err := Dial("tcp", ip + port) if err != nil { log.Debug("[HTTPS] ", err) return @@ -135,7 +144,7 @@ func (lConn *Conn) HandleHttps(p packet.HttpPacket) { log.Debug("[HTTPS] Connected to ", p.Domain()) - _, err = lConn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n")) + _, err = lConn.Write([]byte(p.Version() + " 200 Connection Established\r\n\r\n")) if err != nil { log.Debug("[HTTPS] Error sending 200 Connection Established to the client", err) return @@ -152,6 +161,10 @@ func (lConn *Conn) HandleHttps(p packet.HttpPacket) { log.Debug("[HTTPS] Client sent hello ", len(clientHello), "bytes") + // Generate a go routine that reads from the server + go lConn.Serve(rConn, "[HTTPS]", "localhost", p.Domain()) + go rConn.Serve(lConn, "[HTTPS]", p.Domain(), "localhost") + pkt := packet.NewHttpsPacket(clientHello) chunks := pkt.SplitInChunks() @@ -161,9 +174,6 @@ func (lConn *Conn) HandleHttps(p packet.HttpPacket) { return } - // Generate a go routine that reads from the server - go rConn.Serve(lConn, "[HTTPS]", p.Domain(), "localhost") - lConn.Serve(rConn, "[HTTPS]", "localhost", p.Domain()) } func (from *Conn) Serve(to *Conn, proto string, fd string, td string) { diff --git a/packet/http.go b/packet/http.go index 4428f4d..5e3de00 100644 --- a/packet/http.go +++ b/packet/http.go @@ -44,17 +44,23 @@ type HttpPacket struct { raw []byte method string domain string + port string path string version string } +func ParseUrl(raw []byte) { + +} + func NewHttpPacket(raw []byte) HttpPacket { - method, domain, path, version := parse(raw) + method, domain, port, path, version := parse(raw) return HttpPacket{ raw: raw, method: method, domain: domain, + port: port, path: path, version: version, } @@ -71,6 +77,10 @@ func (p *HttpPacket) Domain() string { return p.domain } +func (p *HttpPacket) Port() string { + return p.port +} + func (p *HttpPacket) Version() string { return p.version } @@ -115,7 +125,7 @@ func (p *HttpPacket) Tidy() { p.raw = []byte(result) } -func parse(raw []byte) (string, string, string, string) { +func parse(raw []byte) (string, string, string, string, string) { var firstLine string for i := 0; i < len(raw); i++ { if (raw)[i] == '\n' { @@ -133,10 +143,12 @@ func parse(raw []byte) (string, string, string, string) { url = strings.Replace(url, "http://", "", 1) url = strings.Replace(url, "https://", "", 1) - domain := url + var domain string + var port string for i := 0; i < len(url); i++ { if url[i] == ':' { domain = url[:i] + port = url[i:] break } @@ -154,5 +166,5 @@ func parse(raw []byte) (string, string, string, string) { } } - return method, domain, path, version + return method, domain, port, path, version }