mihomo/constant/provider/interface.go

131 lines
2.1 KiB
Go
Raw Normal View History

package provider
2021-11-25 23:14:31 +08:00
import "C"
import (
2021-11-26 00:27:00 +08:00
"errors"
2021-11-25 23:14:31 +08:00
"github.com/Dreamacro/clash/component/trie"
"github.com/Dreamacro/clash/constant"
)
// Vehicle Type
const (
File VehicleType = iota
HTTP
Compatible
)
// VehicleType defined
type VehicleType int
func (v VehicleType) String() string {
switch v {
case File:
return "File"
case HTTP:
return "HTTP"
case Compatible:
return "Compatible"
default:
return "Unknown"
}
}
type Vehicle interface {
Read() ([]byte, error)
Path() string
Type() VehicleType
}
// Provider Type
const (
Proxy ProviderType = iota
Rule
)
// ProviderType defined
type ProviderType int
func (pt ProviderType) String() string {
switch pt {
case Proxy:
return "Proxy"
case Rule:
return "Rule"
default:
return "Unknown"
}
}
// Provider interface
type Provider interface {
Name() string
VehicleType() VehicleType
Type() ProviderType
Initial() error
Update() error
}
// ProxyProvider interface
type ProxyProvider interface {
Provider
Proxies() []constant.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() []constant.Proxy
HealthCheck()
}
// Rule Type
const (
Domain RuleType = iota
IPCIDR
Classical
)
// RuleType defined
type RuleType int
func (rt RuleType) String() string {
switch rt {
case Domain:
return "Domain"
case IPCIDR:
return "IPCIDR"
case Classical:
return "Classical"
default:
return "Unknown"
}
}
// RuleProvider interface
type RuleProvider interface {
Provider
Behavior() RuleType
Match(*constant.Metadata) bool
ShouldResolveIP() bool
AsRule(adaptor string) constant.Rule
}
2021-11-25 23:14:31 +08:00
var (
2021-11-26 00:27:00 +08:00
parse = func(ruleType, rule string, params []string) (constant.Rule, error) {
return nil, errors.New("unimplemented function")
}
2021-11-25 23:14:31 +08:00
ruleProviders = map[string]*RuleProvider{}
)
func RuleProviders() map[string]*RuleProvider {
return ruleProviders
}
type ruleSetProvider struct {
count int
DomainRules *trie.DomainTrie
IPCIDRRules *trie.IpCidrTrie
2021-11-26 00:27:00 +08:00
ClassicalRules []constant.Rule
2021-11-25 23:14:31 +08:00
}
type RuleSetProvider struct {
*ruleSetProvider
}