sing-box/route/rule/rule_set_local.go

195 lines
4.7 KiB
Go
Raw Normal View History

2024-10-21 23:38:34 +08:00
package rule
2023-12-01 13:24:12 +08:00
import (
"context"
"os"
"path/filepath"
"strings"
2023-12-01 13:24:12 +08:00
"github.com/sagernet/fswatch"
2023-12-01 13:24:12 +08:00
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/srs"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
2024-06-07 15:55:21 +08:00
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/atomic"
2023-12-01 13:24:12 +08:00
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
"github.com/sagernet/sing/common/json"
"github.com/sagernet/sing/common/logger"
2024-06-07 15:55:21 +08:00
"github.com/sagernet/sing/common/x/list"
"github.com/sagernet/sing/service/filemanager"
2024-06-07 15:55:21 +08:00
"go4.org/netipx"
2023-12-01 13:24:12 +08:00
)
var _ adapter.RuleSet = (*LocalRuleSet)(nil)
type LocalRuleSet struct {
2024-11-10 12:11:21 +08:00
ctx context.Context
logger logger.Logger
tag string
rules []adapter.HeadlessRule
metadata adapter.RuleSetMetadata
fileFormat string
watcher *fswatch.Watcher
refs atomic.Int32
2023-12-01 13:24:12 +08:00
}
2024-11-10 12:11:21 +08:00
func NewLocalRuleSet(ctx context.Context, logger logger.Logger, options option.RuleSet) (*LocalRuleSet, error) {
ruleSet := &LocalRuleSet{
2024-11-10 12:11:21 +08:00
ctx: ctx,
logger: logger,
tag: options.Tag,
fileFormat: options.Format,
}
if options.Type == C.RuleSetTypeInline {
if len(options.InlineOptions.Rules) == 0 {
return nil, E.New("empty inline rule-set")
2023-12-01 13:24:12 +08:00
}
err := ruleSet.reloadRules(options.InlineOptions.Rules)
2023-12-01 13:24:12 +08:00
if err != nil {
return nil, err
}
} else {
err := ruleSet.reloadFile(filemanager.BasePath(ctx, options.LocalOptions.Path))
2023-12-01 13:24:12 +08:00
if err != nil {
return nil, err
}
}
if options.Type == C.RuleSetTypeLocal {
filePath, _ := filepath.Abs(options.LocalOptions.Path)
watcher, err := fswatch.NewWatcher(fswatch.Options{
Path: []string{filePath},
Callback: func(path string) {
uErr := ruleSet.reloadFile(path)
if uErr != nil {
logger.Error(E.Cause(uErr, "reload rule-set ", options.Tag))
}
},
})
2023-12-01 13:24:12 +08:00
if err != nil {
return nil, err
2023-12-01 13:24:12 +08:00
}
ruleSet.watcher = watcher
2023-12-01 13:24:12 +08:00
}
return ruleSet, nil
2023-12-01 13:24:12 +08:00
}
2024-06-07 15:55:21 +08:00
func (s *LocalRuleSet) Name() string {
return s.tag
2023-12-01 13:24:12 +08:00
}
func (s *LocalRuleSet) String() string {
return strings.Join(F.MapToString(s.rules), " ")
}
2024-10-25 22:24:19 +08:00
func (s *LocalRuleSet) StartContext(ctx context.Context, startContext *adapter.HTTPStartContext) error {
if s.watcher != nil {
err := s.watcher.Start()
if err != nil {
s.logger.Error(E.Cause(err, "watch rule-set file"))
}
}
return nil
}
func (s *LocalRuleSet) reloadFile(path string) error {
2024-11-11 16:27:28 +08:00
var ruleSet option.PlainRuleSetCompat
switch s.fileFormat {
case C.RuleSetFormatSource, "":
content, err := os.ReadFile(path)
if err != nil {
return err
}
2024-11-11 16:27:28 +08:00
ruleSet, err = json.UnmarshalExtended[option.PlainRuleSetCompat](content)
if err != nil {
return err
}
2024-11-11 16:27:28 +08:00
case C.RuleSetFormatBinary:
setFile, err := os.Open(path)
if err != nil {
return err
}
2024-11-11 16:27:28 +08:00
ruleSet, err = srs.Read(setFile, false)
if err != nil {
return err
}
default:
return E.New("unknown rule-set format: ", s.fileFormat)
}
2024-11-11 16:27:28 +08:00
plainRuleSet, err := ruleSet.Upgrade()
if err != nil {
return err
}
return s.reloadRules(plainRuleSet.Rules)
}
func (s *LocalRuleSet) reloadRules(headlessRules []option.HeadlessRule) error {
rules := make([]adapter.HeadlessRule, len(headlessRules))
var err error
for i, ruleOptions := range headlessRules {
2024-11-10 12:11:21 +08:00
rules[i], err = NewHeadlessRule(s.ctx, ruleOptions)
if err != nil {
return E.Cause(err, "parse rule_set.rules.[", i, "]")
}
}
var metadata adapter.RuleSetMetadata
metadata.ContainsProcessRule = hasHeadlessRule(headlessRules, isProcessHeadlessRule)
metadata.ContainsWIFIRule = hasHeadlessRule(headlessRules, isWIFIHeadlessRule)
metadata.ContainsIPCIDRRule = hasHeadlessRule(headlessRules, isIPCIDRHeadlessRule)
s.rules = rules
s.metadata = metadata
2023-12-01 13:24:12 +08:00
return nil
}
2024-06-07 15:55:21 +08:00
func (s *LocalRuleSet) PostStart() error {
return nil
}
2023-12-01 13:24:12 +08:00
func (s *LocalRuleSet) Metadata() adapter.RuleSetMetadata {
return s.metadata
}
2024-06-07 15:55:21 +08:00
func (s *LocalRuleSet) ExtractIPSet() []*netipx.IPSet {
return common.FlatMap(s.rules, extractIPSetFromRule)
}
func (s *LocalRuleSet) IncRef() {
s.refs.Add(1)
}
func (s *LocalRuleSet) DecRef() {
if s.refs.Add(-1) < 0 {
panic("rule-set: negative refs")
}
}
func (s *LocalRuleSet) Cleanup() {
if s.refs.Load() == 0 {
s.rules = nil
}
}
func (s *LocalRuleSet) RegisterCallback(callback adapter.RuleSetUpdateCallback) *list.Element[adapter.RuleSetUpdateCallback] {
return nil
}
func (s *LocalRuleSet) UnregisterCallback(element *list.Element[adapter.RuleSetUpdateCallback]) {
}
2023-12-01 13:24:12 +08:00
func (s *LocalRuleSet) Close() error {
2024-06-07 15:55:21 +08:00
s.rules = nil
return common.Close(common.PtrOrNil(s.watcher))
2023-12-01 13:24:12 +08:00
}
2024-06-07 15:55:21 +08:00
func (s *LocalRuleSet) Match(metadata *adapter.InboundContext) bool {
for _, rule := range s.rules {
if rule.Match(metadata) {
return true
}
}
return false
}