2019-12-28 18:44:01 +08:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
2023-06-13 23:25:32 +08:00
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
|
2019-12-28 18:44:01 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-05-13 22:18:49 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2019-12-28 18:44:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2023-06-13 23:25:32 +08:00
|
|
|
func NewPacket(target socks5.Addr, originTarget net.Addr, packet C.UDPPacket, source C.Type) *PacketAdapter {
|
2019-12-28 18:44:01 +08:00
|
|
|
metadata := parseSocksAddr(target)
|
2020-01-31 14:43:54 +08:00
|
|
|
metadata.NetWork = C.UDP
|
2019-12-28 18:44:01 +08:00
|
|
|
metadata.Type = source
|
2023-08-03 23:20:40 +08:00
|
|
|
if ip, port, err := parseAddr(packet.LocalAddr()); err == nil {
|
2019-12-28 18:44:01 +08:00
|
|
|
metadata.SrcIP = ip
|
2023-08-03 23:20:40 +08:00
|
|
|
metadata.SrcPort = C.Port(port)
|
2019-12-28 18:44:01 +08:00
|
|
|
}
|
2023-06-18 11:16:40 +08:00
|
|
|
if originTarget != nil {
|
|
|
|
if addrPort, err := netip.ParseAddrPort(originTarget.String()); err == nil {
|
|
|
|
metadata.OriginDst = addrPort
|
|
|
|
}
|
2023-06-13 23:25:32 +08:00
|
|
|
}
|
2019-12-28 18:44:01 +08:00
|
|
|
return &PacketAdapter{
|
|
|
|
UDPPacket: packet,
|
|
|
|
metadata: metadata,
|
|
|
|
}
|
|
|
|
}
|