From 0a7b7894bd450bf9c262fb1ea891efd1a71f5979 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Thu, 24 Aug 2023 23:33:03 +0800 Subject: [PATCH] feat: proxies support `direct` type --- adapter/outbound/direct.go | 20 ++++++++++++++++++++ adapter/parser.go | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/adapter/outbound/direct.go b/adapter/outbound/direct.go index 94b59cd0..af183b52 100644 --- a/adapter/outbound/direct.go +++ b/adapter/outbound/direct.go @@ -12,6 +12,11 @@ type Direct struct { *Base } +type DirectOption struct { + BasicOption + Name string `proxy:"name"` +} + // DialContext implements C.ProxyAdapter func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) { opts = append(opts, dialer.WithResolver(resolver.DefaultResolver)) @@ -40,6 +45,21 @@ func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata, return newPacketConn(pc, d), nil } +func NewDirectWithOption(option DirectOption) *Direct { + return &Direct{ + Base: &Base{ + name: option.Name, + tp: C.Direct, + udp: true, + tfo: option.TFO, + mpTcp: option.MPTCP, + iface: option.Interface, + rmark: option.RoutingMark, + prefer: C.NewDNSPrefer(option.IPVersion), + }, + } +} + func NewDirect() *Direct { return &Direct{ Base: &Base{ diff --git a/adapter/parser.go b/adapter/parser.go index a561a1ed..78e287f9 100644 --- a/adapter/parser.go +++ b/adapter/parser.go @@ -106,6 +106,13 @@ func ParseProxy(mapping map[string]any) (C.Proxy, error) { break } proxy, err = outbound.NewTuic(*tuicOption) + case "direct": + directOption := &outbound.DirectOption{} + err = decoder.Decode(mapping, directOption) + if err != nil { + break + } + proxy = outbound.NewDirectWithOption(*directOption) default: return nil, fmt.Errorf("unsupport proxy type: %s", proxyType) }