sing-box/route/rule/rule_set_local.go

194 lines
4.7 KiB
Go
Raw Normal View History

2024-10-21 15:38:34 +00:00
package rule
2023-12-01 05:24:12 +00:00
import (
"context"
"os"
"path/filepath"
"strings"
2023-12-01 05:24:12 +00:00
"github.com/sagernet/fswatch"
2023-12-01 05:24:12 +00: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 07:55:21 +00:00
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/atomic"
2023-12-01 05:24:12 +00: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 07:55:21 +00:00
"github.com/sagernet/sing/common/x/list"
"github.com/sagernet/sing/service/filemanager"
2024-06-07 07:55:21 +00:00
"go4.org/netipx"
2023-12-01 05:24:12 +00:00
)
var _ adapter.RuleSet = (*LocalRuleSet)(nil)
type LocalRuleSet struct {
router adapter.Router
logger logger.Logger
tag string
rules []adapter.HeadlessRule
metadata adapter.RuleSetMetadata
fileFormat string
watcher *fswatch.Watcher
refs atomic.Int32
2023-12-01 05:24:12 +00:00
}
func NewLocalRuleSet(ctx context.Context, router adapter.Router, logger logger.Logger, options option.RuleSet) (*LocalRuleSet, error) {
ruleSet := &LocalRuleSet{
router: router,
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 05:24:12 +00:00
}
err := ruleSet.reloadRules(options.InlineOptions.Rules)
2023-12-01 05:24:12 +00:00
if err != nil {
return nil, err
}
} else {
err := ruleSet.reloadFile(filemanager.BasePath(ctx, options.LocalOptions.Path))
2023-12-01 05:24:12 +00: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 05:24:12 +00:00
if err != nil {
return nil, err
2023-12-01 05:24:12 +00:00
}
ruleSet.watcher = watcher
2023-12-01 05:24:12 +00:00
}
return ruleSet, nil
2023-12-01 05:24:12 +00:00
}
2024-06-07 07:55:21 +00:00
func (s *LocalRuleSet) Name() string {
return s.tag
2023-12-01 05:24:12 +00:00
}
func (s *LocalRuleSet) String() string {
return strings.Join(F.MapToString(s.rules), " ")
}
2024-10-25 14:24:19 +00: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 {
var plainRuleSet option.PlainRuleSet
switch s.fileFormat {
case C.RuleSetFormatSource, "":
content, err := os.ReadFile(path)
if err != nil {
return err
}
compat, err := json.UnmarshalExtended[option.PlainRuleSetCompat](content)
if err != nil {
return err
}
plainRuleSet, err = compat.Upgrade()
if err != nil {
return err
}
case C.RuleSetFormatBinary:
setFile, err := os.Open(path)
if err != nil {
return err
}
plainRuleSet, err = srs.Read(setFile, false)
if err != nil {
return err
}
default:
return E.New("unknown rule-set format: ", s.fileFormat)
}
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 {
rules[i], err = NewHeadlessRule(s.router, 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 05:24:12 +00:00
return nil
}
2024-06-07 07:55:21 +00:00
func (s *LocalRuleSet) PostStart() error {
return nil
}
2023-12-01 05:24:12 +00:00
func (s *LocalRuleSet) Metadata() adapter.RuleSetMetadata {
return s.metadata
}
2024-06-07 07:55:21 +00: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 05:24:12 +00:00
func (s *LocalRuleSet) Close() error {
2024-06-07 07:55:21 +00:00
s.rules = nil
return common.Close(common.PtrOrNil(s.watcher))
2023-12-01 05:24:12 +00:00
}
2024-06-07 07:55:21 +00:00
func (s *LocalRuleSet) Match(metadata *adapter.InboundContext) bool {
for _, rule := range s.rules {
if rule.Match(metadata) {
return true
}
}
return false
}