@@ -3,6 +3,7 @@ package provider
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "strconv"
6
7
"time"
7
8
8
9
"github.com/metacubex/mihomo/common/structure"
21
22
type healthCheckSchema struct {
22
23
Enable bool `provider:"enable"`
23
24
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"`
26
27
Lazy bool `provider:"lazy,omitempty"`
27
28
ExpectedStatus string `provider:"expected-status,omitempty"`
28
29
}
@@ -45,7 +46,7 @@ type proxyProviderSchema struct {
45
46
Path string `provider:"path,omitempty"`
46
47
URL string `provider:"url,omitempty"`
47
48
Proxy string `provider:"proxy,omitempty"`
48
- Interval int `provider:"interval,omitempty"`
49
+ Interval string `provider:"interval,omitempty"`
49
50
Filter string `provider:"filter,omitempty"`
50
51
ExcludeFilter string `provider:"exclude-filter,omitempty"`
51
52
ExcludeType string `provider:"exclude-type,omitempty"`
@@ -73,14 +74,27 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
73
74
return nil , err
74
75
}
75
76
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
+ }
77
85
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
80
91
}
81
- hcInterval = uint (schema .HealthCheck .Interval )
82
92
}
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 )
84
98
85
99
var vehicle types.Vehicle
86
100
switch schema .Type {
@@ -100,7 +114,6 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
100
114
return nil , fmt .Errorf ("%w: %s" , errVehicleType , schema .Type )
101
115
}
102
116
103
- interval := time .Duration (uint (schema .Interval )) * time .Second
104
117
filter := schema .Filter
105
118
excludeFilter := schema .ExcludeFilter
106
119
excludeType := schema .ExcludeType
@@ -109,3 +122,22 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
109
122
110
123
return NewProxySetProvider (name , interval , filter , excludeFilter , excludeType , dialerProxy , override , vehicle , hc )
111
124
}
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
+ }
0 commit comments