config_json.go 8.3 KB

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