Skip to content

Commit ae45564

Browse files
committed
chore: Support Duration format
1 parent c44949b commit ae45564

File tree

5 files changed

+79
-25
lines changed

5 files changed

+79
-25
lines changed

adapter/outboundgroup/groupbase.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
"time"
99

1010
"github.com/metacubex/mihomo/adapter/outbound"
11+
AP "github.com/metacubex/mihomo/adapter/provider"
1112
"github.com/metacubex/mihomo/common/atomic"
1213
"github.com/metacubex/mihomo/common/utils"
1314
C "github.com/metacubex/mihomo/constant"
1415
"github.com/metacubex/mihomo/constant/provider"
15-
types "github.com/metacubex/mihomo/constant/provider"
1616
"github.com/metacubex/mihomo/log"
1717
"github.com/metacubex/mihomo/tunnel"
1818

@@ -31,7 +31,7 @@ type GroupBase struct {
3131
failedTesting atomic.Bool
3232
proxies [][]C.Proxy
3333
versions []atomic.Uint32
34-
TestTimeout int
34+
TestTimeout string
3535
maxFailedTimes int
3636
}
3737

@@ -40,7 +40,7 @@ type GroupBaseOption struct {
4040
filter string
4141
excludeFilter string
4242
excludeType string
43-
TestTimeout int
43+
TestTimeout string
4444
maxFailedTimes int
4545
providers []provider.ProxyProvider
4646
}
@@ -74,8 +74,8 @@ func NewGroupBase(opt GroupBaseOption) *GroupBase {
7474
maxFailedTimes: opt.maxFailedTimes,
7575
}
7676

