Browse Source

optimize d udp.go

pull/15/head
Jason 5 years ago
parent
commit
e3b1ff47bd
  1. 39
      proxy/d/udp.go

39
proxy/d/udp.go

@ -8,17 +8,18 @@ import (
"github.com/xjasonlyu/tun2socks/common/log" "github.com/xjasonlyu/tun2socks/common/log"
"github.com/xjasonlyu/tun2socks/common/lsof" "github.com/xjasonlyu/tun2socks/common/lsof"
"github.com/xjasonlyu/tun2socks/common/pool"
"github.com/xjasonlyu/tun2socks/core" "github.com/xjasonlyu/tun2socks/core"
) )
type udpHandler struct { type udpHandler struct {
sync.Mutex timeout time.Duration
exceptionConnMap sync.Map
proxyHandler core.UDPConnHandler
exceptionApps []string exceptionApps []string
sendThrough net.Addr sendThrough net.Addr
exceptionConns map[core.UDPConn]*net.UDPConn proxyHandler core.UDPConnHandler
timeout time.Duration
} }
func (h *udpHandler) isExceptionApp(name string) bool { func (h *udpHandler) isExceptionApp(name string) bool {
@ -35,17 +36,16 @@ func NewUDPHandler(proxyHandler core.UDPConnHandler, exceptionApps []string, sen
proxyHandler: proxyHandler, proxyHandler: proxyHandler,
exceptionApps: exceptionApps, exceptionApps: exceptionApps,
sendThrough: sendThrough, sendThrough: sendThrough,
exceptionConns: make(map[core.UDPConn]*net.UDPConn),
timeout: timeout, timeout: timeout,
} }
} }
func (h *udpHandler) handleInput(conn core.UDPConn, pc *net.UDPConn) { func (h *udpHandler) handleInput(conn core.UDPConn, pc *net.UDPConn) {
buf := core.NewBytes(core.BufSize) buf := pool.BufPool.Get().([]byte)
defer func() { defer func() {
h.Close(conn) h.Close(conn)
core.FreeBytes(buf) pool.BufPool.Put(buf[:cap(buf)])
}() }()
for { for {
@ -55,8 +55,7 @@ func (h *udpHandler) handleInput(conn core.UDPConn, pc *net.UDPConn) {
return return
} }
_, err = conn.WriteFrom(buf[:n], addr) if _, err := conn.WriteFrom(buf[:n], addr); err != nil {
if err != nil {
return return
} }
} }
@ -79,14 +78,12 @@ func (h *udpHandler) Connect(conn core.UDPConn, target *net.UDPAddr) error {
if err != nil { if err != nil {
return err return err
} }
h.Lock()
h.exceptionConns[conn] = pc h.exceptionConnMap.Store(conn, pc)
h.Unlock()
go h.handleInput(conn, pc) go h.handleInput(conn, pc)
log.Access(cmd, "direct", target.Network(), conn.LocalAddr().String(), target.String()) log.Access(cmd, "direct", target.Network(), conn.LocalAddr().String(), target.String())
return nil return nil
} else { } else {
return h.proxyHandler.Connect(conn, target) return h.proxyHandler.Connect(conn, target)
@ -94,11 +91,8 @@ func (h *udpHandler) Connect(conn core.UDPConn, target *net.UDPAddr) error {
} }
func (h *udpHandler) ReceiveTo(conn core.UDPConn, data []byte, addr *net.UDPAddr) error { func (h *udpHandler) ReceiveTo(conn core.UDPConn, data []byte, addr *net.UDPAddr) error {
h.Lock() if pc, ok := h.exceptionConnMap.Load(conn); ok {
defer h.Unlock() _, err := pc.(*net.UDPConn).WriteTo(data, addr)
if pc, found := h.exceptionConns[conn]; found {
_, err := pc.WriteTo(data, addr)
if err != nil { if err != nil {
return err return err
} }
@ -111,11 +105,8 @@ func (h *udpHandler) ReceiveTo(conn core.UDPConn, data []byte, addr *net.UDPAddr
func (h *udpHandler) Close(conn core.UDPConn) { func (h *udpHandler) Close(conn core.UDPConn) {
conn.Close() conn.Close()
h.Lock() if pc, ok := h.exceptionConnMap.Load(conn); ok {
defer h.Unlock() pc.(*net.UDPConn).Close()
h.exceptionConnMap.Delete(conn)
if pc, ok := h.exceptionConns[conn]; ok {
pc.Close()
delete(h.exceptionConns, conn)
} }
} }

Loading…
Cancel
Save