set deadline

This commit is contained in:
xvzc 2022-05-12 17:56:12 +09:00
parent 8893af16d3
commit f9f1f00404
2 changed files with 21 additions and 23 deletions

View File

@ -2,7 +2,6 @@ package net
import ( import (
"errors" "errors"
"io"
"net" "net"
"time" "time"
@ -41,6 +40,11 @@ func (c *Conn) Write(b []byte) (n int, err error) {
return c.conn.Write(b) 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) { func (c *Conn) SetDeadLine(t time.Time) (error) {
c.conn.SetDeadline(t) c.conn.SetDeadline(t)
return nil return nil
@ -69,8 +73,6 @@ func (conn *Conn) ReadBytes() ([]byte, error) {
ret := make([]byte, 0) ret := make([]byte, 0)
buf := make([]byte, BUF_SIZE) buf := make([]byte, BUF_SIZE)
conn.conn.SetReadDeadline(time.Now().Add(5 * time.Second))
for { for {
n, err := conn.Read(buf) n, err := conn.Read(buf)
if err != nil { if err != nil {
@ -119,6 +121,7 @@ func (lConn *Conn) HandleHttp(p *packet.HttpPacket) {
log.Debug("[HTTP] ", err) log.Debug("[HTTP] ", err)
return return
} }
defer func() { defer func() {
defer rConn.Close() defer rConn.Close()
log.Debug("[HTTP] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr()) 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()) log.Debug("[HTTP] Sent a request to ", p.Domain())
go io.Copy(lConn, rConn) go lConn.Serve(rConn, "[HTTP]", lConn.RemoteAddr().String(), p.Domain())
io.Copy(rConn, lConn) rConn.Serve(lConn, "[HTTP]", lConn.RemoteAddr().String(), p.Domain())
} }
@ -165,6 +168,7 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) {
log.Debug("[HTTPS] ", err) log.Debug("[HTTPS] ", err)
return return
} }
defer func() { defer func() {
defer rConn.Close() defer rConn.Close()
log.Debug("[HTTPS] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr()) log.Debug("[HTTPS] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr())
@ -199,29 +203,24 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) {
return return
} }
// go io.Copy(lConn, rConn) go lConn.Serve(rConn, "[HTTPS]", lConn.RemoteAddr().String(), p.Domain())
// io.Copy(rConn, lConn) rConn.Serve(lConn, "[HTTPS]", lConn.RemoteAddr().String(), p.Domain())
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) { func (from *Conn) Serve(to *Conn, proto string, fd string, td string) {
proto += " " proto += " "
for { for {
from.conn.SetReadDeadline(time.Now().Add(2000 * time.Millisecond))
buf, err := from.ReadBytes() buf, err := from.ReadBytes()
if err != nil { if err != nil {
log.Debug(proto, "Error reading from ", fd, " ", err) log.Debug(proto, "Error reading from ", fd, " ", err)
return return
} else {
} }
// log.Debug(proto, fd, " sent data: ", len(buf), "bytes")
if _, err := to.Write(buf); err != nil { if _, err := to.Write(buf); err != nil {
log.Debug(proto, "Error Writing to ", td) log.Debug(proto, "Error Writing to ", td)
return return
} else {
} }
} }
} }

View File

@ -43,7 +43,6 @@ func (p *Proxy) Start() {
log.Fatal("Error accepting connection: ", err) log.Fatal("Error accepting connection: ", err)
continue continue
} }
conn.SetKeepAlive(false)
go func() { go func() {
b, err := conn.ReadBytes() b, err := conn.ReadBytes()