v2ray.go 12 KB

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