Fix direct udp outbound

This commit is contained in:
世界 2022-07-25 16:01:10 +08:00
parent cc78f0347d
commit 146008a631
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
5 changed files with 17 additions and 51 deletions

View file

@ -69,7 +69,7 @@ func (d *ResolveDialer) ListenPacket(ctx context.Context, destination M.Socksadd
if err != nil { if err != nil {
return nil, err 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 { func (d *ResolveDialer) Upstream() any {

View file

@ -12,16 +12,17 @@ import (
N "github.com/sagernet/sing/common/network" 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 { if udpConn, ok := conn.(*net.UDPConn); ok {
return &ResolveUDPConn{udpConn, router, strategy} return &ResolveUDPConn{udpConn, ctx, router, strategy}
} else { } else {
return &ResolvePacketConn{conn, router, strategy} return &ResolvePacketConn{conn, ctx, router, strategy}
} }
} }
type ResolveUDPConn struct { type ResolveUDPConn struct {
*net.UDPConn *net.UDPConn
ctx context.Context
router adapter.Router router adapter.Router
strategy dns.DomainStrategy 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 { func (w *ResolveUDPConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
defer buffer.Release() defer buffer.Release()
if destination.IsFqdn() { 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 { if err != nil {
return err return err
} }
@ -53,6 +54,7 @@ func (w *ResolveUDPConn) Upstream() any {
type ResolvePacketConn struct { type ResolvePacketConn struct {
net.PacketConn net.PacketConn
ctx context.Context
router adapter.Router router adapter.Router
strategy dns.DomainStrategy 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 { func (w *ResolvePacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
defer buffer.Release() defer buffer.Release()
if destination.IsFqdn() { 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 { if err != nil {
return err return err
} }

View file

@ -70,7 +70,7 @@ func (h *Outbound) UnmarshalJSON(bytes []byte) error {
case C.TypeURLTest: case C.TypeURLTest:
v = &h.URLTestOptions v = &h.URLTestOptions
default: default:
return nil return E.New("unknown outbound type: ", h.Type)
} }
err = UnmarshallExcluded(bytes, (*_Outbound)(h), v) err = UnmarshallExcluded(bytes, (*_Outbound)(h), v)
if err != nil { if err != nil {

View file

@ -1,9 +1,5 @@
package option package option
import (
E "github.com/sagernet/sing/common/exceptions"
)
type VMessInboundOptions struct { type VMessInboundOptions struct {
ListenOptions ListenOptions
Users []VMessUser `json:"users,omitempty"` Users []VMessUser `json:"users,omitempty"`
@ -18,37 +14,11 @@ type VMessUser struct {
type VMessOutboundOptions struct { type VMessOutboundOptions struct {
OutboundDialerOptions OutboundDialerOptions
ServerOptions ServerOptions
UUID string `json:"uuid"` UUID string `json:"uuid"`
Security string `json:"security"` Security string `json:"security"`
AlterId int `json:"alter_id,omitempty"` AlterId int `json:"alter_id,omitempty"`
GlobalPadding bool `json:"global_padding,omitempty"` GlobalPadding bool `json:"global_padding,omitempty"`
AuthenticatedLength bool `json:"authenticated_length,omitempty"` AuthenticatedLength bool `json:"authenticated_length,omitempty"`
Network NetworkList `json:"network,omitempty"` Network NetworkList `json:"network,omitempty"`
TLSOptions *OutboundTLSOptions `json:"tls,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"`
} }

View file

@ -9,7 +9,6 @@ import (
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common/bufio"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network" 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 { func (h *Direct) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
ctx = adapter.WithContext(ctx, &metadata) return NewConnection(ctx, h, conn, metadata)
outConn, err := h.DialContext(ctx, C.NetworkTCP, metadata.Destination)
if err != nil {
return err
}
return bufio.CopyConn(ctx, conn, outConn)
} }
func (h *Direct) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error { func (h *Direct) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {