From 4362dfacc9a7b65c7f71023dcaf1e4fc6e94e012 Mon Sep 17 00:00:00 2001 From: H1JK Date: Sat, 18 Nov 2023 15:30:35 +0800 Subject: [PATCH] fix: Mux missing sing logger & initializing race --- adapter/outbound/singmux.go | 2 ++ listener/sing/sing.go | 24 +++++++++--------------- listener/sing_hysteria2/server.go | 4 ++++ listener/sing_shadowsocks/server.go | 4 ++++ listener/sing_tun/server.go | 4 ++++ listener/sing_vmess/server.go | 4 ++++ listener/tuic/server.go | 4 ++++ 7 files changed, 31 insertions(+), 15 deletions(-) diff --git a/adapter/outbound/singmux.go b/adapter/outbound/singmux.go index 292ad1d8..e9dc660b 100644 --- a/adapter/outbound/singmux.go +++ b/adapter/outbound/singmux.go @@ -10,6 +10,7 @@ import ( "github.com/metacubex/mihomo/component/proxydialer" "github.com/metacubex/mihomo/component/resolver" C "github.com/metacubex/mihomo/constant" + "github.com/metacubex/mihomo/log" mux "github.com/sagernet/sing-mux" E "github.com/sagernet/sing/common/exceptions" @@ -107,6 +108,7 @@ func NewSingMux(option SingMuxOption, proxy C.ProxyAdapter, base ProxyBase) (C.P singDialer := proxydialer.NewSingDialer(proxy, dialer.NewDialer(), option.Statistic) client, err := mux.NewClient(mux.Options{ Dialer: singDialer, + Logger: log.SingLogger, Protocol: option.Protocol, MaxConnections: option.MaxConnections, MinStreams: option.MinStreams, diff --git a/listener/sing/sing.go b/listener/sing/sing.go index deec7bf8..990bbb14 100644 --- a/listener/sing/sing.go +++ b/listener/sing/sing.go @@ -31,7 +31,7 @@ type ListenerHandler struct { Type C.Type Additions []inbound.Addition UDPTimeout time.Duration - MuxService *mux.Service + muxService *mux.Service } func UpstreamMetadata(metadata M.Metadata) M.Metadata { @@ -49,8 +49,8 @@ func ConvertMetadata(metadata *C.Metadata) M.Metadata { } } -func (h *ListenerHandler) ConfigureSingMux() (err error) { - h.MuxService, err = mux.NewService(mux.ServiceOptions{ +func (h *ListenerHandler) Initialize() (err error) { + h.muxService, err = mux.NewService(mux.ServiceOptions{ NewStreamContext: func(ctx context.Context, conn net.Conn) context.Context { return ctx }, @@ -65,26 +65,20 @@ func (h *ListenerHandler) ConfigureSingMux() (err error) { func (h *ListenerHandler) IsSpecialFqdn(fqdn string) bool { switch fqdn { - case mux.Destination.Fqdn: - case vmess.MuxDestination.Fqdn: - case uot.MagicAddress: - case uot.LegacyMagicAddress: + case mux.Destination.Fqdn, + vmess.MuxDestination.Fqdn, + uot.MagicAddress, + uot.LegacyMagicAddress: + return true default: return false } - return true } func (h *ListenerHandler) ParseSpecialFqdn(ctx context.Context, conn net.Conn, metadata M.Metadata) error { switch metadata.Destination.Fqdn { case mux.Destination.Fqdn: - if h.MuxService == nil { - err := h.ConfigureSingMux() - if err != nil { - return err - } - } - return h.MuxService.NewConnection(ctx, conn, UpstreamMetadata(metadata)) + return h.muxService.NewConnection(ctx, conn, UpstreamMetadata(metadata)) case vmess.MuxDestination.Fqdn: return vmess.HandleMuxConnection(ctx, conn, h) case uot.MagicAddress: diff --git a/listener/sing_hysteria2/server.go b/listener/sing_hysteria2/server.go index 96553995..f6c646a8 100644 --- a/listener/sing_hysteria2/server.go +++ b/listener/sing_hysteria2/server.go @@ -47,6 +47,10 @@ func New(config LC.Hysteria2Server, tunnel C.Tunnel, additions ...inbound.Additi Type: C.HYSTERIA2, Additions: additions, } + err = h.Initialize() + if err != nil { + return nil, err + } sl = &Listener{false, config, nil, nil} diff --git a/listener/sing_shadowsocks/server.go b/listener/sing_shadowsocks/server.go index 5a4896af..af122775 100644 --- a/listener/sing_shadowsocks/server.go +++ b/listener/sing_shadowsocks/server.go @@ -55,6 +55,10 @@ func New(config LC.ShadowsocksServer, tunnel C.Tunnel, additions ...inbound.Addi Type: C.SHADOWSOCKS, Additions: additions, } + err = h.Initialize() + if err != nil { + return nil, err + } sl = &Listener{false, config, nil, nil, nil} diff --git a/listener/sing_tun/server.go b/listener/sing_tun/server.go index 212c3d90..5b28da04 100644 --- a/listener/sing_tun/server.go +++ b/listener/sing_tun/server.go @@ -159,6 +159,10 @@ func New(options LC.Tun, tunnel C.Tunnel, additions ...inbound.Addition) (l *Lis }, DnsAdds: dnsAdds, } + err = handler.Initialize() + if err != nil { + return nil, err + } l = &Listener{ closed: false, options: options, diff --git a/listener/sing_vmess/server.go b/listener/sing_vmess/server.go index e790e3bc..d444a53e 100644 --- a/listener/sing_vmess/server.go +++ b/listener/sing_vmess/server.go @@ -45,6 +45,10 @@ func New(config LC.VmessServer, tunnel C.Tunnel, additions ...inbound.Addition) Type: C.VMESS, Additions: additions, } + err = h.Initialize() + if err != nil { + return nil, err + } service := vmess.NewService[string](h, vmess.ServiceWithDisableHeaderProtection(), vmess.ServiceWithTimeFunc(ntp.Now)) err = service.UpdateUsers( diff --git a/listener/tuic/server.go b/listener/tuic/server.go index 7fa7b18e..36d86e7f 100644 --- a/listener/tuic/server.go +++ b/listener/tuic/server.go @@ -43,6 +43,10 @@ func New(config LC.TuicServer, tunnel C.Tunnel, additions ...inbound.Addition) ( Type: C.TUIC, Additions: additions, } + err := h.Initialize() + if err != nil { + return nil, err + } cert, err := CN.ParseCert(config.Certificate, config.PrivateKey, C.Path) if err != nil {