config_json.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // +build json
  2. package core
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "io"
  7. "v2ray.com/core/app/dns"
  8. "v2ray.com/core/app/router"
  9. "v2ray.com/core/common"
  10. "v2ray.com/core/common/log"
  11. v2net "v2ray.com/core/common/net"
  12. "v2ray.com/core/transport"
  13. "v2ray.com/core/transport/internet"
  14. )
  15. const (
  16. DefaultRefreshMinute = int(9999)
  17. )
  18. func (this *Config) UnmarshalJSON(data []byte) error {
  19. type JsonConfig struct {
  20. Port v2net.Port `json:"port"` // Port of this Point server.
  21. LogConfig *log.Config `json:"log"`
  22. RouterConfig *router.Config `json:"routing"`
  23. DNSConfig *dns.Config `json:"dns"`
  24. InboundConfig *InboundConnectionConfig `json:"inbound"`
  25. OutboundConfig *OutboundConnectionConfig `json:"outbound"`
  26. InboundDetours []*InboundDetourConfig `json:"inboundDetour"`
  27. OutboundDetours []*OutboundDetourConfig `json:"outboundDetour"`
  28. Transport *transport.Config `json:"transport"`
  29. }
  30. jsonConfig := new(JsonConfig)
  31. if err := json.Unmarshal(data, jsonConfig); err != nil {
  32. return errors.New("Point: Failed to parse config: " + err.Error())
  33. }
  34. this.Port = jsonConfig.Port
  35. this.LogConfig = jsonConfig.LogConfig
  36. this.RouterConfig = jsonConfig.RouterConfig
  37. if jsonConfig.InboundConfig == nil {
  38. return errors.New("Point: Inbound config is not specified.")
  39. }
  40. this.InboundConfig = jsonConfig.InboundConfig
  41. if jsonConfig.OutboundConfig == nil {
  42. return errors.New("Point: Outbound config is not specified.")
  43. }
  44. this.OutboundConfig = jsonConfig.OutboundConfig
  45. this.InboundDetours = jsonConfig.InboundDetours
  46. this.OutboundDetours = jsonConfig.OutboundDetours
  47. if jsonConfig.DNSConfig == nil {
  48. jsonConfig.DNSConfig = &dns.Config{
  49. NameServers: []*v2net.DestinationPB{{
  50. Network: v2net.Network_UDP,
  51. Address: &v2net.AddressPB{
  52. Address: &v2net.AddressPB_Domain{
  53. Domain: "localhost",
  54. },
  55. },
  56. Port: 53,
  57. }},
  58. }
  59. }
  60. this.DNSConfig = jsonConfig.DNSConfig
  61. this.TransportConfig = jsonConfig.Transport
  62. return nil
  63. }
  64. func (this *InboundConnectionConfig) UnmarshalJSON(data []byte) error {
  65. type JsonConfig struct {
  66. Port uint16 `json:"port"`
  67. Listen *v2net.AddressPB `json:"listen"`
  68. Protocol string `json:"protocol"`
  69. StreamSetting *internet.StreamConfig `json:"streamSettings"`
  70. Settings json.RawMessage `json:"settings"`
  71. AllowPassive bool `json:"allowPassive"`
  72. }
  73. jsonConfig := new(JsonConfig)
  74. if err := json.Unmarshal(data, jsonConfig); err != nil {
  75. return errors.New("Point: Failed to parse inbound config: " + err.Error())
  76. }
  77. this.Port = v2net.Port(jsonConfig.Port)
  78. this.ListenOn = v2net.AnyIP
  79. if jsonConfig.Listen != nil {
  80. if jsonConfig.Listen.AsAddress().Family().IsDomain() {
  81. return errors.New("Point: Unable to listen on domain address: " + jsonConfig.Listen.AsAddress().Domain())
  82. }
  83. this.ListenOn = jsonConfig.Listen.AsAddress()
  84. }
  85. if jsonConfig.StreamSetting != nil {
  86. this.StreamSettings = jsonConfig.StreamSetting
  87. }
  88. this.Protocol = jsonConfig.Protocol
  89. this.Settings = jsonConfig.Settings
  90. this.AllowPassiveConnection = jsonConfig.AllowPassive
  91. return nil
  92. }
  93. func (this *OutboundConnectionConfig) UnmarshalJSON(data []byte) error {
  94. type JsonConnectionConfig struct {
  95. Protocol string `json:"protocol"`
  96. SendThrough *v2net.AddressPB `json:"sendThrough"`
  97. StreamSetting *internet.StreamConfig `json:"streamSettings"`
  98. Settings json.RawMessage `json:"settings"`
  99. }
  100. jsonConfig := new(JsonConnectionConfig)
  101. if err := json.Unmarshal(data, jsonConfig); err != nil {
  102. return errors.New("Point: Failed to parse outbound config: " + err.Error())
  103. }
  104. this.Protocol = jsonConfig.Protocol
  105. this.Settings = jsonConfig.Settings
  106. if jsonConfig.SendThrough != nil {
  107. address := jsonConfig.SendThrough.AsAddress()
  108. if address.Family().IsDomain() {
  109. return errors.New("Point: Unable to send through: " + address.String())
  110. }
  111. this.SendThrough = address
  112. }
  113. if jsonConfig.StreamSetting != nil {
  114. this.StreamSettings = jsonConfig.StreamSetting
  115. }
  116. return nil
  117. }
  118. func (this *InboundDetourAllocationConfig) UnmarshalJSON(data []byte) error {
  119. type JsonInboundDetourAllocationConfig struct {
  120. Strategy string `json:"strategy"`
  121. Concurrency int `json:"concurrency"`
  122. RefreshMin int `json:"refresh"`
  123. }
  124. jsonConfig := new(JsonInboundDetourAllocationConfig)
  125. if err := json.Unmarshal(data, jsonConfig); err != nil {
  126. return errors.New("Point: Failed to parse inbound detour allocation config: " + err.Error())
  127. }
  128. this.Strategy = jsonConfig.Strategy
  129. this.Concurrency = jsonConfig.Concurrency
  130. this.Refresh = jsonConfig.RefreshMin
  131. if this.Strategy == AllocationStrategyRandom {
  132. if this.Refresh == 0 {
  133. this.Refresh = 5
  134. }
  135. if this.Concurrency == 0 {
  136. this.Concurrency = 3
  137. }
  138. }
  139. if this.Refresh == 0 {
  140. this.Refresh = DefaultRefreshMinute
  141. }
  142. return nil
  143. }
  144. func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
  145. type JsonInboundDetourConfig struct {
  146. Protocol string `json:"protocol"`
  147. PortRange *v2net.PortRange `json:"port"`
  148. ListenOn *v2net.AddressPB `json:"listen"`
  149. Settings json.RawMessage `json:"settings"`
  150. Tag string `json:"tag"`
  151. Allocation *InboundDetourAllocationConfig `json:"allocate"`
  152. StreamSetting *internet.StreamConfig `json:"streamSettings"`
  153. AllowPassive bool `json:"allowPassive"`
  154. }
  155. jsonConfig := new(JsonInboundDetourConfig)
  156. if err := json.Unmarshal(data, jsonConfig); err != nil {
  157. return errors.New("Point: Failed to parse inbound detour config: " + err.Error())
  158. }
  159. if jsonConfig.PortRange == nil {
  160. log.Error("Point: Port range not specified in InboundDetour.")
  161. return common.ErrBadConfiguration
  162. }
  163. this.ListenOn = v2net.AnyIP
  164. if jsonConfig.ListenOn != nil {
  165. if jsonConfig.ListenOn.AsAddress().Family().IsDomain() {
  166. return errors.New("Point: Unable to listen on domain address: " + jsonConfig.ListenOn.AsAddress().Domain())
  167. }
  168. this.ListenOn = jsonConfig.ListenOn.AsAddress()
  169. }
  170. this.Protocol = jsonConfig.Protocol
  171. this.PortRange = *jsonConfig.PortRange
  172. this.Settings = jsonConfig.Settings
  173. this.Tag = jsonConfig.Tag
  174. this.Allocation = jsonConfig.Allocation
  175. if this.Allocation == nil {
  176. this.Allocation = &InboundDetourAllocationConfig{
  177. Strategy: AllocationStrategyAlways,
  178. Refresh: DefaultRefreshMinute,
  179. }
  180. }
  181. if jsonConfig.StreamSetting != nil {
  182. this.StreamSettings = jsonConfig.StreamSetting
  183. }
  184. this.AllowPassiveConnection = jsonConfig.AllowPassive
  185. return nil
  186. }
  187. func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
  188. type JsonOutboundDetourConfig struct {
  189. Protocol string `json:"protocol"`
  190. SendThrough *v2net.AddressPB `json:"sendThrough"`
  191. Tag string `json:"tag"`
  192. Settings json.RawMessage `json:"settings"`
  193. StreamSetting *internet.StreamConfig `json:"streamSettings"`
  194. }
  195. jsonConfig := new(JsonOutboundDetourConfig)
  196. if err := json.Unmarshal(data, jsonConfig); err != nil {
  197. return errors.New("Point: Failed to parse outbound detour config: " + err.Error())
  198. }
  199. this.Protocol = jsonConfig.Protocol
  200. this.Tag = jsonConfig.Tag
  201. this.Settings = jsonConfig.Settings
  202. if jsonConfig.SendThrough != nil {
  203. address := jsonConfig.SendThrough.AsAddress()
  204. if address.Family().IsDomain() {
  205. return errors.New("Point: Unable to send through: " + address.String())
  206. }
  207. this.SendThrough = address
  208. }
  209. if jsonConfig.StreamSetting != nil {
  210. this.StreamSettings = jsonConfig.StreamSetting
  211. }
  212. return nil
  213. }
  214. func JsonLoadConfig(input io.Reader) (*Config, error) {
  215. jsonConfig := &Config{}
  216. decoder := json.NewDecoder(input)
  217. err := decoder.Decode(jsonConfig)
  218. if err != nil {
  219. log.Error("Point: Failed to load server config: ", err)
  220. return nil, err
  221. }
  222. return jsonConfig, err
  223. }
  224. func init() {
  225. configLoader = JsonLoadConfig
  226. }