11package main
22
3- import "C"
43import (
54 "context"
6- "core/state "
5+ "encoding/json "
76 "errors"
87 "fmt"
9- "github.com/metacubex/mihomo/constant/features"
10- "github.com/metacubex/mihomo/hub/route"
11- "github.com/samber/lo"
12- "os"
13- "os/exec"
14- "path/filepath"
15- "runtime"
16- "strings"
17- "sync"
18- "syscall"
19- "time"
20-
218 "github.com/metacubex/mihomo/adapter"
229 "github.com/metacubex/mihomo/adapter/inbound"
2310 "github.com/metacubex/mihomo/adapter/outboundgroup"
@@ -27,84 +14,38 @@ import (
2714 "github.com/metacubex/mihomo/component/resolver"
2815 "github.com/metacubex/mihomo/config"
2916 "github.com/metacubex/mihomo/constant"
17+ "github.com/metacubex/mihomo/constant/features"
3018 cp "github.com/metacubex/mihomo/constant/provider"
3119 "github.com/metacubex/mihomo/hub"
32- "github.com/metacubex/mihomo/hub/executor "
20+ "github.com/metacubex/mihomo/hub/route "
3321 "github.com/metacubex/mihomo/listener"
3422 "github.com/metacubex/mihomo/log"
3523 rp "github.com/metacubex/mihomo/rules/provider"
3624 "github.com/metacubex/mihomo/tunnel"
25+ "github.com/samber/lo"
26+ "os"
27+ "path/filepath"
28+ "runtime"
29+ "strings"
30+ "sync"
3731)
3832
39- type ConfigExtendedParams struct {
40- IsPatch bool `json:"is-patch"`
41- IsCompatible bool `json:"is-compatible"`
42- SelectedMap map [string ]string `json:"selected-map"`
43- TestURL * string `json:"test-url"`
44- OverrideDns bool `json:"override-dns"`
45- }
46-
47- type GenerateConfigParams struct {
48- ProfileId string `json:"profile-id"`
49- Config config.RawConfig `json:"config" `
50- Params ConfigExtendedParams `json:"params"`
51- }
52-
53- type ChangeProxyParams struct {
54- GroupName * string `json:"group-name"`
55- ProxyName * string `json:"proxy-name"`
56- }
57-
58- type TestDelayParams struct {
59- ProxyName string `json:"proxy-name"`
60- Timeout int64 `json:"timeout"`
61- }
62-
63- type ProcessMapItem struct {
64- Id int64 `json:"id"`
65- Value string `json:"value"`
66- }
67-
68- type ExternalProvider struct {
69- Name string `json:"name"`
70- Type string `json:"type"`
71- VehicleType string `json:"vehicle-type"`
72- Count int `json:"count"`
73- Path string `json:"path"`
74- UpdateAt time.Time `json:"update-at"`
75- SubscriptionInfo * provider.SubscriptionInfo `json:"subscription-info"`
76- }
33+ var (
34+ isRunning = false
35+ runLock sync.Mutex
36+ ips = []string {"ipinfo.io" , "ipapi.co" , "api.ip.sb" , "ipwho.is" }
37+ b , _ = batch .New [bool ](context .Background (), batch.WithConcurrencyNum [bool ](50 ))
38+ )
7739
7840type ExternalProviders []ExternalProvider
7941
8042func (a ExternalProviders ) Len () int { return len (a ) }
8143func (a ExternalProviders ) Less (i , j int ) bool { return a [i ].Name < a [j ].Name }
8244func (a ExternalProviders ) Swap (i , j int ) { a [i ], a [j ] = a [j ], a [i ] }
8345
84- var b , _ = batch .New [bool ](context .Background (), batch.WithConcurrencyNum [bool ](50 ))
85-
86- func restartExecutable (execPath string ) {
87- var err error
88- executor .Shutdown ()
89- if runtime .GOOS == "windows" {
90- cmd := exec .Command (execPath , os .Args [1 :]... )
91- log .Infoln ("restarting: %q %q" , execPath , os .Args [1 :])
92- cmd .Stdin = os .Stdin
93- cmd .Stdout = os .Stdout
94- cmd .Stderr = os .Stderr
95- err = cmd .Start ()
96- if err != nil {
97- log .Fatalln ("restarting: %s" , err )
98- }
99-
100- os .Exit (0 )
101- }
102-
103- log .Infoln ("restarting: %q %q" , execPath , os .Args [1 :])
104- err = syscall .Exec (execPath , os .Args , os .Environ ())
105- if err != nil {
106- log .Fatalln ("restarting: %s" , err )
107- }
46+ func (message * Message ) Json () (string , error ) {
47+ data , err := json .Marshal (message )
48+ return string (data ), err
10849}
10950
11051func readFile (path string ) ([]byte , error ) {
@@ -119,19 +60,6 @@ func readFile(path string) ([]byte, error) {
11960 return data , err
12061}
12162
122- func removeFile (path string ) error {
123- absPath , err := filepath .Abs (path )
124- if err != nil {
125- return err
126- }
127- err = os .Remove (absPath )
128- if err != nil {
129- return err
130- }
131-
132- return nil
133- }
134-
13563func getProfilePath (id string ) string {
13664 return filepath .Join (constant .Path .HomeDir (), "profiles" , id + ".yaml" )
13765}
@@ -262,8 +190,6 @@ func trimArr(arr []string) (r []string) {
262190 return
263191}
264192
265- var ips = []string {"ipinfo.io" , "ipapi.co" , "api.ip.sb" , "ipwho.is" }
266-
267193func overrideRules (rules * []string ) {
268194 var target = ""
269195 for _ , line := range * rules {
@@ -325,20 +251,13 @@ func overwriteConfig(targetConfig *config.RawConfig, patchConfig config.RawConfi
325251 }
326252 }
327253 overrideRules (& targetConfig .Rule )
328- //if runtime.GOOS == "android" {
329- // targetConfig.DNS.NameServer = append(targetConfig.DNS.NameServer, "dhcp://"+dns.SystemDNSPlaceholder)
330- //} else if runtime.GOOS == "windows" {
331- // targetConfig.DNS.NameServer = append(targetConfig.DNS.NameServer, dns.SystemDNSPlaceholder)
332- //}
333- //if configParams.IsCompatible == false {
334- // targetConfig.ProxyProvider = make(map[string]map[string]any)
335- // targetConfig.RuleProvider = make(map[string]map[string]any)
336- // generateProxyGroupAndRule(&targetConfig.ProxyGroup, &targetConfig.Rule)
337- //}
338254}
339255
340- func patchConfig (general * config. General , controller * config. Controller , tls * config. TLS ) {
256+ func patchConfig () {
341257 log .Infoln ("[Apply] patch" )
258+ general := currentConfig .General
259+ controller := currentConfig .Controller
260+ tls := currentConfig .TLS
342261 tunnel .SetSniffing (general .Sniffing )
343262 tunnel .SetFindProcessMode (general .FindProcessMode )
344263 dialer .SetTcpConcurrent (general .TCPConcurrent )
@@ -365,17 +284,15 @@ func patchConfig(general *config.General, controller *config.Controller, tls *co
365284 })
366285}
367286
368- var isRunning = false
369-
370- var runLock sync.Mutex
371-
372- func updateListeners (general * config.General , listeners map [string ]constant.InboundListener ) {
287+ func updateListeners (force bool ) {
373288 if ! isRunning {
374289 return
375290 }
376- runLock .Lock ()
377- defer runLock .Unlock ()
378- stopListeners ()
291+ general := currentConfig .General
292+ listeners := currentConfig .Listeners
293+ if force == true {
294+ stopListeners ()
295+ }
379296 listener .PatchInboundListeners (listeners , tunnel .Tunnel , true )
380297 listener .SetAllowLan (general .AllowLan )
381298 inbound .SetSkipAuthPrefixes (general .SkipAuthPrefixes )
@@ -424,19 +341,22 @@ func patchSelectGroup() {
424341 }
425342}
426343
427- func applyConfig () error {
428- cfg , err := config .ParseRawConfig (state .CurrentRawConfig )
344+ func applyConfig (rawConfig * config.RawConfig ) error {
345+ runLock .Lock ()
346+ defer runLock .Unlock ()
347+ var err error
348+ currentConfig , err = config .ParseRawConfig (rawConfig )
429349 if err != nil {
430- cfg , _ = config .ParseRawConfig (config .DefaultRawConfig ())
350+ currentConfig , _ = config .ParseRawConfig (config .DefaultRawConfig ())
431351 }
432352 if configParams .IsPatch {
433- patchConfig (cfg . General , cfg . Controller , cfg . TLS )
353+ patchConfig ()
434354 } else {
435- closeConnections ()
355+ handleCloseConnectionsUnLock ()
436356 runtime .GC ()
437- hub .ApplyConfig (cfg )
357+ hub .ApplyConfig (currentConfig )
438358 patchSelectGroup ()
439359 }
440- updateListeners (cfg . General , cfg . Listeners )
360+ updateListeners (false )
441361 return err
442362}
0 commit comments