rule_dns.go 11 KB

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