point.go 4.7 KB

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