v2ray.go 11 KB

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