point.go 5.9 KB

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