v2ray.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package core
  2. import (
  3. "v2ray.com/core/app"
  4. "v2ray.com/core/app/dispatcher"
  5. dispatchers "v2ray.com/core/app/dispatcher/impl"
  6. "v2ray.com/core/app/dns"
  7. "v2ray.com/core/app/proxyman"
  8. "v2ray.com/core/app/router"
  9. "v2ray.com/core/common"
  10. "v2ray.com/core/common/log"
  11. "v2ray.com/core/proxy"
  12. proxyregistry "v2ray.com/core/proxy/registry"
  13. )
  14. // Point shell of V2Ray.
  15. type Point struct {
  16. inboundHandlers []InboundDetourHandler
  17. taggedInboundHandlers map[string]InboundDetourHandler
  18. outboundHandlers []proxy.OutboundHandler
  19. taggedOutboundHandlers map[string]proxy.OutboundHandler
  20. router *router.Router
  21. space app.Space
  22. }
  23. // NewPoint returns a new Point server based on given configuration.
  24. // The server is not started at this point.
  25. func NewPoint(pConfig *Config) (*Point, error) {
  26. var vpoint = new(Point)
  27. if err := pConfig.Transport.Apply(); err != nil {
  28. return nil, err
  29. }
  30. if err := pConfig.Log.Apply(); err != nil {
  31. return nil, err
  32. }
  33. vpoint.space = app.NewSpace()
  34. vpoint.space.BindApp(proxyman.APP_ID_INBOUND_MANAGER, vpoint)
  35. outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager()
  36. vpoint.space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager)
  37. dnsConfig := pConfig.Dns
  38. if dnsConfig != nil {
  39. dnsServer := dns.NewCacheServer(vpoint.space, dnsConfig)
  40. vpoint.space.BindApp(dns.APP_ID, dnsServer)
  41. }
  42. routerConfig := pConfig.Router
  43. if routerConfig != nil {
  44. r := router.NewRouter(routerConfig, vpoint.space)
  45. vpoint.space.BindApp(router.APP_ID, r)
  46. vpoint.router = r
  47. }
  48. vpoint.space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(vpoint.space))
  49. vpoint.inboundHandlers = make([]InboundDetourHandler, 8)
  50. vpoint.taggedInboundHandlers = make(map[string]InboundDetourHandler)
  51. for _, inbound := range pConfig.Inbound {
  52. allocConfig := inbound.GetAllocationStrategyValue()
  53. var inboundHandler InboundDetourHandler
  54. switch allocConfig.Type {
  55. case AllocationStrategy_Always:
  56. dh, err := NewInboundDetourHandlerAlways(vpoint.space, inbound)
  57. if err != nil {
  58. log.Error("Point: Failed to create detour handler: ", err)
  59. return nil, common.ErrBadConfiguration
  60. }
  61. inboundHandler = dh
  62. case AllocationStrategy_Random:
  63. dh, err := NewInboundDetourHandlerDynamic(vpoint.space, inbound)
  64. if err != nil {
  65. log.Error("Point: Failed to create detour handler: ", err)
  66. return nil, common.ErrBadConfiguration
  67. }
  68. inboundHandler = dh
  69. default:
  70. log.Error("Point: Unknown allocation strategy: ", allocConfig.Type)
  71. return nil, common.ErrBadConfiguration
  72. }
  73. vpoint.inboundHandlers = append(vpoint.inboundHandlers, inboundHandler)
  74. if len(inbound.Tag) > 0 {
  75. vpoint.taggedInboundHandlers[inbound.Tag] = inboundHandler
  76. }
  77. }
  78. vpoint.outboundHandlers = make([]proxy.OutboundHandler, 8)
  79. vpoint.taggedOutboundHandlers = make(map[string]proxy.OutboundHandler)
  80. for idx, outbound := range pConfig.Outbound {
  81. outboundSettings, err := outbound.GetTypedSettings()
  82. if err != nil {
  83. return nil, err
  84. }
  85. outboundHandler, err := proxyregistry.CreateOutboundHandler(
  86. outbound.Settings.Type, vpoint.space, outboundSettings, &proxy.OutboundHandlerMeta{
  87. Tag: outbound.Tag,
  88. Address: outbound.SendThrough.AsAddress(),
  89. StreamSettings: outbound.StreamSettings,
  90. })
  91. if err != nil {
  92. log.Error("Point: Failed to create detour outbound connection handler: ", err)
  93. return nil, err
  94. }
  95. if idx == 0 {
  96. outboundHandlerManager.SetDefaultHandler(outboundHandler)
  97. }
  98. if len(outbound.Tag) > 0 {
  99. outboundHandlerManager.SetHandler(outbound.Tag, outboundHandler)
  100. }
  101. }
  102. if err := vpoint.space.Initialize(); err != nil {
  103. return nil, err
  104. }
  105. return vpoint, nil
  106. }
  107. func (this *Point) Close() {
  108. for _, inbound := range this.inboundHandlers {
  109. inbound.Close()
  110. }
  111. }
  112. // Start starts the Point server, and return any error during the process.
  113. // In the case of any errors, the state of the server is unpredicatable.
  114. func (this *Point) Start() error {
  115. for _, inbound := range this.inboundHandlers {
  116. err := inbound.Start()
  117. if err != nil {
  118. return err
  119. }
  120. }
  121. log.Warning("V2Ray started.")
  122. return nil
  123. }
  124. func (this *Point) GetHandler(tag string) (proxy.InboundHandler, int) {
  125. handler, found := this.taggedInboundHandlers[tag]
  126. if !found {
  127. log.Warning("Point: Unable to find an inbound handler with tag: ", tag)
  128. return nil, 0
  129. }
  130. return handler.GetConnectionHandler()
  131. }
  132. func (this *Point) Release() {
  133. }