2019-12-08 12:17:24 +08:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-04 11:51:30 +08:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2019-12-08 12:17:24 +08:00
|
|
|
"time"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/common/atomic"
|
|
|
|
"github.com/metacubex/mihomo/common/batch"
|
|
|
|
"github.com/metacubex/mihomo/common/singledo"
|
|
|
|
"github.com/metacubex/mihomo/common/utils"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
2023-06-04 11:51:30 +08:00
|
|
|
|
|
|
|
"github.com/dlclark/regexp2"
|
2019-12-08 12:17:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultURLTestTimeout = time.Second * 5
|
2023-11-17 01:19:20 +08:00
|
|
|
defaultURLTestURL = "https://www.gstatic.com/generate_204"
|
2019-12-08 12:17:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type HealthCheckOption struct {
|
|
|
|
URL string
|
|
|
|
Interval uint
|
|
|
|
}
|
|
|
|
|
2023-06-04 11:51:30 +08:00
|
|
|
type extraOption struct {
|
|
|
|
expectedStatus utils.IntRanges[uint16]
|
|
|
|
filters map[string]struct{}
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
type HealthCheck struct {
|
2023-06-04 14:00:24 +08:00
|
|
|
url string
|
|
|
|
extra map[string]*extraOption
|
|
|
|
mu sync.Mutex
|
2023-10-10 16:34:33 +08:00
|
|
|
started atomic.Bool
|
2023-06-04 14:00:24 +08:00
|
|
|
proxies []C.Proxy
|
2023-10-10 16:34:33 +08:00
|
|
|
interval time.Duration
|
2023-06-04 14:00:24 +08:00
|
|
|
lazy bool
|
|
|
|
expectedStatus utils.IntRanges[uint16]
|
2023-10-10 16:34:33 +08:00
|
|
|
lastTouch atomic.TypedValue[time.Time]
|
2023-06-04 14:00:24 +08:00
|
|
|
done chan struct{}
|
|
|
|
singleDo *singledo.Single[struct{}]
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
func (hc *HealthCheck) process() {
|
2023-06-04 11:51:30 +08:00
|
|
|
if hc.started.Load() {
|
|
|
|
log.Warnln("Skip start health check timer due to it's started")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-10 16:34:33 +08:00
|
|
|
ticker := time.NewTicker(hc.interval)
|
2023-06-04 11:51:30 +08:00
|
|
|
hc.start()
|
2019-12-26 18:41:06 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2023-10-10 16:34:33 +08:00
|
|
|
lastTouch := hc.lastTouch.Load()
|
|
|
|
since := time.Since(lastTouch)
|
|
|
|
if !hc.lazy || since < hc.interval {
|
2023-03-14 20:10:52 +08:00
|
|
|
hc.check()
|
|
|
|
} else {
|
|
|
|
log.Debugln("Skip once health check because we are lazy")
|
|
|
|
}
|
2019-12-26 18:41:06 +08:00
|
|
|
case <-hc.done:
|
|
|
|
ticker.Stop()
|
2023-06-04 11:51:30 +08:00
|
|
|
hc.stop()
|
2019-12-26 18:41:06 +08:00
|
|
|
return
|
|
|
|
}
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
func (hc *HealthCheck) setProxy(proxies []C.Proxy) {
|
|
|
|
hc.proxies = proxies
|
|
|
|
}
|
|
|
|
|
2023-06-04 11:51:30 +08:00
|
|
|
func (hc *HealthCheck) registerHealthCheckTask(url string, expectedStatus utils.IntRanges[uint16], filter string, interval uint) {
|
|
|
|
url = strings.TrimSpace(url)
|
|
|
|
if len(url) == 0 || url == hc.url {
|
|
|
|
log.Debugln("ignore invalid health check url: %s", url)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hc.mu.Lock()
|
|
|
|
defer hc.mu.Unlock()
|
|
|
|
|
|
|
|
// if the provider has not set up health checks, then modify it to be the same as the group's interval
|
|
|
|
if hc.interval == 0 {
|
2023-10-10 16:34:33 +08:00
|
|
|
hc.interval = time.Duration(interval) * time.Second
|
2023-06-04 11:51:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if hc.extra == nil {
|
|
|
|
hc.extra = make(map[string]*extraOption)
|
|
|
|
}
|
|
|
|
|
|
|
|
// prioritize the use of previously registered configurations, especially those from provider
|
|
|
|
if _, ok := hc.extra[url]; ok {
|
|
|
|
// provider default health check does not set filter
|
|
|
|
if url != hc.url && len(filter) != 0 {
|
|
|
|
splitAndAddFiltersToExtra(filter, hc.extra[url])
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugln("health check url: %s exists", url)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// due to the time-consuming nature of health checks, a maximum of defaultMaxTestURLNum URLs can be set for testing
|
2023-06-07 11:04:03 +08:00
|
|
|
if len(hc.extra) > C.DefaultMaxHealthCheckUrlNum {
|
|
|
|
log.Debugln("skip add url: %s to health check because it has reached the maximum limit: %d", url, C.DefaultMaxHealthCheckUrlNum)
|
2023-06-04 11:51:30 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
option := &extraOption{filters: map[string]struct{}{}, expectedStatus: expectedStatus}
|
|
|
|
splitAndAddFiltersToExtra(filter, option)
|
|
|
|
hc.extra[url] = option
|
|
|
|
|
|
|
|
if hc.auto() && !hc.started.Load() {
|
|
|
|
go hc.process()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitAndAddFiltersToExtra(filter string, option *extraOption) {
|
|
|
|
filter = strings.TrimSpace(filter)
|
|
|
|
if len(filter) != 0 {
|
|
|
|
for _, regex := range strings.Split(filter, "`") {
|
|
|
|
regex = strings.TrimSpace(regex)
|
|
|
|
if len(regex) != 0 {
|
|
|
|
option.filters[regex] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
func (hc *HealthCheck) auto() bool {
|
|
|
|
return hc.interval != 0
|
|
|
|
}
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
func (hc *HealthCheck) touch() {
|
2023-10-10 16:34:33 +08:00
|
|
|
hc.lastTouch.Store(time.Now())
|
2020-11-19 00:53:22 +08:00
|
|
|
}
|
|
|
|
|
2023-06-04 11:51:30 +08:00
|
|
|
func (hc *HealthCheck) start() {
|
|
|
|
hc.started.Store(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hc *HealthCheck) stop() {
|
|
|
|
hc.started.Store(false)
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
func (hc *HealthCheck) check() {
|
2023-11-17 01:19:20 +08:00
|
|
|
if len(hc.proxies) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-25 08:53:04 +08:00
|
|
|
_, _, _ = hc.singleDo.Do(func() (struct{}, error) {
|
2023-03-15 10:10:03 +08:00
|
|
|
id := utils.NewUUIDV4().String()
|
2022-10-31 16:04:50 +08:00
|
|
|
log.Debugln("Start New Health Checking {%s}", id)
|
2022-06-25 08:53:04 +08:00
|
|
|
b, _ := batch.New[bool](context.Background(), batch.WithConcurrencyNum[bool](10))
|
|
|
|
|
2023-06-04 11:51:30 +08:00
|
|
|
// execute default health check
|
2023-06-04 14:00:24 +08:00
|
|
|
option := &extraOption{filters: nil, expectedStatus: hc.expectedStatus}
|
|
|
|
hc.execute(b, hc.url, id, option)
|
2023-06-04 11:51:30 +08:00
|
|
|
|
|
|
|
// execute extra health check
|
|
|
|
if len(hc.extra) != 0 {
|
|
|
|
for url, option := range hc.extra {
|
|
|
|
hc.execute(b, url, id, option)
|
|
|
|
}
|
|
|
|
}
|
2022-06-25 08:53:04 +08:00
|
|
|
b.Wait()
|
2022-10-31 16:04:50 +08:00
|
|
|
log.Debugln("Finish A Health Checking {%s}", id)
|
2022-06-25 08:53:04 +08:00
|
|
|
return struct{}{}, nil
|
|
|
|
})
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2023-06-04 11:51:30 +08:00
|
|
|
func (hc *HealthCheck) execute(b *batch.Batch[bool], url, uid string, option *extraOption) {
|
|
|
|
url = strings.TrimSpace(url)
|
|
|
|
if len(url) == 0 {
|
|
|
|
log.Debugln("Health Check has been skipped due to testUrl is empty, {%s}", uid)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var filterReg *regexp2.Regexp
|
|
|
|
var store = C.OriginalHistory
|
|
|
|
var expectedStatus utils.IntRanges[uint16]
|
|
|
|
if option != nil {
|
2023-06-04 14:00:24 +08:00
|
|
|
if url != hc.url {
|
|
|
|
store = C.ExtraHistory
|
|
|
|
}
|
|
|
|
|
2023-06-04 11:51:30 +08:00
|
|
|
expectedStatus = option.expectedStatus
|
|
|
|
if len(option.filters) != 0 {
|
|
|
|
filters := make([]string, 0, len(option.filters))
|
|
|
|
for filter := range option.filters {
|
|
|
|
filters = append(filters, filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
filterReg = regexp2.MustCompile(strings.Join(filters, "|"), 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, proxy := range hc.proxies {
|
|
|
|
// skip proxies that do not require health check
|
|
|
|
if filterReg != nil {
|
|
|
|
if match, _ := filterReg.FindStringMatch(proxy.Name()); match == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p := proxy
|
|
|
|
b.Go(p.Name(), func() (bool, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultURLTestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
log.Debugln("Health Checking, proxy: %s, url: %s, id: {%s}", p.Name(), url, uid)
|
|
|
|
_, _ = p.URLTest(ctx, url, expectedStatus, store)
|
|
|
|
log.Debugln("Health Checked, proxy: %s, url: %s, alive: %t, delay: %d ms uid: {%s}", p.Name(), url, p.AliveForTestUrl(url), p.LastDelayForTestUrl(url), uid)
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
func (hc *HealthCheck) close() {
|
|
|
|
hc.done <- struct{}{}
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2023-06-04 14:00:24 +08:00
|
|
|
func NewHealthCheck(proxies []C.Proxy, url string, interval uint, lazy bool, expectedStatus utils.IntRanges[uint16]) *HealthCheck {
|
2023-06-07 11:04:03 +08:00
|
|
|
if len(url) == 0 {
|
|
|
|
interval = 0
|
|
|
|
expectedStatus = nil
|
2023-11-17 01:19:20 +08:00
|
|
|
url = defaultURLTestURL
|
2023-06-07 11:04:03 +08:00
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
return &HealthCheck{
|
2023-06-04 14:00:24 +08:00
|
|
|
proxies: proxies,
|
|
|
|
url: url,
|
|
|
|
extra: map[string]*extraOption{},
|
2023-10-10 16:34:33 +08:00
|
|
|
interval: time.Duration(interval) * time.Second,
|
2023-06-04 14:00:24 +08:00
|
|
|
lazy: lazy,
|
|
|
|
expectedStatus: expectedStatus,
|
|
|
|
done: make(chan struct{}, 1),
|
|
|
|
singleDo: singledo.NewSingle[struct{}](time.Second),
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
}
|