refactor: udp

This commit is contained in:
世界
2022-06-09 21:16:42 +08:00
parent cd466f05d3
commit 637a8b6ed5
23 changed files with 231 additions and 127 deletions

View File

@ -7,6 +7,8 @@ import (
"time"
"github.com/Dreamacro/clash/component/dialer"
"github.com/sagernet/sing/common/buf"
N "github.com/sagernet/sing/common/network"
)
// Adapter Type
@ -75,6 +77,7 @@ type Conn interface {
type PacketConn interface {
net.PacketConn
Connection
N.PacketConn
// Deprecate WriteWithMetadata because of remote resolve DNS cause TURN failed
// WriteWithMetadata(p []byte, metadata *Metadata) (n int, err error)
}
@ -184,7 +187,7 @@ func (at AdapterType) String() string {
// UDPPacket contains the data of UDP packet, and offers control/info of UDP packet's source
type UDPPacket interface {
// Data get the payload of UDP Packet
Data() []byte
Data() *buf.Buffer
// WriteBack writes the payload with source IP/Port equals addr
// - variable source IP/Port is important to STUN
@ -192,8 +195,7 @@ type UDPPacket interface {
// this is important when using Fake-IP.
WriteBack(b []byte, addr net.Addr) (n int, err error)
// Drop call after packet is used, could recycle buffer in this function.
Drop()
N.PacketWriter
// LocalAddr returns the source IP/Port of packet
LocalAddr() net.Addr

View File

@ -6,6 +6,8 @@ import (
"net"
"net/netip"
"strconv"
M "github.com/sagernet/sing/common/metadata"
)
// Socks addr type
@ -170,6 +172,18 @@ func (m *Metadata) UDPAddr() *net.UDPAddr {
}
}
func (m *Metadata) Socksaddr() M.Socksaddr {
port, _ := strconv.ParseUint(m.DstPort, 10, 16)
if m.Host != "" {
return M.Socksaddr{
Fqdn: m.Host,
Port: uint16(port),
}
} else {
return M.SocksaddrFromAddrPort(m.DstIP, uint16(port))
}
}
func (m *Metadata) String() string {
if m.Host != "" {
return m.Host