Skip to content

Commit 7fc54c5

Browse files
committed
Optimize provider page
Optimize delay test Support local backup and recovery
1 parent 00a78b5 commit 7fc54c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1986
-1121
lines changed

core/common.go

Lines changed: 111 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@ package main
22

33
import "C"
44
import (
5+
"context"
56
"github.com/metacubex/mihomo/adapter"
67
"github.com/metacubex/mihomo/adapter/inbound"
78
"github.com/metacubex/mihomo/adapter/outboundgroup"
8-
ap "github.com/metacubex/mihomo/adapter/provider"
9+
"github.com/metacubex/mihomo/adapter/provider"
10+
"github.com/metacubex/mihomo/common/batch"
911
"github.com/metacubex/mihomo/component/dialer"
1012
"github.com/metacubex/mihomo/component/resolver"
1113
"github.com/metacubex/mihomo/config"
1214
"github.com/metacubex/mihomo/constant"
15+
cp "github.com/metacubex/mihomo/constant/provider"
1316
"github.com/metacubex/mihomo/hub"
1417
"github.com/metacubex/mihomo/hub/executor"
1518
"github.com/metacubex/mihomo/hub/route"
1619
"github.com/metacubex/mihomo/listener"
1720
"github.com/metacubex/mihomo/log"
21+
rp "github.com/metacubex/mihomo/rules/provider"
1822
"github.com/metacubex/mihomo/tunnel"
1923
"os"
2024
"os/exec"
@@ -26,40 +30,40 @@ import (
2630
"time"
2731
)
2832

29-
type healthCheckSchema struct {
30-
Enable bool `provider:"enable"`
31-
URL string `provider:"url"`
32-
Interval int `provider:"interval"`
33-
TestTimeout int `provider:"timeout,omitempty"`
34-
Lazy bool `provider:"lazy,omitempty"`
35-
ExpectedStatus string `provider:"expected-status,omitempty"`
36-
}
37-
38-
type proxyProviderSchema struct {
39-
Type string `provider:"type"`
40-
Path string `provider:"path,omitempty"`
41-
URL string `provider:"url,omitempty"`
42-
Proxy string `provider:"proxy,omitempty"`
43-
Interval int `provider:"interval,omitempty"`
44-
Filter string `provider:"filter,omitempty"`
45-
ExcludeFilter string `provider:"exclude-filter,omitempty"`
46-
ExcludeType string `provider:"exclude-type,omitempty"`
47-
DialerProxy string `provider:"dialer-proxy,omitempty"`
48-
49-
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
50-
Override ap.OverrideSchema `provider:"override,omitempty"`
51-
Header map[string][]string `provider:"header,omitempty"`
52-
}
53-
54-
type ruleProviderSchema struct {
55-
Type string `provider:"type"`
56-
Behavior string `provider:"behavior"`
57-
Path string `provider:"path,omitempty"`
58-
URL string `provider:"url,omitempty"`
59-
Proxy string `provider:"proxy,omitempty"`
60-
Format string `provider:"format,omitempty"`
61-
Interval int `provider:"interval,omitempty"`
62-
}
33+
//type healthCheckSchema struct {
34+
// Enable bool `provider:"enable"`
35+
// URL string `provider:"url"`
36+
// Interval int `provider:"interval"`
37+
// TestTimeout int `provider:"timeout,omitempty"`
38+
// Lazy bool `provider:"lazy,omitempty"`
39+
// ExpectedStatus string `provider:"expected-status,omitempty"`
40+
//}
41+
42+
//type proxyProviderSchema struct {
43+
// Type string `provider:"type"`
44+
// Path string `provider:"path,omitempty"`
45+
// URL string `provider:"url,omitempty"`
46+
// Proxy string `provider:"proxy,omitempty"`
47+
// Interval int `provider:"interval,omitempty"`
48+
// Filter string `provider:"filter,omitempty"`
49+
// ExcludeFilter string `provider:"exclude-filter,omitempty"`
50+
// ExcludeType string `provider:"exclude-type,omitempty"`
51+
// DialerProxy string `provider:"dialer-proxy,omitempty"`
52+
//
53+
// HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
54+
// Override ap.OverrideSchema `provider:"override,omitempty"`
55+
// Header map[string][]string `provider:"header,omitempty"`
56+
//}
57+
//
58+
//type ruleProviderSchema struct {
59+
// Type string `provider:"type"`
60+
// Behavior string `provider:"behavior"`
61+
// Path string `provider:"path,omitempty"`
62+
// URL string `provider:"url,omitempty"`
63+
// Proxy string `provider:"proxy,omitempty"`
64+
// Format string `provider:"format,omitempty"`
65+
// Interval int `provider:"interval,omitempty"`
66+
//}
6367

6468
type ConfigExtendedParams struct {
6569
IsPatch bool `json:"is-patch"`
@@ -69,9 +73,9 @@ type ConfigExtendedParams struct {
6973
}
7074

7175
type GenerateConfigParams struct {
72-
ProfilePath *string `json:"profile-path"`
73-
Config config.RawConfig `json:"config" `
74-
Params ConfigExtendedParams `json:"params"`
76+
ProfileId string `json:"profile-id"`
77+
Config config.RawConfig `json:"config" `
78+
Params ConfigExtendedParams `json:"params"`
7579
}
7680

7781
type ChangeProxyParams struct {
@@ -93,9 +97,13 @@ type ExternalProvider struct {
9397
Name string `json:"name"`
9498
Type string `json:"type"`
9599
VehicleType string `json:"vehicle-type"`
100+
Count int `json:"count"`
101+
Path string `json:"path"`
96102
UpdateAt time.Time `json:"update-at"`
97103
}
98104

105+
var b, _ = batch.New[bool](context.Background(), batch.WithConcurrencyNum[bool](50))
106+
99107
func restartExecutable(execPath string) {
100108
var err error
101109
executor.Shutdown()
@@ -145,26 +153,76 @@ func removeFile(path string) error {
145153
return nil
146154
}
147155

148-
func getRawConfigWithPath(path *string) *config.RawConfig {
149-
if path == nil {
156+
func getProfilePath(id string) string {
157+
return filepath.Join(constant.Path.HomeDir(), "profiles", id+".yaml")
158+
}
159+
160+
func getProfileProvidersPath(id string) string {
161+
return filepath.Join(constant.Path.HomeDir(), "providers", id)
162+
}
163+
164+
func getRawConfigWithId(id string) *config.RawConfig {
165+
path := getProfilePath(id)
166+
bytes, err := readFile(path)
167+
if err != nil {
168+
log.Errorln("profile is not exist")
150169
return config.DefaultRawConfig()
151-
} else {
152-
bytes, err := readFile(*path)
153-
if err != nil {
154-
log.Errorln("getProfile readFile error %v", err)
155-
return config.DefaultRawConfig()
170+
}
171+
prof, err := config.UnmarshalRawConfig(bytes)
172+
if err != nil {
173+
log.Errorln("unmarshalRawConfig error %v", err)
174+
return config.DefaultRawConfig()
175+
}
176+
for _, mapping := range prof.ProxyProvider {
177+
value, exist := mapping["path"].(string)
178+
if !exist {
179+
continue
156180
}
157-
prof, err := config.UnmarshalRawConfig(bytes)
158-
if err != nil {
159-
log.Errorln("getProfile UnmarshalRawConfig error %v", err)
160-
return config.DefaultRawConfig()
181+
mapping["path"] = filepath.Join(getProfileProvidersPath(id), value)
182+
}
183+
for _, mapping := range prof.RuleProvider {
184+
value, exist := mapping["path"].(string)
185+
if !exist {
186+
continue
187+
}
188+
mapping["path"] = filepath.Join(getProfileProvidersPath(id), value)
189+
}
190+
return prof
191+
}
192+
193+
func getExternalProvidersRaw() map[string]ExternalProvider {
194+
externalProviders := make(map[string]ExternalProvider)
195+
for n, p := range tunnel.Providers() {
196+
if p.VehicleType() != cp.Compatible {
197+
p := p.(*provider.ProxySetProvider)
198+
externalProviders[n] = ExternalProvider{
199+
Name: n,
200+
Type: p.Type().String(),
201+
VehicleType: p.VehicleType().String(),
202+
Count: p.Count(),
203+
Path: p.Vehicle().Path(),
204+
UpdateAt: p.UpdatedAt,
205+
}
206+
}
207+
}
208+
for n, p := range tunnel.RuleProviders() {
209+
if p.VehicleType() != cp.Compatible {
210+
p := p.(*rp.RuleSetProvider)
211+
externalProviders[n] = ExternalProvider{
212+
Name: n,
213+
Type: p.Type().String(),
214+
VehicleType: p.VehicleType().String(),
215+
Count: p.Count(),
216+
Path: p.Vehicle().Path(),
217+
UpdateAt: p.UpdatedAt,
218+
}
161219
}
162-
return prof
163220
}
221+
return externalProviders
164222
}
165223

166-
func decorationConfig(profilePath *string, cfg config.RawConfig) *config.RawConfig {
167-
prof := getRawConfigWithPath(profilePath)
224+
func decorationConfig(profileId string, cfg config.RawConfig) *config.RawConfig {
225+
prof := getRawConfigWithId(profileId)
168226
overwriteConfig(prof, cfg)
169227
return prof
170228
}
@@ -327,6 +385,7 @@ func overwriteConfig(targetConfig *config.RawConfig, patchConfig config.RawConfi
327385
targetConfig.LogLevel = patchConfig.LogLevel
328386
targetConfig.Port = 0
329387
targetConfig.SocksPort = 0
388+
targetConfig.KeepAliveInterval = patchConfig.KeepAliveInterval
330389
targetConfig.MixedPort = patchConfig.MixedPort
331390
targetConfig.FindProcessMode = patchConfig.FindProcessMode
332391
targetConfig.AllowLan = patchConfig.AllowLan

0 commit comments

Comments
 (0)