77-
if gb.TestTimeout == 0 {
78-
gb.TestTimeout = 5000
77+
if gb.TestTimeout == "" {
78+
gb.TestTimeout = "5000"
7979
}
8080
if gb.maxFailedTimes == 0 {
8181
gb.maxFailedTimes = 5
@@ -108,7 +108,7 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
108108
pd.Touch()
109109
}
110110

111-
if pd.VehicleType() == types.Compatible {
111+
if pd.VehicleType() == provider.Compatible {
112112
gb.versions[i].Store(pd.Version())
113113
gb.proxies[i] = pd.Proxies()
114114
continue
@@ -244,6 +244,11 @@ func (gb *GroupBase) onDialFailed(adapterType C.AdapterType, err error) {
244244
return
245245
}
246246

247+
var timeout time.Duration
248+
if gb.TestTimeout != "" {
249+
timeout = AP.ParsedDuration(gb.TestTimeout, "ms")
250+
}
251+
247252
go func() {
248253
gb.failedTestMux.Lock()
249254
defer gb.failedTestMux.Unlock()
@@ -253,7 +258,7 @@ func (gb *GroupBase) onDialFailed(adapterType C.AdapterType, err error) {
253258
log.Debugln("ProxyGroup: %s first failed", gb.Name())
254259
gb.failedTime = time.Now()
255260
} else {
256-
if time.Since(gb.failedTime) > time.Duration(gb.TestTimeout)*time.Millisecond {
261+
if time.Since(gb.failedTime) > timeout {
257262
gb.failedTimes = 0
258263
return
259264
}

adapter/outboundgroup/parser.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ type GroupCommonOption struct {
2727
Proxies []string `group:"proxies,omitempty"`
2828
Use []string `group:"use,omitempty"`
2929
URL string `group:"url,omitempty"`
30-
Interval int `group:"interval,omitempty"`
31-
TestTimeout int `group:"timeout,omitempty"`
30+
Interval string `group:"interval,omitempty"`
31+
TestTimeout string `group:"timeout,omitempty"`
3232
MaxFailedTimes int `group:"max-failed-times,omitempty"`
3333
Lazy bool `group:"lazy,omitempty"`
3434
DisableUDP bool `group:"disable-udp,omitempty"`
@@ -88,6 +88,17 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
8888
}
8989
groupOption.ExpectedStatus = status
9090

91+
var (
92+
interval uint
93+
timeout uint
94+
)
95+
if groupOption.Interval != "" {
96+
interval = uint(provider.ParsedDuration(groupOption.Interval, "s").Seconds())
97+
}
98+
if groupOption.TestTimeout != "" {
99+
timeout = uint(provider.ParsedDuration(groupOption.TestTimeout, "ms").Milliseconds())
100+
}
101+
91102
if len(groupOption.Use) != 0 {
92103
PDs, err := getProviders(providersMap, groupOption.Use)
93104
if err != nil {
@@ -106,7 +117,7 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
106117
groupOption.URL = C.DefaultTestURL
107118
}
108119
} else {
109-
addTestUrlToProviders(PDs, groupOption.URL, expectedStatus, groupOption.Filter, uint(groupOption.Interval))
120+
addTestUrlToProviders(PDs, groupOption.URL, expectedStatus, groupOption.Filter, interval)
110121
}
111122
providers = append(providers, PDs...)
112123
}
@@ -127,12 +138,12 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
127138

128139
// select don't need auto health check
129140
if groupOption.Type != "select" && groupOption.Type != "relay" {
130-
if groupOption.Interval == 0 {
131-
groupOption.Interval = 300
141+
if interval == 0 {
142+
interval = 300
132143
}
133144
}
134145

135-
hc := provider.NewHealthCheck(ps, groupOption.URL, uint(groupOption.TestTimeout), uint(groupOption.Interval), groupOption.Lazy, expectedStatus)
146+
hc := provider.NewHealthCheck(ps, groupOption.URL, timeout, interval, groupOption.Lazy, expectedStatus)
136147

137148
pd, err := provider.NewCompatibleProvider(groupName, ps, hc)
138149
if err != nil {

adapter/outboundgroup/relay.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func NewRelay(option *GroupCommonOption, providers []provider.ProxyProvider) *Re
160160
"",
161161
"",
162162
"",
163-
5000,
163+
"5000",
164164
5,
165165
providers,
166166
}),

adapter/provider/parser.go

+41-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provider
33
import (
44
"errors"
55
"fmt"
6+
"strconv"
67
"time"
78

89
"github.com/metacubex/mihomo/common/structure"
@@ -21,8 +22,8 @@ var (
2122
type healthCheckSchema struct {
2223
Enable bool `provider:"enable"`
2324
URL string `provider:"url"`
24-
Interval int `provider:"interval"`
25-
TestTimeout int `provider:"timeout,omitempty"`
25+
Interval string `provider:"interval"`
26+
TestTimeout string `provider:"timeout,omitempty"`
2627
Lazy bool `provider:"lazy,omitempty"`
2728
ExpectedStatus string `provider:"expected-status,omitempty"`
2829
}
@@ -45,7 +46,7 @@ type proxyProviderSchema struct {
4546
Path string `provider:"path,omitempty"`
4647
URL string `provider:"url,omitempty"`
4748
Proxy string `provider:"proxy,omitempty"`
48-
Interval int `provider:"interval,omitempty"`
49+
Interval string `provider:"interval,omitempty"`
4950
Filter string `provider:"filter,omitempty"`
5051
ExcludeFilter string `provider:"exclude-filter,omitempty"`
5152
ExcludeType string `provider:"exclude-type,omitempty"`
@@ -73,14 +74,27 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
7374
return nil, err
7475
}
7576

76-
var hcInterval uint
77+
var (
78+
interval time.Duration
79+
hcInterval uint
80+
timeout uint
81+
)
82+
if schema.Interval != "" {
83+
interval = ParsedDuration(schema.Interval, "s")
84+
}
7785
if schema.HealthCheck.Enable {
78-
if schema.HealthCheck.Interval == 0 {
79-
schema.HealthCheck.Interval = 300
86+
if schema.HealthCheck.Interval != "" {
87+
hcInterval = uint(ParsedDuration(schema.HealthCheck.Interval, "s").Seconds())
88+
}
89+
if hcInterval == 0 {
90+
hcInterval = 300
8091
}
81-
hcInterval = uint(schema.HealthCheck.Interval)
8292
}
83-
hc := NewHealthCheck([]C.Proxy{}, schema.HealthCheck.URL, uint(schema.HealthCheck.TestTimeout), hcInterval, schema.HealthCheck.Lazy, expectedStatus)
93+
if schema.HealthCheck.TestTimeout != "" {
94+
timeout = uint(ParsedDuration(schema.HealthCheck.TestTimeout, "ms").Milliseconds())
95+
}
96+
97+
hc := NewHealthCheck([]C.Proxy{}, schema.HealthCheck.URL, timeout, hcInterval, schema.HealthCheck.Lazy, expectedStatus)
8498

8599
var vehicle types.Vehicle
86100
switch schema.Type {
@@ -100,7 +114,6 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
100114
return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type)
101115
}
102116

103-
interval := time.Duration(uint(schema.Interval)) * time.Second
104117
filter := schema.Filter
105118
excludeFilter := schema.ExcludeFilter
106119
excludeType := schema.ExcludeType
@@ -109,3 +122,22 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
109122

110123
return NewProxySetProvider(name, interval, filter, excludeFilter, excludeType, dialerProxy, override, vehicle, hc)
111124
}
125+
126+
func ParsedDuration(interval string, unit string) time.Duration {
127+
var Duration time.Duration
128+
switch unit {
129+
case "ms":
130+
_, err := strconv.Atoi(interval)
131+
if err == nil {
132+
interval += "ms"
133+
}
134+
Duration, _ = time.ParseDuration(interval)
135+
case "s":
136+
_, err := strconv.Atoi(interval)
137+
if err == nil {
138+
interval += "s"
139+
}
140+
Duration, _ = time.ParseDuration(interval)
141+
}
142+
return Duration
143+
}

rules/provider/parse.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"time"
77

8+
AP "github.com/metacubex/mihomo/adapter/provider"
89
"github.com/metacubex/mihomo/common/structure"
910
"github.com/metacubex/mihomo/component/resource"
1011
C "github.com/metacubex/mihomo/constant"
@@ -23,7 +24,7 @@ type ruleProviderSchema struct {
2324
URL string `provider:"url,omitempty"`
2425
Proxy string `provider:"proxy,omitempty"`
2526
Format string `provider:"format,omitempty"`
26-
Interval int `provider:"interval,omitempty"`
27+
Interval string `provider:"interval,omitempty"`
2728
}
2829

2930
func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error)) (P.RuleProvider, error) {
@@ -56,6 +57,11 @@ func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(t
5657
return nil, fmt.Errorf("unsupported format type: %s", schema.Format)
5758
}
5859

60+
var interval time.Duration
61+
if schema.Interval != "" {
62+
interval = AP.ParsedDuration(schema.Interval, "s")
63+
}
64+
5965
var vehicle P.Vehicle
6066
switch schema.Type {
6167
case "file":
@@ -74,5 +80,5 @@ func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(t
7480
return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type)
7581
}
7682

77-
return NewRuleSetProvider(name, behavior, format, time.Duration(uint(schema.Interval))*time.Second, vehicle, parse), nil
83+
return NewRuleSetProvider(name, behavior, format, interval, vehicle, parse), nil
7884
}

0 commit comments

Comments
 (0)