From 146008a6311ca52c406d385a0b7686d1ebe31833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 25 Jul 2022 16:01:10 +0800 Subject: [PATCH] Fix direct udp outbound --- common/dialer/resolve.go | 2 +- common/dialer/resolve_conn.go | 12 ++++++---- option/outbound.go | 2 +- option/vmess.go | 44 ++++++----------------------------- outbound/direct.go | 8 +------ 5 files changed, 17 insertions(+), 51 deletions(-) diff --git a/common/dialer/resolve.go b/common/dialer/resolve.go index 50334166..d5f77fcd 100644 --- a/common/dialer/resolve.go +++ b/common/dialer/resolve.go @@ -69,7 +69,7 @@ func (d *ResolveDialer) ListenPacket(ctx context.Context, destination M.Socksadd if err != nil { return nil, err } - return NewResolvePacketConn(d.router, d.strategy, conn), nil + return NewResolvePacketConn(ctx, d.router, d.strategy, conn), nil } func (d *ResolveDialer) Upstream() any { diff --git a/common/dialer/resolve_conn.go b/common/dialer/resolve_conn.go index 5d5d112a..b6084971 100644 --- a/common/dialer/resolve_conn.go +++ b/common/dialer/resolve_conn.go @@ -12,16 +12,17 @@ import ( N "github.com/sagernet/sing/common/network" ) -func NewResolvePacketConn(router adapter.Router, strategy dns.DomainStrategy, conn net.PacketConn) N.NetPacketConn { +func NewResolvePacketConn(ctx context.Context, router adapter.Router, strategy dns.DomainStrategy, conn net.PacketConn) N.NetPacketConn { if udpConn, ok := conn.(*net.UDPConn); ok { - return &ResolveUDPConn{udpConn, router, strategy} + return &ResolveUDPConn{udpConn, ctx, router, strategy} } else { - return &ResolvePacketConn{conn, router, strategy} + return &ResolvePacketConn{conn, ctx, router, strategy} } } type ResolveUDPConn struct { *net.UDPConn + ctx context.Context router adapter.Router strategy dns.DomainStrategy } @@ -38,7 +39,7 @@ func (w *ResolveUDPConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) { func (w *ResolveUDPConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error { defer buffer.Release() if destination.IsFqdn() { - addresses, err := w.router.Lookup(context.Background(), destination.Fqdn, w.strategy) + addresses, err := w.router.Lookup(w.ctx, destination.Fqdn, w.strategy) if err != nil { return err } @@ -53,6 +54,7 @@ func (w *ResolveUDPConn) Upstream() any { type ResolvePacketConn struct { net.PacketConn + ctx context.Context router adapter.Router strategy dns.DomainStrategy } @@ -68,7 +70,7 @@ func (w *ResolvePacketConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) func (w *ResolvePacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error { defer buffer.Release() if destination.IsFqdn() { - addresses, err := w.router.Lookup(context.Background(), destination.Fqdn, w.strategy) + addresses, err := w.router.Lookup(w.ctx, destination.Fqdn, w.strategy) if err != nil { return err } diff --git a/option/outbound.go b/option/outbound.go index 4d68458d..fab6dd07 100644 --- a/option/outbound.go +++ b/option/outbound.go @@ -70,7 +70,7 @@ func (h *Outbound) UnmarshalJSON(bytes []byte) error { case C.TypeURLTest: v = &h.URLTestOptions default: - return nil + return E.New("unknown outbound type: ", h.Type) } err = UnmarshallExcluded(bytes, (*_Outbound)(h), v) if err != nil { diff --git a/option/vmess.go b/option/vmess.go index 9a1e5299..8899dab6 100644 --- a/option/vmess.go +++ b/option/vmess.go @@ -1,9 +1,5 @@ package option -import ( - E "github.com/sagernet/sing/common/exceptions" -) - type VMessInboundOptions struct { ListenOptions Users []VMessUser `json:"users,omitempty"` @@ -18,37 +14,11 @@ type VMessUser struct { type VMessOutboundOptions struct { OutboundDialerOptions ServerOptions - UUID string `json:"uuid"` - Security string `json:"security"` - AlterId int `json:"alter_id,omitempty"` - GlobalPadding bool `json:"global_padding,omitempty"` - AuthenticatedLength bool `json:"authenticated_length,omitempty"` - Network NetworkList `json:"network,omitempty"` - TLSOptions *OutboundTLSOptions `json:"tls,omitempty"` - TransportOptions *VMessOutboundTransportOptions `json:"transport,omitempty"` -} - -type _VMessOutboundTransportOptions struct { - Type string `json:"network,omitempty"` - HTTPOptions *VMessOutboundHTTPOptions `json:"-"` -} - -type VMessOutboundTransportOptions _VMessOutboundTransportOptions - -func (o VMessOutboundTransportOptions) MarshalJSON() ([]byte, error) { - var v any - switch o.Type { - case "http": - v = o.HTTPOptions - default: - return nil, E.New("unknown transport type: ", o.Type) - } - return MarshallObjects(_VMessOutboundTransportOptions(o), v) -} - -type VMessOutboundHTTPOptions struct { - Method string `json:"method,omitempty"` - Host string `json:"host,omitempty"` - Path []string `proxy:"path,omitempty"` - Headers map[string]string `proxy:"headers,omitempty"` + UUID string `json:"uuid"` + Security string `json:"security"` + AlterId int `json:"alter_id,omitempty"` + GlobalPadding bool `json:"global_padding,omitempty"` + AuthenticatedLength bool `json:"authenticated_length,omitempty"` + Network NetworkList `json:"network,omitempty"` + TLSOptions *OutboundTLSOptions `json:"tls,omitempty"` } diff --git a/outbound/direct.go b/outbound/direct.go index b6a411f4..d0aaac00 100644 --- a/outbound/direct.go +++ b/outbound/direct.go @@ -9,7 +9,6 @@ import ( C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" - "github.com/sagernet/sing/common/bufio" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" ) @@ -79,12 +78,7 @@ func (h *Direct) ListenPacket(ctx context.Context, destination M.Socksaddr) (net } func (h *Direct) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { - ctx = adapter.WithContext(ctx, &metadata) - outConn, err := h.DialContext(ctx, C.NetworkTCP, metadata.Destination) - if err != nil { - return err - } - return bufio.CopyConn(ctx, conn, outConn) + return NewConnection(ctx, h, conn, metadata) } func (h *Direct) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {