router.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. package conf
  2. import (
  3. "encoding/json"
  4. "runtime"
  5. "strconv"
  6. "strings"
  7. "github.com/xtls/xray-core/app/router"
  8. "github.com/xtls/xray-core/common/errors"
  9. "github.com/xtls/xray-core/common/net"
  10. "github.com/xtls/xray-core/common/platform/filesystem"
  11. "github.com/xtls/xray-core/common/serial"
  12. "google.golang.org/protobuf/proto"
  13. )
  14. // StrategyConfig represents a strategy config
  15. type StrategyConfig struct {
  16. Type string `json:"type"`
  17. Settings *json.RawMessage `json:"settings"`
  18. }
  19. type BalancingRule struct {
  20. Tag string `json:"tag"`
  21. Selectors StringList `json:"selector"`
  22. Strategy StrategyConfig `json:"strategy"`
  23. FallbackTag string `json:"fallbackTag"`
  24. }
  25. // Build builds the balancing rule
  26. func (r *BalancingRule) Build() (*router.BalancingRule, error) {
  27. if r.Tag == "" {
  28. return nil, errors.New("empty balancer tag")
  29. }
  30. if len(r.Selectors) == 0 {
  31. return nil, errors.New("empty selector list")
  32. }
  33. r.Strategy.Type = strings.ToLower(r.Strategy.Type)
  34. switch r.Strategy.Type {
  35. case "":
  36. r.Strategy.Type = strategyRandom
  37. case strategyRandom, strategyLeastLoad, strategyLeastPing, strategyRoundRobin:
  38. default:
  39. return nil, errors.New("unknown balancing strategy: " + r.Strategy.Type)
  40. }
  41. settings := []byte("{}")
  42. if r.Strategy.Settings != nil {
  43. settings = ([]byte)(*r.Strategy.Settings)
  44. }
  45. rawConfig, err := strategyConfigLoader.LoadWithID(settings, r.Strategy.Type)
  46. if err != nil {
  47. return nil, errors.New("failed to parse to strategy config.").Base(err)
  48. }
  49. var ts proto.Message
  50. if builder, ok := rawConfig.(Buildable); ok {
  51. ts, err = builder.Build()
  52. if err != nil {
  53. return nil, err
  54. }
  55. }
  56. return &router.BalancingRule{
  57. Strategy: r.Strategy.Type,
  58. StrategySettings: serial.ToTypedMessage(ts),
  59. FallbackTag: r.FallbackTag,
  60. OutboundSelector: r.Selectors,
  61. Tag: r.Tag,
  62. }, nil
  63. }
  64. type RouterConfig struct {
  65. RuleList []json.RawMessage `json:"rules"`
  66. DomainStrategy *string `json:"domainStrategy"`
  67. Balancers []*BalancingRule `json:"balancers"`
  68. }
  69. func (c *RouterConfig) getDomainStrategy() router.Config_DomainStrategy {
  70. ds := ""
  71. if c.DomainStrategy != nil {
  72. ds = *c.DomainStrategy
  73. }
  74. switch strings.ToLower(ds) {
  75. case "alwaysip":
  76. return router.Config_UseIp
  77. case "ipifnonmatch":
  78. return router.Config_IpIfNonMatch
  79. case "ipondemand":
  80. return router.Config_IpOnDemand
  81. default:
  82. return router.Config_AsIs
  83. }
  84. }
  85. func (c *RouterConfig) Build() (*router.Config, error) {
  86. config := new(router.Config)
  87. config.DomainStrategy = c.getDomainStrategy()
  88. var rawRuleList []json.RawMessage
  89. if c != nil {
  90. rawRuleList = c.RuleList
  91. }
  92. for _, rawRule := range rawRuleList {
  93. rule, err := ParseRule(rawRule)
  94. if err != nil {
  95. return nil, err
  96. }
  97. config.Rule = append(config.Rule, rule)
  98. }
  99. for _, rawBalancer := range c.Balancers {
  100. balancer, err := rawBalancer.Build()
  101. if err != nil {
  102. return nil, err
  103. }
  104. config.BalancingRule = append(config.BalancingRule, balancer)
  105. }
  106. return config, nil
  107. }
  108. type RouterRule struct {
  109. RuleTag string `json:"ruleTag"`
  110. OutboundTag string `json:"outboundTag"`
  111. BalancerTag string `json:"balancerTag"`
  112. }
  113. func ParseIP(s string) (*router.CIDR, error) {
  114. var addr, mask string
  115. i := strings.Index(s, "/")
  116. if i < 0 {
  117. addr = s
  118. } else {
  119. addr = s[:i]
  120. mask = s[i+1:]
  121. }
  122. ip := net.ParseAddress(addr)
  123. switch ip.Family() {
  124. case net.AddressFamilyIPv4:
  125. bits := uint32(32)
  126. if len(mask) > 0 {
  127. bits64, err := strconv.ParseUint(mask, 10, 32)
  128. if err != nil {
  129. return nil, errors.New("invalid network mask for router: ", mask).Base(err)
  130. }
  131. bits = uint32(bits64)
  132. }
  133. if bits > 32 {
  134. return nil, errors.New("invalid network mask for router: ", bits)
  135. }
  136. return &router.CIDR{
  137. Ip: []byte(ip.IP()),
  138. Prefix: bits,
  139. }, nil
  140. case net.AddressFamilyIPv6:
  141. bits := uint32(128)
  142. if len(mask) > 0 {
  143. bits64, err := strconv.ParseUint(mask, 10, 32)
  144. if err != nil {
  145. return nil, errors.New("invalid network mask for router: ", mask).Base(err)
  146. }
  147. bits = uint32(bits64)
  148. }
  149. if bits > 128 {
  150. return nil, errors.New("invalid network mask for router: ", bits)
  151. }
  152. return &router.CIDR{
  153. Ip: []byte(ip.IP()),
  154. Prefix: bits,
  155. }, nil
  156. default:
  157. return nil, errors.New("unsupported address for router: ", s)
  158. }
  159. }
  160. func loadGeoIP(code string) ([]*router.CIDR, error) {
  161. return loadIP("geoip.dat", code)
  162. }
  163. var (
  164. FileCache = make(map[string][]byte)
  165. IPCache = make(map[string]*router.GeoIP)
  166. SiteCache = make(map[string]*router.GeoSite)
  167. )
  168. func loadFile(file string) ([]byte, error) {
  169. if FileCache[file] == nil {
  170. bs, err := filesystem.ReadAsset(file)
  171. if err != nil {
  172. return nil, errors.New("failed to open file: ", file).Base(err)
  173. }
  174. if len(bs) == 0 {
  175. return nil, errors.New("empty file: ", file)
  176. }
  177. // Do not cache file, may save RAM when there
  178. // are many files, but consume CPU each time.
  179. return bs, nil
  180. FileCache[file] = bs
  181. }
  182. return FileCache[file], nil
  183. }
  184. func loadIP(file, code string) ([]*router.CIDR, error) {
  185. index := file + ":" + code
  186. if IPCache[index] == nil {
  187. bs, err := loadFile(file)
  188. if err != nil {
  189. return nil, errors.New("failed to load file: ", file).Base(err)
  190. }
  191. bs = find(bs, []byte(code))
  192. if bs == nil {
  193. return nil, errors.New("code not found in ", file, ": ", code)
  194. }
  195. var geoip router.GeoIP
  196. if err := proto.Unmarshal(bs, &geoip); err != nil {
  197. return nil, errors.New("error unmarshal IP in ", file, ": ", code).Base(err)
  198. }
  199. defer runtime.GC() // or debug.FreeOSMemory()
  200. return geoip.Cidr, nil // do not cache geoip
  201. IPCache[index] = &geoip
  202. }
  203. return IPCache[index].Cidr, nil
  204. }
  205. func loadSite(file, code string) ([]*router.Domain, error) {
  206. index := file + ":" + code
  207. if SiteCache[index] == nil {
  208. bs, err := loadFile(file)
  209. if err != nil {
  210. return nil, errors.New("failed to load file: ", file).Base(err)
  211. }
  212. bs = find(bs, []byte(code))
  213. if bs == nil {
  214. return nil, errors.New("list not found in ", file, ": ", code)
  215. }
  216. var geosite router.GeoSite
  217. if err := proto.Unmarshal(bs, &geosite); err != nil {
  218. return nil, errors.New("error unmarshal Site in ", file, ": ", code).Base(err)
  219. }
  220. defer runtime.GC() // or debug.FreeOSMemory()
  221. return geosite.Domain, nil // do not cache geosite
  222. SiteCache[index] = &geosite
  223. }
  224. return SiteCache[index].Domain, nil
  225. }
  226. func DecodeVarint(buf []byte) (x uint64, n int) {
  227. for shift := uint(0); shift < 64; shift += 7 {
  228. if n >= len(buf) {
  229. return 0, 0
  230. }
  231. b := uint64(buf[n])
  232. n++
  233. x |= (b & 0x7F) << shift
  234. if (b & 0x80) == 0 {
  235. return x, n
  236. }
  237. }
  238. // The number is too large to represent in a 64-bit value.
  239. return 0, 0
  240. }
  241. func find(data, code []byte) []byte {
  242. codeL := len(code)
  243. if codeL == 0 {
  244. return nil
  245. }
  246. for {
  247. dataL := len(data)
  248. if dataL < 2 {
  249. return nil
  250. }
  251. x, y := DecodeVarint(data[1:])
  252. if x == 0 && y == 0 {
  253. return nil
  254. }
  255. headL, bodyL := 1+y, int(x)
  256. dataL -= headL
  257. if dataL < bodyL {
  258. return nil
  259. }
  260. data = data[headL:]
  261. if int(data[1]) == codeL {
  262. for i := 0; i < codeL && data[2+i] == code[i]; i++ {
  263. if i+1 == codeL {
  264. return data[:bodyL]
  265. }
  266. }
  267. }
  268. if dataL == bodyL {
  269. return nil
  270. }
  271. data = data[bodyL:]
  272. }
  273. }
  274. type AttributeMatcher interface {
  275. Match(*router.Domain) bool
  276. }
  277. type BooleanMatcher string
  278. func (m BooleanMatcher) Match(domain *router.Domain) bool {
  279. for _, attr := range domain.Attribute {
  280. if attr.Key == string(m) {
  281. return true
  282. }
  283. }
  284. return false
  285. }
  286. type AttributeList struct {
  287. matcher []AttributeMatcher
  288. }
  289. func (al *AttributeList) Match(domain *router.Domain) bool {
  290. for _, matcher := range al.matcher {
  291. if !matcher.Match(domain) {
  292. return false
  293. }
  294. }
  295. return true
  296. }
  297. func (al *AttributeList) IsEmpty() bool {
  298. return len(al.matcher) == 0
  299. }
  300. func parseAttrs(attrs []string) *AttributeList {
  301. al := new(AttributeList)
  302. for _, attr := range attrs {
  303. lc := strings.ToLower(attr)
  304. al.matcher = append(al.matcher, BooleanMatcher(lc))
  305. }
  306. return al
  307. }
  308. func loadGeositeWithAttr(file string, siteWithAttr string) ([]*router.Domain, error) {
  309. parts := strings.Split(siteWithAttr, "@")
  310. if len(parts) == 0 {
  311. return nil, errors.New("empty site")
  312. }
  313. country := strings.ToUpper(parts[0])
  314. attrs := parseAttrs(parts[1:])
  315. domains, err := loadSite(file, country)
  316. if err != nil {
  317. return nil, err
  318. }
  319. if attrs.IsEmpty() {
  320. return domains, nil
  321. }
  322. filteredDomains := make([]*router.Domain, 0, len(domains))
  323. for _, domain := range domains {
  324. if attrs.Match(domain) {
  325. filteredDomains = append(filteredDomains, domain)
  326. }
  327. }
  328. return filteredDomains, nil
  329. }
  330. func parseDomainRule(domain string) ([]*router.Domain, error) {
  331. if strings.HasPrefix(domain, "geosite:") {
  332. country := strings.ToUpper(domain[8:])
  333. domains, err := loadGeositeWithAttr("geosite.dat", country)
  334. if err != nil {
  335. return nil, errors.New("failed to load geosite: ", country).Base(err)
  336. }
  337. return domains, nil
  338. }
  339. isExtDatFile := 0
  340. {
  341. const prefix = "ext:"
  342. if strings.HasPrefix(domain, prefix) {
  343. isExtDatFile = len(prefix)
  344. }
  345. const prefixQualified = "ext-domain:"
  346. if strings.HasPrefix(domain, prefixQualified) {
  347. isExtDatFile = len(prefixQualified)
  348. }
  349. }
  350. if isExtDatFile != 0 {
  351. kv := strings.Split(domain[isExtDatFile:], ":")
  352. if len(kv) != 2 {
  353. return nil, errors.New("invalid external resource: ", domain)
  354. }
  355. filename := kv[0]
  356. country := kv[1]
  357. domains, err := loadGeositeWithAttr(filename, country)
  358. if err != nil {
  359. return nil, errors.New("failed to load external sites: ", country, " from ", filename).Base(err)
  360. }
  361. return domains, nil
  362. }
  363. domainRule := new(router.Domain)
  364. switch {
  365. case strings.HasPrefix(domain, "regexp:"):
  366. domainRule.Type = router.Domain_Regex
  367. domainRule.Value = domain[7:]
  368. case strings.HasPrefix(domain, "domain:"):
  369. domainRule.Type = router.Domain_Domain
  370. domainRule.Value = domain[7:]
  371. case strings.HasPrefix(domain, "full:"):
  372. domainRule.Type = router.Domain_Full
  373. domainRule.Value = domain[5:]
  374. case strings.HasPrefix(domain, "keyword:"):
  375. domainRule.Type = router.Domain_Plain
  376. domainRule.Value = domain[8:]
  377. case strings.HasPrefix(domain, "dotless:"):
  378. domainRule.Type = router.Domain_Regex
  379. switch substr := domain[8:]; {
  380. case substr == "":
  381. domainRule.Value = "^[^.]*$"
  382. case !strings.Contains(substr, "."):
  383. domainRule.Value = "^[^.]*" + substr + "[^.]*$"
  384. default:
  385. return nil, errors.New("substr in dotless rule should not contain a dot: ", substr)
  386. }
  387. default:
  388. domainRule.Type = router.Domain_Plain
  389. domainRule.Value = domain
  390. }
  391. return []*router.Domain{domainRule}, nil
  392. }
  393. func ToCidrList(ips StringList) ([]*router.GeoIP, error) {
  394. var geoipList []*router.GeoIP
  395. var customCidrs []*router.CIDR
  396. for _, ip := range ips {
  397. if strings.HasPrefix(ip, "geoip:") {
  398. country := ip[6:]
  399. isReverseMatch := false
  400. if strings.HasPrefix(ip, "geoip:!") {
  401. country = ip[7:]
  402. isReverseMatch = true
  403. }
  404. if len(country) == 0 {
  405. return nil, errors.New("empty country name in rule")
  406. }
  407. geoip, err := loadGeoIP(strings.ToUpper(country))
  408. if err != nil {
  409. return nil, errors.New("failed to load GeoIP: ", country).Base(err)
  410. }
  411. geoipList = append(geoipList, &router.GeoIP{
  412. CountryCode: strings.ToUpper(country),
  413. Cidr: geoip,
  414. ReverseMatch: isReverseMatch,
  415. })
  416. continue
  417. }
  418. isExtDatFile := 0
  419. {
  420. const prefix = "ext:"
  421. if strings.HasPrefix(ip, prefix) {
  422. isExtDatFile = len(prefix)
  423. }
  424. const prefixQualified = "ext-ip:"
  425. if strings.HasPrefix(ip, prefixQualified) {
  426. isExtDatFile = len(prefixQualified)
  427. }
  428. }
  429. if isExtDatFile != 0 {
  430. kv := strings.Split(ip[isExtDatFile:], ":")
  431. if len(kv) != 2 {
  432. return nil, errors.New("invalid external resource: ", ip)
  433. }
  434. filename := kv[0]
  435. country := kv[1]
  436. if len(filename) == 0 || len(country) == 0 {
  437. return nil, errors.New("empty filename or empty country in rule")
  438. }
  439. isReverseMatch := false
  440. if strings.HasPrefix(country, "!") {
  441. country = country[1:]
  442. isReverseMatch = true
  443. }
  444. geoip, err := loadIP(filename, strings.ToUpper(country))
  445. if err != nil {
  446. return nil, errors.New("failed to load IPs: ", country, " from ", filename).Base(err)
  447. }
  448. geoipList = append(geoipList, &router.GeoIP{
  449. CountryCode: strings.ToUpper(filename + "_" + country),
  450. Cidr: geoip,
  451. ReverseMatch: isReverseMatch,
  452. })
  453. continue
  454. }
  455. ipRule, err := ParseIP(ip)
  456. if err != nil {
  457. return nil, errors.New("invalid IP: ", ip).Base(err)
  458. }
  459. customCidrs = append(customCidrs, ipRule)
  460. }
  461. if len(customCidrs) > 0 {
  462. geoipList = append(geoipList, &router.GeoIP{
  463. Cidr: customCidrs,
  464. })
  465. }
  466. return geoipList, nil
  467. }
  468. func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
  469. type RawFieldRule struct {
  470. RouterRule
  471. Domain *StringList `json:"domain"`
  472. Domains *StringList `json:"domains"`
  473. IP *StringList `json:"ip"`
  474. Port *PortList `json:"port"`
  475. Network *NetworkList `json:"network"`
  476. SourceIP *StringList `json:"sourceIP"`
  477. Source *StringList `json:"source"`
  478. SourcePort *PortList `json:"sourcePort"`
  479. User *StringList `json:"user"`
  480. VlessRoute *PortList `json:"vlessRoute"`
  481. InboundTag *StringList `json:"inboundTag"`
  482. Protocols *StringList `json:"protocol"`
  483. Attributes map[string]string `json:"attrs"`
  484. LocalIP *StringList `json:"localIP"`
  485. LocalPort *PortList `json:"localPort"`
  486. }
  487. rawFieldRule := new(RawFieldRule)
  488. err := json.Unmarshal(msg, rawFieldRule)
  489. if err != nil {
  490. return nil, err
  491. }
  492. rule := new(router.RoutingRule)
  493. rule.RuleTag = rawFieldRule.RuleTag
  494. switch {
  495. case len(rawFieldRule.OutboundTag) > 0:
  496. rule.TargetTag = &router.RoutingRule_Tag{
  497. Tag: rawFieldRule.OutboundTag,
  498. }
  499. case len(rawFieldRule.BalancerTag) > 0:
  500. rule.TargetTag = &router.RoutingRule_BalancingTag{
  501. BalancingTag: rawFieldRule.BalancerTag,
  502. }
  503. default:
  504. return nil, errors.New("neither outboundTag nor balancerTag is specified in routing rule")
  505. }
  506. if rawFieldRule.Domain != nil {
  507. for _, domain := range *rawFieldRule.Domain {
  508. rules, err := parseDomainRule(domain)
  509. if err != nil {
  510. return nil, errors.New("failed to parse domain rule: ", domain).Base(err)
  511. }
  512. rule.Domain = append(rule.Domain, rules...)
  513. }
  514. }
  515. if rawFieldRule.Domains != nil {
  516. for _, domain := range *rawFieldRule.Domains {
  517. rules, err := parseDomainRule(domain)
  518. if err != nil {
  519. return nil, errors.New("failed to parse domain rule: ", domain).Base(err)
  520. }
  521. rule.Domain = append(rule.Domain, rules...)
  522. }
  523. }
  524. if rawFieldRule.IP != nil {
  525. geoipList, err := ToCidrList(*rawFieldRule.IP)
  526. if err != nil {
  527. return nil, err
  528. }
  529. rule.Geoip = geoipList
  530. }
  531. if rawFieldRule.Port != nil {
  532. rule.PortList = rawFieldRule.Port.Build()
  533. }
  534. if rawFieldRule.Network != nil {
  535. rule.Networks = rawFieldRule.Network.Build()
  536. }
  537. if rawFieldRule.SourceIP == nil {
  538. rawFieldRule.SourceIP = rawFieldRule.Source
  539. }
  540. if rawFieldRule.SourceIP != nil {
  541. geoipList, err := ToCidrList(*rawFieldRule.SourceIP)
  542. if err != nil {
  543. return nil, err
  544. }
  545. rule.SourceGeoip = geoipList
  546. }
  547. if rawFieldRule.SourcePort != nil {
  548. rule.SourcePortList = rawFieldRule.SourcePort.Build()
  549. }
  550. if rawFieldRule.LocalIP != nil {
  551. geoipList, err := ToCidrList(*rawFieldRule.LocalIP)
  552. if err != nil {
  553. return nil, err
  554. }
  555. rule.LocalGeoip = geoipList
  556. }
  557. if rawFieldRule.LocalPort != nil {
  558. rule.LocalPortList = rawFieldRule.LocalPort.Build()
  559. }
  560. if rawFieldRule.User != nil {
  561. for _, s := range *rawFieldRule.User {
  562. rule.UserEmail = append(rule.UserEmail, s)
  563. }
  564. }
  565. if rawFieldRule.VlessRoute != nil {
  566. rule.VlessRouteList = rawFieldRule.VlessRoute.Build()
  567. }
  568. if rawFieldRule.InboundTag != nil {
  569. for _, s := range *rawFieldRule.InboundTag {
  570. rule.InboundTag = append(rule.InboundTag, s)
  571. }
  572. }
  573. if rawFieldRule.Protocols != nil {
  574. for _, s := range *rawFieldRule.Protocols {
  575. rule.Protocol = append(rule.Protocol, s)
  576. }
  577. }
  578. if len(rawFieldRule.Attributes) > 0 {
  579. rule.Attributes = rawFieldRule.Attributes
  580. }
  581. return rule, nil
  582. }
  583. func ParseRule(msg json.RawMessage) (*router.RoutingRule, error) {
  584. rawRule := new(RouterRule)
  585. err := json.Unmarshal(msg, rawRule)
  586. if err != nil {
  587. return nil, errors.New("invalid router rule").Base(err)
  588. }
  589. fieldrule, err := parseFieldRule(msg)
  590. if err != nil {
  591. return nil, errors.New("invalid field rule").Base(err)
  592. }
  593. return fieldrule, nil
  594. }