v2ray.go 11 KB

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