config_json.go 5.9 KB

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