v2ray.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. package conf
  2. import (
  3. "encoding/json"
  4. "io"
  5. "strings"
  6. "v2ray.com/core"
  7. "v2ray.com/core/common/errors"
  8. "v2ray.com/core/common/loader"
  9. v2net "v2ray.com/core/common/net"
  10. )
  11. var (
  12. inboundConfigLoader = NewJSONConfigLoader(ConfigCreatorCache{
  13. "dokodemo-door": func() interface{} { return new(DokodemoConfig) },
  14. "http": func() interface{} { return new(HttpServerConfig) },
  15. "shadowsocks": func() interface{} { return new(ShadowsocksServerConfig) },
  16. "socks": func() interface{} { return new(SocksServerConfig) },
  17. "vmess": func() interface{} { return new(VMessInboundConfig) },
  18. }, "protocol", "settings")
  19. outboundConfigLoader = NewJSONConfigLoader(ConfigCreatorCache{
  20. "blackhole": func() interface{} { return new(BlackholeConfig) },
  21. "freedom": func() interface{} { return new(FreedomConfig) },
  22. "shadowsocks": func() interface{} { return new(ShadowsocksClientConfig) },
  23. "vmess": func() interface{} { return new(VMessOutboundConfig) },
  24. }, "protocol", "settings")
  25. )
  26. type InboundConnectionConfig struct {
  27. Port uint16 `json:"port"`
  28. Listen *Address `json:"listen"`
  29. Protocol string `json:"protocol"`
  30. StreamSetting *StreamConfig `json:"streamSettings"`
  31. Settings json.RawMessage `json:"settings"`
  32. AllowPassive bool `json:"allowPassive"`
  33. Tag string `json:"tag"`
  34. }
  35. func (v *InboundConnectionConfig) Build() (*core.InboundConnectionConfig, error) {
  36. config := new(core.InboundConnectionConfig)
  37. config.PortRange = &v2net.PortRange{
  38. From: uint32(v.Port),
  39. To: uint32(v.Port),
  40. }
  41. if v.Listen != nil {
  42. if v.Listen.Family().IsDomain() {
  43. return nil, errors.New("Point: Unable to listen on domain address: " + v.Listen.Domain())
  44. }
  45. config.ListenOn = v.Listen.Build()
  46. }
  47. if v.StreamSetting != nil {
  48. ts, err := v.StreamSetting.Build()
  49. if err != nil {
  50. return nil, err
  51. }
  52. config.StreamSettings = ts
  53. }
  54. config.AllowPassiveConnection = v.AllowPassive
  55. jsonConfig, err := inboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
  56. if err != nil {
  57. return nil, errors.Base(err).Message("Failed to load inbound config.")
  58. }
  59. ts, err := jsonConfig.(Buildable).Build()
  60. if err != nil {
  61. return nil, err
  62. }
  63. config.Settings = ts
  64. if len(v.Tag) > 0 {
  65. config.Tag = v.Tag
  66. }
  67. return config, nil
  68. }
  69. type OutboundConnectionConfig struct {
  70. Protocol string `json:"protocol"`
  71. SendThrough *Address `json:"sendThrough"`
  72. StreamSetting *StreamConfig `json:"streamSettings"`
  73. ProxySettings *ProxyConfig `json:"proxySettings"`
  74. Settings json.RawMessage `json:"settings"`
  75. Tag string `json:"tag"`
  76. }
  77. func (v *OutboundConnectionConfig) Build() (*core.OutboundConnectionConfig, error) {
  78. config := new(core.OutboundConnectionConfig)
  79. rawConfig, err := outboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
  80. if err != nil {
  81. return nil, errors.Base(err).Message("Failed to parse outbound config.")
  82. }
  83. ts, err := rawConfig.(Buildable).Build()
  84. if err != nil {
  85. return nil, err
  86. }
  87. config.Settings = ts
  88. if v.SendThrough != nil {
  89. address := v.SendThrough
  90. if address.Family().IsDomain() {
  91. return nil, errors.New("Invalid sendThrough address: " + address.String())
  92. }
  93. config.SendThrough = address.Build()
  94. }
  95. if v.StreamSetting != nil {
  96. ss, err := v.StreamSetting.Build()
  97. if err != nil {
  98. return nil, err
  99. }
  100. config.StreamSettings = ss
  101. }
  102. if v.ProxySettings != nil {
  103. ps, err := v.ProxySettings.Build()
  104. if err != nil {
  105. return nil, errors.Base(err).Message("Invalid outbound proxy settings.")
  106. }
  107. config.ProxySettings = ps
  108. }
  109. if len(v.Tag) > 0 {
  110. config.Tag = v.Tag
  111. }
  112. return config, nil
  113. }
  114. type InboundDetourAllocationConfig struct {
  115. Strategy string `json:"strategy"`
  116. Concurrency *uint32 `json:"concurrency"`
  117. RefreshMin *uint32 `json:"refresh"`
  118. }
  119. func (v *InboundDetourAllocationConfig) Build() (*core.AllocationStrategy, error) {
  120. config := new(core.AllocationStrategy)
  121. switch strings.ToLower(v.Strategy) {
  122. case "always":
  123. config.Type = core.AllocationStrategy_Always
  124. case "random":
  125. config.Type = core.AllocationStrategy_Random
  126. case "external":
  127. config.Type = core.AllocationStrategy_External
  128. default:
  129. return nil, errors.New("Unknown allocation strategy: ", v.Strategy)
  130. }
  131. if v.Concurrency != nil {
  132. config.Concurrency = &core.AllocationStrategyConcurrency{
  133. Value: *v.Concurrency,
  134. }
  135. }
  136. if v.RefreshMin != nil {
  137. config.Refresh = &core.AllocationStrategyRefresh{
  138. Value: *v.RefreshMin,
  139. }
  140. }
  141. return config, nil
  142. }
  143. type InboundDetourConfig struct {
  144. Protocol string `json:"protocol"`
  145. PortRange *PortRange `json:"port"`
  146. ListenOn *Address `json:"listen"`
  147. Settings json.RawMessage `json:"settings"`
  148. Tag string `json:"tag"`
  149. Allocation *InboundDetourAllocationConfig `json:"allocate"`
  150. StreamSetting *StreamConfig `json:"streamSettings"`
  151. AllowPassive bool `json:"allowPassive"`
  152. }
  153. func (v *InboundDetourConfig) Build() (*core.InboundConnectionConfig, error) {
  154. config := new(core.InboundConnectionConfig)
  155. if v.PortRange == nil {
  156. return nil, errors.New("Port range not specified in InboundDetour.")
  157. }
  158. config.PortRange = v.PortRange.Build()
  159. if v.ListenOn != nil {
  160. if v.ListenOn.Family().IsDomain() {
  161. return nil, errors.New("Unable to listen on domain address: ", v.ListenOn.Domain())
  162. }
  163. config.ListenOn = v.ListenOn.Build()
  164. }
  165. config.Tag = v.Tag
  166. if v.Allocation != nil {
  167. as, err := v.Allocation.Build()
  168. if err != nil {
  169. return nil, err
  170. }
  171. config.AllocationStrategy = as
  172. }
  173. if v.StreamSetting != nil {
  174. ss, err := v.StreamSetting.Build()
  175. if err != nil {
  176. return nil, err
  177. }
  178. config.StreamSettings = ss
  179. }
  180. config.AllowPassiveConnection = v.AllowPassive
  181. rawConfig, err := inboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
  182. if err != nil {
  183. return nil, errors.Base(err).Message("Failed to load inbound detour config.")
  184. }
  185. ts, err := rawConfig.(Buildable).Build()
  186. if err != nil {
  187. return nil, err
  188. }
  189. config.Settings = ts
  190. return config, nil
  191. }
  192. type OutboundDetourConfig struct {
  193. Protocol string `json:"protocol"`
  194. SendThrough *Address `json:"sendThrough"`
  195. Tag string `json:"tag"`
  196. Settings json.RawMessage `json:"settings"`
  197. StreamSetting *StreamConfig `json:"streamSettings"`
  198. ProxySettings *ProxyConfig `json:"proxySettings"`
  199. }
  200. func (v *OutboundDetourConfig) Build() (*core.OutboundConnectionConfig, error) {
  201. config := new(core.OutboundConnectionConfig)
  202. config.Tag = v.Tag
  203. if v.SendThrough != nil {
  204. address := v.SendThrough
  205. if address.Family().IsDomain() {
  206. return nil, errors.New("Point: Unable to send through: " + address.String())
  207. }
  208. config.SendThrough = address.Build()
  209. }
  210. if v.StreamSetting != nil {
  211. ss, err := v.StreamSetting.Build()
  212. if err != nil {
  213. return nil, err
  214. }
  215. config.StreamSettings = ss
  216. }
  217. rawConfig, err := outboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
  218. if err != nil {
  219. return nil, errors.Base(err).Message("Failed to parse to outbound detour config.")
  220. }
  221. ts, err := rawConfig.(Buildable).Build()
  222. if err != nil {
  223. return nil, err
  224. }
  225. if v.ProxySettings != nil {
  226. ps, err := v.ProxySettings.Build()
  227. if err != nil {
  228. return nil, errors.Base(err).Message("Invalid outbound detour proxy settings.")
  229. }
  230. config.ProxySettings = ps
  231. }
  232. config.Settings = ts
  233. return config, nil
  234. }
  235. type Config struct {
  236. Port uint16 `json:"port"` // Port of this Point server.
  237. LogConfig *LogConfig `json:"log"`
  238. RouterConfig *RouterConfig `json:"routing"`
  239. DNSConfig *DnsConfig `json:"dns"`
  240. InboundConfig *InboundConnectionConfig `json:"inbound"`
  241. OutboundConfig *OutboundConnectionConfig `json:"outbound"`
  242. InboundDetours []InboundDetourConfig `json:"inboundDetour"`
  243. OutboundDetours []OutboundDetourConfig `json:"outboundDetour"`
  244. Transport *TransportConfig `json:"transport"`
  245. }
  246. func (v *Config) Build() (*core.Config, error) {
  247. config := new(core.Config)
  248. if v.LogConfig != nil {
  249. config.Log = v.LogConfig.Build()
  250. }
  251. if v.Transport != nil {
  252. ts, err := v.Transport.Build()
  253. if err != nil {
  254. return nil, err
  255. }
  256. config.Transport = ts
  257. }
  258. if v.RouterConfig != nil {
  259. routerConfig, err := v.RouterConfig.Build()
  260. if err != nil {
  261. return nil, err
  262. }
  263. config.App = append(config.App, loader.NewTypedSettings(routerConfig))
  264. }
  265. if v.DNSConfig != nil {
  266. config.App = append(config.App, loader.NewTypedSettings(v.DNSConfig.Build()))
  267. }
  268. if v.InboundConfig == nil {
  269. return nil, errors.New("No inbound config specified.")
  270. }
  271. if v.InboundConfig.Port == 0 && v.Port > 0 {
  272. v.InboundConfig.Port = v.Port
  273. }
  274. ic, err := v.InboundConfig.Build()
  275. if err != nil {
  276. return nil, err
  277. }
  278. config.Inbound = append(config.Inbound, ic)
  279. for _, rawInboundConfig := range v.InboundDetours {
  280. ic, err := rawInboundConfig.Build()
  281. if err != nil {
  282. return nil, err
  283. }
  284. config.Inbound = append(config.Inbound, ic)
  285. }
  286. oc, err := v.OutboundConfig.Build()
  287. if err != nil {
  288. return nil, err
  289. }
  290. config.Outbound = append(config.Outbound, oc)
  291. for _, rawOutboundConfig := range v.OutboundDetours {
  292. oc, err := rawOutboundConfig.Build()
  293. if err != nil {
  294. return nil, err
  295. }
  296. config.Outbound = append(config.Outbound, oc)
  297. }
  298. return config, nil
  299. }
  300. func init() {
  301. core.RegisterConfigLoader(core.ConfigFormat_JSON, func(input io.Reader) (*core.Config, error) {
  302. jsonConfig := &Config{}
  303. decoder := json.NewDecoder(input)
  304. err := decoder.Decode(jsonConfig)
  305. if err != nil {
  306. return nil, errors.Base(err).Message("Invalid V2Ray config.")
  307. }
  308. return jsonConfig.Build()
  309. })
  310. }