github.com/igoogolx/clash@v1.19.8/listener/socks/utils.go (about)

     1  package socks
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/igoogolx/clash/common/pool"
     7  	"github.com/igoogolx/clash/transport/socks5"
     8  )
     9  
    10  type packet struct {
    11  	pc      net.PacketConn
    12  	rAddr   net.Addr
    13  	payload []byte
    14  	bufRef  []byte
    15  }
    16  
    17  func (c *packet) Data() []byte {
    18  	return c.payload
    19  }
    20  
    21  // WriteBack write UDP packet with source(ip, port) = `addr`
    22  func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
    23  	packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b)
    24  	if err != nil {
    25  		return
    26  	}
    27  	return c.pc.WriteTo(packet, c.rAddr)
    28  }
    29  
    30  // LocalAddr returns the source IP/Port of UDP Packet
    31  func (c *packet) LocalAddr() net.Addr {
    32  	return c.rAddr
    33  }
    34  
    35  func (c *packet) Drop() {
    36  	pool.Put(c.bufRef)
    37  }