github.com/chwjbn/xclash@v0.2.0/adapter/inbound/packet.go (about)

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