Clash.Meta/rules/provider/provider.go

257 lines
5.7 KiB
Go
Raw Normal View History

package provider
2021-12-02 22:56:17 +08:00
import (
"bytes"
2021-12-02 22:56:17 +08:00
"encoding/json"
"errors"
2024-07-26 22:30:42 +08:00
"io"
2021-12-02 22:56:17 +08:00
"runtime"
2023-04-14 13:51:26 +08:00
"strings"
2021-12-02 22:56:17 +08:00
"time"
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/common/pool"
"github.com/metacubex/mihomo/component/resource"
C "github.com/metacubex/mihomo/constant"
P "github.com/metacubex/mihomo/constant/provider"
2024-07-26 22:30:42 +08:00
"gopkg.in/yaml.v3"
2021-12-02 22:56:17 +08:00
)
var tunnel P.Tunnel
func SetTunnel(t P.Tunnel) {
tunnel = t
}
2021-12-02 22:56:17 +08:00
type ruleSetProvider struct {
*resource.Fetcher[ruleStrategy]
2023-04-14 13:51:26 +08:00
behavior P.RuleBehavior
format P.RuleFormat
2022-03-26 18:34:15 +08:00
strategy ruleStrategy
2021-12-02 22:56:17 +08:00
}
type RuleSetProvider struct {
*ruleSetProvider
}
type RulePayload struct {
/**
key: Domain or IP Cidr
value: Rule type or is empty
*/
Payload []string `yaml:"payload"`
Rules []string `yaml:"rules"`
2021-12-02 22:56:17 +08:00
}
2022-03-26 18:34:15 +08:00
type ruleStrategy interface {
2024-07-27 10:36:11 +08:00
Behavior() P.RuleBehavior
2022-03-26 18:34:15 +08:00
Match(metadata *C.Metadata) bool
Count() int
ShouldResolveIP() bool
ShouldFindProcess() bool
Reset()
Insert(rule string)
FinishInsert()
2022-03-26 18:34:15 +08:00
}
2024-07-26 22:30:42 +08:00
type mrsRuleStrategy interface {
ruleStrategy
2024-07-27 10:36:11 +08:00
FromMrs(r io.Reader, count int) error
2024-07-26 22:30:42 +08:00
WriteMrs(w io.Writer) error
DumpMrs(f func(key string) bool)
2024-07-26 22:30:42 +08:00
}
2021-12-02 22:56:17 +08:00
func (rp *ruleSetProvider) Type() P.ProviderType {
return P.Rule
}
func (rp *ruleSetProvider) Initial() error {
2024-09-22 11:36:31 +08:00
_, err := rp.Fetcher.Initial()
return err
2021-12-02 22:56:17 +08:00
}
func (rp *ruleSetProvider) Update() error {
2024-09-22 11:36:31 +08:00
_, _, err := rp.Fetcher.Update()
2021-12-02 22:56:17 +08:00
return err
}
2023-04-14 13:51:26 +08:00
func (rp *ruleSetProvider) Behavior() P.RuleBehavior {
2021-12-02 22:56:17 +08:00
return rp.behavior
}
2024-08-27 11:04:42 +08:00
func (rp *ruleSetProvider) Count() int {
return rp.strategy.Count()
}
2021-12-02 22:56:17 +08:00
func (rp *ruleSetProvider) Match(metadata *C.Metadata) bool {
2022-03-26 18:34:15 +08:00
return rp.strategy != nil && rp.strategy.Match(metadata)
2021-12-02 22:56:17 +08:00
}
func (rp *ruleSetProvider) ShouldResolveIP() bool {
2022-03-26 18:34:15 +08:00
return rp.strategy.ShouldResolveIP()
2021-12-02 22:56:17 +08:00
}
func (rp *ruleSetProvider) ShouldFindProcess() bool {
return rp.strategy.ShouldFindProcess()
}
func (rp *ruleSetProvider) Strategy() any {
return rp.strategy
2021-12-02 22:56:17 +08:00
}
func (rp *ruleSetProvider) MarshalJSON() ([]byte, error) {
return json.Marshal(
map[string]interface{}{
"behavior": rp.behavior.String(),
2023-04-14 13:51:26 +08:00
"format": rp.format.String(),
2021-12-02 22:56:17 +08:00
"name": rp.Name(),
2022-03-26 18:34:15 +08:00
"ruleCount": rp.strategy.Count(),
2021-12-02 22:56:17 +08:00
"type": rp.Type().String(),
2024-08-27 11:04:42 +08:00
"updatedAt": rp.UpdatedAt(),
2021-12-02 22:56:17 +08:00
"vehicleType": rp.VehicleType().String(),
})
}
2024-08-27 11:04:42 +08:00
func (rp *RuleSetProvider) Close() error {
runtime.SetFinalizer(rp, nil)
return rp.ruleSetProvider.Close()
}
2023-04-14 13:51:26 +08:00
func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleFormat, interval time.Duration, vehicle P.Vehicle,
2022-12-04 13:37:14 +08:00
parse func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error)) P.RuleProvider {
2021-12-02 22:56:17 +08:00
rp := &ruleSetProvider{
behavior: behavior,
2023-04-14 13:51:26 +08:00
format: format,
2021-12-02 22:56:17 +08:00
}
onUpdate := func(strategy ruleStrategy) {
rp.strategy = strategy
tunnel.RuleUpdateCallback().Emit(rp)
2021-12-02 22:56:17 +08:00
}
rp.strategy = newStrategy(behavior, parse)
rp.Fetcher = resource.NewFetcher(name, interval, vehicle, func(bytes []byte) (ruleStrategy, error) {
return rulesParse(bytes, newStrategy(behavior, parse), format)
}, onUpdate)
2022-03-26 18:34:15 +08:00
2021-12-02 22:56:17 +08:00
wrapper := &RuleSetProvider{
rp,
}
2024-08-27 11:04:42 +08:00
runtime.SetFinalizer(wrapper, (*RuleSetProvider).Close)
2021-12-02 22:56:17 +08:00
return wrapper
}
2023-04-14 13:51:26 +08:00
func newStrategy(behavior P.RuleBehavior, parse func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error)) ruleStrategy {
2021-12-02 22:56:17 +08:00
switch behavior {
case P.Domain:
2022-03-26 18:34:15 +08:00
strategy := NewDomainStrategy()
return strategy
2021-12-02 22:56:17 +08:00
case P.IPCIDR:
2022-03-26 18:34:15 +08:00
strategy := NewIPCidrStrategy()
return strategy
2021-12-02 22:56:17 +08:00
case P.Classical:
strategy := NewClassicalStrategy(parse)
2022-03-26 18:34:15 +08:00
return strategy
2021-12-02 22:56:17 +08:00
default:
2022-03-26 18:34:15 +08:00
return nil
2021-12-02 22:56:17 +08:00
}
}
var ErrNoPayload = errors.New("file must have a `payload` field")
2024-07-26 22:30:42 +08:00
var ErrInvalidFormat = errors.New("invalid format")
func rulesParse(buf []byte, strategy ruleStrategy, format P.RuleFormat) (ruleStrategy, error) {
strategy.Reset()
2024-07-26 22:30:42 +08:00
if format == P.MrsRule {
2024-07-27 10:36:11 +08:00
return rulesMrsParse(buf, strategy)
2024-07-26 22:30:42 +08:00
}
schema := &RulePayload{}
firstLineBuffer := pool.GetBuffer()
defer pool.PutBuffer(firstLineBuffer)
firstLineLength := 0
s := 0 // search start index
for s < len(buf) {
// search buffer for a new line.
line := buf[s:]
if i := bytes.IndexByte(line, '\n'); i >= 0 {
i += s
line = buf[s : i+1]
s = i + 1
} else {
s = len(buf) // stop loop in next step
if firstLineLength == 0 && format == P.YamlRule { // no head or only one line body
return nil, ErrNoPayload
}
}
2023-04-14 13:51:26 +08:00
var str string
switch format {
case P.TextRule:
str = string(line)
str = strings.TrimSpace(str)
2023-04-14 19:06:25 +08:00
if len(str) == 0 {
continue
}
2023-04-14 13:51:26 +08:00
if str[0] == '#' { // comment
continue
}
if strings.HasPrefix(str, "//") { // comment in Premium core
continue
}
case P.YamlRule:
2023-04-14 19:06:25 +08:00
trimLine := bytes.TrimSpace(line)
if len(trimLine) == 0 {
continue
}
if trimLine[0] == '#' { // comment
2023-04-14 13:51:26 +08:00
continue
}
firstLineBuffer.Write(line)
if firstLineLength == 0 { // find payload head
firstLineLength = firstLineBuffer.Len()
firstLineBuffer.WriteString(" - ''") // a test line
err := yaml.Unmarshal(firstLineBuffer.Bytes(), schema)
firstLineBuffer.Truncate(firstLineLength)
if err == nil && (len(schema.Rules) > 0 || len(schema.Payload) > 0) { // found
continue
}
// not found or err!=nil
firstLineBuffer.Truncate(0)
firstLineLength = 0
continue
}
2023-04-14 13:51:26 +08:00
// parse payload body
err := yaml.Unmarshal(firstLineBuffer.Bytes(), schema)
firstLineBuffer.Truncate(firstLineLength)
2023-04-14 13:51:26 +08:00
if err != nil {
continue
}
2023-04-14 13:51:26 +08:00
if len(schema.Rules) > 0 {
str = schema.Rules[0]
}
if len(schema.Payload) > 0 {
str = schema.Payload[0]
}
2024-07-26 22:30:42 +08:00
default:
return nil, ErrInvalidFormat
}
if str == "" {
continue
}
strategy.Insert(str)
2021-12-02 22:56:17 +08:00
}
strategy.FinishInsert()
return strategy, nil
2021-12-02 22:56:17 +08:00
}