github.com/yaling888/clash@v1.53.0/adapter/inbound/packet.go (about) 1 package inbound 2 3 import ( 4 "net" 5 "net/netip" 6 7 C "github.com/yaling888/clash/constant" 8 "github.com/yaling888/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 ip, port, err := parseAddr(originTarget); err == nil { 32 metadata.OriginDst = netip.AddrPortFrom(ip, uint16(port)) 33 } 34 return &PacketAdapter{ 35 UDPPacket: packet, 36 metadata: metadata, 37 } 38 } 39 40 // NewPacketBy is PacketAdapter generator 41 func NewPacketBy(packet C.UDPPacket, src, dst netip.AddrPort, tp C.Type) *PacketAdapter { 42 metadata := &C.Metadata{} 43 metadata.NetWork = C.UDP 44 metadata.Type = tp 45 metadata.SrcIP = src.Addr() 46 metadata.SrcPort = C.Port(src.Port()) 47 metadata.DstIP = dst.Addr() 48 metadata.DstPort = C.Port(dst.Port()) 49 metadata.OriginDst = dst 50 51 return &PacketAdapter{ 52 UDPPacket: packet, 53 metadata: metadata, 54 } 55 }