2019-12-08 12:17:24 +08:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
2022-11-06 08:43:39 +08:00
|
|
|
"context"
|
2019-12-11 17:31:15 +08:00
|
|
|
"encoding/json"
|
2019-12-08 12:17:24 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-11-05 02:24:08 +08:00
|
|
|
"net/http"
|
2020-04-26 22:38:15 +08:00
|
|
|
"runtime"
|
2022-10-30 21:04:33 +08:00
|
|
|
"strings"
|
2019-12-08 12:17:24 +08:00
|
|
|
"time"
|
|
|
|
|
2021-06-10 14:05:56 +08:00
|
|
|
"github.com/Dreamacro/clash/adapter"
|
2022-11-12 20:43:48 +08:00
|
|
|
"github.com/Dreamacro/clash/common/convert"
|
2023-06-04 11:51:30 +08:00
|
|
|
"github.com/Dreamacro/clash/common/utils"
|
2022-11-12 20:43:48 +08:00
|
|
|
clashHttp "github.com/Dreamacro/clash/component/http"
|
|
|
|
"github.com/Dreamacro/clash/component/resource"
|
2019-12-08 12:17:24 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-07-04 20:32:59 +08:00
|
|
|
types "github.com/Dreamacro/clash/constant/provider"
|
2022-11-12 20:43:48 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2023-04-12 18:50:51 +08:00
|
|
|
"github.com/Dreamacro/clash/tunnel/statistic"
|
2023-01-07 12:24:28 +08:00
|
|
|
|
|
|
|
"github.com/dlclark/regexp2"
|
|
|
|
"gopkg.in/yaml.v3"
|
2019-12-08 12:17:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ReservedName = "default"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProxySchema struct {
|
2022-03-16 12:10:13 +08:00
|
|
|
Proxies []map[string]any `yaml:"proxies"`
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2022-06-03 04:47:58 +08:00
|
|
|
// ProxySetProvider for auto gc
|
2019-12-08 12:17:24 +08:00
|
|
|
type ProxySetProvider struct {
|
2020-04-26 22:38:15 +08:00
|
|
|
*proxySetProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
type proxySetProvider struct {
|
2022-07-11 21:30:34 +08:00
|
|
|
*resource.Fetcher[[]C.Proxy]
|
2022-11-05 02:24:08 +08:00
|
|
|
proxies []C.Proxy
|
|
|
|
healthCheck *HealthCheck
|
|
|
|
version uint32
|
|
|
|
subscriptionInfo *SubscriptionInfo
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func (pp *proxySetProvider) MarshalJSON() ([]byte, error) {
|
2022-03-16 12:10:13 +08:00
|
|
|
return json.Marshal(map[string]any{
|
2022-11-05 02:24:08 +08:00
|
|
|
"name": pp.Name(),
|
|
|
|
"type": pp.Type().String(),
|
|
|
|
"vehicleType": pp.VehicleType().String(),
|
|
|
|
"proxies": pp.Proxies(),
|
2023-06-04 11:51:30 +08:00
|
|
|
"testUrl": pp.healthCheck.url,
|
2022-11-05 02:24:08 +08:00
|
|
|
"updatedAt": pp.UpdatedAt,
|
|
|
|
"subscriptionInfo": pp.subscriptionInfo,
|
2019-12-11 17:31:15 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:53:54 +08:00
|
|
|
func (pp *proxySetProvider) Version() uint32 {
|
2022-06-05 16:54:56 +08:00
|
|
|
return pp.version
|
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func (pp *proxySetProvider) Name() string {
|
2022-07-11 21:30:34 +08:00
|
|
|
return pp.Fetcher.Name()
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func (pp *proxySetProvider) HealthCheck() {
|
2019-12-26 18:41:06 +08:00
|
|
|
pp.healthCheck.check()
|
2019-12-11 17:31:15 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func (pp *proxySetProvider) Update() error {
|
2022-07-11 21:30:34 +08:00
|
|
|
elm, same, err := pp.Fetcher.Update()
|
2020-04-26 22:38:15 +08:00
|
|
|
if err == nil && !same {
|
2022-07-11 21:30:34 +08:00
|
|
|
pp.OnUpdate(elm)
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
2020-04-26 22:38:15 +08:00
|
|
|
return err
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func (pp *proxySetProvider) Initial() error {
|
2022-07-11 21:30:34 +08:00
|
|
|
elm, err := pp.Fetcher.Initial()
|
2019-12-08 12:17:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-11 21:30:34 +08:00
|
|
|
pp.OnUpdate(elm)
|
2023-03-15 09:05:16 +08:00
|
|
|
pp.getSubscriptionInfo()
|
2023-04-12 18:50:51 +08:00
|
|
|
pp.closeAllConnections()
|
2019-12-08 12:17:24 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-04 20:32:59 +08:00
|
|
|
func (pp *proxySetProvider) Type() types.ProviderType {
|
|
|
|
return types.Proxy
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func (pp *proxySetProvider) Proxies() []C.Proxy {
|
2019-12-08 12:17:24 +08:00
|
|
|
return pp.proxies
|
|
|
|
}
|
|
|
|
|
2022-06-07 17:19:25 +08:00
|
|
|
func (pp *proxySetProvider) Touch() {
|
2020-11-19 00:53:22 +08:00
|
|
|
pp.healthCheck.touch()
|
|
|
|
}
|
|
|
|
|
2023-06-04 11:51:30 +08:00
|
|
|
func (pp *proxySetProvider) RegisterHealthCheckTask(url string, expectedStatus utils.IntRanges[uint16], filter string, interval uint) {
|
|
|
|
pp.healthCheck.registerHealthCheckTask(url, expectedStatus, filter, interval)
|
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func (pp *proxySetProvider) setProxies(proxies []C.Proxy) {
|
2019-12-08 12:17:24 +08:00
|
|
|
pp.proxies = proxies
|
2019-12-26 18:41:06 +08:00
|
|
|
pp.healthCheck.setProxy(proxies)
|
2020-06-14 00:32:04 +08:00
|
|
|
if pp.healthCheck.auto() {
|
2023-03-14 20:10:52 +08:00
|
|
|
go pp.healthCheck.check()
|
2020-06-14 00:32:04 +08:00
|
|
|
}
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2022-11-05 02:24:08 +08:00
|
|
|
func (pp *proxySetProvider) getSubscriptionInfo() {
|
|
|
|
if pp.VehicleType() != types.HTTP {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
|
|
|
|
defer cancel()
|
2022-11-09 08:06:37 +08:00
|
|
|
resp, err := clashHttp.HttpRequest(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(),
|
2022-11-05 02:24:08 +08:00
|
|
|
http.MethodGet, http.Header{"User-Agent": {"clash"}}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
userInfoStr := strings.TrimSpace(resp.Header.Get("subscription-userinfo"))
|
|
|
|
if userInfoStr == "" {
|
2022-11-09 08:06:37 +08:00
|
|
|
resp2, err := clashHttp.HttpRequest(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(),
|
2022-11-05 02:24:08 +08:00
|
|
|
http.MethodGet, http.Header{"User-Agent": {"Quantumultx"}}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp2.Body.Close()
|
|
|
|
userInfoStr = strings.TrimSpace(resp2.Header.Get("subscription-userinfo"))
|
|
|
|
if userInfoStr == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pp.subscriptionInfo, err = NewSubscriptionInfo(userInfoStr)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln("[Provider] get subscription-userinfo: %e", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-04-12 18:50:51 +08:00
|
|
|
func (pp *proxySetProvider) closeAllConnections() {
|
2023-06-26 18:25:36 +08:00
|
|
|
statistic.DefaultManager.Range(func(c statistic.Tracker) bool {
|
2023-04-12 18:50:51 +08:00
|
|
|
for _, chain := range c.Chains() {
|
|
|
|
if chain == pp.Name() {
|
|
|
|
_ = c.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-06-26 17:46:14 +08:00
|
|
|
return true
|
|
|
|
})
|
2023-04-12 18:50:51 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func stopProxyProvider(pd *ProxySetProvider) {
|
|
|
|
pd.healthCheck.close()
|
2022-07-11 21:30:34 +08:00
|
|
|
_ = pd.Fetcher.Destroy()
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
2019-12-08 12:17:24 +08:00
|
|
|
|
2023-04-12 10:39:24 +08:00
|
|
|
func NewProxySetProvider(name string, interval time.Duration, filter string, excludeFilter string, excludeType string, dialerProxy string, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
|
2022-10-30 21:04:33 +08:00
|
|
|
excludeFilterReg, err := regexp2.Compile(excludeFilter, 0)
|
2021-12-26 20:47:12 +08:00
|
|
|
if err != nil {
|
2022-10-30 21:04:33 +08:00
|
|
|
return nil, fmt.Errorf("invalid excludeFilter regex: %w", err)
|
|
|
|
}
|
2023-01-07 12:24:28 +08:00
|
|
|
var excludeTypeArray []string
|
|
|
|
if excludeType != "" {
|
|
|
|
excludeTypeArray = strings.Split(excludeType, "|")
|
|
|
|
}
|
2023-01-03 21:27:07 +08:00
|
|
|
|
2022-10-30 21:04:33 +08:00
|
|
|
var filterRegs []*regexp2.Regexp
|
|
|
|
for _, filter := range strings.Split(filter, "`") {
|
|
|
|
filterReg, err := regexp2.Compile(filter, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid filter regex: %w", err)
|
|
|
|
}
|
|
|
|
filterRegs = append(filterRegs, filterReg)
|
2021-11-21 17:44:03 +08:00
|
|
|
}
|
|
|
|
|
2022-06-03 04:47:58 +08:00
|
|
|
if hc.auto() {
|
|
|
|
go hc.process()
|
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
pd := &proxySetProvider{
|
2019-12-26 18:41:06 +08:00
|
|
|
proxies: []C.Proxy{},
|
|
|
|
healthCheck: hc,
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
2020-04-26 22:38:15 +08:00
|
|
|
|
2023-04-12 10:39:24 +08:00
|
|
|
fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, excludeFilter, excludeTypeArray, filterRegs, excludeFilterReg, dialerProxy), proxiesOnUpdate(pd))
|
2022-07-11 21:30:34 +08:00
|
|
|
pd.Fetcher = fetcher
|
2020-04-26 22:38:15 +08:00
|
|
|
wrapper := &ProxySetProvider{pd}
|
|
|
|
runtime.SetFinalizer(wrapper, stopProxyProvider)
|
2021-11-21 17:44:03 +08:00
|
|
|
return wrapper, nil
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2022-06-03 04:47:58 +08:00
|
|
|
// CompatibleProvider for auto gc
|
2020-01-11 21:02:55 +08:00
|
|
|
type CompatibleProvider struct {
|
2020-04-26 22:38:15 +08:00
|
|
|
*compatibleProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
type compatibleProvider struct {
|
2019-12-08 12:17:24 +08:00
|
|
|
name string
|
2019-12-26 18:41:06 +08:00
|
|
|
healthCheck *HealthCheck
|
2019-12-08 12:17:24 +08:00
|
|
|
proxies []C.Proxy
|
2022-07-20 08:53:54 +08:00
|
|
|
version uint32
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func (cp *compatibleProvider) MarshalJSON() ([]byte, error) {
|
2022-03-16 12:10:13 +08:00
|
|
|
return json.Marshal(map[string]any{
|
2019-12-11 17:31:15 +08:00
|
|
|
"name": cp.Name(),
|
|
|
|
"type": cp.Type().String(),
|
|
|
|
"vehicleType": cp.VehicleType().String(),
|
|
|
|
"proxies": cp.Proxies(),
|
2023-06-04 11:51:30 +08:00
|
|
|
"testUrl": cp.healthCheck.url,
|
2019-12-11 17:31:15 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:53:54 +08:00
|
|
|
func (cp *compatibleProvider) Version() uint32 {
|
2022-06-05 16:54:56 +08:00
|
|
|
return cp.version
|
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func (cp *compatibleProvider) Name() string {
|
2019-12-08 12:17:24 +08:00
|
|
|
return cp.name
|
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func (cp *compatibleProvider) HealthCheck() {
|
2019-12-26 18:41:06 +08:00
|
|
|
cp.healthCheck.check()
|
2019-12-11 17:31:15 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func (cp *compatibleProvider) Update() error {
|
2019-12-13 00:29:24 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func (cp *compatibleProvider) Initial() error {
|
2019-12-08 12:17:24 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-04 20:32:59 +08:00
|
|
|
func (cp *compatibleProvider) VehicleType() types.VehicleType {
|
|
|
|
return types.Compatible
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2021-07-04 20:32:59 +08:00
|
|
|
func (cp *compatibleProvider) Type() types.ProviderType {
|
|
|
|
return types.Proxy
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func (cp *compatibleProvider) Proxies() []C.Proxy {
|
2019-12-08 12:17:24 +08:00
|
|
|
return cp.proxies
|
|
|
|
}
|
|
|
|
|
2022-06-07 17:19:25 +08:00
|
|
|
func (cp *compatibleProvider) Touch() {
|
2020-11-19 00:53:22 +08:00
|
|
|
cp.healthCheck.touch()
|
|
|
|
}
|
|
|
|
|
2023-06-04 11:51:30 +08:00
|
|
|
func (cp *compatibleProvider) RegisterHealthCheckTask(url string, expectedStatus utils.IntRanges[uint16], filter string, interval uint) {
|
|
|
|
cp.healthCheck.registerHealthCheckTask(url, expectedStatus, filter, interval)
|
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
func stopCompatibleProvider(pd *CompatibleProvider) {
|
|
|
|
pd.healthCheck.close()
|
|
|
|
}
|
|
|
|
|
2020-01-11 21:02:55 +08:00
|
|
|
func NewCompatibleProvider(name string, proxies []C.Proxy, hc *HealthCheck) (*CompatibleProvider, error) {
|
2019-12-08 12:17:24 +08:00
|
|
|
if len(proxies) == 0 {
|
2021-07-06 00:33:13 +08:00
|
|
|
return nil, errors.New("provider need one proxy at least")
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2022-06-03 04:47:58 +08:00
|
|
|
if hc.auto() {
|
|
|
|
go hc.process()
|
|
|
|
}
|
|
|
|
|
2020-04-26 22:38:15 +08:00
|
|
|
pd := &compatibleProvider{
|
2019-12-08 12:17:24 +08:00
|
|
|
name: name,
|
|
|
|
proxies: proxies,
|
|
|
|
healthCheck: hc,
|
2020-04-26 22:38:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
wrapper := &CompatibleProvider{pd}
|
|
|
|
runtime.SetFinalizer(wrapper, stopCompatibleProvider)
|
|
|
|
return wrapper, nil
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
2022-06-03 04:47:58 +08:00
|
|
|
|
|
|
|
func proxiesOnUpdate(pd *proxySetProvider) func([]C.Proxy) {
|
|
|
|
return func(elm []C.Proxy) {
|
|
|
|
pd.setProxies(elm)
|
2022-07-20 08:53:54 +08:00
|
|
|
pd.version += 1
|
2022-11-05 02:24:08 +08:00
|
|
|
pd.getSubscriptionInfo()
|
2022-06-03 04:47:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 10:39:24 +08:00
|
|
|
func proxiesParseAndFilter(filter string, excludeFilter string, excludeTypeArray []string, filterRegs []*regexp2.Regexp, excludeFilterReg *regexp2.Regexp, dialerProxy string) resource.Parser[[]C.Proxy] {
|
2022-06-03 04:47:58 +08:00
|
|
|
return func(buf []byte) ([]C.Proxy, error) {
|
|
|
|
schema := &ProxySchema{}
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal(buf, schema); err != nil {
|
2022-06-05 16:54:56 +08:00
|
|
|
proxies, err1 := convert.ConvertsV2Ray(buf)
|
|
|
|
if err1 != nil {
|
2023-07-02 09:59:18 +08:00
|
|
|
return nil, fmt.Errorf("%w, %w", err, err1)
|
2022-06-05 16:54:56 +08:00
|
|
|
}
|
|
|
|
schema.Proxies = proxies
|
2022-06-03 04:47:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if schema.Proxies == nil {
|
|
|
|
return nil, errors.New("file must have a `proxies` field")
|
|
|
|
}
|
|
|
|
|
|
|
|
proxies := []C.Proxy{}
|
2022-10-30 21:04:33 +08:00
|
|
|
proxiesSet := map[string]struct{}{}
|
|
|
|
for _, filterReg := range filterRegs {
|
|
|
|
for idx, mapping := range schema.Proxies {
|
2023-01-07 12:24:28 +08:00
|
|
|
if nil != excludeTypeArray && len(excludeTypeArray) > 0 {
|
|
|
|
mType, ok := mapping["type"]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pType, ok := mType.(string)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
flag := false
|
|
|
|
for i := range excludeTypeArray {
|
|
|
|
if strings.EqualFold(pType, excludeTypeArray[i]) {
|
|
|
|
flag = true
|
2023-01-24 16:34:52 +08:00
|
|
|
break
|
2023-01-07 12:24:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if flag {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-10-30 21:04:33 +08:00
|
|
|
mName, ok := mapping["name"]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name, ok := mName.(string)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(excludeFilter) > 0 {
|
|
|
|
if mat, _ := excludeFilterReg.FindStringMatch(name); mat != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(filter) > 0 {
|
|
|
|
if mat, _ := filterReg.FindStringMatch(name); mat == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := proxiesSet[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2023-04-12 10:39:24 +08:00
|
|
|
if len(dialerProxy) > 0 {
|
|
|
|
mapping["dialer-proxy"] = dialerProxy
|
|
|
|
}
|
2022-10-30 21:04:33 +08:00
|
|
|
proxy, err := adapter.ParseProxy(mapping)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("proxy %d error: %w", idx, err)
|
|
|
|
}
|
|
|
|
proxiesSet[name] = struct{}{}
|
|
|
|
proxies = append(proxies, proxy)
|
2022-06-03 04:47:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(proxies) == 0 {
|
|
|
|
if len(filter) > 0 {
|
|
|
|
return nil, errors.New("doesn't match any proxy, please check your filter")
|
|
|
|
}
|
|
|
|
return nil, errors.New("file doesn't have any proxy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return proxies, nil
|
|
|
|
}
|
|
|
|
}
|