v2ray.go 11 KB

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