v2ray.go 9.2 KB

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