From 34b035f4ece554c1bc1d1aebc95233551ae1654d Mon Sep 17 00:00:00 2001 From: xvzc Date: Fri, 14 Jan 2022 01:06:14 +0900 Subject: [PATCH] fix http --- net/conn.go | 61 ++++++++++++++++++++++++++++++++--------------------- net/dial.go | 6 +++--- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/net/conn.go b/net/conn.go index d1bdff9..7685ae8 100644 --- a/net/conn.go +++ b/net/conn.go @@ -14,6 +14,10 @@ type Conn struct { conn net.Conn } +func (c *Conn) CloseWrite() { + c.conn.(*net.TCPConn).CloseWrite() +} + func (c *Conn) Close() { c.conn.Close() } @@ -70,29 +74,37 @@ func (conn *Conn) ReadBytes() ([]byte, error) { func (lConn *Conn) HandleHttp(p packet.HttpPacket) { ip, err := doh.Lookup(p.Domain()) if err != nil { - log.Debug("[HTTPS] Error looking up for domain: ", err) + log.Debug("[HTTP] Error looking up for domain: ", err) } - log.Debug("[HTTPS] Found ip over HTTPS: ", ip) + log.Debug("[HTTP] Found ip over HTTPS: ", ip) - // Create connection to server - rConn, err := Dial("tcp", ip+":80") + rConn, err := Dial("tcp", ip+":80") // create connection to server if err != nil { - log.Debug("[HTTPS] ", err) + log.Debug(err) return } defer rConn.Close() - log.Debug("[HTTP] Connected to the server.") - - go rConn.Serve(lConn, "HTTP") - - _, err = rConn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n")) - if err != nil { - log.Debug("[HTTP] Error sending request to the server: ", err) + if _, err := rConn.Write(p.Raw()); err != nil { + log.Debug("failed:", err) + return } - log.Debug("[HTTP] Sent a request to the server") + defer rConn.CloseWrite() - go lConn.Serve(&rConn, "HTTP") + buf, err := rConn.ReadBytes() + if err != nil { + log.Debug("failed:", err) + return + } + + log.Debug("[HTTP] Response from the server : \n\n", string(buf)) + + // Write to client + if _, err = lConn.Write(buf); err != nil { + log.Debug("failed:", err) + return + } + defer lConn.CloseWrite() } func (lConn *Conn) HandleHttps(p packet.HttpPacket) { @@ -128,7 +140,7 @@ func (lConn *Conn) HandleHttps(p packet.HttpPacket) { log.Debug("[HTTPS] Client "+lConn.RemoteAddr().String()+" sent hello: ", len(clientHello), "bytes") // Generate a go routine that reads from the server - go rConn.Serve(lConn, "HTTPS") + go rConn.ServeHttps(lConn) pkt := packet.NewHttpsPacket(clientHello) @@ -139,25 +151,26 @@ func (lConn *Conn) HandleHttps(p packet.HttpPacket) { } // Read from the client - lConn.Serve(&rConn, "HTTPS") + lConn.ServeHttps(rConn) } -func (from *Conn) Serve(to *Conn, proto string) { +func (from *Conn) ServeHttps(to *Conn) { for { buf, err := from.ReadBytes() if err != nil { - log.Debug("["+proto+"] "+"Error reading from ", from.RemoteAddr()) - log.Debug("["+proto+"] ", err) - log.Debug("[" + proto + "] " + "Exiting Serve() method. ") + log.Debug("[HTTPS] "+"Error reading from ", from.RemoteAddr()) + log.Debug("[HTTPS] ", err) + log.Debug("[HTTPS] " + "Exiting Serve() method. ") break } - log.Debug("["+proto+"] ", from.RemoteAddr(), " sent data: ", len(buf), "bytes") + log.Debug("[HTTPS] ", from.RemoteAddr(), " sent data: ", len(buf), "bytes") if _, err := to.Write(buf); err != nil { - log.Debug("["+proto+"] "+"Error Writing to ", to.RemoteAddr()) - log.Debug("["+proto+"] ", err) - log.Debug("[" + proto + "] " + "Exiting Serve() method. ") + log.Debug("[HTTPS] "+"Error Writing to ", to.RemoteAddr()) + log.Debug("[HTTPS] ", err) + log.Debug("[HTTPS] " + "Exiting Serve() method. ") break } + defer to.CloseWrite() } } diff --git a/net/dial.go b/net/dial.go index 515f9c2..7c49ab5 100644 --- a/net/dial.go +++ b/net/dial.go @@ -13,11 +13,11 @@ func Listen(network, address string) (Listener, error) { return Listener{listener: l}, nil } -func Dial(network, address string) (Conn, error) { +func Dial(network, address string) (*Conn, error) { conn, err := net.Dial(network, address) if err != nil { - return Conn{}, err + return &Conn{}, err } - return Conn{conn: conn}, nil + return &Conn{conn: conn}, nil }