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
|
|
|
"bytes"
|
2021-12-02 22:56:17 +08:00
|
|
|
"encoding/json"
|
2023-04-01 14:11:09 +08:00
|
|
|
"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-04-01 14:11:09 +08:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2024-06-17 22:04:51 +08:00
|
|
|
var tunnel P.Tunnel
|
|
|
|
|
|
|
|
func SetTunnel(t P.Tunnel) {
|
|
|
|
tunnel = t
|
|
|
|
}
|
2021-12-02 22:56:17 +08:00
|
|
|
|
|
|
|
type ruleSetProvider struct {
|
2024-06-17 22:04:51 +08:00
|
|
|
*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
|
|
|
|
*/
|
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 {
|
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
|
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
|
|
|
}
|
|
|
|
|
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
|
2024-07-28 10:07:37 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-20 16:29:08 +08:00
|
|
|
func (rp *ruleSetProvider) ShouldFindProcess() bool {
|
|
|
|
return rp.strategy.ShouldFindProcess()
|
|
|
|
}
|
|
|
|
|
2024-06-17 22:04:51 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-06-17 22:04:51 +08:00
|
|
|
onUpdate := func(strategy ruleStrategy) {
|
2023-04-01 14:11:09 +08:00
|
|
|
rp.strategy = strategy
|
2024-06-17 22:04:51 +08:00
|
|
|
tunnel.RuleUpdateCallback().Emit(rp)
|
2021-12-02 22:56:17 +08:00
|
|
|
}
|
|
|
|
|
2022-06-10 13:36:09 +08:00
|
|
|
rp.strategy = newStrategy(behavior, parse)
|
2024-06-17 22:04:51 +08:00
|
|
|
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:
|
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")
|
2024-07-26 22:30:42 +08:00
|
|
|
var ErrInvalidFormat = errors.New("invalid format")
|
2023-04-01 14:11:09 +08:00
|
|
|
|
2024-06-17 22:04:51 +08:00
|
|
|
func rulesParse(buf []byte, strategy ruleStrategy, format P.RuleFormat) (ruleStrategy, error) {
|
2023-04-01 14:11:09 +08:00
|
|
|
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
|
|
|
}
|
2023-04-01 14:11:09 +08:00
|
|
|
|
|
|
|
schema := &RulePayload{}
|
|
|
|
|
|
|
|
firstLineBuffer := pool.GetBuffer()
|
|
|
|
defer pool.PutBuffer(firstLineBuffer)
|
|
|
|
firstLineLength := 0
|
|
|
|
|
2023-04-01 16:55:02 +08:00
|
|
|
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 {
|
2024-06-28 14:14:36 +08:00
|
|
|
s = len(buf) // stop loop in next step
|
|
|
|
if firstLineLength == 0 && format == P.YamlRule { // no head or only one line body
|
2023-04-01 16:55:02 +08:00
|
|
|
return nil, ErrNoPayload
|
2023-04-01 14:11:09 +08:00
|
|
|
}
|
|
|
|
}
|
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-01 14:11:09 +08:00
|
|
|
|
2023-04-14 13:51:26 +08:00
|
|
|
// parse payload body
|
2023-04-01 16:55:02 +08:00
|
|
|
err := yaml.Unmarshal(firstLineBuffer.Bytes(), schema)
|
2023-04-01 14:11:09 +08:00
|
|
|
firstLineBuffer.Truncate(firstLineLength)
|
2023-04-14 13:51:26 +08:00
|
|
|
if err != nil {
|
2023-04-01 14:11:09 +08:00
|
|
|
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
|
2023-04-01 14:11:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|