2022-01-22 22:10:45 +08:00
|
|
|
package provider
|
2021-12-02 22:56:17 +08:00
|
|
|
|
|
|
|
import (
|
2023-04-01 14:11:09 +08:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2021-12-02 22:56:17 +08:00
|
|
|
"encoding/json"
|
2023-04-01 14:11:09 +08:00
|
|
|
"errors"
|
2022-05-26 17:47:05 +08:00
|
|
|
"gopkg.in/yaml.v3"
|
2023-04-01 14:11:09 +08:00
|
|
|
"io"
|
2021-12-02 22:56:17 +08:00
|
|
|
"runtime"
|
|
|
|
"time"
|
2023-04-01 14:11:09 +08:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/pool"
|
|
|
|
"github.com/Dreamacro/clash/component/resource"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
P "github.com/Dreamacro/clash/constant/provider"
|
2021-12-02 22:56:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-12-07 20:49:39 +08:00
|
|
|
ruleProviders = map[string]P.RuleProvider{}
|
2021-12-02 22:56:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ruleSetProvider struct {
|
2022-07-11 21:30:34 +08:00
|
|
|
*resource.Fetcher[any]
|
2022-03-26 18:34:15 +08:00
|
|
|
behavior P.RuleType
|
|
|
|
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
|
|
|
|
*/
|
2023-04-01 14:11:09 +08:00
|
|
|
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 {
|
|
|
|
Match(metadata *C.Metadata) bool
|
|
|
|
Count() int
|
|
|
|
ShouldResolveIP() bool
|
2023-01-20 16:29:08 +08:00
|
|
|
ShouldFindProcess() bool
|
2023-04-01 14:11:09 +08:00
|
|
|
Reset()
|
|
|
|
Insert(rule string)
|
|
|
|
FinishInsert()
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-07 20:49:39 +08:00
|
|
|
func RuleProviders() map[string]P.RuleProvider {
|
2021-12-02 22:56:17 +08:00
|
|
|
return ruleProviders
|
|
|
|
}
|
|
|
|
|
2021-12-07 20:49:39 +08:00
|
|
|
func SetRuleProvider(ruleProvider P.RuleProvider) {
|
2021-12-02 22:56:17 +08:00
|
|
|
if ruleProvider != nil {
|
2021-12-07 20:49:39 +08:00
|
|
|
ruleProviders[(ruleProvider).Name()] = ruleProvider
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ruleSetProvider) Type() P.ProviderType {
|
|
|
|
return P.Rule
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ruleSetProvider) Initial() error {
|
2022-07-11 21:30:34 +08:00
|
|
|
elm, err := rp.Fetcher.Initial()
|
2021-12-02 22:56:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-11 21:30:34 +08:00
|
|
|
rp.OnUpdate(elm)
|
|
|
|
return nil
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ruleSetProvider) Update() error {
|
2022-07-11 21:30:34 +08:00
|
|
|
elm, same, err := rp.Fetcher.Update()
|
2021-12-02 22:56:17 +08:00
|
|
|
if err == nil && !same {
|
2022-07-11 21:30:34 +08:00
|
|
|
rp.OnUpdate(elm)
|
|
|
|
return nil
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ruleSetProvider) Behavior() P.RuleType {
|
|
|
|
return rp.behavior
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-20 16:29:08 +08:00
|
|
|
func (rp *ruleSetProvider) ShouldFindProcess() bool {
|
|
|
|
return rp.strategy.ShouldFindProcess()
|
|
|
|
}
|
|
|
|
|
2021-12-02 22:56:17 +08:00
|
|
|
func (rp *ruleSetProvider) AsRule(adaptor string) C.Rule {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ruleSetProvider) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(
|
|
|
|
map[string]interface{}{
|
|
|
|
"behavior": rp.behavior.String(),
|
|
|
|
"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(),
|
2022-07-11 21:30:34 +08:00
|
|
|
"updatedAt": rp.UpdatedAt,
|
2021-12-02 22:56:17 +08:00
|
|
|
"vehicleType": rp.VehicleType().String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-10 13:36:09 +08:00
|
|
|
func NewRuleSetProvider(name string, behavior P.RuleType, 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,
|
|
|
|
}
|
|
|
|
|
2022-07-11 21:30:34 +08:00
|
|
|
onUpdate := func(elm interface{}) {
|
2023-04-01 14:11:09 +08:00
|
|
|
strategy := elm.(ruleStrategy)
|
|
|
|
rp.strategy = strategy
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|
|
|
|
|
2022-06-10 13:36:09 +08:00
|
|
|
rp.strategy = newStrategy(behavior, parse)
|
2023-04-01 14:11:09 +08:00
|
|
|
rp.Fetcher = resource.NewFetcher(name, interval, vehicle, func(bytes []byte) (any, error) { return rulesParse(bytes, newStrategy(behavior, parse)) }, onUpdate)
|
2022-03-26 18:34:15 +08:00
|
|
|
|
2021-12-02 22:56:17 +08:00
|
|
|
wrapper := &RuleSetProvider{
|
|
|
|
rp,
|
|
|
|
}
|
|
|
|
|
2022-07-11 21:30:34 +08:00
|
|
|
final := func(provider *RuleSetProvider) { _ = rp.Fetcher.Destroy() }
|
2022-05-08 00:04:16 +08:00
|
|
|
runtime.SetFinalizer(wrapper, final)
|
2021-12-02 22:56:17 +08:00
|
|
|
return wrapper
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:37:14 +08:00
|
|
|
func newStrategy(behavior P.RuleType, 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:
|
2022-06-10 13:36:09 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:09 +08:00
|
|
|
var ErrNoPayload = errors.New("file must have a `payload` field")
|
|
|
|
|
|
|
|
func rulesParse(buf []byte, strategy ruleStrategy) (any, error) {
|
|
|
|
strategy.Reset()
|
|
|
|
|
|
|
|
schema := &RulePayload{}
|
|
|
|
|
|
|
|
reader := bufio.NewReader(bytes.NewReader(buf))
|
|
|
|
|
|
|
|
firstLineBuffer := pool.GetBuffer()
|
|
|
|
defer pool.PutBuffer(firstLineBuffer)
|
|
|
|
firstLineLength := 0
|
|
|
|
|
|
|
|
for {
|
|
|
|
line, isPrefix, err := reader.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
if firstLineLength == 0 { // find payload head
|
|
|
|
return nil, ErrNoPayload
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
firstLineBuffer.Write(line) // need a copy because the returned buffer is only valid until the next call to ReadLine
|
|
|
|
if isPrefix {
|
|
|
|
// If the line was too long for the buffer then isPrefix is set and the
|
|
|
|
// beginning of the line is returned. The rest of the line will be returned
|
|
|
|
// from future calls.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if firstLineLength == 0 { // find payload head
|
|
|
|
firstLineBuffer.WriteByte('\n')
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse payload body
|
|
|
|
err = yaml.Unmarshal(firstLineBuffer.Bytes(), schema)
|
|
|
|
firstLineBuffer.Truncate(firstLineLength)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var str string
|
|
|
|
if len(schema.Rules) > 0 {
|
|
|
|
str = schema.Rules[0]
|
|
|
|
}
|
|
|
|
if len(schema.Payload) > 0 {
|
|
|
|
str = schema.Payload[0]
|
|
|
|
}
|
|
|
|
if str == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
strategy.Insert(str)
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:09 +08:00
|
|
|
strategy.FinishInsert()
|
|
|
|
|
|
|
|
return strategy, nil
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|