mihomo/adapter/inbound/packet.go

34 lines
772 B
Go
Raw Normal View History

package inbound
import (
C "github.com/Dreamacro/clash/constant"
2022-06-09 21:16:42 +08:00
M "github.com/sagernet/sing/common/metadata"
)
// PacketAdapter is a UDP Packet adapter for socks/redir/tun
type PacketAdapter struct {
C.UDPPacket
metadata *C.Metadata
}
// Metadata returns destination metadata
func (s *PacketAdapter) Metadata() *C.Metadata {
return s.metadata
}
// NewPacket is PacketAdapter generator
2022-06-09 21:16:42 +08:00
func NewPacket(target M.Socksaddr, packet C.UDPPacket, source C.Type) *PacketAdapter {
metadata := socksAddrToMetadata(target)
2020-01-31 14:43:54 +08:00
metadata.NetWork = C.UDP
metadata.Type = source
if ip, port, err := parseAddr(packet.LocalAddr().String()); err == nil {
metadata.SrcIP = ip
metadata.SrcPort = port
}
return &PacketAdapter{
UDPPacket: packet,
metadata: metadata,
}
}