v2ray.go 9.7 KB

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