point.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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"
  8. "github.com/v2ray/v2ray-core/app/router"
  9. "github.com/v2ray/v2ray-core/common/log"
  10. v2net "github.com/v2ray/v2ray-core/common/net"
  11. "github.com/v2ray/v2ray-core/common/retry"
  12. "github.com/v2ray/v2ray-core/proxy/common/connhandler"
  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. space *app.Space
  24. }
  25. // NewPoint returns a new Point server based on given configuration.
  26. // The server is not started at this point.
  27. func NewPoint(pConfig PointConfig) (*Point, error) {
  28. var vpoint = new(Point)
  29. vpoint.port = pConfig.Port()
  30. if pConfig.LogConfig() != nil {
  31. logConfig := pConfig.LogConfig()
  32. if len(logConfig.AccessLog()) > 0 {
  33. err := log.InitAccessLogger(logConfig.AccessLog())
  34. if err != nil {
  35. return nil, err
  36. }
  37. }
  38. if len(logConfig.ErrorLog()) > 0 {
  39. err := log.InitErrorLogger(logConfig.ErrorLog())
  40. if err != nil {
  41. return nil, err
  42. }
  43. }
  44. log.SetLogLevel(logConfig.LogLevel())
  45. }
  46. vpoint.space = app.NewSpace()
  47. vpoint.space.Bind(vpoint)
  48. ichFactory := connhandler.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol())
  49. if ichFactory == nil {
  50. log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol())
  51. return nil, BadConfiguration
  52. }
  53. ichConfig := pConfig.InboundConfig().Settings()
  54. ich, err := ichFactory.Create(vpoint.space, ichConfig)
  55. if err != nil {
  56. log.Error("Failed to create inbound connection handler: %v", err)
  57. return nil, err
  58. }
  59. vpoint.ich = ich
  60. ochFactory := connhandler.GetOutboundConnectionHandlerFactory(pConfig.OutboundConfig().Protocol())
  61. if ochFactory == nil {
  62. log.Error("Unknown outbound connection handler factory %s", pConfig.OutboundConfig().Protocol())
  63. return nil, BadConfiguration
  64. }
  65. ochConfig := pConfig.OutboundConfig().Settings()
  66. och, err := ochFactory.Create(vpoint.space, ochConfig)
  67. if err != nil {
  68. log.Error("Failed to create outbound connection handler: %v", err)
  69. return nil, err
  70. }
  71. vpoint.och = och
  72. detours := pConfig.InboundDetours()
  73. if len(detours) > 0 {
  74. vpoint.idh = make([]*InboundDetourHandler, len(detours))
  75. for idx, detourConfig := range detours {
  76. detourHandler := &InboundDetourHandler{
  77. space: vpoint.space,
  78. config: detourConfig,
  79. }
  80. err := detourHandler.Initialize()
  81. if err != nil {
  82. return nil, err
  83. }
  84. vpoint.idh[idx] = detourHandler
  85. }
  86. }
  87. outboundDetours := pConfig.OutboundDetours()
  88. if len(outboundDetours) > 0 {
  89. vpoint.odh = make(map[string]connhandler.OutboundConnectionHandler)
  90. for _, detourConfig := range outboundDetours {
  91. detourFactory := connhandler.GetOutboundConnectionHandlerFactory(detourConfig.Protocol())
  92. if detourFactory == nil {
  93. log.Error("Unknown detour outbound connection handler factory %s", detourConfig.Protocol())
  94. return nil, BadConfiguration
  95. }
  96. detourHandler, err := detourFactory.Create(vpoint.space, detourConfig.Settings())
  97. if err != nil {
  98. log.Error("Failed to create detour outbound connection handler: %v", err)
  99. return nil, err
  100. }
  101. vpoint.odh[detourConfig.Tag()] = detourHandler
  102. }
  103. }
  104. routerConfig := pConfig.RouterConfig()
  105. if routerConfig != nil {
  106. r, err := router.CreateRouter(routerConfig.Strategy(), routerConfig.Settings())
  107. if err != nil {
  108. log.Error("Failed to create router: %v", err)
  109. return nil, BadConfiguration
  110. }
  111. vpoint.router = r
  112. }
  113. return vpoint, nil
  114. }
  115. // Start starts the Point server, and return any error during the process.
  116. // In the case of any errors, the state of the server is unpredicatable.
  117. func (this *Point) Start() error {
  118. if this.port <= 0 {
  119. log.Error("Invalid port %d", this.port)
  120. return BadConfiguration
  121. }
  122. err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
  123. err := this.ich.Listen(this.port)
  124. if err != nil {
  125. return err
  126. }
  127. log.Warning("Point server started on port %d", this.port)
  128. return nil
  129. })
  130. if err != nil {
  131. return err
  132. }
  133. for _, detourHandler := range this.idh {
  134. err := detourHandler.Start()
  135. if err != nil {
  136. return err
  137. }
  138. }
  139. return nil
  140. }
  141. // Dispatches a Packet to an OutboundConnection.
  142. // The packet will be passed through the router (if configured), and then sent to an outbound
  143. // connection with matching tag.
  144. func (this *Point) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
  145. direct := ray.NewRay()
  146. dest := packet.Destination()
  147. if this.router != nil {
  148. tag, err := this.router.TakeDetour(dest)
  149. if err == nil {
  150. handler, found := this.odh[tag]
  151. if found {
  152. go handler.Dispatch(packet, direct)
  153. return direct
  154. }
  155. }
  156. }
  157. go this.och.Dispatch(packet, direct)
  158. return direct
  159. }