From f9f1f0040451bb34e3597c626cfe47ee1d516da3 Mon Sep 17 00:00:00 2001 From: xvzc Date: Thu, 12 May 2022 17:56:12 +0900 Subject: [PATCH] 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()