From 0f32c054f47641a2ee9c9362fc65652e772924b2 Mon Sep 17 00:00:00 2001 From: enfein <83481737+enfein@users.noreply.github.com> Date: Thu, 20 Mar 2025 05:58:04 +0000 Subject: [PATCH] feat: support UDP over TCP in mieru (#1926) --- adapter/outbound/mieru.go | 22 +++++++++++++++++++++- docs/config.yaml | 1 + go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/adapter/outbound/mieru.go b/adapter/outbound/mieru.go index 7aab2f58..d2209442 100644 --- a/adapter/outbound/mieru.go +++ b/adapter/outbound/mieru.go @@ -8,11 +8,13 @@ import ( "strconv" "sync" + CN "github.com/metacubex/mihomo/common/net" "github.com/metacubex/mihomo/component/dialer" "github.com/metacubex/mihomo/component/proxydialer" C "github.com/metacubex/mihomo/constant" mieruclient "github.com/enfein/mieru/v3/apis/client" + mierucommon "github.com/enfein/mieru/v3/apis/common" mierumodel "github.com/enfein/mieru/v3/apis/model" mierupb "github.com/enfein/mieru/v3/pkg/appctl/appctlpb" "google.golang.org/protobuf/proto" @@ -32,6 +34,7 @@ type MieruOption struct { Port int `proxy:"port,omitempty"` PortRange string `proxy:"port-range,omitempty"` Transport string `proxy:"transport"` + UDP bool `proxy:"udp,omitempty"` UserName string `proxy:"username"` Password string `proxy:"password"` Multiplexing string `proxy:"multiplexing,omitempty"` @@ -50,6 +53,23 @@ func (m *Mieru) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d return NewConn(c, m), nil } +// ListenPacketContext implements C.ProxyAdapter +func (m *Mieru) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) { + if err := m.ensureClientIsRunning(opts...); err != nil { + return nil, err + } + c, err := m.client.DialContext(ctx, metadata.UDPAddr()) + if err != nil { + return nil, fmt.Errorf("dial to %s failed: %w", metadata.UDPAddr(), err) + } + return newPacketConn(CN.NewRefPacketConn(CN.NewThreadSafePacketConn(mierucommon.NewUDPAssociateWrapper(mierucommon.NewPacketOverStreamTunnel(c))), m), m), nil +} + +// SupportUOT implements C.ProxyAdapter +func (m *Mieru) SupportUOT() bool { + return true +} + // ProxyInfo implements C.ProxyAdapter func (m *Mieru) ProxyInfo() C.ProxyInfo { info := m.Base.ProxyInfo() @@ -113,7 +133,7 @@ func NewMieru(option MieruOption) (*Mieru, error) { addr: addr, iface: option.Interface, tp: C.Mieru, - udp: false, + udp: option.UDP, xudp: false, rmark: option.RoutingMark, prefer: C.NewDNSPrefer(option.IPVersion), diff --git a/docs/config.yaml b/docs/config.yaml index 263d67b6..fc11dca6 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -879,6 +879,7 @@ proxies: # socks5 port: 2999 # port-range: 2090-2099 #(不可同时填写 port 和 port-range) transport: TCP # 只支持 TCP + udp: true # 支持 UDP over TCP username: user password: password # 可以使用的值包括 MULTIPLEXING_OFF, MULTIPLEXING_LOW, MULTIPLEXING_MIDDLE, MULTIPLEXING_HIGH。其中 MULTIPLEXING_OFF 会关闭多路复用功能。默认值为 MULTIPLEXING_LOW。 diff --git a/go.mod b/go.mod index 4787606a..b327e196 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/bahlo/generic-list-go v0.2.0 github.com/coreos/go-iptables v0.8.0 github.com/dlclark/regexp2 v1.11.5 - github.com/enfein/mieru/v3 v3.12.0 + github.com/enfein/mieru/v3 v3.13.0 github.com/go-chi/chi/v5 v5.2.1 github.com/go-chi/render v1.0.3 github.com/gobwas/ws v1.4.0 diff --git a/go.sum b/go.sum index c7711bea..9cfd6e0e 100644 --- a/go.sum +++ b/go.sum @@ -28,8 +28,8 @@ github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZ github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/ebitengine/purego v0.8.2 h1:jPPGWs2sZ1UgOSgD2bClL0MJIqu58nOmIcBuXr62z1I= github.com/ebitengine/purego v0.8.2/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= -github.com/enfein/mieru/v3 v3.12.0 h1:sV3moozWpRjjqwqFZJjGtMB0EacN8+D7BpjzsmacsXM= -github.com/enfein/mieru/v3 v3.12.0/go.mod h1:zJBUCsi5rxyvHM8fjFf+GLaEl4OEjjBXr1s5F6Qd3hM= +github.com/enfein/mieru/v3 v3.13.0 h1:eGyxLGkb+lut9ebmx+BGwLJ5UMbEc/wGIYO0AXEKy98= +github.com/enfein/mieru/v3 v3.13.0/go.mod h1:zJBUCsi5rxyvHM8fjFf+GLaEl4OEjjBXr1s5F6Qd3hM= github.com/ericlagergren/aegis v0.0.0-20230312195928-b4ce538b56f9 h1:/5RkVc9Rc81XmMyVqawCiDyrBHZbLAZgTTCqou4mwj8= github.com/ericlagergren/aegis v0.0.0-20230312195928-b4ce538b56f9/go.mod h1:hkIFzoiIPZYxdFOOLyDho59b7SrDfo+w3h+yWdlg45I= github.com/ericlagergren/polyval v0.0.0-20220411101811-e25bc10ba391 h1:8j2RH289RJplhA6WfdaPqzg1MjH2K8wX5e0uhAxrw2g=