Skip to content

Commit 8e6eb70

Browse files
committed
chore: temporary update general in ParseRawConfig and rollback before its retur
1 parent 9937ae1 commit 8e6eb70

File tree

3 files changed

+46
-33
lines changed

3 files changed

+46
-33
lines changed

component/updater/update_ui.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (u *UIUpdater) AutoDownloadUI() {
5858
log.Infoln("External UI downloading ...")
5959
err := u.downloadUI()
6060
if err != nil {
61-
log.Errorln("Error downloading UI:", err)
61+
log.Errorln("Error downloading UI: %s", err)
6262
}
6363
}
6464
}

config/config.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/url"
1010
"strings"
1111
"time"
12+
_ "unsafe"
1213

1314
"github.com/metacubex/mihomo/adapter"
1415
"github.com/metacubex/mihomo/adapter/outbound"
@@ -19,12 +20,9 @@ import (
1920
"github.com/metacubex/mihomo/component/cidr"
2021
"github.com/metacubex/mihomo/component/fakeip"
2122
"github.com/metacubex/mihomo/component/geodata"
22-
mihomoHttp "github.com/metacubex/mihomo/component/http"
2323
P "github.com/metacubex/mihomo/component/process"
2424
"github.com/metacubex/mihomo/component/resolver"
25-
"github.com/metacubex/mihomo/component/resource"
2625
"github.com/metacubex/mihomo/component/sniffer"
27-
tlsC "github.com/metacubex/mihomo/component/tls"
2826
"github.com/metacubex/mihomo/component/trie"
2927
C "github.com/metacubex/mihomo/constant"
3028
providerTypes "github.com/metacubex/mihomo/constant/provider"
@@ -588,10 +586,11 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
588586
}
589587
config.General = general
590588

591-
if len(config.General.GlobalClientFingerprint) != 0 {
592-
log.Debugln("GlobalClientFingerprint: %s", config.General.GlobalClientFingerprint)
593-
tlsC.SetGlobalUtlsClient(config.General.GlobalClientFingerprint)
594-
}
589+
// We need to temporarily apply some configuration in general and roll back after parsing the complete configuration.
590+
// The loading and downloading of geodata in the parseRules and parseRuleProviders rely on these.
591+
// This implementation is very disgusting, but there is currently no better solution
592+
rollback := temporaryUpdateGeneral(config.General)
593+
defer rollback()
595594

596595
controller, err := parseController(rawCfg)
597596
if err != nil {
@@ -707,17 +706,10 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
707706
return config, nil
708707
}
709708

