config_json.go 4.9 KB

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