router_config.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // +build json
  2. package rules
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "strings"
  7. router "github.com/v2ray/v2ray-core/app/router"
  8. "github.com/v2ray/v2ray-core/common/log"
  9. v2net "github.com/v2ray/v2ray-core/common/net"
  10. "github.com/v2ray/v2ray-core/common/serial"
  11. )
  12. type JsonRule struct {
  13. Type string `json:"type"`
  14. OutboundTag string `json:"outboundTag"`
  15. }
  16. func parseFieldRule(msg json.RawMessage) (*Rule, error) {
  17. type RawFieldRule struct {
  18. JsonRule
  19. Domain *serial.StringLiteralList `json:"domain"`
  20. IP *serial.StringLiteralList `json:"ip"`
  21. Port *v2net.PortRange `json:"port"`
  22. Network *v2net.NetworkList `json:"network"`
  23. }
  24. rawFieldRule := new(RawFieldRule)
  25. err := json.Unmarshal(msg, rawFieldRule)
  26. if err != nil {
  27. return nil, err
  28. }
  29. conds := NewConditionChan()
  30. if rawFieldRule.Domain != nil && rawFieldRule.Domain.Len() > 0 {
  31. for _, rawDomain := range *(rawFieldRule.Domain) {
  32. var matcher Condition
  33. if strings.HasPrefix(rawDomain.String(), "regexp:") {
  34. rawMatcher, err := NewRegexpDomainMatcher(rawDomain.String()[7:])
  35. if err != nil {
  36. return nil, err
  37. }
  38. matcher = rawMatcher
  39. } else {
  40. matcher = NewPlainDomainMatcher(rawDomain.String())
  41. }
  42. conds.Add(matcher)
  43. }
  44. }
  45. if rawFieldRule.IP != nil && rawFieldRule.IP.Len() > 0 {
  46. for _, ipStr := range *(rawFieldRule.IP) {
  47. cidrMatcher, err := NewCIDRMatcher(ipStr.String())
  48. if err != nil {
  49. log.Error("Router: Invalid IP range in router rule: ", err)
  50. return nil, err
  51. }
  52. conds.Add(cidrMatcher)
  53. }
  54. }
  55. if rawFieldRule.Port != nil {
  56. conds.Add(NewPortMatcher(*rawFieldRule.Port))
  57. }
  58. if rawFieldRule.Network != nil {
  59. conds.Add(NewNetworkMatcher(rawFieldRule.Network))
  60. }
  61. if conds.Len() == 0 {
  62. return nil, errors.New("Router: This rule has no effective fields.")
  63. }
  64. return &Rule{
  65. Tag: rawFieldRule.OutboundTag,
  66. Condition: conds,
  67. }, nil
  68. }
  69. func parseRule(msg json.RawMessage) *Rule {
  70. rawRule := new(JsonRule)
  71. err := json.Unmarshal(msg, rawRule)
  72. if err != nil {
  73. log.Error("Router: Invalid router rule: ", err)
  74. return nil
  75. }
  76. if rawRule.Type == "field" {
  77. fieldrule, err := parseFieldRule(msg)
  78. if err != nil {
  79. log.Error("Invalid field rule: ", err)
  80. return nil
  81. }
  82. return fieldrule
  83. }
  84. if rawRule.Type == "chinaip" {
  85. chinaiprule, err := parseChinaIPRule(msg)
  86. if err != nil {
  87. log.Error("Router: Invalid chinaip rule: ", err)
  88. return nil
  89. }
  90. return chinaiprule
  91. }
  92. if rawRule.Type == "chinasites" {
  93. chinasitesrule, err := parseChinaSitesRule(msg)
  94. if err != nil {
  95. log.Error("Invalid chinasites rule: ", err)
  96. return nil
  97. }
  98. return chinasitesrule
  99. }
  100. log.Error("Unknown router rule type: ", rawRule.Type)
  101. return nil
  102. }
  103. func init() {
  104. router.RegisterRouterConfig("rules", func(data []byte) (interface{}, error) {
  105. type JsonConfig struct {
  106. RuleList []json.RawMessage `json:"rules"`
  107. }
  108. jsonConfig := new(JsonConfig)
  109. if err := json.Unmarshal(data, jsonConfig); err != nil {
  110. return nil, err
  111. }
  112. config := NewRouterRuleConfig()
  113. for _, rawRule := range jsonConfig.RuleList {
  114. rule := parseRule(rawRule)
  115. config.Add(rule)
  116. }
  117. return config, nil
  118. })
  119. }