mihomo/constant/provider/interface.go

186 lines
3.0 KiB
Go
Raw Normal View History

package provider
import (
2024-08-27 11:04:42 +08:00
"context"
2024-07-26 22:30:42 +08:00
"fmt"
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/common/utils"
"github.com/metacubex/mihomo/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 {
2024-09-22 11:36:31 +08:00
Read(ctx context.Context, oldHash HashType) (buf []byte, hash HashType, err error)
Path() string
Url() string
Proxy() 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
2024-09-09 09:15:37 +08:00
Count() int
// Touch 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
2022-06-07 17:19:25 +08:00
Touch()
HealthCheck()
2022-07-20 08:53:54 +08:00
Version() uint32
RegisterHealthCheckTask(url string, expectedStatus utils.IntRanges[uint16], filter string, interval uint)
HealthCheckURL() string
}
2023-04-14 13:51:26 +08:00
// RuleProvider interface
type RuleProvider interface {
Provider
Behavior() RuleBehavior
2024-08-27 11:04:42 +08:00
Count() int
2023-04-14 13:51:26 +08:00
Match(*constant.Metadata) bool
ShouldResolveIP() bool
ShouldFindProcess() bool
Strategy() any
2023-04-14 13:51:26 +08:00
}
// Rule Behavior
const (
2023-04-14 13:51:26 +08:00
Domain RuleBehavior = iota
IPCIDR
Classical
)
2023-04-14 13:51:26 +08:00
// RuleBehavior defined
type RuleBehavior int
2023-04-14 13:51:26 +08:00
func (rt RuleBehavior) String() string {
switch rt {
case Domain:
return "Domain"
case IPCIDR:
return "IPCIDR"
case Classical:
return "Classical"
default:
return "Unknown"
}
}
2024-07-27 10:36:11 +08:00
func (rt RuleBehavior) Byte() byte {
switch rt {
case Domain:
return 0
case IPCIDR:
return 1
case Classical:
return 2
default:
return 255
}
}
2024-07-26 22:30:42 +08:00
func ParseBehavior(s string) (behavior RuleBehavior, err error) {
switch s {
case "domain":
behavior = Domain
case "ipcidr":
behavior = IPCIDR
case "classical":
behavior = Classical
default:
err = fmt.Errorf("unsupported behavior type: %s", s)
}
return
}
2023-04-14 13:51:26 +08:00
const (
YamlRule RuleFormat = iota
TextRule
2024-07-26 22:30:42 +08:00
MrsRule
2023-04-14 13:51:26 +08:00
)
type RuleFormat int
func (rf RuleFormat) String() string {
switch rf {
case YamlRule:
return "YamlRule"
case TextRule:
return "TextRule"
2024-07-26 22:30:42 +08:00
case MrsRule:
return "MrsRule"
2023-04-14 13:51:26 +08:00
default:
return "Unknown"
}
}
2024-07-26 22:30:42 +08:00
func ParseRuleFormat(s string) (format RuleFormat, err error) {
switch s {
case "", "yaml":
format = YamlRule
case "text":
format = TextRule
case "mrs":
format = MrsRule
default:
err = fmt.Errorf("unsupported format type: %s", s)
}
return
}
type Tunnel interface {
Providers() map[string]ProxyProvider
RuleProviders() map[string]RuleProvider
RuleUpdateCallback() *utils.Callback[RuleProvider]
}