point.go 6.0 KB

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