github.com/igoogolx/clash@v1.19.8/listener/tproxy/packet.go (about)

     1  package tproxy
     2  
     3  import (
     4  	"net"
     5  	"net/netip"
     6  
     7  	"github.com/igoogolx/clash/common/pool"
     8  )
     9  
    10  type packet struct {
    11  	lAddr netip.AddrPort
    12  	buf   []byte
    13  }
    14  
    15  func (c *packet) Data() []byte {
    16  	return c.buf
    17  }
    18  
    19  // WriteBack opens a new socket binding `addr` to write UDP packet back
    20  func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
    21  	tc, err := dialUDP("udp", addr.(*net.UDPAddr).AddrPort(), c.lAddr)
    22  	if err != nil {
    23  		n = 0
    24  		return
    25  	}
    26  	n, err = tc.Write(b)
    27  	tc.Close()
    28  	return
    29  }
    30  
    31  // LocalAddr returns the source IP/Port of UDP Packet
    32  func (c *packet) LocalAddr() net.Addr {
    33  	return &net.UDPAddr{IP: c.lAddr.Addr().AsSlice(), Port: int(c.lAddr.Port()), Zone: c.lAddr.Addr().Zone()}
    34  }
    35  
    36  func (c *packet) Drop() {
    37  	pool.Put(c.buf)
    38  }