config_json.go 6.0 KB

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