github.com/chwjbn/xclash@v0.2.0/listener/tproxy/packet.go (about)

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