diff --git a/adapter/adapter.go b/adapter/adapter.go index da6171f17..dbf3db6eb 100644 --- a/adapter/adapter.go +++ b/adapter/adapter.go @@ -3,7 +3,6 @@ package adapter import ( "context" "encoding/json" - "errors" "fmt" "net" "net/http" @@ -163,6 +162,8 @@ func (p *Proxy) MarshalJSON() ([]byte, error) { // URLTest get the delay for the specified URL // implements C.Proxy func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (t uint16, err error) { + var satisfied bool + defer func() { alive := err == nil record := C.DelayHistory{Time: time.Now()} @@ -185,6 +186,11 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In p.extra.Store(url, state) } + if !satisfied { + record.Delay = 0 + alive = false + } + state.alive.Store(alive) state.history.Put(record) if state.history.Len() > defaultHistoriesNum { @@ -253,15 +259,10 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In } } - if expectedStatus != nil && !expectedStatus.Check(uint16(resp.StatusCode)) { - // maybe another value should be returned for differentiation - err = errors.New("response status is inconsistent with the expected status") - } - + satisfied = resp != nil && (expectedStatus == nil || expectedStatus.Check(uint16(resp.StatusCode))) t = uint16(time.Since(start) / time.Millisecond) return } - func NewProxy(adapter C.ProxyAdapter) *Proxy { return &Proxy{ ProxyAdapter: adapter, diff --git a/docs/config.yaml b/docs/config.yaml index 2a44a9936..eee6122be 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -788,6 +788,7 @@ proxy-groups: - vmess1 # tolerance: 150 # lazy: true + # expected-status: 204 # 当健康检查返回状态码与期望值不符时,认为节点不可用 url: "https://cp.cloudflare.com/generate_204" interval: 300 @@ -851,6 +852,7 @@ proxy-providers: interval: 600 # lazy: true url: https://cp.cloudflare.com/generate_204 + # expected-status: 204 # 当健康检查返回状态码与期望值不符时,认为节点不可用 override: # 覆写节点加载时的一些配置项 skip-cert-verify: true udp: true @@ -1068,4 +1070,4 @@ listeners: # authentication-timeout: 1000 # alpn: # - h3 -# max-udp-relay-packet-size: 1500 +# max-udp-relay-packet-size: 1500 \ No newline at end of file