point.go 6.7 KB

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