|
|
@ -16,16 +16,20 @@ import ( |
|
|
|
"github.com/xjasonlyu/tun2socks/tunnel/statistic" |
|
|
|
) |
|
|
|
|
|
|
|
const ( |
|
|
|
udpSessionTimeout = 60 * time.Second |
|
|
|
) |
|
|
|
|
|
|
|
var ( |
|
|
|
// _natTable uses source udp packet information
|
|
|
|
// as key to store destination udp packetConn.
|
|
|
|
_natTable = nat.NewTable() |
|
|
|
|
|
|
|
// _udpSessionTimeout is the default timeout for
|
|
|
|
// each UDP session.
|
|
|
|
_udpSessionTimeout = 60 * time.Second |
|
|
|
) |
|
|
|
|
|
|
|
func SetUDPTimeout(v int) { |
|
|
|
_udpSessionTimeout = time.Duration(v) * time.Second |
|
|
|
} |
|
|
|
|
|
|
|
func newUDPTracker(conn net.PacketConn, metadata *M.Metadata) net.PacketConn { |
|
|
|
return statistic.NewUDPTracker(conn, metadata, statistic.DefaultManager) |
|
|
|
} |
|
|
@ -115,7 +119,7 @@ func handleUDPToRemote(packet core.UDPPacket, pc net.PacketConn, remote net.Addr |
|
|
|
if _, err := pc.WriteTo(packet.Data() /* data */, remote); err != nil { |
|
|
|
log.Warnf("[UDP] write to %s error: %v", remote, err) |
|
|
|
} |
|
|
|
pc.SetReadDeadline(time.Now().Add(udpSessionTimeout)) /* reset timeout */ |
|
|
|
pc.SetReadDeadline(time.Now().Add(_udpSessionTimeout)) /* reset timeout */ |
|
|
|
|
|
|
|
log.Infof("[UDP] %s --> %s", packet.RemoteAddr(), remote) |
|
|
|
} |
|
|
@ -125,7 +129,7 @@ func handleUDPToLocal(packet core.UDPPacket, pc net.PacketConn) { |
|
|
|
defer pool.Put(buf) |
|
|
|
|
|
|
|
for /* just loop */ { |
|
|
|
pc.SetReadDeadline(time.Now().Add(udpSessionTimeout)) |
|
|
|
pc.SetReadDeadline(time.Now().Add(_udpSessionTimeout)) |
|
|
|
n, from, err := pc.ReadFrom(buf) |
|
|
|
if err != nil { |
|
|
|
if !errors.Is(err, os.ErrDeadlineExceeded) /* ignore i/o timeout */ { |
|
|
|