v2ray.go 9.7 KB

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