proxy: optimize timeout handling in udp copy

This commit is contained in:
nadoo committed 2022-02-26 22:32:11 +08:00
1 parent 4b313a3fe1
commit 2ad3498abd
15 files changed
+46 -41

No files matched your search

+10 -3
View File
@@ -175,13 +175,20 @@ func CopyBuffer(dst io.Writer, src io.Reader) (written int64, err error) {
return written, err
}
// RelayUDP copys from src to dst at target with read timeout.
func RelayUDP(dst net.PacketConn, target net.Addr, src net.PacketConn, timeout time.Duration) error {
// CopyUDP copys from src to dst at target with read timeout.
// if step sets to non-zero value,
// the read timeout will be increased from 0 to timeout by step in every read operation.
func CopyUDP(dst net.PacketConn, target net.Addr, src net.PacketConn, timeout time.Duration, step time.Duration) error {
b := pool.GetBuffer(UDPBufSize)
defer pool.PutBuffer(b)
var t time.Duration
for {
src.SetReadDeadline(time.Now().Add(timeout))
if t += step; t == 0 || t > timeout {
t = timeout
}
src.SetReadDeadline(time.Now().Add(t))
n, _, err := src.ReadFrom(b)
if err != nil {
return err
+1 -1
View File
@@ -151,7 +151,7 @@ func (s *Socks5) serveSession(session *Session) {
defer dstPC.Close()
go func() {
proxy.RelayUDP(session.srcPC, session.src, dstPC, 2*time.Minute)
proxy.CopyUDP(session.srcPC, session.src, dstPC, 2*time.Minute, 5*time.Second)
nm.Delete(session.key)
close(session.finCh)
}()
+1 -1
View File
@@ -138,7 +138,7 @@ func (s *SS) serveSession(session *Session) {
defer dstPC.Close()
go func() {
proxy.RelayUDP(session.srcPC, session.src, dstPC, 2*time.Minute)
proxy.CopyUDP(session.srcPC, session.src, dstPC, 2*time.Minute, 5*time.Second)
nm.Delete(session.key)
close(session.finCh)
}()
+1 -1
View File
@@ -118,7 +118,7 @@ func (s *TProxy) serveSession(session *Session) {
defer srcPC.Close()
go func() {
proxy.RelayUDP(srcPC, session.src, dstPC, 2*time.Minute)
proxy.CopyUDP(srcPC, session.src, dstPC, 2*time.Minute, 5*time.Second)
nm.Delete(session.key)
close(session.finCh)
}()
+1 -1
View File
@@ -35,7 +35,7 @@ func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
return 0, nil, err
}
// TODO: we know that we use it in proxy.RelayUDP and the length of b is enough, check it later.
// TODO: we know that we use it in proxy.CopyUDP and the length of b is enough, check it later.
if len(b) < 2 {
return 0, nil, errors.New("buf size is not enough")
}
+2 -2
View File
@@ -210,6 +210,6 @@ func (s *Trojan) ServeUoT(c net.Conn, tgt socks.Addr) {
pc := NewPktConn(c, tgt)
log.F("[trojan] %s <-tcp-> %s - %s <-udp-> %s", c.RemoteAddr(), c.LocalAddr(), rc.LocalAddr(), tgt)
go proxy.RelayUDP(rc, tgtAddr, pc, 2*time.Minute)
proxy.RelayUDP(pc, nil, rc, 2*time.Minute)
go proxy.CopyUDP(rc, tgtAddr, pc, 2*time.Minute, 5*time.Second)
proxy.CopyUDP(pc, nil, rc, 2*time.Minute, 5*time.Second)
}
+1 -1
View File
@@ -99,7 +99,7 @@ func (s *UDP) serveSession(session *Session) {
defer dstPC.Close()
go func() {
proxy.RelayUDP(session.srcPC, session.src, dstPC, 2*time.Minute)
proxy.CopyUDP(session.srcPC, session.src, dstPC, 2*time.Minute, 5*time.Second)
nm.Delete(session.key)
close(session.finCh)
}()
+1 -1
View File
@@ -148,7 +148,7 @@ func (s *Unix) serveSession(session *Session) {
defer dstPC.Close()
go func() {
proxy.RelayUDP(session.srcPC, session.src, dstPC, 2*time.Minute)
proxy.CopyUDP(session.srcPC, session.src, dstPC, 2*time.Minute, 5*time.Second)
nm.Delete(session.key)
close(session.finCh)
}()
+1 -1
View File
@@ -16,7 +16,7 @@ type PktConn struct{ net.Conn }
func NewPktConn(c net.Conn) *PktConn { return &PktConn{Conn: c} }
// ReadFrom implements the necessary function of net.PacketConn.
// TODO: we know that we use it in proxy.RelayUDP and the length of b is enough, check it later.
// TODO: we know that we use it in proxy.CopyUDP and the length of b is enough, check it later.
func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
if len(b) < 2 {
return 0, nil, errors.New("buf size is not enough")
+2 -2
View File
@@ -165,8 +165,8 @@ func (s *VLess) ServeUoT(c net.Conn, tgt string) {
pc := NewPktConn(c)
log.F("[vless] %s <-tcp-> %s - %s <-udp-> %s", c.RemoteAddr(), c.LocalAddr(), rc.LocalAddr(), tgt)
go proxy.RelayUDP(rc, tgtAddr, pc, 2*time.Minute)
proxy.RelayUDP(pc, nil, rc, 2*time.Minute)
go proxy.CopyUDP(rc, tgtAddr, pc, 2*time.Minute, 5*time.Second)
proxy.CopyUDP(pc, nil, rc, 2*time.Minute, 5*time.Second)
}
// ServerConn is a vless client connection.