From e319e0a0fe2a48bd3732fd9fec6d284d18b2b77a Mon Sep 17 00:00:00 2001 From: xvzc Date: Fri, 14 Jan 2022 17:23:41 +0900 Subject: [PATCH] add go routines for handlers, fixed http handler --- net/conn.go | 60 +++++++++++++++++++++++--------------------------- proxy/proxy.go | 6 ++--- 2 files changed, 29 insertions(+), 37 deletions(-) diff --git a/net/conn.go b/net/conn.go index 7685ae8..2f80479 100644 --- a/net/conn.go +++ b/net/conn.go @@ -78,33 +78,24 @@ func (lConn *Conn) HandleHttp(p packet.HttpPacket) { } log.Debug("[HTTP] Found ip over HTTPS: ", ip) - rConn, err := Dial("tcp", ip+":80") // create connection to server + // Create connection to server + rConn, err := Dial("tcp", ip+":80") if err != nil { - log.Debug(err) + log.Debug("[HTTPS] ", err) return } - defer rConn.Close() - if _, err := rConn.Write(p.Raw()); err != nil { - log.Debug("failed:", err) - return - } - defer rConn.CloseWrite() + log.Debug("[HTTP] Connected to the server.") - buf, err := rConn.ReadBytes() + go rConn.Serve(lConn, "[HTTP]") + go lConn.Serve(rConn, "[HTTP]") + + _, err = rConn.Write(p.Raw()) if err != nil { - log.Debug("failed:", err) + log.Debug("[HTTP] Error sending request to the server: ", 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() + log.Debug("[HTTP] Sent a request to the server") } func (lConn *Conn) HandleHttps(p packet.HttpPacket) { @@ -120,7 +111,6 @@ func (lConn *Conn) HandleHttps(p packet.HttpPacket) { log.Debug("[HTTPS] ", err) return } - defer rConn.Close() log.Debug("[HTTPS] Connected to the server.") @@ -140,37 +130,41 @@ 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.ServeHttps(lConn) + go rConn.Serve(lConn, "[HTTPS]") + go lConn.Serve(rConn, "[HTTPS]") pkt := packet.NewHttpsPacket(clientHello) chunks := pkt.SplitInChunks() if _, err := rConn.WriteChunks(chunks); err != nil { + log.Debug("[HTTPS] Error writing client hello: ", err) return } - - // Read from the client - lConn.ServeHttps(rConn) } -func (from *Conn) ServeHttps(to *Conn) { +func (from *Conn) Serve(to *Conn, proto string) { + defer from.Close() + defer to.CloseWrite() + + proto += " " + for { buf, err := from.ReadBytes() if err != nil { - log.Debug("[HTTPS] "+"Error reading from ", from.RemoteAddr()) - log.Debug("[HTTPS] ", err) - log.Debug("[HTTPS] " + "Exiting Serve() method. ") + log.Debug(proto+"Error reading from ", from.RemoteAddr()) + log.Debug(proto, err) + log.Debug(proto + "Exiting Serve() method. ") break } - log.Debug("[HTTPS] ", from.RemoteAddr(), " sent data: ", len(buf), "bytes") + + log.Debug(proto, from.RemoteAddr(), " sent data: ", len(buf), "bytes\n\n", string(buf)) if _, err := to.Write(buf); err != nil { - log.Debug("[HTTPS] "+"Error Writing to ", to.RemoteAddr()) - log.Debug("[HTTPS] ", err) - log.Debug("[HTTPS] " + "Exiting Serve() method. ") + log.Debug(proto+"Error Writing to ", to.RemoteAddr()) + log.Debug(proto, err) + log.Debug(proto + "Exiting Serve() method. ") break } - defer to.CloseWrite() } } diff --git a/proxy/proxy.go b/proxy/proxy.go index 07c4ed7..e9a8a3f 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -41,8 +41,6 @@ func (p *Proxy) Start() { log.Debug("Accepted a new connection.", conn.RemoteAddr()) go func() { - defer conn.Close() - b, err := conn.ReadBytes() if err != nil { return @@ -59,10 +57,10 @@ func (p *Proxy) Start() { if pkt.IsConnectMethod() { log.Debug("[HTTPS] Start") - conn.HandleHttps(pkt) + go conn.HandleHttps(pkt) } else { log.Debug("[HTTP] Start") - conn.HandleHttp(pkt) + go conn.HandleHttp(pkt) } }() }