rule.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package route
  2. import (
  3. "strings"
  4. "github.com/sagernet/sing-box/adapter"
  5. C "github.com/sagernet/sing-box/constant"
  6. "github.com/sagernet/sing-box/log"
  7. "github.com/sagernet/sing-box/option"
  8. "github.com/sagernet/sing/common"
  9. E "github.com/sagernet/sing/common/exceptions"
  10. F "github.com/sagernet/sing/common/format"
  11. )
  12. func NewRule(router adapter.Router, logger log.Logger, options option.Rule) (adapter.Rule, error) {
  13. if common.IsEmptyByEquals(options) {
  14. return nil, E.New("empty rule config")
  15. }
  16. switch options.Type {
  17. case "", C.RuleTypeDefault:
  18. if !options.DefaultOptions.IsValid() {
  19. return nil, E.New("missing conditions")
  20. }
  21. if options.DefaultOptions.Outbound == "" {
  22. return nil, E.New("missing outbound field")
  23. }
  24. return NewDefaultRule(router, logger, options.DefaultOptions)
  25. case C.RuleTypeLogical:
  26. if !options.LogicalOptions.IsValid() {
  27. return nil, E.New("missing conditions")
  28. }
  29. if options.LogicalOptions.Outbound == "" {
  30. return nil, E.New("missing outbound field")
  31. }
  32. return NewLogicalRule(router, logger, options.LogicalOptions)
  33. default:
  34. return nil, E.New("unknown rule type: ", options.Type)
  35. }
  36. }
  37. var _ adapter.Rule = (*DefaultRule)(nil)
  38. type DefaultRule struct {
  39. items []RuleItem
  40. sourceAddressItems []RuleItem
  41. destinationAddressItems []RuleItem
  42. allItems []RuleItem
  43. outbound string
  44. }
  45. type RuleItem interface {
  46. Match(metadata *adapter.InboundContext) bool
  47. String() string
  48. }
  49. func NewDefaultRule(router adapter.Router, logger log.Logger, options option.DefaultRule) (*DefaultRule, error) {
  50. rule := &DefaultRule{
  51. outbound: options.Outbound,
  52. }
  53. if len(options.Inbound) > 0 {
  54. item := NewInboundRule(options.Inbound)
  55. rule.items = append(rule.items, item)
  56. rule.allItems = append(rule.allItems, item)
  57. }
  58. if options.IPVersion > 0 {
  59. switch options.IPVersion {
  60. case 4, 6:
  61. item := NewIPVersionItem(options.IPVersion == 6)
  62. rule.items = append(rule.items, item)
  63. rule.allItems = append(rule.allItems, item)
  64. default:
  65. return nil, E.New("invalid ip version: ", options.IPVersion)
  66. }
  67. }
  68. if options.Network != "" {
  69. switch options.Network {
  70. case C.NetworkTCP, C.NetworkUDP:
  71. item := NewNetworkItem(options.Network)
  72. rule.items = append(rule.items, item)
  73. rule.allItems = append(rule.allItems, item)
  74. default:
  75. return nil, E.New("invalid network: ", options.Network)
  76. }
  77. }
  78. if len(options.Protocol) > 0 {
  79. item := NewProtocolItem(options.Protocol)
  80. rule.items = append(rule.items, item)
  81. rule.allItems = append(rule.allItems, item)
  82. }
  83. if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
  84. item := NewDomainItem(options.Domain, options.DomainSuffix)
  85. rule.destinationAddressItems = append(rule.destinationAddressItems, item)
  86. rule.allItems = append(rule.allItems, item)
  87. }
  88. if len(options.DomainKeyword) > 0 {
  89. item := NewDomainKeywordItem(options.DomainKeyword)
  90. rule.destinationAddressItems = append(rule.destinationAddressItems, item)
  91. rule.allItems = append(rule.allItems, item)
  92. }
  93. if len(options.DomainRegex) > 0 {
  94. item, err := NewDomainRegexItem(options.DomainRegex)
  95. if err != nil {
  96. return nil, E.Cause(err, "domain_regex")
  97. }
  98. rule.destinationAddressItems = append(rule.destinationAddressItems, item)
  99. rule.allItems = append(rule.allItems, item)
  100. }
  101. if len(options.Geosite) > 0 {
  102. item := NewGeositeItem(router, logger, options.Geosite)
  103. rule.destinationAddressItems = append(rule.destinationAddressItems, item)
  104. rule.allItems = append(rule.allItems, item)
  105. }
  106. if len(options.SourceGeoIP) > 0 {
  107. item := NewGeoIPItem(router, logger, true, options.SourceGeoIP)
  108. rule.sourceAddressItems = append(rule.sourceAddressItems, item)
  109. rule.allItems = append(rule.allItems, item)
  110. }
  111. if len(options.GeoIP) > 0 {
  112. item := NewGeoIPItem(router, logger, false, options.GeoIP)
  113. rule.destinationAddressItems = append(rule.destinationAddressItems, item)
  114. rule.allItems = append(rule.allItems, item)
  115. }
  116. if len(options.SourceIPCIDR) > 0 {
  117. item, err := NewIPCIDRItem(true, options.SourceIPCIDR)
  118. if err != nil {
  119. return nil, E.Cause(err, "source_ipcidr")
  120. }
  121. rule.sourceAddressItems = append(rule.sourceAddressItems, item)
  122. rule.allItems = append(rule.allItems, item)
  123. }
  124. if len(options.IPCIDR) > 0 {
  125. item, err := NewIPCIDRItem(false, options.IPCIDR)
  126. if err != nil {
  127. return nil, E.Cause(err, "ipcidr")
  128. }
  129. rule.destinationAddressItems = append(rule.destinationAddressItems, item)
  130. rule.allItems = append(rule.allItems, item)
  131. }
  132. if len(options.SourcePort) > 0 {
  133. item := NewPortItem(true, options.SourcePort)
  134. rule.items = append(rule.items, item)
  135. rule.allItems = append(rule.allItems, item)
  136. }
  137. if len(options.Port) > 0 {
  138. item := NewPortItem(false, options.Port)
  139. rule.items = append(rule.items, item)
  140. rule.allItems = append(rule.allItems, item)
  141. }
  142. return rule, nil
  143. }
  144. func (r *DefaultRule) Start() error {
  145. for _, item := range r.allItems {
  146. err := common.Start(item)
  147. if err != nil {
  148. return err
  149. }
  150. }
  151. return nil
  152. }
  153. func (r *DefaultRule) Close() error {
  154. for _, item := range r.allItems {
  155. err := common.Close(item)
  156. if err != nil {
  157. return err
  158. }
  159. }
  160. return nil
  161. }
  162. func (r *DefaultRule) UpdateGeosite() error {
  163. for _, item := range r.allItems {
  164. if geositeItem, isSite := item.(*GeositeItem); isSite {
  165. err := geositeItem.Update()
  166. if err != nil {
  167. return err
  168. }
  169. }
  170. }
  171. return nil
  172. }
  173. func (r *DefaultRule) Match(metadata *adapter.InboundContext) bool {
  174. for _, item := range r.items {
  175. if !item.Match(metadata) {
  176. return false
  177. }
  178. }
  179. if len(r.sourceAddressItems) > 0 {
  180. var sourceAddressMatch bool
  181. for _, item := range r.sourceAddressItems {
  182. if item.Match(metadata) {
  183. sourceAddressMatch = true
  184. break
  185. }
  186. }
  187. if !sourceAddressMatch {
  188. return false
  189. }
  190. }
  191. if len(r.destinationAddressItems) > 0 {
  192. var destinationAddressMatch bool
  193. for _, item := range r.destinationAddressItems {
  194. if item.Match(metadata) {
  195. destinationAddressMatch = true
  196. break
  197. }
  198. }
  199. if !destinationAddressMatch {
  200. return false
  201. }
  202. }
  203. return true
  204. }
  205. func (r *DefaultRule) Outbound() string {
  206. return r.outbound
  207. }
  208. func (r *DefaultRule) String() string {
  209. return strings.Join(common.Map(r.allItems, F.ToString0[RuleItem]), " ")
  210. }
  211. var _ adapter.Rule = (*LogicalRule)(nil)
  212. type LogicalRule struct {
  213. mode string
  214. rules []*DefaultRule
  215. outbound string
  216. }
  217. func (r *LogicalRule) UpdateGeosite() error {
  218. for _, rule := range r.rules {
  219. err := rule.UpdateGeosite()
  220. if err != nil {
  221. return err
  222. }
  223. }
  224. return nil
  225. }
  226. func (r *LogicalRule) Start() error {
  227. for _, rule := range r.rules {
  228. err := rule.Start()
  229. if err != nil {
  230. return err
  231. }
  232. }
  233. return nil
  234. }
  235. func (r *LogicalRule) Close() error {
  236. for _, rule := range r.rules {
  237. err := rule.Close()
  238. if err != nil {
  239. return err
  240. }
  241. }
  242. return nil
  243. }
  244. func NewLogicalRule(router adapter.Router, logger log.Logger, options option.LogicalRule) (*LogicalRule, error) {
  245. r := &LogicalRule{
  246. rules: make([]*DefaultRule, len(options.Rules)),
  247. outbound: options.Outbound,
  248. }
  249. switch options.Mode {
  250. case C.LogicalTypeAnd:
  251. r.mode = C.LogicalTypeAnd
  252. case C.LogicalTypeOr:
  253. r.mode = C.LogicalTypeOr
  254. default:
  255. return nil, E.New("unknown logical mode: ", options.Mode)
  256. }
  257. for i, subRule := range options.Rules {
  258. rule, err := NewDefaultRule(router, logger, subRule)
  259. if err != nil {
  260. return nil, E.Cause(err, "sub rule[", i, "]")
  261. }
  262. r.rules[i] = rule
  263. }
  264. return r, nil
  265. }
  266. func (r *LogicalRule) Match(metadata *adapter.InboundContext) bool {
  267. if r.mode == C.LogicalTypeAnd {
  268. return common.All(r.rules, func(it *DefaultRule) bool {
  269. return it.Match(metadata)
  270. })
  271. } else {
  272. return common.Any(r.rules, func(it *DefaultRule) bool {
  273. return it.Match(metadata)
  274. })
  275. }
  276. }
  277. func (r *LogicalRule) Outbound() string {
  278. return r.outbound
  279. }
  280. func (r *LogicalRule) String() string {
  281. var op string
  282. switch r.mode {
  283. case C.LogicalTypeAnd:
  284. op = "&&"
  285. case C.LogicalTypeOr:
  286. op = "||"
  287. }
  288. return "logical(" + strings.Join(common.Map(r.rules, F.ToString0[*DefaultRule]), " "+op+" ") + ")"
  289. }