config_json.go 8.8 KB

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