point.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. proxyrepo "github.com/v2ray/v2ray-core/proxy/repo"
  15. "github.com/v2ray/v2ray-core/transport/ray"
  16. )
  17. // Point shell of V2Ray.
  18. type Point struct {
  19. port v2net.Port
  20. ich proxy.InboundConnectionHandler
  21. och proxy.OutboundConnectionHandler
  22. idh []*InboundDetourHandler
  23. odh map[string]proxy.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 := proxyrepo.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 := proxyrepo.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]proxy.OutboundConnectionHandler)
  82. for _, detourConfig := range outboundDetours {
  83. detourHandler, err := proxyrepo.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. func (this *Point) Close() {
  103. this.ich.Close()
  104. for _, idh := range this.idh {
  105. idh.Close()
  106. }
  107. }
  108. // Start starts the Point server, and return any error during the process.
  109. // In the case of any errors, the state of the server is unpredicatable.
  110. func (this *Point) Start() error {
  111. if this.port <= 0 {
  112. log.Error("Invalid port %d", this.port)
  113. return BadConfiguration
  114. }
  115. err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
  116. err := this.ich.Listen(this.port)
  117. if err != nil {
  118. return err
  119. }
  120. log.Warning("Point server started on port %d", this.port)
  121. return nil
  122. })
  123. if err != nil {
  124. return err
  125. }
  126. for _, detourHandler := range this.idh {
  127. err := detourHandler.Start()
  128. if err != nil {
  129. return err
  130. }
  131. }
  132. return nil
  133. }
  134. // Dispatches a Packet to an OutboundConnection.
  135. // The packet will be passed through the router (if configured), and then sent to an outbound
  136. // connection with matching tag.
  137. func (this *Point) DispatchToOutbound(context app.Context, packet v2net.Packet) ray.InboundRay {
  138. direct := ray.NewRay()
  139. dest := packet.Destination()
  140. dispatcher := this.och
  141. if this.router != nil {
  142. if tag, err := this.router.TakeDetour(dest); err == nil {
  143. if handler, found := this.odh[tag]; found {
  144. dispatcher = handler
  145. }
  146. }
  147. }
  148. go this.FilterPacketAndDispatch(packet, direct, dispatcher)
  149. return direct
  150. }
  151. func (this *Point) FilterPacketAndDispatch(packet v2net.Packet, link ray.OutboundRay, dispatcher proxy.OutboundConnectionHandler) {
  152. // Filter empty packets
  153. chunk := packet.Chunk()
  154. moreChunks := packet.MoreChunks()
  155. changed := false
  156. for chunk == nil && moreChunks {
  157. changed = true
  158. chunk, moreChunks = <-link.OutboundInput()
  159. }
  160. if chunk == nil && !moreChunks {
  161. close(link.OutboundOutput())
  162. return
  163. }
  164. if changed {
  165. packet = v2net.NewPacket(packet.Destination(), chunk, moreChunks)
  166. }
  167. dispatcher.Dispatch(packet, link)
  168. }