point.go 5.0 KB

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