fix: Mux missing sing logger & initializing race

This commit is contained in:
H1JK 2023-11-18 15:30:35 +08:00
parent 05b9071ca6
commit 4362dfacc9
7 changed files with 31 additions and 15 deletions

View File

@ -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,

View File

@ -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:

View File

@ -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}

View File

@ -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}

View File

@ -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,

View File

@ -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(

View File

@ -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 {