clash/adapter/provider/provider.go

429 lines
10 KiB
Go
Raw Normal View History

2019-12-08 04:17:24 +00:00
package provider
import (
2022-11-06 00:43:39 +00:00
"context"
"encoding/json"
2019-12-08 04:17:24 +00:00
"errors"
"fmt"
2022-11-04 18:24:08 +00:00
"net/http"
2020-04-26 14:38:15 +00:00
"runtime"
"strings"
2019-12-08 04:17:24 +00:00
"time"
2023-11-03 13:01:45 +00:00
"github.com/metacubex/mihomo/adapter"
"github.com/metacubex/mihomo/common/convert"
"github.com/metacubex/mihomo/common/utils"
mihomoHttp "github.com/metacubex/mihomo/component/http"
"github.com/metacubex/mihomo/component/resource"
C "github.com/metacubex/mihomo/constant"
types "github.com/metacubex/mihomo/constant/provider"
"github.com/metacubex/mihomo/log"
"github.com/metacubex/mihomo/tunnel/statistic"
"github.com/dlclark/regexp2"
"gopkg.in/yaml.v3"
2019-12-08 04:17:24 +00:00
)
const (
ReservedName = "default"
)
type ProxySchema struct {
2022-03-16 04:10:13 +00:00
Proxies []map[string]any `yaml:"proxies"`
2019-12-08 04:17:24 +00:00
}
// ProxySetProvider for auto gc
2019-12-08 04:17:24 +00:00
type ProxySetProvider struct {
2020-04-26 14:38:15 +00:00
*proxySetProvider
}
type proxySetProvider struct {
*resource.Fetcher[[]C.Proxy]
2022-11-04 18:24:08 +00:00
proxies []C.Proxy
healthCheck *HealthCheck
version uint32
subscriptionInfo *SubscriptionInfo
}
2020-04-26 14:38:15 +00:00
func (pp *proxySetProvider) MarshalJSON() ([]byte, error) {
2022-03-16 04:10:13 +00:00
return json.Marshal(map[string]any{
2022-11-04 18:24:08 +00:00
"name": pp.Name(),
"type": pp.Type().String(),
"vehicleType": pp.VehicleType().String(),
"proxies": pp.Proxies(),
"testUrl": pp.healthCheck.url,
2024-03-12 07:14:56 +00:00
"expectedStatus": pp.healthCheck.expectedStatus.String(),
2022-11-04 18:24:08 +00:00
"updatedAt": pp.UpdatedAt,
"subscriptionInfo": pp.subscriptionInfo,
})
}
2022-07-20 00:53:54 +00:00
func (pp *proxySetProvider) Version() uint32 {
return pp.version
}
2020-04-26 14:38:15 +00:00
func (pp *proxySetProvider) Name() string {
return pp.Fetcher.Name()
2019-12-08 04:17:24 +00:00
}
2020-04-26 14:38:15 +00:00
func (pp *proxySetProvider) HealthCheck() {
pp.healthCheck.check()
}
2020-04-26 14:38:15 +00:00
func (pp *proxySetProvider) Update() error {
elm, same, err := pp.Fetcher.Update()
2020-04-26 14:38:15 +00:00
if err == nil && !same {
pp.OnUpdate(elm)
2019-12-08 04:17:24 +00:00
}
2020-04-26 14:38:15 +00:00
return err
2019-12-08 04:17:24 +00:00
}
2020-04-26 14:38:15 +00:00
func (pp *proxySetProvider) Initial() error {
elm, err := pp.Fetcher.Initial()
2019-12-08 04:17:24 +00:00
if err != nil {
return err
}
pp.OnUpdate(elm)
pp.getSubscriptionInfo()
pp.closeAllConnections()
2019-12-08 04:17:24 +00:00
return nil
}
func (pp *proxySetProvider) Type() types.ProviderType {
return types.Proxy
2019-12-08 04:17:24 +00:00
}
2020-04-26 14:38:15 +00:00
func (pp *proxySetProvider) Proxies() []C.Proxy {
2019-12-08 04:17:24 +00:00
return pp.proxies
}
2022-06-07 09:19:25 +00:00
func (pp *proxySetProvider) Touch() {
pp.healthCheck.touch()
}
func (pp *proxySetProvider) HealthCheckURL() string {
return pp.healthCheck.url
}
func (pp *proxySetProvider) RegisterHealthCheckTask(url string, expectedStatus utils.IntRanges[uint16], filter string, interval uint) {
pp.healthCheck.registerHealthCheckTask(url, expectedStatus, filter, interval)
}
2020-04-26 14:38:15 +00:00
func (pp *proxySetProvider) setProxies(proxies []C.Proxy) {
2019-12-08 04:17:24 +00:00
pp.proxies = proxies
pp.healthCheck.setProxy(proxies)
if pp.healthCheck.auto() {
2023-03-14 12:10:52 +00:00
go pp.healthCheck.check()
}
2019-12-08 04:17:24 +00:00
}
2022-11-04 18:24:08 +00:00
func (pp *proxySetProvider) getSubscriptionInfo() {
if pp.VehicleType() != types.HTTP {
return
}
go func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
defer cancel()
2023-11-03 13:01:45 +00:00
resp, err := mihomoHttp.HttpRequest(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(),
2023-11-11 18:44:55 +00:00
http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil)
2022-11-04 18:24:08 +00:00
if err != nil {
return
}
defer resp.Body.Close()
userInfoStr := strings.TrimSpace(resp.Header.Get("subscription-userinfo"))
if userInfoStr == "" {
2023-11-03 13:01:45 +00:00
resp2, err := mihomoHttp.HttpRequest(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(),
2022-11-04 18:24:08 +00: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)
}
}()
}
func (pp *proxySetProvider) closeAllConnections() {
statistic.DefaultManager.Range(func(c statistic.Tracker) bool {
for _, chain := range c.Chains() {
if chain == pp.Name() {
_ = c.Close()
break
}
}
return true
})
}
2020-04-26 14:38:15 +00:00
func stopProxyProvider(pd *ProxySetProvider) {
pd.healthCheck.close()
_ = pd.Fetcher.Destroy()
2020-04-26 14:38:15 +00:00
}
2019-12-08 04:17:24 +00:00
func NewProxySetProvider(name string, interval time.Duration, filter string, excludeFilter string, excludeType string, dialerProxy string, override OverrideSchema, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
excludeFilterReg, err := regexp2.Compile(excludeFilter, 0)
if err != nil {
return nil, fmt.Errorf("invalid excludeFilter regex: %w", err)
}
var excludeTypeArray []string
if excludeType != "" {
excludeTypeArray = strings.Split(excludeType, "|")
}
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 09:44:03 +00:00
}
if hc.auto() {
go hc.process()
}
2020-04-26 14:38:15 +00:00
pd := &proxySetProvider{
proxies: []C.Proxy{},
healthCheck: hc,
2019-12-08 04:17:24 +00:00
}
2020-04-26 14:38:15 +00:00
fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, excludeFilter, excludeTypeArray, filterRegs, excludeFilterReg, dialerProxy, override), proxiesOnUpdate(pd))
pd.Fetcher = fetcher
2020-04-26 14:38:15 +00:00
wrapper := &ProxySetProvider{pd}
runtime.SetFinalizer(wrapper, stopProxyProvider)
2021-11-21 09:44:03 +00:00
return wrapper, nil
2019-12-08 04:17:24 +00:00
}
// CompatibleProvider for auto gc
2020-01-11 13:02:55 +00:00
type CompatibleProvider struct {
2020-04-26 14:38:15 +00:00
*compatibleProvider
}
type compatibleProvider struct {
2019-12-08 04:17:24 +00:00
name string
healthCheck *HealthCheck
2019-12-08 04:17:24 +00:00
proxies []C.Proxy
2022-07-20 00:53:54 +00:00
version uint32
2019-12-08 04:17:24 +00:00
}
2020-04-26 14:38:15 +00:00
func (cp *compatibleProvider) MarshalJSON() ([]byte, error) {
2022-03-16 04:10:13 +00:00
return json.Marshal(map[string]any{
"name": cp.Name(),
"type": cp.Type().String(),
"vehicleType": cp.VehicleType().String(),
"proxies": cp.Proxies(),
"testUrl": cp.healthCheck.url,
2024-03-12 07:14:56 +00:00
"expectedStatus": cp.healthCheck.expectedStatus.String(),
})
}
2022-07-20 00:53:54 +00:00
func (cp *compatibleProvider) Version() uint32 {
return cp.version
}
2020-04-26 14:38:15 +00:00
func (cp *compatibleProvider) Name() string {
2019-12-08 04:17:24 +00:00
return cp.name
}
2020-04-26 14:38:15 +00:00
func (cp *compatibleProvider) HealthCheck() {
cp.healthCheck.check()
}
2020-04-26 14:38:15 +00:00
func (cp *compatibleProvider) Update() error {
2019-12-12 16:29:24 +00:00
return nil
}
2020-04-26 14:38:15 +00:00
func (cp *compatibleProvider) Initial() error {
if cp.healthCheck.interval != 0 && cp.healthCheck.url != "" {
cp.HealthCheck()
}
2019-12-08 04:17:24 +00:00
return nil
}
func (cp *compatibleProvider) VehicleType() types.VehicleType {
return types.Compatible
2019-12-08 04:17:24 +00:00
}
func (cp *compatibleProvider) Type() types.ProviderType {
return types.Proxy
2019-12-08 04:17:24 +00:00
}
2020-04-26 14:38:15 +00:00
func (cp *compatibleProvider) Proxies() []C.Proxy {
2019-12-08 04:17:24 +00:00
return cp.proxies
}
2022-06-07 09:19:25 +00:00
func (cp *compatibleProvider) Touch() {
cp.healthCheck.touch()
}
func (cp *compatibleProvider) HealthCheckURL() string {
return cp.healthCheck.url
}
func (cp *compatibleProvider) RegisterHealthCheckTask(url string, expectedStatus utils.IntRanges[uint16], filter string, interval uint) {
cp.healthCheck.registerHealthCheckTask(url, expectedStatus, filter, interval)
}
2020-04-26 14:38:15 +00:00
func stopCompatibleProvider(pd *CompatibleProvider) {
pd.healthCheck.close()
}
2020-01-11 13:02:55 +00:00
func NewCompatibleProvider(name string, proxies []C.Proxy, hc *HealthCheck) (*CompatibleProvider, error) {
2019-12-08 04:17:24 +00:00
if len(proxies) == 0 {
2021-07-05 16:33:13 +00:00
return nil, errors.New("provider need one proxy at least")
2019-12-08 04:17:24 +00:00
}
if hc.auto() {
go hc.process()
}
2020-04-26 14:38:15 +00:00
pd := &compatibleProvider{
2019-12-08 04:17:24 +00:00
name: name,
proxies: proxies,
healthCheck: hc,
2020-04-26 14:38:15 +00:00
}
wrapper := &CompatibleProvider{pd}
runtime.SetFinalizer(wrapper, stopCompatibleProvider)
return wrapper, nil
2019-12-08 04:17:24 +00:00
}
func proxiesOnUpdate(pd *proxySetProvider) func([]C.Proxy) {
return func(elm []C.Proxy) {
pd.setProxies(elm)
2022-07-20 00:53:54 +00:00
pd.version += 1
2022-11-04 18:24:08 +00:00
pd.getSubscriptionInfo()
}
}
func proxiesParseAndFilter(filter string, excludeFilter string, excludeTypeArray []string, filterRegs []*regexp2.Regexp, excludeFilterReg *regexp2.Regexp, dialerProxy string, override OverrideSchema) resource.Parser[[]C.Proxy] {
return func(buf []byte) ([]C.Proxy, error) {
schema := &ProxySchema{}
if err := yaml.Unmarshal(buf, schema); err != nil {
proxies, err1 := convert.ConvertsV2Ray(buf)
if err1 != nil {
return nil, fmt.Errorf("%w, %w", err, err1)
}
schema.Proxies = proxies
}
if schema.Proxies == nil {
return nil, errors.New("file must have a `proxies` field")
}
proxies := []C.Proxy{}
proxiesSet := map[string]struct{}{}
for _, filterReg := range filterRegs {
for idx, mapping := range schema.Proxies {
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
break
}
}
if flag {
continue
}
}
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
}
if len(dialerProxy) > 0 {
mapping["dialer-proxy"] = dialerProxy
}
if override.UDP != nil {
mapping["udp"] = *override.UDP
}
if override.Up != nil {
mapping["up"] = *override.Up
}
if override.Down != nil {
mapping["down"] = *override.Down
}
if override.DialerProxy != nil {
mapping["dialer-proxy"] = *override.DialerProxy
}
if override.SkipCertVerify != nil {
mapping["skip-cert-verify"] = *override.SkipCertVerify
}
2023-12-25 17:45:32 +00:00
if override.Interface != nil {
mapping["interface-name"] = *override.Interface
}
if override.RoutingMark != nil {
mapping["routing-mark"] = *override.RoutingMark
}
if override.IPVersion != nil {
mapping["ip-version"] = *override.IPVersion
}
2024-02-20 07:15:22 +00:00
if override.AdditionalPrefix != nil {
name := mapping["name"].(string)
mapping["name"] = *override.AdditionalPrefix + name
}
if override.AdditionalSuffix != nil {
name := mapping["name"].(string)
mapping["name"] = name + *override.AdditionalSuffix
}
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)
}
}
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
}
}