config_json.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. "github.com/v2ray/v2ray-core/transport/internet"
  15. )
  16. const (
  17. DefaultRefreshMinute = int(9999)
  18. )
  19. func (this *Config) UnmarshalJSON(data []byte) error {
  20. type JsonConfig struct {
  21. Port v2net.Port `json:"port"` // Port of this Point server.
  22. LogConfig *LogConfig `json:"log"`
  23. RouterConfig *router.Config `json:"routing"`
  24. DNSConfig *dns.Config `json:"dns"`
  25. InboundConfig *InboundConnectionConfig `json:"inbound"`
  26. OutboundConfig *OutboundConnectionConfig `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 errors.New("Point: Failed to parse config: " + err.Error())
  34. }
  35. this.Port = jsonConfig.Port
  36. this.LogConfig = jsonConfig.LogConfig
  37. this.RouterConfig = jsonConfig.RouterConfig
  38. if jsonConfig.InboundConfig == nil {
  39. return errors.New("Point: Inbound config is not specified.")
  40. }
  41. this.InboundConfig = jsonConfig.InboundConfig
  42. if jsonConfig.OutboundConfig == nil {
  43. return errors.New("Point: Outbound config is not specified.")
  44. }
  45. this.OutboundConfig = jsonConfig.OutboundConfig
  46. this.InboundDetours = jsonConfig.InboundDetours
  47. this.OutboundDetours = jsonConfig.OutboundDetours
  48. if jsonConfig.DNSConfig == nil {
  49. jsonConfig.DNSConfig = &dns.Config{
  50. NameServers: []v2net.Destination{
  51. v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
  52. },
  53. }
  54. }
  55. this.DNSConfig = jsonConfig.DNSConfig
  56. this.TransportConfig = jsonConfig.Transport
  57. return nil
  58. }
  59. func (this *InboundConnectionConfig) UnmarshalJSON(data []byte) error {
  60. type JsonConfig struct {
  61. Port uint16 `json:"port"`
  62. Listen *v2net.AddressJson `json:"listen"`
  63. Protocol string `json:"protocol"`
  64. StreamSetting *internet.StreamSettings `json:"streamSettings"`
  65. Settings json.RawMessage `json:"settings"`
  66. }
  67. jsonConfig := new(JsonConfig)
  68. if err := json.Unmarshal(data, jsonConfig); err != nil {
  69. return errors.New("Point: Failed to parse inbound config: " + err.Error())
  70. }
  71. this.Port = v2net.Port(jsonConfig.Port)
  72. this.ListenOn = v2net.AnyIP
  73. if jsonConfig.Listen != nil {
  74. if jsonConfig.Listen.Address.IsDomain() {
  75. return errors.New("Point: Unable to listen on domain address: " + jsonConfig.Listen.Address.Domain())
  76. }
  77. this.ListenOn = jsonConfig.Listen.Address
  78. }
  79. if jsonConfig.StreamSetting != nil {
  80. this.StreamSettings = jsonConfig.StreamSetting
  81. }
  82. this.Protocol = jsonConfig.Protocol
  83. this.Settings = jsonConfig.Settings
  84. return nil
  85. }
  86. func (this *OutboundConnectionConfig) UnmarshalJSON(data []byte) error {
  87. type JsonConnectionConfig struct {
  88. Protocol string `json:"protocol"`
  89. SendThrough *v2net.AddressJson `json:"sendThrough"`
  90. StreamSetting *internet.StreamSettings `json:"streamSettings"`
  91. Settings json.RawMessage `json:"settings"`
  92. }
  93. jsonConfig := new(JsonConnectionConfig)
  94. if err := json.Unmarshal(data, jsonConfig); err != nil {
  95. return errors.New("Point: Failed to parse outbound config: " + err.Error())
  96. }
  97. this.Protocol = jsonConfig.Protocol
  98. this.Settings = jsonConfig.Settings
  99. if jsonConfig.SendThrough != nil {
  100. address := jsonConfig.SendThrough.Address
  101. if address.IsDomain() {
  102. return errors.New("Point: Unable to send through: " + address.String())
  103. }
  104. this.SendThrough = address
  105. }
  106. if jsonConfig.StreamSetting != nil {
  107. this.StreamSettings = jsonConfig.StreamSetting
  108. }
  109. return nil
  110. }
  111. func (this *LogConfig) UnmarshalJSON(data []byte) error {
  112. type JsonLogConfig struct {
  113. AccessLog string `json:"access"`
  114. ErrorLog string `json:"error"`
  115. LogLevel string `json:"loglevel"`
  116. }
  117. jsonConfig := new(JsonLogConfig)
  118. if err := json.Unmarshal(data, jsonConfig); err != nil {
  119. return errors.New("Point: Failed to parse log config: " + err.Error())
  120. }
  121. this.AccessLog = jsonConfig.AccessLog
  122. this.ErrorLog = jsonConfig.ErrorLog
  123. level := strings.ToLower(jsonConfig.LogLevel)
  124. switch level {
  125. case "debug":
  126. this.LogLevel = log.DebugLevel
  127. case "info":
  128. this.LogLevel = log.InfoLevel
  129. case "error":
  130. this.LogLevel = log.ErrorLevel
  131. case "none":
  132. this.LogLevel = log.NoneLevel
  133. default:
  134. this.LogLevel = log.WarningLevel
  135. }
  136. return nil
  137. }
  138. func (this *InboundDetourAllocationConfig) UnmarshalJSON(data []byte) error {
  139. type JsonInboundDetourAllocationConfig struct {
  140. Strategy string `json:"strategy"`
  141. Concurrency int `json:"concurrency"`
  142. RefreshMin int `json:"refresh"`
  143. }
  144. jsonConfig := new(JsonInboundDetourAllocationConfig)
  145. if err := json.Unmarshal(data, jsonConfig); err != nil {
  146. return errors.New("Point: Failed to parse inbound detour allocation config: " + err.Error())
  147. }
  148. this.Strategy = jsonConfig.Strategy
  149. this.Concurrency = jsonConfig.Concurrency
  150. this.Refresh = jsonConfig.RefreshMin
  151. if this.Strategy == AllocationStrategyRandom {
  152. if this.Refresh == 0 {
  153. this.Refresh = 5
  154. }
  155. if this.Concurrency == 0 {
  156. this.Concurrency = 3
  157. }
  158. }
  159. if this.Refresh == 0 {
  160. this.Refresh = DefaultRefreshMinute
  161. }
  162. return nil
  163. }
  164. func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
  165. type JsonInboundDetourConfig struct {
  166. Protocol string `json:"protocol"`
  167. PortRange *v2net.PortRange `json:"port"`
  168. ListenOn *v2net.AddressJson `json:"listen"`
  169. Settings json.RawMessage `json:"settings"`
  170. Tag string `json:"tag"`
  171. Allocation *InboundDetourAllocationConfig `json:"allocate"`
  172. StreamSetting *internet.StreamSettings `json:"streamSettings"`
  173. }
  174. jsonConfig := new(JsonInboundDetourConfig)
  175. if err := json.Unmarshal(data, jsonConfig); err != nil {
  176. return errors.New("Point: Failed to parse inbound detour config: " + err.Error())
  177. }
  178. if jsonConfig.PortRange == nil {
  179. log.Error("Point: Port range not specified in InboundDetour.")
  180. return ErrorBadConfiguration
  181. }
  182. this.ListenOn = v2net.AnyIP
  183. if jsonConfig.ListenOn != nil {
  184. if jsonConfig.ListenOn.Address.IsDomain() {
  185. return errors.New("Point: Unable to listen on domain address: " + jsonConfig.ListenOn.Address.Domain())
  186. }
  187. this.ListenOn = jsonConfig.ListenOn.Address
  188. }
  189. this.Protocol = jsonConfig.Protocol
  190. this.PortRange = *jsonConfig.PortRange
  191. this.Settings = jsonConfig.Settings
  192. this.Tag = jsonConfig.Tag
  193. this.Allocation = jsonConfig.Allocation
  194. if this.Allocation == nil {
  195. this.Allocation = &InboundDetourAllocationConfig{
  196. Strategy: AllocationStrategyAlways,
  197. Refresh: DefaultRefreshMinute,
  198. }
  199. }
  200. if jsonConfig.StreamSetting != nil {
  201. this.StreamSettings = jsonConfig.StreamSetting
  202. }
  203. return nil
  204. }
  205. func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
  206. type JsonOutboundDetourConfig struct {
  207. Protocol string `json:"protocol"`
  208. SendThrough *v2net.AddressJson `json:"sendThrough"`
  209. Tag string `json:"tag"`
  210. Settings json.RawMessage `json:"settings"`
  211. StreamSetting *internet.StreamSettings `json:"streamSettings"`
  212. }
  213. jsonConfig := new(JsonOutboundDetourConfig)
  214. if err := json.Unmarshal(data, jsonConfig); err != nil {
  215. return errors.New("Point: Failed to parse outbound detour config: " + err.Error())
  216. }
  217. this.Protocol = jsonConfig.Protocol
  218. this.Tag = jsonConfig.Tag
  219. this.Settings = jsonConfig.Settings
  220. if jsonConfig.SendThrough != nil {
  221. address := jsonConfig.SendThrough.Address
  222. if address.IsDomain() {
  223. return errors.New("Point: Unable to send through: " + address.String())
  224. }
  225. this.SendThrough = address
  226. }
  227. if jsonConfig.StreamSetting != nil {
  228. this.StreamSettings = jsonConfig.StreamSetting
  229. }
  230. return nil
  231. }
  232. func JsonLoadConfig(file string) (*Config, error) {
  233. fixedFile := os.ExpandEnv(file)
  234. rawConfig, err := ioutil.ReadFile(fixedFile)
  235. if err != nil {
  236. log.Error("Failed to read server config file (", file, "): ", file, err)
  237. return nil, err
  238. }
  239. jsonConfig := &Config{}
  240. err = json.Unmarshal(rawConfig, jsonConfig)
  241. if err != nil {
  242. log.Error("Failed to load server config: ", err)
  243. return nil, err
  244. }
  245. return jsonConfig, err
  246. }
  247. func init() {
  248. configLoader = JsonLoadConfig
  249. }