github.com/igoogolx/clash@v1.19.8/adapter/inbound/packet.go (about)

     1  package inbound
     2  
     3  import (
     4  	"net"
     5  	"net/netip"
     6  
     7  	C "github.com/igoogolx/clash/constant"
     8  	"github.com/igoogolx/clash/transport/socks5"
     9  )
    10  
    11  // PacketAdapter is a UDP Packet adapter for socks/redir/tun
    12  type PacketAdapter struct {
    13  	C.UDPPacket
    14  	metadata *C.Metadata
    15  }
    16  
    17  // Metadata returns destination metadata
    18  func (s *PacketAdapter) Metadata() *C.Metadata {
    19  	return s.metadata
    20  }
    21  
    22  // NewPacket is PacketAdapter generator
    23  func NewPacket(target socks5.Addr, originTarget net.Addr, packet C.UDPPacket, source C.Type) *PacketAdapter {
    24  	metadata := parseSocksAddr(target)
    25  	metadata.NetWork = C.UDP
    26  	metadata.Type = source
    27  	if ip, port, err := parseAddr(packet.LocalAddr()); err == nil {
    28  		metadata.SrcIP = ip
    29  		metadata.SrcPort = C.Port(port)
    30  	}
    31  	if originTarget != nil {
    32  		if addrPort, err := netip.ParseAddrPort(originTarget.String()); err == nil {
    33  			metadata.OriginDst = addrPort
    34  		}
    35  	}
    36  	return &PacketAdapter{
    37  		UDPPacket: packet,
    38  		metadata:  metadata,
    39  	}
    40  }