Compare commits

..

3 Commits

Author SHA1 Message Date
Shtorm
545424b86b Fix wireguard 2026-08-08 20:49:55 +03:00
Shtorm
1d10636a5f Fix mieru outbound with fqdn server address 2026-08-08 20:46:52 +03:00
Shtorm
9f34cdb2f8 Fix DefaultGateway 2026-08-08 20:43:13 +03:00
4 changed files with 43 additions and 22 deletions

2
go.mod
View File

@@ -246,7 +246,7 @@ require (
lukechampine.com/blake3 v1.4.1 lukechampine.com/blake3 v1.4.1
) )
replace github.com/sagernet/wireguard-go => github.com/shtorm-7/wireguard-go v0.0.2-beta.1-extended-1.5.0 replace github.com/sagernet/wireguard-go => github.com/shtorm-7/wireguard-go v0.0.2-beta.1-extended-1.5.1
replace github.com/sagernet/tailscale => github.com/shtorm-7/tailscale v1.92.4-sing-box-1.13-mod.7-extended-1.0.2 replace github.com/sagernet/tailscale => github.com/shtorm-7/tailscale v1.92.4-sing-box-1.13-mod.7-extended-1.0.2

4
go.sum
View File

@@ -457,8 +457,8 @@ github.com/shtorm-7/sing-vmess v0.2.8-extended-1.0.0 h1:OjvqZOhYSi9eIJGYC0iPDPqv
github.com/shtorm-7/sing-vmess v0.2.8-extended-1.0.0/go.mod h1:P11scgTxMxVVQ8dlM27yNm3Cro40mD0+gHbnqrNGDuY= github.com/shtorm-7/sing-vmess v0.2.8-extended-1.0.0/go.mod h1:P11scgTxMxVVQ8dlM27yNm3Cro40mD0+gHbnqrNGDuY=
github.com/shtorm-7/tailscale v1.92.4-sing-box-1.13-mod.7-extended-1.0.2 h1:hSMjh97OszszOd8HrzpaYUQH9dWRRBluJCbwQyz8ZOk= github.com/shtorm-7/tailscale v1.92.4-sing-box-1.13-mod.7-extended-1.0.2 h1:hSMjh97OszszOd8HrzpaYUQH9dWRRBluJCbwQyz8ZOk=
github.com/shtorm-7/tailscale v1.92.4-sing-box-1.13-mod.7-extended-1.0.2/go.mod h1:TYIIqO5sZpWq873rLIeO2usszSMUpR3h6WdqVVs65ug= github.com/shtorm-7/tailscale v1.92.4-sing-box-1.13-mod.7-extended-1.0.2/go.mod h1:TYIIqO5sZpWq873rLIeO2usszSMUpR3h6WdqVVs65ug=
github.com/shtorm-7/wireguard-go v0.0.2-beta.1-extended-1.5.0 h1:7YhTkPhDUK81OV9mRMH26Efobh0VCGlig/HgHAlsf5U= github.com/shtorm-7/wireguard-go v0.0.2-beta.1-extended-1.5.1 h1:diJg9JIT7+x1i4atESz9pHryqFLi2QiC4BZjFlHmkHU=
github.com/shtorm-7/wireguard-go v0.0.2-beta.1-extended-1.5.0/go.mod h1:Me2JlCDYHxnd0mnuX7L5LXAeDHCltI7vSKq3eTE6SVE= github.com/shtorm-7/wireguard-go v0.0.2-beta.1-extended-1.5.1/go.mod h1:Me2JlCDYHxnd0mnuX7L5LXAeDHCltI7vSKq3eTE6SVE=
github.com/shtorm-7/workerpool v0.5.0 h1:NPZuNgyH0EUm4aQsTL09xR1iV+7GCFw6jX9Z4aAVp2s= github.com/shtorm-7/workerpool v0.5.0 h1:NPZuNgyH0EUm4aQsTL09xR1iV+7GCFw6jX9Z4aAVp2s=
github.com/shtorm-7/workerpool v0.5.0/go.mod h1:NI0pUZgmGu0BdKO9j3mct1DNZmgXbyTS9foorljdH6E= github.com/shtorm-7/workerpool v0.5.0/go.mod h1:NI0pUZgmGu0BdKO9j3mct1DNZmgXbyTS9foorljdH6E=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=

