point.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/dispatcher"
  9. "github.com/v2ray/v2ray-core/app/proxyman"
  10. "github.com/v2ray/v2ray-core/app/router"
  11. "github.com/v2ray/v2ray-core/common/log"
  12. v2net "github.com/v2ray/v2ray-core/common/net"
  13. "github.com/v2ray/v2ray-core/common/retry"
  14. "github.com/v2ray/v2ray-core/proxy"
  15. proxyrepo "github.com/v2ray/v2ray-core/proxy/repo"
  16. "github.com/v2ray/v2ray-core/transport/ray"
  17. )
  18. // Point shell of V2Ray.
  19. type Point struct {
  20. port v2net.Port
  21. ich proxy.InboundHandler
  22. och proxy.OutboundHandler
  23. idh []InboundDetourHandler
  24. taggedIdh map[string]InboundDetourHandler
  25. odh map[string]proxy.OutboundHandler
  26. router router.Router
  27. space *app.SpaceController
  28. }
  29. // NewPoint returns a new Point server based on given configuration.
  30. // The server is not started at this point.
  31. func NewPoint(pConfig *Config) (*Point, error) {
  32. var vpoint = new(Point)
  33. vpoint.port = pConfig.Port
  34. if pConfig.LogConfig != nil {
  35. logConfig := pConfig.LogConfig
  36. if len(logConfig.AccessLog) > 0 {
  37. err := log.InitAccessLogger(logConfig.AccessLog)
  38. if err != nil {
  39. return nil, err
  40. }
  41. }
  42. if len(logConfig.ErrorLog) > 0 {
  43. err := log.InitErrorLogger(logConfig.ErrorLog)
  44. if err != nil {
  45. return nil, err
  46. }
  47. }
  48. log.SetLogLevel(logConfig.LogLevel)
  49. }
  50. vpoint.space = app.NewController()
  51. vpoint.space.Bind(dispatcher.APP_ID, vpoint)
  52. vpoint.space.Bind(proxyman.APP_ID_INBOUND_MANAGER, vpoint)
  53. ichConfig := pConfig.InboundConfig.Settings
  54. ich, err := proxyrepo.CreateInboundHandler(pConfig.InboundConfig.Protocol, vpoint.space.ForContext("vpoint-default-inbound"), ichConfig)
  55. if err != nil {
  56. log.Error("Failed to create inbound connection handler: ", err)
  57. return nil, err
  58. }
  59. vpoint.ich = ich
  60. ochConfig := pConfig.OutboundConfig.Settings
  61. och, err := proxyrepo.CreateOutboundHandler(pConfig.OutboundConfig.Protocol, vpoint.space.ForContext("vpoint-default-outbound"), ochConfig)
  62. if err != nil {
  63. log.Error("Failed to create outbound connection handler: ", err)
  64. return nil, err
  65. }
  66. vpoint.och = och
  67. vpoint.taggedIdh = make(map[string]InboundDetourHandler)
  68. detours := pConfig.InboundDetours
  69. if len(detours) > 0 {
  70. vpoint.idh = make([]InboundDetourHandler, len(detours))
  71. for idx, detourConfig := range detours {
  72. allocConfig := detourConfig.Allocation
  73. var detourHandler InboundDetourHandler
  74. switch allocConfig.Strategy {
  75. case AllocationStrategyAlways:
  76. dh, err := NewInboundDetourHandlerAlways(vpoint.space.ForContext(detourConfig.Tag), detourConfig)
  77. if err != nil {
  78. log.Error("Point: Failed to create detour handler: ", err)
  79. return nil, ErrorBadConfiguration
  80. }
  81. detourHandler = dh
  82. case AllocationStrategyRandom:
  83. dh, err := NewInboundDetourHandlerDynamic(vpoint.space.ForContext(detourConfig.Tag), detourConfig)
  84. if err != nil {
  85. log.Error("Point: Failed to create detour handler: ", err)
  86. return nil, ErrorBadConfiguration
  87. }
  88. detourHandler = dh
  89. default:
  90. log.Error("Point: Unknown allocation strategy: ", allocConfig.Strategy)
  91. return nil, ErrorBadConfiguration
  92. }
  93. vpoint.idh[idx] = detourHandler
  94. if len(detourConfig.Tag) > 0 {
  95. vpoint.taggedIdh[detourConfig.Tag] = detourHandler
  96. }
  97. }
  98. }
  99. outboundDetours := pConfig.OutboundDetours
  100. if len(outboundDetours) > 0 {
  101. vpoint.odh = make(map[string]proxy.OutboundHandler)
  102. for _, detourConfig := range outboundDetours {
  103. detourHandler, err := proxyrepo.CreateOutboundHandler(detourConfig.Protocol, vpoint.space.ForContext(detourConfig.Tag), detourConfig.Settings)
  104. if err != nil {
  105. log.Error("Failed to create detour outbound connection handler: ", err)
  106. return nil, err
  107. }
  108. vpoint.odh[detourConfig.Tag] = detourHandler
  109. }
  110. }
  111. routerConfig := pConfig.RouterConfig
  112. if routerConfig != nil {
  113. r, err := router.CreateRouter(routerConfig.Strategy, routerConfig.Settings)
  114. if err != nil {
  115. log.Error("Failed to create router: ", err)
  116. return nil, ErrorBadConfiguration
  117. }
  118. vpoint.router = r
  119. }
  120. return vpoint, nil
  121. }
  122. func (this *Point) Close() {
  123. this.ich.Close()
  124. for _, idh := range this.idh {
  125. idh.Close()
  126. }
  127. }
  128. // Start starts the Point server, and return any error during the process.
  129. // In the case of any errors, the state of the server is unpredicatable.
  130. func (this *Point) Start() error {
  131. if this.port <= 0 {
  132. log.Error("Invalid port ", this.port)
  133. return ErrorBadConfiguration
  134. }
  135. err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
  136. err := this.ich.Listen(this.port)
  137. if err != nil {
  138. return err
  139. }
  140. log.Warning("Point server started on port ", this.port)
  141. return nil
  142. })
  143. if err != nil {
  144. return err
  145. }
  146. for _, detourHandler := range this.idh {
  147. err := detourHandler.Start()
  148. if err != nil {
  149. return err
  150. }
  151. }
  152. return nil
  153. }
  154. // Dispatches a Packet to an OutboundConnection.
  155. // The packet will be passed through the router (if configured), and then sent to an outbound
  156. // connection with matching tag.
  157. func (this *Point) DispatchToOutbound(context app.Context, packet v2net.Packet) ray.InboundRay {
  158. direct := ray.NewRay()
  159. dest := packet.Destination()
  160. dispatcher := this.och
  161. if this.router != nil {
  162. if tag, err := this.router.TakeDetour(dest); err == nil {
  163. if handler, found := this.odh[tag]; found {
  164. log.Info("Point: Taking detour [", tag, "] for [", dest, "]", tag, dest)
  165. dispatcher = handler
  166. } else {
  167. log.Warning("Point: Unable to find routing destination: ", tag)
  168. }
  169. }
  170. }
  171. go this.FilterPacketAndDispatch(packet, direct, dispatcher)
  172. return direct
  173. }
  174. func (this *Point) FilterPacketAndDispatch(packet v2net.Packet, link ray.OutboundRay, dispatcher proxy.OutboundHandler) {
  175. // Filter empty packets
  176. chunk := packet.Chunk()
  177. moreChunks := packet.MoreChunks()
  178. changed := false
  179. for chunk == nil && moreChunks {
  180. changed = true
  181. chunk, moreChunks = <-link.OutboundInput()
  182. }
  183. if chunk == nil && !moreChunks {
  184. log.Info("Point: No payload to dispatch, stopping dispatching now.")
  185. close(link.OutboundOutput())
  186. return
  187. }
  188. if changed {
  189. packet = v2net.NewPacket(packet.Destination(), chunk, moreChunks)
  190. }
  191. dispatcher.Dispatch(packet, link)
  192. }
  193. func (this *Point) GetHandler(context app.Context, tag string) (proxy.InboundHandler, int) {
  194. handler, found := this.taggedIdh[tag]
  195. if !found {
  196. log.Warning("Point: Unable to find an inbound handler with tag: ", tag)
  197. return nil, 0
  198. }
  199. return handler.GetConnectionHandler()
  200. }