github.com/yaling888/clash@v1.53.0/listener/socks/utils.go (about)

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