config_json.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. default:
  72. this.LogLevel = log.WarningLevel
  73. }
  74. return nil
  75. }
  76. func (this *InboundDetourAllocationConfig) UnmarshalJSON(data []byte) error {
  77. type JsonInboundDetourAllocationConfig struct {
  78. Strategy string `json:"strategy"`
  79. Concurrency int `json:"concurrency"`
  80. RefreshMin int `json:"refresh"`
  81. }
  82. jsonConfig := new(JsonInboundDetourAllocationConfig)
  83. if err := json.Unmarshal(data, jsonConfig); err != nil {
  84. return err
  85. }
  86. this.Strategy = jsonConfig.Strategy
  87. this.Concurrency = jsonConfig.Concurrency
  88. this.Refresh = jsonConfig.RefreshMin
  89. if this.Refresh == 0 {
  90. this.Refresh = DefaultRefreshMinute
  91. }
  92. return nil
  93. }
  94. func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
  95. type JsonInboundDetourConfig struct {
  96. Protocol string `json:"protocol"`
  97. PortRange *v2net.PortRange `json:"port"`
  98. Settings json.RawMessage `json:"settings"`
  99. Tag string `json:"tag"`
  100. Allocation *InboundDetourAllocationConfig `json:"allocate"`
  101. }
  102. jsonConfig := new(JsonInboundDetourConfig)
  103. if err := json.Unmarshal(data, jsonConfig); err != nil {
  104. return err
  105. }
  106. if jsonConfig.PortRange == nil {
  107. log.Error("Point: Port range not specified in InboundDetour.")
  108. return ErrorBadConfiguration
  109. }
  110. this.Protocol = jsonConfig.Protocol
  111. this.PortRange = *jsonConfig.PortRange
  112. this.Settings = jsonConfig.Settings
  113. this.Tag = jsonConfig.Tag
  114. this.Allocation = jsonConfig.Allocation
  115. if this.Allocation == nil {
  116. this.Allocation = &InboundDetourAllocationConfig{
  117. Strategy: AllocationStrategyAlways,
  118. Refresh: DefaultRefreshMinute,
  119. }
  120. }
  121. if this.Allocation.Strategy == AllocationStrategyRandom {
  122. if this.Allocation.Refresh == DefaultRefreshMinute {
  123. this.Allocation.Refresh = 5
  124. }
  125. if this.Allocation.Concurrency == 0 {
  126. this.Allocation.Concurrency = 3
  127. }
  128. }
  129. return nil
  130. }
  131. func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
  132. type JsonOutboundDetourConfig struct {
  133. Protocol string `json:"protocol"`
  134. Tag string `json:"tag"`
  135. Settings json.RawMessage `json:"settings"`
  136. }
  137. jsonConfig := new(JsonOutboundDetourConfig)
  138. if err := json.Unmarshal(data, jsonConfig); err != nil {
  139. return err
  140. }
  141. this.Protocol = jsonConfig.Protocol
  142. this.Tag = jsonConfig.Tag
  143. this.Settings = jsonConfig.Settings
  144. return nil
  145. }
  146. func JsonLoadConfig(file string) (*Config, error) {
  147. fixedFile := os.ExpandEnv(file)
  148. rawConfig, err := ioutil.ReadFile(fixedFile)
  149. if err != nil {
  150. log.Error("Failed to read server config file (", file, "): ", file, err)
  151. return nil, err
  152. }
  153. jsonConfig := &Config{}
  154. err = json.Unmarshal(rawConfig, jsonConfig)
  155. if err != nil {
  156. log.Error("Failed to load server config: ", err)
  157. return nil, err
  158. }
  159. return jsonConfig, err
  160. }
  161. func init() {
  162. configLoader = JsonLoadConfig
  163. }