point.go 5.2 KB

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