sing-box/common/dialer/resolve_conn.go

85 lines
2.3 KiB
Go
Raw Permalink Normal View History

2022-07-08 12:58:43 +08:00
package dialer
import (
"context"
"net"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing-box/adapter"
2022-07-11 18:44:59 +08:00
"github.com/sagernet/sing-dns"
2022-07-08 12:58:43 +08:00
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
2022-07-25 16:01:10 +08:00
func NewResolvePacketConn(ctx context.Context, router adapter.Router, strategy dns.DomainStrategy, conn net.PacketConn) N.NetPacketConn {
2022-07-08 12:58:43 +08:00
if udpConn, ok := conn.(*net.UDPConn); ok {
2022-07-25 16:01:10 +08:00
return &ResolveUDPConn{udpConn, ctx, router, strategy}
2022-07-08 12:58:43 +08:00
} else {
2022-07-25 16:01:10 +08:00
return &ResolvePacketConn{conn, ctx, router, strategy}
2022-07-08 12:58:43 +08:00
}
}
type ResolveUDPConn struct {
*net.UDPConn
2022-07-25 16:01:10 +08:00
ctx context.Context
2022-07-08 12:58:43 +08:00
router adapter.Router
2022-07-11 18:44:59 +08:00
strategy dns.DomainStrategy
2022-07-08 12:58:43 +08:00
}
func (w *ResolveUDPConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) {
n, addr, err := w.ReadFromUDPAddrPort(buffer.FreeBytes())
if err != nil {
return M.Socksaddr{}, err
}
buffer.Truncate(n)
return M.SocksaddrFromNetIP(addr), nil
}
func (w *ResolveUDPConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
defer buffer.Release()
2022-07-10 14:22:28 +08:00
if destination.IsFqdn() {
2022-07-25 16:01:10 +08:00
addresses, err := w.router.Lookup(w.ctx, destination.Fqdn, w.strategy)
2022-07-08 12:58:43 +08:00
if err != nil {
return err
}
return common.Error(w.UDPConn.WriteToUDPAddrPort(buffer.Bytes(), M.SocksaddrFrom(addresses[0], destination.Port).AddrPort()))
2022-07-08 12:58:43 +08:00
}
return common.Error(w.UDPConn.WriteToUDPAddrPort(buffer.Bytes(), destination.AddrPort()))
2022-07-08 12:58:43 +08:00
}
func (w *ResolveUDPConn) Upstream() any {
return w.UDPConn
}
type ResolvePacketConn struct {
net.PacketConn
2022-07-25 16:01:10 +08:00
ctx context.Context
2022-07-08 12:58:43 +08:00
router adapter.Router
2022-07-11 18:44:59 +08:00
strategy dns.DomainStrategy
2022-07-08 12:58:43 +08:00
}
func (w *ResolvePacketConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) {
_, addr, err := buffer.ReadPacketFrom(w)
if err != nil {
return M.Socksaddr{}, err
}
return M.SocksaddrFromNet(addr), err
}
func (w *ResolvePacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
defer buffer.Release()
2022-07-20 22:05:11 +08:00
if destination.IsFqdn() {
2022-07-25 16:01:10 +08:00
addresses, err := w.router.Lookup(w.ctx, destination.Fqdn, w.strategy)
2022-07-08 12:58:43 +08:00
if err != nil {
return err
}
2022-07-22 16:20:24 +08:00
return common.Error(w.WriteTo(buffer.Bytes(), M.SocksaddrFrom(addresses[0], destination.Port).UDPAddr()))
2022-07-08 12:58:43 +08:00
}
return common.Error(w.WriteTo(buffer.Bytes(), destination.UDPAddr()))
}
func (w *ResolvePacketConn) Upstream() any {
return w.PacketConn
}