mihomo/adapter/inbound/packet.go

43 lines
988 B
Go
Raw Normal View History

package inbound
import (
C "github.com/Dreamacro/clash/constant"
2021-05-13 22:18:49 +08:00
"github.com/Dreamacro/clash/transport/socks5"
)
// 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
func NewPacket(target socks5.Addr, packet C.UDPPacket, source C.Type, additions ...Addition) C.PacketAdapter {
metadata := parseSocksAddr(target)
2020-01-31 14:43:54 +08:00
metadata.NetWork = C.UDP
metadata.Type = source
for _, addition := range additions {
addition.Apply(metadata)
}
2023-01-17 15:41:51 +08:00
if ip, port, err := parseAddr(packet.LocalAddr()); err == nil {
metadata.SrcIP = ip
metadata.SrcPort = port
}
2022-11-11 23:36:06 +08:00
if p, ok := packet.(C.UDPPacketInAddr); ok {
2023-01-17 15:41:51 +08:00
if ip, port, err := parseAddr(p.InAddr()); err == nil {
2022-11-11 23:36:06 +08:00
metadata.InIP = ip
metadata.InPort = port
}
}
return &PacketAdapter{
2022-12-04 13:37:14 +08:00
packet,
metadata,
}
2022-12-04 13:37:14 +08:00
}