From ed17a1bf2386241ad6c26f283a4fec13e1d24051 Mon Sep 17 00:00:00 2001 From: adlyq Date: Tue, 7 Jun 2022 17:19:25 +0800 Subject: [PATCH] fix: group filter touch provider --- adapter/outbound/hysteria.go | 42 +++++++++++++++--------------- adapter/outboundgroup/groupbase.go | 22 ++++++---------- adapter/outboundgroup/parser.go | 6 +++-- adapter/provider/healthcheck.go | 4 +-- adapter/provider/provider.go | 6 ++--- constant/provider/interface.go | 4 +-- 6 files changed, 37 insertions(+), 47 deletions(-) diff --git a/adapter/outbound/hysteria.go b/adapter/outbound/hysteria.go index dd0f78c0..f782f55a 100644 --- a/adapter/outbound/hysteria.go +++ b/adapter/outbound/hysteria.go @@ -68,26 +68,26 @@ func (h *Hysteria) ListenPacketContext(ctx context.Context, metadata *C.Metadata type HysteriaOption struct { BasicOption - Name string `proxy:"name"` - Server string `proxy:"server"` - Port int `proxy:"port"` - Protocol string `proxy:"protocol,omitempty"` - Up string `proxy:"up,omitempty"` - UpMbps int `proxy:"up_mbps,omitempty"` - Down string `proxy:"down,omitempty"` - DownMbps int `proxy:"down_mbps,omitempty"` - Auth string `proxy:"auth,omitempty"` - AuthString string `proxy:"auth_str,omitempty"` - Obfs string `proxy:"obfs,omitempty"` - SNI string `proxy:"sni,omitempty"` - SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"` - ALPN []string `proxy:"alpn,omitempty"` - CustomCA string `proxy:"ca,omitempty"` - CustomCAString string `proxy:"ca_str,omitempty"` - ReceiveWindowConn uint64 `proxy:"recv_window_conn,omitempty"` - ReceiveWindow uint64 `proxy:"recv_window,omitempty"` - DisableMTUDiscovery bool `proxy:"disable_mtu_discovery,omitempty"` - UDP bool `proxy:"udp,omitempty"` + Name string `proxy:"name"` + Server string `proxy:"server"` + Port int `proxy:"port"` + Protocol string `proxy:"protocol,omitempty"` + Up string `proxy:"up,omitempty"` + UpMbps int `proxy:"up_mbps,omitempty"` + Down string `proxy:"down,omitempty"` + DownMbps int `proxy:"down_mbps,omitempty"` + Auth string `proxy:"auth,omitempty"` + AuthString string `proxy:"auth_str,omitempty"` + Obfs string `proxy:"obfs,omitempty"` + SNI string `proxy:"sni,omitempty"` + SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"` + ALPN string `proxy:"alpn,omitempty"` + CustomCA string `proxy:"ca,omitempty"` + CustomCAString string `proxy:"ca_str,omitempty"` + ReceiveWindowConn uint64 `proxy:"recv_window_conn,omitempty"` + ReceiveWindow uint64 `proxy:"recv_window,omitempty"` + DisableMTUDiscovery bool `proxy:"disable_mtu_discovery,omitempty"` + UDP bool `proxy:"udp,omitempty"` } func (c *HysteriaOption) Speed() (uint64, uint64, error) { @@ -126,7 +126,7 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) { MinVersion: tls.VersionTLS13, } if len(option.ALPN) > 0 { - tlsConfig.NextProtos = option.ALPN + tlsConfig.NextProtos = []string{option.ALPN} } else { tlsConfig.NextProtos = []string{DefaultALPN} } diff --git a/adapter/outboundgroup/groupbase.go b/adapter/outboundgroup/groupbase.go index 707e1a6a..42b61dd4 100644 --- a/adapter/outboundgroup/groupbase.go +++ b/adapter/outboundgroup/groupbase.go @@ -51,10 +51,9 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy { var proxies []C.Proxy for _, pd := range gb.providers { if touch { - proxies = append(proxies, pd.ProxiesWithTouch()...) - } else { - proxies = append(proxies, pd.Proxies()...) + pd.Touch() } + proxies = append(proxies, pd.Proxies()...) } if len(proxies) == 0 { return append(proxies, tunnel.Proxies()["COMPATIBLE"]) @@ -63,13 +62,12 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy { } for _, pd := range gb.providers { - if pd.VehicleType() == types.Compatible { - if touch { - gb.proxies.Store(pd.Name(), pd.ProxiesWithTouch()) - } else { - gb.proxies.Store(pd.Name(), pd.Proxies()) - } + if touch { + pd.Touch() + } + if pd.VehicleType() == types.Compatible { + gb.proxies.Store(pd.Name(), pd.Proxies()) gb.versions.Store(pd.Name(), pd.Version()) continue } @@ -80,11 +78,7 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy { newProxies []C.Proxy ) - if touch { - proxies = pd.ProxiesWithTouch() - } else { - proxies = pd.Proxies() - } + proxies = pd.Proxies() for _, p := range proxies { if mat, _ := gb.filter.FindStringMatch(p.Name()); mat != nil { diff --git a/adapter/outboundgroup/parser.go b/adapter/outboundgroup/parser.go index 76a1efea..b808079b 100644 --- a/adapter/outboundgroup/parser.go +++ b/adapter/outboundgroup/parser.go @@ -75,9 +75,11 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide providers = append(providers, pd) providersMap[groupName] = pd } else { - if groupOption.URL == "" || groupOption.Interval == 0 { - //return nil, errMissHealthCheck + if groupOption.URL == "" { groupOption.URL = "http://www.gstatic.com/generate_204" + } + + if groupOption.Interval == 0 { groupOption.Interval = 300 } diff --git a/adapter/provider/healthcheck.go b/adapter/provider/healthcheck.go index 430225c4..cc664c9e 100644 --- a/adapter/provider/healthcheck.go +++ b/adapter/provider/healthcheck.go @@ -32,9 +32,7 @@ func (hc *HealthCheck) process() { ticker := time.NewTicker(time.Duration(hc.interval) * time.Second) go func() { - t := time.NewTicker(30 * time.Second) - <-t.C - t.Stop() + time.Sleep(30 * time.Second) hc.check() }() diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index 32b95701..a38718a9 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -84,9 +84,8 @@ func (pp *proxySetProvider) Proxies() []C.Proxy { return pp.proxies } -func (pp *proxySetProvider) ProxiesWithTouch() []C.Proxy { +func (pp *proxySetProvider) Touch() { pp.healthCheck.touch() - return pp.Proxies() } func (pp *proxySetProvider) setProxies(proxies []C.Proxy) { @@ -178,9 +177,8 @@ func (cp *compatibleProvider) Proxies() []C.Proxy { return cp.proxies } -func (cp *compatibleProvider) ProxiesWithTouch() []C.Proxy { +func (cp *compatibleProvider) Touch() { cp.healthCheck.touch() - return cp.Proxies() } func stopCompatibleProvider(pd *CompatibleProvider) { diff --git a/constant/provider/interface.go b/constant/provider/interface.go index 646e459b..4e281925 100644 --- a/constant/provider/interface.go +++ b/constant/provider/interface.go @@ -66,9 +66,7 @@ type Provider interface { type ProxyProvider interface { Provider Proxies() []C.Proxy - // ProxiesWithTouch is used to inform the provider that the proxy is actually being used while getting the list of proxies. - // Commonly used in DialContext and DialPacketConn - ProxiesWithTouch() []C.Proxy + Touch() HealthCheck() Version() uint }