v2ray.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. outboundHandler, err := proxyregistry.CreateOutboundHandler(
  82. outbound.Protocol, vpoint.space, outbound.Settings, &proxy.OutboundHandlerMeta{
  83. Tag: outbound.Tag,
  84. Address: outbound.SendThrough.AsAddress(),
  85. StreamSettings: outbound.StreamSettings,
  86. })
  87. if err != nil {
  88. log.Error("Point: Failed to create detour outbound connection handler: ", err)
  89. return nil, err
  90. }
  91. if idx == 0 {
  92. outboundHandlerManager.SetDefaultHandler(outboundHandler)
  93. }
  94. if len(outbound.Tag) > 0 {
  95. outboundHandlerManager.SetHandler(outbound.Tag, outboundHandler)
  96. }
  97. }
  98. if err := vpoint.space.Initialize(); err != nil {
  99. return nil, err
  100. }
  101. return vpoint, nil
  102. }
  103. func (this *Point) Close() {
  104. for _, inbound := range this.inboundHandlers {
  105. inbound.Close()
  106. }
  107. }
  108. // Start starts the Point server, and return any error during the process.
  109. // In the case of any errors, the state of the server is unpredicatable.
  110. func (this *Point) Start() error {
  111. for _, inbound := range this.inboundHandlers {
  112. err := inbound.Start()
  113. if err != nil {
  114. return err
  115. }
  116. }
  117. log.Warning("V2Ray started.")
  118. return nil
  119. }
  120. func (this *Point) GetHandler(tag string) (proxy.InboundHandler, int) {
  121. handler, found := this.taggedInboundHandlers[tag]
  122. if !found {
  123. log.Warning("Point: Unable to find an inbound handler with tag: ", tag)
  124. return nil, 0
  125. }
  126. return handler.GetConnectionHandler()
  127. }
  128. func (this *Point) Release() {
  129. }