config_json.go 4.4 KB

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