config_json.go 8.8 KB

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