2019-12-28 10:44:01 +00:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-05-13 14:18:49 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2019-12-28 10:44:01 +00:00
|
|
|
)
|
|
|
|
|
2022-12-04 06:37:52 +00:00
|
|
|
// PacketAdapter is a UDP Packet adapter for socks/redir/tun
|
|
|
|
type PacketAdapter struct {
|
|
|
|
C.UDPPacket
|
|
|
|
metadata *C.Metadata
|
|
|
|
}
|
2019-12-28 10:44:01 +00:00
|
|
|
|
2022-12-04 06:37:52 +00:00
|
|
|
// Metadata returns destination metadata
|
|
|
|
func (s *PacketAdapter) Metadata() *C.Metadata {
|
|
|
|
return s.metadata
|
|
|
|
}
|
2019-12-28 10:44:01 +00:00
|
|
|
|
2022-12-04 14:08:20 +00:00
|
|
|
func NewPacketWithInfos(target socks5.Addr, packet C.UDPPacket, source C.Type, inName, specialRules string) C.PacketAdapter {
|
2019-12-28 10:44:01 +00:00
|
|
|
metadata := parseSocksAddr(target)
|
2020-01-31 06:43:54 +00:00
|
|
|
metadata.NetWork = C.UDP
|
2019-12-28 10:44:01 +00:00
|
|
|
metadata.Type = source
|
2022-12-04 05:37:14 +00:00
|
|
|
metadata.InName = inName
|
2022-12-04 14:08:20 +00:00
|
|
|
metadata.SpecialRules = specialRules
|
2019-12-28 10:44:01 +00:00
|
|
|
if ip, port, err := parseAddr(packet.LocalAddr().String()); err == nil {
|
|
|
|
metadata.SrcIP = ip
|
|
|
|
metadata.SrcPort = port
|
|
|
|
}
|
2022-11-11 15:36:06 +00:00
|
|
|
if p, ok := packet.(C.UDPPacketInAddr); ok {
|
|
|
|
if ip, port, err := parseAddr(p.InAddr().String()); err == nil {
|
|
|
|
metadata.InIP = ip
|
|
|
|
metadata.InPort = port
|
|
|
|
}
|
|
|
|
}
|
2019-12-28 10:44:01 +00:00
|
|
|
|
2022-12-04 06:37:52 +00:00
|
|
|
return &PacketAdapter{
|
2022-12-04 05:37:14 +00:00
|
|
|
packet,
|
|
|
|
metadata,
|
2022-12-04 06:37:52 +00:00
|
|
|
}
|
2022-12-04 05:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPacket is PacketAdapter generator
|
2022-12-04 06:37:52 +00:00
|
|
|
func NewPacket(target socks5.Addr, packet C.UDPPacket, source C.Type) C.PacketAdapter {
|
2022-12-04 05:37:14 +00:00
|
|
|
return NewPacketWithInfos(target, packet, source, "", "")
|
2019-12-28 10:44:01 +00:00
|
|
|
}
|