2019-12-28 18:44:01 +08:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
// PacketAdapter is a UDP Packet adapter for socks/redir/tun
|
|
|
|
type PacketAdapter struct {
|
|
|
|
C.UDPPacket
|
|
|
|
metadata *C.Metadata
|
|
|
|
}
|
2019-12-28 18:44:01 +08:00
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
// Metadata returns destination metadata
|
|
|
|
func (s *PacketAdapter) Metadata() *C.Metadata {
|
|
|
|
return s.metadata
|
|
|
|
}
|
2019-12-28 18:44:01 +08:00
|
|
|
|
2022-12-05 00:20:50 +08:00
|
|
|
// NewPacket is PacketAdapter generator
|
|
|
|
func NewPacket(target socks5.Addr, packet C.UDPPacket, source C.Type, additions ...Addition) C.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-10-10 19:43:26 +08:00
|
|
|
additions = append(additions, WithSrcAddr(packet.LocalAddr()))
|
|
|
|
if p, ok := packet.(C.UDPPacketInAddr); ok {
|
|
|
|
additions = append(additions, WithInAddr(p.InAddr()))
|
|
|
|
}
|
2022-12-05 00:20:50 +08:00
|
|
|
for _, addition := range additions {
|
|
|
|
addition.Apply(metadata)
|
|
|
|
}
|
2019-12-28 18:44:01 +08:00
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
return &PacketAdapter{
|
2022-12-04 13:37:14 +08:00
|
|
|
packet,
|
|
|
|
metadata,
|
2022-12-04 14:37:52 +08:00
|
|
|
}
|
2022-12-04 13:37:14 +08:00
|
|
|
}
|