v2ray.go 11 KB

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