rule_dns.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. N "github.com/sagernet/sing/common/network"
  12. )
  13. func NewDNSRule(router adapter.Router, logger log.ContextLogger, options option.DNSRule) (adapter.DNSRule, error) {
  14. switch options.Type {
  15. case "", C.RuleTypeDefault:
  16. if !options.DefaultOptions.IsValid() {
  17. return nil, E.New("missing conditions")
  18. }
  19. if options.DefaultOptions.Server == "" {
  20. return nil, E.New("missing server field")
  21. }
  22. return NewDefaultDNSRule(router, logger, options.DefaultOptions)
  23. case C.RuleTypeLogical:
  24. if !options.LogicalOptions.IsValid() {
  25. return nil, E.New("missing conditions")
  26. }
  27. if options.LogicalOptions.Server == "" {
  28. return nil, E.New("missing server field")
  29. }
  30. return NewLogicalDNSRule(router, logger, options.LogicalOptions)
  31. default:
  32. return nil, E.New("unknown rule type: ", options.Type)
  33. }
  34. }
  35. var _ adapter.DNSRule = (*DefaultDNSRule)(nil)
  36. type DefaultDNSRule struct {
  37. items []RuleItem
  38. sourceAddressItems []RuleItem
  39. sourcePortItems []RuleItem
  40. destinationAddressItems []RuleItem
  41. destinationPortItems []RuleItem
  42. allItems []RuleItem
  43. invert bool
  44. outbound string
  45. disableCache bool
  46. }
  47. func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options option.DefaultDNSRule) (*DefaultDNSRule, error) {
  48. rule := &DefaultDNSRule{
  49. invert: options.Invert,
  50. outbound: options.Server,
  51. disableCache: options.DisableCache,
  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 N.NetworkTCP, N.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.AuthUser) > 0 {
  79. item := NewAuthUserItem(options.AuthUser)
  80. rule.items = append(rule.items, item)
  81. rule.allItems = append(rule.allItems, item)
  82. }
  83. if len(options.Protocol) > 0 {
  84. item := NewProtocolItem(options.Protocol)
  85. rule.items = append(rule.items, item)
  86. rule.allItems = append(rule.allItems, item)
  87. }
  88. if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
  89. item := NewDomainItem(options.Domain, options.DomainSuffix)
  90. rule.destinationAddressItems = append(rule.destinationAddressItems, item)
  91. rule.allItems = append(rule.allItems, item)
  92. }
  93. if len(options.DomainKeyword) > 0 {
  94. item := NewDomainKeywordItem(options.DomainKeyword)
  95. rule.destinationAddressItems = append(rule.destinationAddressItems, item)
  96. rule.allItems = append(rule.allItems, item)
  97. }
  98. if len(options.DomainRegex) > 0 {
  99. item, err := NewDomainRegexItem(options.DomainRegex)
  100. if err != nil {
  101. return nil, E.Cause(err, "domain_regex")
  102. }
  103. rule.destinationAddressItems = append(rule.destinationAddressItems, item)
  104. rule.allItems = append(rule.allItems, item)
  105. }
  106. if len(options.Geosite) > 0 {
  107. item := NewGeositeItem(router, logger, options.Geosite)
  108. rule.destinationAddressItems = append(rule.destinationAddressItems, item)
  109. rule.allItems = append(rule.allItems, item)
  110. }
  111. if len(options.SourceGeoIP) > 0 {
  112. item := NewGeoIPItem(router, logger, true, options.SourceGeoIP)
  113. rule.sourceAddressItems = append(rule.sourceAddressItems, 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.SourcePort) > 0 {
  125. item := NewPortItem(true, options.SourcePort)
  126. rule.sourcePortItems = append(rule.sourcePortItems, item)
  127. rule.allItems = append(rule.allItems, item)
  128. }
  129. if len(options.SourcePortRange) > 0 {
  130. item, err := NewPortRangeItem(true, options.SourcePortRange)
  131. if err != nil {
  132. return nil, E.Cause(err, "source_port_range")
  133. }
  134. rule.sourcePortItems = append(rule.sourcePortItems, item)
  135. rule.allItems = append(rule.allItems, item)
  136. }
  137. if len(options.Port) > 0 {
  138. item := NewPortItem(false, options.Port)
  139. rule.destinationPortItems = append(rule.destinationPortItems, item)
  140. rule.allItems = append(rule.allItems, item)
  141. }
  142. if len(options.PortRange) > 0 {
  143. item, err := NewPortRangeItem(false, options.PortRange)
  144. if err != nil {
  145. return nil, E.Cause(err, "port_range")
  146. }
  147. rule.destinationPortItems = append(rule.destinationPortItems, item)
  148. rule.allItems = append(rule.allItems, item)
  149. }
  150. if len(options.ProcessName) > 0 {
  151. item := NewProcessItem(options.ProcessName)
  152. rule.items = append(rule.items, item)
  153. rule.allItems = append(rule.allItems, item)
  154. }
  155. if len(options.ProcessPath) > 0 {
  156. item := NewProcessPathItem(options.ProcessPath)
  157. rule.items = append(rule.items, item)
  158. rule.allItems = append(rule.allItems, item)
  159. }
  160. if len(options.PackageName) > 0 {
  161. item := NewPackageNameItem(options.PackageName)
  162. rule.items = append(rule.items, item)
  163. rule.allItems = append(rule.allItems, item)
  164. }
  165. if len(options.User) > 0 {
  166. item := NewUserItem(options.User)
  167. rule.items = append(rule.items, item)
  168. rule.allItems = append(rule.allItems, item)
  169. }
  170. if len(options.UserID) > 0 {
  171. item := NewUserIDItem(options.UserID)
  172. rule.items = append(rule.items, item)
  173. rule.allItems = append(rule.allItems, item)
  174. }
  175. if len(options.Outbound) > 0 {
  176. item := NewOutboundRule(options.Outbound)
  177. rule.items = append(rule.items, item)
  178. rule.allItems = append(rule.allItems, item)
  179. }
  180. if options.ClashMode != "" {
  181. item := NewClashModeItem(router, options.ClashMode)
  182. rule.items = append(rule.items, item)
  183. rule.allItems = append(rule.allItems, item)
  184. }
  185. return rule, nil
  186. }
  187. func (r *DefaultDNSRule) Type() string {
  188. return C.RuleTypeDefault
  189. }
  190. func (r *DefaultDNSRule) Start() error {
  191. for _, item := range r.allItems {
  192. err := common.Start(item)
  193. if err != nil {
  194. return err
  195. }
  196. }
  197. return nil
  198. }
  199. func (r *DefaultDNSRule) Close() error {
  200. for _, item := range r.allItems {
  201. err := common.Close(item)
  202. if err != nil {
  203. return err
  204. }
  205. }
  206. return nil
  207. }
  208. func (r *DefaultDNSRule) UpdateGeosite() error {
  209. for _, item := range r.allItems {
  210. if geositeItem, isSite := item.(*GeositeItem); isSite {
  211. err := geositeItem.Update()
  212. if err != nil {
  213. return err
  214. }
  215. }
  216. }
  217. return nil
  218. }
  219. func (r *DefaultDNSRule) Match(metadata *adapter.InboundContext) bool {
  220. for _, item := range r.items {
  221. if !item.Match(metadata) {
  222. return r.invert
  223. }
  224. }
  225. if len(r.sourceAddressItems) > 0 {
  226. var sourceAddressMatch bool
  227. for _, item := range r.sourceAddressItems {
  228. if item.Match(metadata) {
  229. sourceAddressMatch = true
  230. break
  231. }
  232. }
  233. if !sourceAddressMatch {
  234. return r.invert
  235. }
  236. }
  237. if len(r.sourcePortItems) > 0 {
  238. var sourcePortMatch bool
  239. for _, item := range r.sourcePortItems {
  240. if item.Match(metadata) {
  241. sourcePortMatch = true
  242. break
  243. }
  244. }
  245. if !sourcePortMatch {
  246. return r.invert
  247. }
  248. }
  249. if len(r.destinationAddressItems) > 0 {
  250. var destinationAddressMatch bool
  251. for _, item := range r.destinationAddressItems {
  252. if item.Match(metadata) {
  253. destinationAddressMatch = true
  254. break
  255. }
  256. }
  257. if !destinationAddressMatch {
  258. return r.invert
  259. }
  260. }
  261. if len(r.destinationPortItems) > 0 {
  262. var destinationPortMatch bool
  263. for _, item := range r.destinationPortItems {
  264. if item.Match(metadata) {
  265. destinationPortMatch = true
  266. break
  267. }
  268. }
  269. if !destinationPortMatch {
  270. return r.invert
  271. }
  272. }
  273. return !r.invert
  274. }
  275. func (r *DefaultDNSRule) Outbound() string {
  276. return r.outbound
  277. }
  278. func (r *DefaultDNSRule) DisableCache() bool {
  279. return r.disableCache
  280. }
  281. func (r *DefaultDNSRule) String() string {
  282. return strings.Join(F.MapToString(r.allItems), " ")
  283. }
  284. var _ adapter.DNSRule = (*LogicalDNSRule)(nil)
  285. type LogicalDNSRule struct {
  286. mode string
  287. rules []*DefaultDNSRule
  288. invert bool
  289. outbound string
  290. disableCache bool
  291. }
  292. func NewLogicalDNSRule(router adapter.Router, logger log.ContextLogger, options option.LogicalDNSRule) (*LogicalDNSRule, error) {
  293. r := &LogicalDNSRule{
  294. rules: make([]*DefaultDNSRule, len(options.Rules)),
  295. invert: options.Invert,
  296. outbound: options.Server,
  297. disableCache: options.DisableCache,
  298. }
  299. switch options.Mode {
  300. case C.LogicalTypeAnd:
  301. r.mode = C.LogicalTypeAnd
  302. case C.LogicalTypeOr:
  303. r.mode = C.LogicalTypeOr
  304. default:
  305. return nil, E.New("unknown logical mode: ", options.Mode)
  306. }
  307. for i, subRule := range options.Rules {
  308. rule, err := NewDefaultDNSRule(router, logger, subRule)
  309. if err != nil {
  310. return nil, E.Cause(err, "sub rule[", i, "]")
  311. }
  312. r.rules[i] = rule
  313. }
  314. return r, nil
  315. }
  316. func (r *LogicalDNSRule) Type() string {
  317. return C.RuleTypeLogical
  318. }
  319. func (r *LogicalDNSRule) UpdateGeosite() error {
  320. for _, rule := range r.rules {
  321. err := rule.UpdateGeosite()
  322. if err != nil {
  323. return err
  324. }
  325. }
  326. return nil
  327. }
  328. func (r *LogicalDNSRule) Start() error {
  329. for _, rule := range r.rules {
  330. err := rule.Start()
  331. if err != nil {
  332. return err
  333. }
  334. }
  335. return nil
  336. }
  337. func (r *LogicalDNSRule) Close() error {
  338. for _, rule := range r.rules {
  339. err := rule.Close()
  340. if err != nil {
  341. return err
  342. }
  343. }
  344. return nil
  345. }
  346. func (r *LogicalDNSRule) Match(metadata *adapter.InboundContext) bool {
  347. if r.mode == C.LogicalTypeAnd {
  348. return common.All(r.rules, func(it *DefaultDNSRule) bool {
  349. return it.Match(metadata)
  350. }) != r.invert
  351. } else {
  352. return common.Any(r.rules, func(it *DefaultDNSRule) bool {
  353. return it.Match(metadata)
  354. }) != r.invert
  355. }
  356. }
  357. func (r *LogicalDNSRule) Outbound() string {
  358. return r.outbound
  359. }
  360. func (r *LogicalDNSRule) DisableCache() bool {
  361. return r.disableCache
  362. }
  363. func (r *LogicalDNSRule) String() string {
  364. var op string
  365. switch r.mode {
  366. case C.LogicalTypeAnd:
  367. op = "&&"
  368. case C.LogicalTypeOr:
  369. op = "||"
  370. }
  371. if !r.invert {
  372. return strings.Join(F.MapToString(r.rules), " "+op+" ")
  373. } else {
  374. return "!(" + strings.Join(F.MapToString(r.rules), " "+op+" ") + ")"
  375. }
  376. }