config_json.go 8.8 KB

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