From 8893af16d373190ccfe6445303a34b86e1ce98a2 Mon Sep 17 00:00:00 2001 From: jerry901 Date: Wed, 11 May 2022 22:55:07 +0900 Subject: [PATCH 1/4] refactor --- net/conn.go | 62 +++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/net/conn.go b/net/conn.go index 9f9d149..837dd45 100644 --- a/net/conn.go +++ b/net/conn.go @@ -92,14 +92,17 @@ func (conn *Conn) ReadBytes() ([]byte, error) { } func (lConn *Conn) HandleHttp(p *packet.HttpPacket) { - defer lConn.Close() + defer func() { + lConn.Close() + log.Debug("[HTTP] Closing client Connection.. ", lConn.RemoteAddr()) + }() + p.Tidy() ip, err := doh.Lookup(p.Domain()) if err != nil { log.Error("[HTTP DOH] Error looking up for domain with ", p.Domain() , " ", err) lConn.Write([]byte(p.Version() + " 502 Bad Gateway\r\n\r\n")) - lConn.Close() return } @@ -114,18 +117,18 @@ func (lConn *Conn) HandleHttp(p *packet.HttpPacket) { rConn, err := Dial("tcp", ip + port) if err != nil { log.Debug("[HTTP] ", err) - lConn.Close() return } - defer rConn.Close() + defer func() { + defer rConn.Close() + log.Debug("[HTTP] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr()) + }() - log.Debug("[HTTP] Connected to ", p.Domain()) + log.Debug("[HTTP] New connection to the server ", p.Domain(), " ", rConn.LocalAddr()) _, err = rConn.Write(p.Raw()) if err != nil { log.Debug("[HTTP] Error sending request to ", p.Domain(), err) - lConn.Close() - rConn.Close() return } @@ -134,18 +137,18 @@ func (lConn *Conn) HandleHttp(p *packet.HttpPacket) { go io.Copy(lConn, rConn) io.Copy(rConn, lConn) - log.Debug("[HTTP] Closing Connection..", p.Domain()) - } func (lConn *Conn) HandleHttps(p *packet.HttpPacket) { - defer lConn.Close() + defer func() { + lConn.Close() + log.Debug("[HTTPS] Closing client Connection.. ", lConn.RemoteAddr()) + }() ip, err := doh.Lookup(p.Domain()) if err != nil { log.Error("[HTTPS DOH] Error looking up for domain: ", p.Domain(), " ", err) lConn.Write([]byte(p.Version() + " 502 Bad Gateway\r\n\r\n")) - lConn.Close() return } @@ -160,18 +163,18 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) { rConn, err := Dial("tcp", ip + port) if err != nil { log.Debug("[HTTPS] ", err) - lConn.Close() return } - defer rConn.Close() + defer func() { + defer rConn.Close() + log.Debug("[HTTPS] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr()) + }() - log.Debug("[HTTPS] Connected to ", p.Domain()) + log.Debug("[HTTPS] New connection to the server ", p.Domain(), " ", rConn.LocalAddr()) _, 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) - lConn.Close() - rConn.Close() return } log.Debug("[HTTPS] Sent 200 Connection Estabalished to the client") @@ -180,9 +183,6 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) { clientHello, err := lConn.ReadBytes() if err != nil { log.Debug("[HTTPS] Error reading client hello from the client", err) - log.Debug("[HTTPS] Closing local connection..") - lConn.Close() - rConn.Close() return } @@ -196,36 +196,32 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) { if _, err := rConn.WriteChunks(chunks); err != nil { log.Debug("[HTTPS] Error writing client hello to ", p.Domain(), err) - lConn.Close() - rConn.Close() return } - go io.Copy(lConn, rConn) - io.Copy(rConn, lConn) - - log.Debug("[HTTPS] Closing Connection..", p.Domain()) - + // go io.Copy(lConn, rConn) + // io.Copy(rConn, lConn) + go lConn.Serve(rConn, "[HTTPS]", "client", p.Domain()) + rConn.Serve(lConn, "[HTTPS]", p.Domain(), "client") } func (from *Conn) Serve(to *Conn, proto string, fd string, td string) { - defer from.Close() - defer to.Close() - proto += " " for { buf, err := from.ReadBytes() if err != nil { log.Debug(proto, "Error reading from ", fd, " ", err) - break - } + return + } else { + } // log.Debug(proto, fd, " sent data: ", len(buf), "bytes") if _, err := to.Write(buf); err != nil { log.Debug(proto, "Error Writing to ", td) - break - } + return + } else { + } } } From f9f1f0040451bb34e3597c626cfe47ee1d516da3 Mon Sep 17 00:00:00 2001 From: xvzc Date: Thu, 12 May 2022 17:56:12 +0900 Subject: [PATCH 2/4] set deadline --- net/conn.go | 43 +++++++++++++++++++++---------------------- proxy/proxy.go | 1 - 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/net/conn.go b/net/conn.go index 837dd45..a736ea8 100644 --- a/net/conn.go +++ b/net/conn.go @@ -2,7 +2,6 @@ package net import ( "errors" - "io" "net" "time" @@ -41,6 +40,11 @@ func (c *Conn) Write(b []byte) (n int, err error) { return c.conn.Write(b) } +func (c *Conn) SetReadDeadline(t time.Time) (error) { + c.conn.SetReadDeadline(t) + return nil +} + func (c *Conn) SetDeadLine(t time.Time) (error) { c.conn.SetDeadline(t) return nil @@ -69,8 +73,6 @@ func (conn *Conn) ReadBytes() ([]byte, error) { ret := make([]byte, 0) buf := make([]byte, BUF_SIZE) - conn.conn.SetReadDeadline(time.Now().Add(5 * time.Second)) - for { n, err := conn.Read(buf) if err != nil { @@ -119,6 +121,7 @@ func (lConn *Conn) HandleHttp(p *packet.HttpPacket) { log.Debug("[HTTP] ", err) return } + defer func() { defer rConn.Close() log.Debug("[HTTP] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr()) @@ -134,8 +137,8 @@ func (lConn *Conn) HandleHttp(p *packet.HttpPacket) { log.Debug("[HTTP] Sent a request to ", p.Domain()) - go io.Copy(lConn, rConn) - io.Copy(rConn, lConn) + go lConn.Serve(rConn, "[HTTP]", lConn.RemoteAddr().String(), p.Domain()) + rConn.Serve(lConn, "[HTTP]", lConn.RemoteAddr().String(), p.Domain()) } @@ -165,6 +168,7 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) { log.Debug("[HTTPS] ", err) return } + defer func() { defer rConn.Close() log.Debug("[HTTPS] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr()) @@ -199,29 +203,24 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) { return } - // go io.Copy(lConn, rConn) - // io.Copy(rConn, lConn) - go lConn.Serve(rConn, "[HTTPS]", "client", p.Domain()) - rConn.Serve(lConn, "[HTTPS]", p.Domain(), "client") + go lConn.Serve(rConn, "[HTTPS]", lConn.RemoteAddr().String(), p.Domain()) + rConn.Serve(lConn, "[HTTPS]", lConn.RemoteAddr().String(), p.Domain()) } func (from *Conn) Serve(to *Conn, proto string, fd string, td string) { proto += " " - for { - buf, err := from.ReadBytes() - if err != nil { - log.Debug(proto, "Error reading from ", fd, " ", err) + for { + from.conn.SetReadDeadline(time.Now().Add(2000 * time.Millisecond)) + buf, err := from.ReadBytes() + if err != nil { + log.Debug(proto, "Error reading from ", fd, " ", err) return - } else { - } + } - // log.Debug(proto, fd, " sent data: ", len(buf), "bytes") - - if _, err := to.Write(buf); err != nil { - log.Debug(proto, "Error Writing to ", td) + if _, err := to.Write(buf); err != nil { + log.Debug(proto, "Error Writing to ", td) return - } else { - } - } + } + } } diff --git a/proxy/proxy.go b/proxy/proxy.go index 530a10a..187f21c 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -43,7 +43,6 @@ func (p *Proxy) Start() { log.Fatal("Error accepting connection: ", err) continue } - conn.SetKeepAlive(false) go func() { b, err := conn.ReadBytes() From 8ef36894b7efabbed4daea811db35a558a258a8b Mon Sep 17 00:00:00 2001 From: jerry901 Date: Thu, 12 May 2022 20:05:53 +0900 Subject: [PATCH 3/4] update --- net/conn.go | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/net/conn.go b/net/conn.go index a736ea8..d00f6c8 100644 --- a/net/conn.go +++ b/net/conn.go @@ -1,7 +1,7 @@ package net import ( - "errors" + "io" "net" "time" @@ -70,27 +70,45 @@ func (conn *Conn) WriteChunks(c [][]byte) (n int, err error) { } func (conn *Conn) ReadBytes() ([]byte, error) { - ret := make([]byte, 0) - buf := make([]byte, BUF_SIZE) + // ret := make([]byte, 0) + // buf := make([]byte, BUF_SIZE) - for { - n, err := conn.Read(buf) + // for { + // n, err := conn.Read(buf) + // if err != nil { + // switch err.(type) { + // case *net.OpError: + // return nil, errors.New("timed out") + // default: + // return nil, err + // } + // } + // ret = append(ret, buf[:n]...) + + // if n == 0 { + // return nil, io.EOF + // } + + // if n < BUF_SIZE { + // break + // } + // } + buf := make([]byte, 0, 4096) // big buffer + tmp := make([]byte, 256) // using small tmo buffer for demonstrating + for { + n, err := conn.Read(tmp) if err != nil { - switch err.(type) { - case *net.OpError: - return nil, errors.New("timed out") - default: + if err != io.EOF { return nil, err } - } - ret = append(ret, buf[:n]...) - if n < BUF_SIZE { break - } - } + } - return ret, nil + buf = append(buf, tmp[:n]...) + } + + return buf, nil } func (lConn *Conn) HandleHttp(p *packet.HttpPacket) { From 8f03e7984995683c69c939c11e9958ac2e8242fc Mon Sep 17 00:00:00 2001 From: xvzc Date: Thu, 12 May 2022 21:21:54 +0900 Subject: [PATCH 4/4] update --- net/conn.go | 42 ++++++++++++------------------------------ 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/net/conn.go b/net/conn.go index d00f6c8..2b5989c 100644 --- a/net/conn.go +++ b/net/conn.go @@ -1,7 +1,7 @@ package net import ( - "io" + "errors" "net" "time" @@ -70,45 +70,27 @@ func (conn *Conn) WriteChunks(c [][]byte) (n int, err error) { } func (conn *Conn) ReadBytes() ([]byte, error) { - // ret := make([]byte, 0) - // buf := make([]byte, BUF_SIZE) + ret := make([]byte, 0) + buf := make([]byte, BUF_SIZE) - // for { - // n, err := conn.Read(buf) - // if err != nil { - // switch err.(type) { - // case *net.OpError: - // return nil, errors.New("timed out") - // default: - // return nil, err - // } - // } - // ret = append(ret, buf[:n]...) - - // if n == 0 { - // return nil, io.EOF - // } - - // if n < BUF_SIZE { - // break - // } - // } - buf := make([]byte, 0, 4096) // big buffer - tmp := make([]byte, 256) // using small tmo buffer for demonstrating for { - n, err := conn.Read(tmp) + n, err := conn.Read(buf) if err != nil { - if err != io.EOF { + switch err.(type) { + case *net.OpError: + return nil, errors.New("timed out") + default: return nil, err } + } + ret = append(ret, buf[:n]...) + if n < BUF_SIZE { break } - - buf = append(buf, tmp[:n]...) } - return buf, nil + return ret, nil } func (lConn *Conn) HandleHttp(p *packet.HttpPacket) {