v2ray.go 8.9 KB

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