View File

@@ -16,6 +16,7 @@ import (
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
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"
"github.com/sagernet/sing/service"
mieruclient "github.com/enfein/mieru/v3/apis/client" mieruclient "github.com/enfein/mieru/v3/apis/client"
mierucommon "github.com/enfein/mieru/v3/apis/common" mierucommon "github.com/enfein/mieru/v3/apis/common"
@@ -27,9 +28,12 @@ import (
type Outbound struct { type Outbound struct {
outbound.Adapter outbound.Adapter
dialer N.Dialer ctx context.Context
logger log.ContextLogger dnsRouter adapter.DNSRouter
client mieruclient.Client dialer N.Dialer
logger log.ContextLogger
options option.MieruOutboundOptions
client mieruclient.Client
} }
func RegisterOutbound(registry *outbound.Registry) { func RegisterOutbound(registry *outbound.Registry) {
@@ -41,26 +45,42 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Outbound{
Adapter: outbound.NewAdapterWithDialerOptions(C.TypeMieru, tag, []string{N.NetworkTCP, N.NetworkUDP}, options.DialerOptions),
ctx: ctx,
dnsRouter: service.FromContext[adapter.DNSRouter](ctx),
dialer: outboundDialer,
logger: logger,
options: options,
}, nil
}
config, err := buildMieruClientConfig(options, mieruDialer{dialer: outboundDialer}) func (o *Outbound) Start(stage adapter.StartStage) error {
if stage != adapter.StartStatePostStart {
return nil
}
serverOptions := o.options
if M.IsDomainName(serverOptions.Server) {
addresses, err := o.dnsRouter.Lookup(o.ctx, serverOptions.Server, adapter.DNSQueryOptions{})
if err != nil {
return fmt.Errorf("failed to resolve mieru server address: %w", err)
}
serverOptions.Server = addresses[0].String()
}
config, err := buildMieruClientConfig(serverOptions, mieruDialer{dialer: o.dialer})
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to build mieru client config: %w", err) return fmt.Errorf("failed to build mieru client config: %w", err)
} }
c := mieruclient.NewClient() c := mieruclient.NewClient()
if err := c.Store(config); err != nil { if err := c.Store(config); err != nil {
return nil, fmt.Errorf("failed to store mieru client config: %w", err) return fmt.Errorf("failed to store mieru client config: %w", err)
} }
if err := c.Start(); err != nil { if err := c.Start(); err != nil {
return nil, fmt.Errorf("failed to start mieru client: %w", err) return fmt.Errorf("failed to start mieru client: %w", err)
} }
logger.NoticeContext(ctx, "mieru client is started") o.logger.NoticeContext(o.ctx, "mieru client is started")
o.client = c
return &Outbound{ return nil
Adapter: outbound.NewAdapterWithDialerOptions(C.TypeMieru, tag, []string{N.NetworkTCP, N.NetworkUDP}, options.DialerOptions),
dialer: outboundDialer,
logger: logger,
client: c,
}, nil
} }
func (o *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { func (o *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {

View File

@@ -126,18 +126,19 @@ func (s *ServerEndpoint) DialContext(ctx context.Context, network string, destin
} }
} }
if gateway == nil { if gateway == nil {
if destination.IsIPv4() { if s.defaultGateway != nil {
gateway = s.defaultGateway
} else if destination.IsIPv4() {
gateway = &destination.Addr gateway = &destination.Addr
destination = M.Socksaddr{ destination = M.Socksaddr{
Addr: Loopback, Addr: Loopback,
Port: destination.Port, Port: destination.Port,
} }
} else if s.defaultGateway != nil {
gateway = s.defaultGateway
} else { } else {
return nil, E.New("missing gateway") return nil, E.New("missing gateway")
} }
} else if destination.Addr.Compare(*gateway) == 0 { }
if destination.Addr.Compare(*gateway) == 0 {
destination = M.Socksaddr{ destination = M.Socksaddr{
Addr: Loopback, Addr: Loopback,
Port: destination.Port, Port: destination.Port,