710-
func parseGeneral(cfg *RawConfig) (*General, error) {
711-
geodata.SetGeodataMode(cfg.GeodataMode)
712-
geodata.SetLoader(cfg.GeodataLoader)
713-
geodata.SetSiteMatcher(cfg.GeositeMatcher)
714-
geodata.SetGeoIpUrl(cfg.GeoXUrl.GeoIp)
715-
geodata.SetGeoSiteUrl(cfg.GeoXUrl.GeoSite)
716-
geodata.SetMmdbUrl(cfg.GeoXUrl.Mmdb)
717-
geodata.SetASNUrl(cfg.GeoXUrl.ASN)
718-
mihomoHttp.SetUA(cfg.GlobalUA)
719-
resource.SetETag(cfg.ETagSupport)
709+
//go:linkname temporaryUpdateGeneral
710+
func temporaryUpdateGeneral(general *General) func()
720711

712+
func parseGeneral(cfg *RawConfig) (*General, error) {
721713
return &General{
722714
Inbound: Inbound{
723715
Port: cfg.Port,
@@ -751,6 +743,7 @@ func parseGeneral(cfg *RawConfig) (*General, error) {
751743
GeoUpdateInterval: cfg.GeoUpdateInterval,
752744
GeodataMode: cfg.GeodataMode,
753745
GeodataLoader: cfg.GeodataLoader,
746+
GeositeMatcher: cfg.GeositeMatcher,
754747
TCPConcurrent: cfg.TCPConcurrent,
755748
FindProcessMode: cfg.FindProcessMode,
756749
GlobalClientFingerprint: cfg.GlobalClientFingerprint,

hub/executor/executor.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import (
99
"strconv"
1010
"sync"
1111
"time"
12+
_ "unsafe"
1213

1314
"github.com/metacubex/mihomo/adapter"
1415
"github.com/metacubex/mihomo/adapter/inbound"
1516
"github.com/metacubex/mihomo/adapter/outboundgroup"
1617
"github.com/metacubex/mihomo/component/auth"
1718
"github.com/metacubex/mihomo/component/ca"
1819
"github.com/metacubex/mihomo/component/dialer"
19-
G "github.com/metacubex/mihomo/component/geodata"
20+
"github.com/metacubex/mihomo/component/geodata"
2021
mihomoHttp "github.com/metacubex/mihomo/component/http"
2122
"github.com/metacubex/mihomo/component/iface"
2223
"github.com/metacubex/mihomo/component/keepalive"
@@ -101,7 +102,7 @@ func ApplyConfig(cfg *config.Config, force bool) {
101102
updateRules(cfg.Rules, cfg.SubRules, cfg.RuleProviders)
102103
updateSniffer(cfg.Sniffer)
103104
updateHosts(cfg.Hosts)
104-
updateGeneral(cfg.General)
105+
updateGeneral(cfg.General, true)
105106
updateNTP(cfg.NTP)
106107
updateDNS(cfg.DNS, cfg.General.IPv6)
107108
updateListeners(cfg.General, cfg.Listeners, force)
@@ -161,16 +162,16 @@ func GetGeneral() *config.General {
161162
Interface: dialer.DefaultInterface.Load(),
162163
RoutingMark: int(dialer.DefaultRoutingMark.Load()),
163164
GeoXUrl: config.GeoXUrl{
164-
GeoIp: G.GeoIpUrl(),
165-
Mmdb: G.MmdbUrl(),
166-
ASN: G.ASNUrl(),
167-
GeoSite: G.GeoSiteUrl(),
165+
GeoIp: geodata.GeoIpUrl(),
166+
Mmdb: geodata.MmdbUrl(),
167+
ASN: geodata.ASNUrl(),
168+
GeoSite: geodata.GeoSiteUrl(),
168169
},
169170
GeoAutoUpdate: updater.GeoAutoUpdate(),
170171
GeoUpdateInterval: updater.GeoUpdateInterval(),
171-
GeodataMode: G.GeodataMode(),
172-
GeodataLoader: G.LoaderName(),
173-
GeositeMatcher: G.SiteMatcherName(),
172+
GeodataMode: geodata.GeodataMode(),
173+
GeodataLoader: geodata.LoaderName(),
174+
GeositeMatcher: geodata.SiteMatcherName(),
174175
TCPConcurrent: dialer.GetTcpConcurrent(),
175176
FindProcessMode: tunnel.FindProcessMode(),
176177
Sniffing: tunnel.IsSniffing(),
@@ -408,13 +409,22 @@ func updateUpdater(cfg *config.Config) {
408409
updater.DefaultUiUpdater.AutoDownloadUI()
409410
}
410411

411-
func updateGeneral(general *config.General) {
412+
//go:linkname temporaryUpdateGeneral github.com/metacubex/mihomo/config.temporaryUpdateGeneral
413+
func temporaryUpdateGeneral(general *config.General) func() {
414+
oldGeneral := GetGeneral()
415+
updateGeneral(general, false)
416+
return func() {
417+
updateGeneral(oldGeneral, false)
418+
}
419+
}
420+
421+
func updateGeneral(general *config.General, logging bool) {
412422
tunnel.SetMode(general.Mode)
413423
tunnel.SetFindProcessMode(general.FindProcessMode)
414424
resolver.DisableIPv6 = !general.IPv6
415425

416-
if general.TCPConcurrent {
417-
dialer.SetTcpConcurrent(general.TCPConcurrent)
426+
dialer.SetTcpConcurrent(general.TCPConcurrent)
427+
if logging && general.TCPConcurrent {
418428
log.Infoln("Use tcp concurrent")
419429
}
420430

@@ -429,13 +439,23 @@ func updateGeneral(general *config.General) {
429439

430440
dialer.DefaultInterface.Store(general.Interface)
431441
dialer.DefaultRoutingMark.Store(int32(general.RoutingMark))
432-
if general.RoutingMark > 0 {
442+
if logging && general.RoutingMark > 0 {
433443
log.Infoln("Use routing mark: %#x", general.RoutingMark)
434444
}
435445

436446
iface.FlushCache()
437-
G.SetLoader(general.GeodataLoader)
438-
G.SetSiteMatcher(general.GeositeMatcher)
447+
448+
geodata.SetGeodataMode(general.GeodataMode)
449+
geodata.SetLoader(general.GeodataLoader)
450+
geodata.SetSiteMatcher(general.GeositeMatcher)
451+
geodata.SetGeoIpUrl(general.GeoXUrl.GeoIp)
452+
geodata.SetGeoSiteUrl(general.GeoXUrl.GeoSite)
453+
geodata.SetMmdbUrl(general.GeoXUrl.Mmdb)
454+
geodata.SetASNUrl(general.GeoXUrl.ASN)
455+
mihomoHttp.SetUA(general.GlobalUA)
456+
resource.SetETag(general.ETagSupport)
457+
458+
tlsC.SetGlobalUtlsClient(general.GlobalClientFingerprint)
439459
}
440460

441461
func updateUsers(users []auth.AuthUser) {

0 commit comments

Comments
 (0)