123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- package route
- import (
- "github.com/sagernet/sing-box/adapter"
- C "github.com/sagernet/sing-box/constant"
- "github.com/sagernet/sing-box/log"
- "github.com/sagernet/sing-box/option"
- tun "github.com/sagernet/sing-tun"
- "github.com/sagernet/sing/common"
- E "github.com/sagernet/sing/common/exceptions"
- )
- func NewIPRule(router adapter.Router, logger log.ContextLogger, options option.IPRule) (adapter.IPRule, error) {
- switch options.Type {
- case "", C.RuleTypeDefault:
- if !options.DefaultOptions.IsValid() {
- return nil, E.New("missing conditions")
- }
- if common.IsEmpty(options.DefaultOptions.Action) {
- return nil, E.New("missing action")
- }
- return NewDefaultIPRule(router, logger, options.DefaultOptions)
- case C.RuleTypeLogical:
- if !options.LogicalOptions.IsValid() {
- return nil, E.New("missing conditions")
- }
- if common.IsEmpty(options.DefaultOptions.Action) {
- return nil, E.New("missing action")
- }
- return NewLogicalIPRule(router, logger, options.LogicalOptions)
- default:
- return nil, E.New("unknown rule type: ", options.Type)
- }
- }
- var _ adapter.IPRule = (*DefaultIPRule)(nil)
- type DefaultIPRule struct {
- abstractDefaultRule
- action tun.ActionType
- }
- func NewDefaultIPRule(router adapter.Router, logger log.ContextLogger, options option.DefaultIPRule) (*DefaultIPRule, error) {
- rule := &DefaultIPRule{
- abstractDefaultRule: abstractDefaultRule{
- invert: options.Invert,
- outbound: options.Outbound,
- },
- action: tun.ActionType(options.Action),
- }
- if len(options.Inbound) > 0 {
- item := NewInboundRule(options.Inbound)
- rule.items = append(rule.items, item)
- rule.allItems = append(rule.allItems, item)
- }
- if options.IPVersion > 0 {
- switch options.IPVersion {
- case 4, 6:
- item := NewIPVersionItem(options.IPVersion == 6)
- rule.items = append(rule.items, item)
- rule.allItems = append(rule.allItems, item)
- default:
- return nil, E.New("invalid ip version: ", options.IPVersion)
- }
- }
- if len(options.Network) > 0 {
- item := NewNetworkItem(options.Network)
- rule.items = append(rule.items, item)
- rule.allItems = append(rule.allItems, item)
- }
- if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
- item := NewDomainItem(options.Domain, options.DomainSuffix)
- rule.destinationAddressItems = append(rule.destinationAddressItems, item)
- rule.allItems = append(rule.allItems, item)
- }
- if len(options.DomainKeyword) > 0 {
- item := NewDomainKeywordItem(options.DomainKeyword)
- rule.destinationAddressItems = append(rule.destinationAddressItems, item)
- rule.allItems = append(rule.allItems, item)
- }
- if len(options.DomainRegex) > 0 {
- item, err := NewDomainRegexItem(options.DomainRegex)
- if err != nil {
- return nil, E.Cause(err, "domain_regex")
- }
- rule.destinationAddressItems = append(rule.destinationAddressItems, item)
- rule.allItems = append(rule.allItems, item)
- }
- if len(options.Geosite) > 0 {
- item := NewGeositeItem(router, logger, options.Geosite)
- rule.destinationAddressItems = append(rule.destinationAddressItems, item)
- rule.allItems = append(rule.allItems, item)
- }
- if len(options.SourceGeoIP) > 0 {
- item := NewGeoIPItem(router, logger, true, options.SourceGeoIP)
- rule.sourceAddressItems = append(rule.sourceAddressItems, item)
- rule.allItems = append(rule.allItems, item)
- }
- if len(options.SourceIPCIDR) > 0 {
- item, err := NewIPCIDRItem(true, options.SourceIPCIDR)
- if err != nil {
- return nil, E.Cause(err, "source_ipcidr")
- }
- rule.sourceAddressItems = append(rule.sourceAddressItems, item)
- rule.allItems = append(rule.allItems, item)
- }
- if len(options.SourcePort) > 0 {
- item := NewPortItem(true, options.SourcePort)
- rule.sourcePortItems = append(rule.sourcePortItems, item)
- rule.allItems = append(rule.allItems, item)
- }
- if len(options.SourcePortRange) > 0 {
- item, err := NewPortRangeItem(true, options.SourcePortRange)
- if err != nil {
- return nil, E.Cause(err, "source_port_range")
- }
- rule.sourcePortItems = append(rule.sourcePortItems, item)
- rule.allItems = append(rule.allItems, item)
- }
- if len(options.Port) > 0 {
- item := NewPortItem(false, options.Port)
- rule.destinationPortItems = append(rule.destinationPortItems, item)
- rule.allItems = append(rule.allItems, item)
- }
- if len(options.PortRange) > 0 {
- item, err := NewPortRangeItem(false, options.PortRange)
- if err != nil {
- return nil, E.Cause(err, "port_range")
- }
- rule.destinationPortItems = append(rule.destinationPortItems, item)
- rule.allItems = append(rule.allItems, item)
- }
- return rule, nil
- }
- func (r *DefaultIPRule) Action() tun.ActionType {
- return r.action
- }
- var _ adapter.IPRule = (*LogicalIPRule)(nil)
- type LogicalIPRule struct {
- abstractLogicalRule
- action tun.ActionType
- }
- func NewLogicalIPRule(router adapter.Router, logger log.ContextLogger, options option.LogicalIPRule) (*LogicalIPRule, error) {
- r := &LogicalIPRule{
- abstractLogicalRule: abstractLogicalRule{
- rules: make([]adapter.Rule, len(options.Rules)),
- invert: options.Invert,
- outbound: options.Outbound,
- },
- action: tun.ActionType(options.Action),
- }
- switch options.Mode {
- case C.LogicalTypeAnd:
- r.mode = C.LogicalTypeAnd
- case C.LogicalTypeOr:
- r.mode = C.LogicalTypeOr
- default:
- return nil, E.New("unknown logical mode: ", options.Mode)
- }
- for i, subRule := range options.Rules {
- rule, err := NewDefaultIPRule(router, logger, subRule)
- if err != nil {
- return nil, E.Cause(err, "sub rule[", i, "]")
- }
- r.rules[i] = rule
- }
- return r, nil
- }
- func (r *LogicalIPRule) Action() tun.ActionType {
- return r.action
- }
|