config_json.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. this.DNSConfig = jsonConfig.DNSConfig
  39. return nil
  40. }
  41. func (this *ConnectionConfig) UnmarshalJSON(data []byte) error {
  42. type JsonConnectionConfig struct {
  43. Protocol string `json:"protocol"`
  44. Settings json.RawMessage `json:"settings"`
  45. }
  46. jsonConfig := new(JsonConnectionConfig)
  47. if err := json.Unmarshal(data, jsonConfig); err != nil {
  48. return err
  49. }
  50. this.Protocol = jsonConfig.Protocol
  51. this.Settings = jsonConfig.Settings
  52. return nil
  53. }
  54. func (this *LogConfig) UnmarshalJSON(data []byte) error {
  55. type JsonLogConfig struct {
  56. AccessLog string `json:"access"`
  57. ErrorLog string `json:"error"`
  58. LogLevel string `json:"loglevel"`
  59. }
  60. jsonConfig := new(JsonLogConfig)
  61. if err := json.Unmarshal(data, jsonConfig); err != nil {
  62. return err
  63. }
  64. this.AccessLog = jsonConfig.AccessLog
  65. this.ErrorLog = jsonConfig.ErrorLog
  66. level := strings.ToLower(jsonConfig.LogLevel)
  67. switch level {
  68. case "debug":
  69. this.LogLevel = log.DebugLevel
  70. case "info":
  71. this.LogLevel = log.InfoLevel
  72. case "error":
  73. this.LogLevel = log.ErrorLevel
  74. case "none":
  75. this.LogLevel = log.NoneLevel
  76. default:
  77. this.LogLevel = log.WarningLevel
  78. }
  79. return nil
  80. }
  81. func (this *InboundDetourAllocationConfig) UnmarshalJSON(data []byte) error {
  82. type JsonInboundDetourAllocationConfig struct {
  83. Strategy string `json:"strategy"`
  84. Concurrency int `json:"concurrency"`
  85. RefreshMin int `json:"refresh"`
  86. }
  87. jsonConfig := new(JsonInboundDetourAllocationConfig)
  88. if err := json.Unmarshal(data, jsonConfig); err != nil {
  89. return err
  90. }
  91. this.Strategy = jsonConfig.Strategy
  92. this.Concurrency = jsonConfig.Concurrency
  93. this.Refresh = jsonConfig.RefreshMin
  94. if this.Strategy == AllocationStrategyRandom {
  95. if this.Refresh == 0 {
  96. this.Refresh = 5
  97. }
  98. if this.Concurrency == 0 {
  99. this.Concurrency = 3
  100. }
  101. }
  102. if this.Refresh == 0 {
  103. this.Refresh = DefaultRefreshMinute
  104. }
  105. return nil
  106. }
  107. func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
  108. type JsonInboundDetourConfig struct {
  109. Protocol string `json:"protocol"`
  110. PortRange *v2net.PortRange `json:"port"`
  111. Settings json.RawMessage `json:"settings"`
  112. Tag string `json:"tag"`
  113. Allocation *InboundDetourAllocationConfig `json:"allocate"`
  114. }
  115. jsonConfig := new(JsonInboundDetourConfig)
  116. if err := json.Unmarshal(data, jsonConfig); err != nil {
  117. return err
  118. }
  119. if jsonConfig.PortRange == nil {
  120. log.Error("Point: Port range not specified in InboundDetour.")
  121. return ErrorBadConfiguration
  122. }
  123. this.Protocol = jsonConfig.Protocol
  124. this.PortRange = *jsonConfig.PortRange
  125. this.Settings = jsonConfig.Settings
  126. this.Tag = jsonConfig.Tag
  127. this.Allocation = jsonConfig.Allocation
  128. if this.Allocation == nil {
  129. this.Allocation = &InboundDetourAllocationConfig{
  130. Strategy: AllocationStrategyAlways,
  131. Refresh: DefaultRefreshMinute,
  132. }
  133. }
  134. return nil
  135. }
  136. func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
  137. type JsonOutboundDetourConfig struct {
  138. Protocol string `json:"protocol"`
  139. Tag string `json:"tag"`
  140. Settings json.RawMessage `json:"settings"`
  141. }
  142. jsonConfig := new(JsonOutboundDetourConfig)
  143. if err := json.Unmarshal(data, jsonConfig); err != nil {
  144. return err
  145. }
  146. this.Protocol = jsonConfig.Protocol
  147. this.Tag = jsonConfig.Tag
  148. this.Settings = jsonConfig.Settings
  149. return nil
  150. }
  151. func JsonLoadConfig(file string) (*Config, error) {
  152. fixedFile := os.ExpandEnv(file)
  153. rawConfig, err := ioutil.ReadFile(fixedFile)
  154. if err != nil {
  155. log.Error("Failed to read server config file (", file, "): ", file, err)
  156. return nil, err
  157. }
  158. jsonConfig := &Config{}
  159. err = json.Unmarshal(rawConfig, jsonConfig)
  160. if err != nil {
  161. log.Error("Failed to load server config: ", err)
  162. return nil, err
  163. }
  164. return jsonConfig, err
  165. }
  166. func init() {
  167. configLoader = JsonLoadConfig
  168. }