config_json.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // +build json
  2. package point
  3. import (
  4. "encoding/json"
  5. "io/ioutil"
  6. "os"
  7. "strings"
  8. "github.com/v2ray/v2ray-core/app/dns"
  9. "github.com/v2ray/v2ray-core/app/router"
  10. "github.com/v2ray/v2ray-core/common/log"
  11. v2net "github.com/v2ray/v2ray-core/common/net"
  12. )
  13. const (
  14. DefaultRefreshMinute = int(9999)
  15. )
  16. func (this *Config) UnmarshalJSON(data []byte) error {
  17. type JsonConfig struct {
  18. Port v2net.Port `json:"port"` // Port of this Point server.
  19. LogConfig *LogConfig `json:"log"`
  20. RouterConfig *router.Config `json:"routing"`
  21. DNSConfig *dns.Config `json:"dns"`
  22. InboundConfig *ConnectionConfig `json:"inbound"`
  23. OutboundConfig *ConnectionConfig `json:"outbound"`
  24. InboundDetours []*InboundDetourConfig `json:"inboundDetour"`
  25. OutboundDetours []*OutboundDetourConfig `json:"outboundDetour"`
  26. }
  27. jsonConfig := new(JsonConfig)
  28. if err := json.Unmarshal(data, jsonConfig); err != nil {
  29. return err
  30. }
  31. this.Port = jsonConfig.Port
  32. this.LogConfig = jsonConfig.LogConfig
  33. this.RouterConfig = jsonConfig.RouterConfig
  34. this.InboundConfig = jsonConfig.InboundConfig
  35. this.OutboundConfig = jsonConfig.OutboundConfig
  36. this.InboundDetours = jsonConfig.InboundDetours
  37. this.OutboundDetours = jsonConfig.OutboundDetours
  38. if jsonConfig.DNSConfig == nil {
  39. jsonConfig.DNSConfig = &dns.Config{
  40. NameServers: []v2net.Destination{
  41. v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
  42. },
  43. }
  44. }
  45. this.DNSConfig = jsonConfig.DNSConfig
  46. return nil
  47. }
  48. func (this *ConnectionConfig) UnmarshalJSON(data []byte) error {
  49. type JsonConnectionConfig struct {
  50. Protocol string `json:"protocol"`
  51. Settings json.RawMessage `json:"settings"`
  52. }
  53. jsonConfig := new(JsonConnectionConfig)
  54. if err := json.Unmarshal(data, jsonConfig); err != nil {
  55. return err
  56. }
  57. this.Protocol = jsonConfig.Protocol
  58. this.Settings = jsonConfig.Settings
  59. return nil
  60. }
  61. func (this *LogConfig) UnmarshalJSON(data []byte) error {
  62. type JsonLogConfig struct {
  63. AccessLog string `json:"access"`
  64. ErrorLog string `json:"error"`
  65. LogLevel string `json:"loglevel"`
  66. }
  67. jsonConfig := new(JsonLogConfig)
  68. if err := json.Unmarshal(data, jsonConfig); err != nil {
  69. return err
  70. }
  71. this.AccessLog = jsonConfig.AccessLog
  72. this.ErrorLog = jsonConfig.ErrorLog
  73. level := strings.ToLower(jsonConfig.LogLevel)
  74. switch level {
  75. case "debug":
  76. this.LogLevel = log.DebugLevel
  77. case "info":
  78. this.LogLevel = log.InfoLevel
  79. case "error":
  80. this.LogLevel = log.ErrorLevel
  81. case "none":
  82. this.LogLevel = log.NoneLevel
  83. default:
  84. this.LogLevel = log.WarningLevel
  85. }
  86. return nil
  87. }
  88. func (this *InboundDetourAllocationConfig) UnmarshalJSON(data []byte) error {
  89. type JsonInboundDetourAllocationConfig struct {
  90. Strategy string `json:"strategy"`
  91. Concurrency int `json:"concurrency"`
  92. RefreshMin int `json:"refresh"`
  93. }
  94. jsonConfig := new(JsonInboundDetourAllocationConfig)
  95. if err := json.Unmarshal(data, jsonConfig); err != nil {
  96. return err
  97. }
  98. this.Strategy = jsonConfig.Strategy
  99. this.Concurrency = jsonConfig.Concurrency
  100. this.Refresh = jsonConfig.RefreshMin
  101. if this.Strategy == AllocationStrategyRandom {
  102. if this.Refresh == 0 {
  103. this.Refresh = 5
  104. }
  105. if this.Concurrency == 0 {
  106. this.Concurrency = 3
  107. }
  108. }
  109. if this.Refresh == 0 {
  110. this.Refresh = DefaultRefreshMinute
  111. }
  112. return nil
  113. }
  114. func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
  115. type JsonInboundDetourConfig struct {
  116. Protocol string `json:"protocol"`
  117. PortRange *v2net.PortRange `json:"port"`
  118. Settings json.RawMessage `json:"settings"`
  119. Tag string `json:"tag"`
  120. Allocation *InboundDetourAllocationConfig `json:"allocate"`
  121. }
  122. jsonConfig := new(JsonInboundDetourConfig)
  123. if err := json.Unmarshal(data, jsonConfig); err != nil {
  124. return err
  125. }
  126. if jsonConfig.PortRange == nil {
  127. log.Error("Point: Port range not specified in InboundDetour.")
  128. return ErrorBadConfiguration
  129. }
  130. this.Protocol = jsonConfig.Protocol
  131. this.PortRange = *jsonConfig.PortRange
  132. this.Settings = jsonConfig.Settings
  133. this.Tag = jsonConfig.Tag
  134. this.Allocation = jsonConfig.Allocation
  135. if this.Allocation == nil {
  136. this.Allocation = &InboundDetourAllocationConfig{
  137. Strategy: AllocationStrategyAlways,
  138. Refresh: DefaultRefreshMinute,
  139. }
  140. }
  141. return nil
  142. }
  143. func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
  144. type JsonOutboundDetourConfig struct {
  145. Protocol string `json:"protocol"`
  146. Tag string `json:"tag"`
  147. Settings json.RawMessage `json:"settings"`
  148. }
  149. jsonConfig := new(JsonOutboundDetourConfig)
  150. if err := json.Unmarshal(data, jsonConfig); err != nil {
  151. return err
  152. }
  153. this.Protocol = jsonConfig.Protocol
  154. this.Tag = jsonConfig.Tag
  155. this.Settings = jsonConfig.Settings
  156. return nil
  157. }
  158. func JsonLoadConfig(file string) (*Config, error) {
  159. fixedFile := os.ExpandEnv(file)
  160. rawConfig, err := ioutil.ReadFile(fixedFile)
  161. if err != nil {
  162. log.Error("Failed to read server config file (", file, "): ", file, err)
  163. return nil, err
  164. }
  165. jsonConfig := &Config{}
  166. err = json.Unmarshal(rawConfig, jsonConfig)
  167. if err != nil {
  168. log.Error("Failed to load server config: ", err)
  169. return nil, err
  170. }
  171. return jsonConfig, err
  172. }
  173. func init() {
  174. configLoader = JsonLoadConfig
  175. }