github.com/metacubex/mihomo@v1.18.5/listener/socks/utils.go (about)

     1  package socks
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/metacubex/mihomo/transport/socks5"
     7  )
     8  
     9  type packet struct {
    10  	pc      net.PacketConn
    11  	rAddr   net.Addr
    12  	payload []byte
    13  	put     func()
    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  	if c.put != nil {
    36  		c.put()
    37  		c.put = nil
    38  	}
    39  	c.payload = nil
    40  }
    41  
    42  func (c *packet) InAddr() net.Addr {
    43  	return c.pc.LocalAddr()
    44  }