config_json.go 8.7 KB

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