v2ray.go 9.8 KB

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