point.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Package point is a shell of V2Ray to run on various of systems.
  2. // Point server is a full functionality proxying system. It consists of an inbound and an outbound
  3. // connection, as well as any number of inbound and outbound detours. It provides a way internally
  4. // to route network packets.
  5. package point
  6. import (
  7. "github.com/v2ray/v2ray-core/app/router"
  8. "github.com/v2ray/v2ray-core/common/log"
  9. v2net "github.com/v2ray/v2ray-core/common/net"
  10. "github.com/v2ray/v2ray-core/common/retry"
  11. "github.com/v2ray/v2ray-core/proxy/common/connhandler"
  12. "github.com/v2ray/v2ray-core/shell/point/config"
  13. "github.com/v2ray/v2ray-core/transport/ray"
  14. )
  15. // Point shell of V2Ray.
  16. type Point struct {
  17. port v2net.Port
  18. ich connhandler.InboundConnectionHandler
  19. och connhandler.OutboundConnectionHandler
  20. idh []*InboundDetourHandler
  21. odh map[string]connhandler.OutboundConnectionHandler
  22. router router.Router
  23. }
  24. // NewPoint returns a new Point server based on given configuration.
  25. // The server is not started at this point.
  26. func NewPoint(pConfig config.PointConfig) (*Point, error) {
  27. var vpoint = new(Point)
  28. vpoint.port = pConfig.Port()
  29. ichFactory := connhandler.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol())
  30. if ichFactory == nil {
  31. log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol())
  32. return nil, config.BadConfiguration
  33. }
  34. ichConfig := pConfig.InboundConfig().Settings()
  35. ich, err := ichFactory.Create(vpoint, ichConfig)
  36. if err != nil {
  37. log.Error("Failed to create inbound connection handler: %v", err)
  38. return nil, err
  39. }
  40. vpoint.ich = ich
  41. ochFactory := connhandler.GetOutboundConnectionHandlerFactory(pConfig.OutboundConfig().Protocol())
  42. if ochFactory == nil {
  43. log.Error("Unknown outbound connection handler factory %s", pConfig.OutboundConfig().Protocol())
  44. return nil, config.BadConfiguration
  45. }
  46. ochConfig := pConfig.OutboundConfig().Settings()
  47. och, err := ochFactory.Create(ochConfig)
  48. if err != nil {
  49. log.Error("Failed to create outbound connection handler: %v", err)
  50. return nil, err
  51. }
  52. vpoint.och = och
  53. detours := pConfig.InboundDetours()
  54. if len(detours) > 0 {
  55. vpoint.idh = make([]*InboundDetourHandler, len(detours))
  56. for idx, detourConfig := range detours {
  57. detourHandler := &InboundDetourHandler{
  58. point: vpoint,
  59. config: detourConfig,
  60. }
  61. err := detourHandler.Initialize()
  62. if err != nil {
  63. return nil, err
  64. }
  65. vpoint.idh[idx] = detourHandler
  66. }
  67. }
  68. outboundDetours := pConfig.OutboundDetours()
  69. if len(outboundDetours) > 0 {
  70. vpoint.odh = make(map[string]connhandler.OutboundConnectionHandler)
  71. for _, detourConfig := range outboundDetours {
  72. detourFactory := connhandler.GetOutboundConnectionHandlerFactory(detourConfig.Protocol())
  73. if detourFactory == nil {
  74. log.Error("Unknown detour outbound connection handler factory %s", detourConfig.Protocol())
  75. return nil, config.BadConfiguration
  76. }
  77. detourHandler, err := detourFactory.Create(detourConfig.Settings())
  78. if err != nil {
  79. log.Error("Failed to create detour outbound connection handler: %v", err)
  80. return nil, err
  81. }
  82. vpoint.odh[detourConfig.Tag()] = detourHandler
  83. }
  84. }
  85. routerConfig := pConfig.RouterConfig()
  86. if routerConfig != nil {
  87. r, err := router.CreateRouter(routerConfig.Strategy(), routerConfig.Settings())
  88. if err != nil {
  89. log.Error("Failed to create router: %v", err)
  90. return nil, config.BadConfiguration
  91. }
  92. vpoint.router = r
  93. }
  94. return vpoint, nil
  95. }
  96. // Start starts the Point server, and return any error during the process.
  97. // In the case of any errors, the state of the server is unpredicatable.
  98. func (this *Point) Start() error {
  99. if this.port <= 0 {
  100. log.Error("Invalid port %d", this.port)
  101. return config.BadConfiguration
  102. }
  103. err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
  104. err := this.ich.Listen(this.port)
  105. if err != nil {
  106. return err
  107. }
  108. log.Warning("Point server started on port %d", this.port)
  109. return nil
  110. })
  111. if err != nil {
  112. return err
  113. }
  114. for _, detourHandler := range this.idh {
  115. err := detourHandler.Start()
  116. if err != nil {
  117. return err
  118. }
  119. }
  120. return nil
  121. }
  122. // Dispatches a Packet to an OutboundConnection.
  123. // The packet will be passed through the router (if configured), and then sent to an outbound
  124. // connection with matching tag.
  125. func (this *Point) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
  126. direct := ray.NewRay()
  127. dest := packet.Destination()
  128. if this.router != nil {
  129. tag, err := this.router.TakeDetour(dest)
  130. if err == nil {
  131. handler, found := this.odh[tag]
  132. if found {
  133. go handler.Dispatch(packet, direct)
  134. return direct
  135. }
  136. }
  137. }
  138. go this.och.Dispatch(packet, direct)
  139. return direct
  140. }