v2ray.go 11 KB

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