point.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. 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.Space
  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.NewSpace()
  52. vpoint.space.BindApp(proxyman.APP_ID_INBOUND_MANAGER, vpoint)
  53. outboundHandlerManager := &proxyman.DefaultOutboundHandlerManager{}
  54. vpoint.space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager)
  55. dnsConfig := pConfig.DNSConfig
  56. if dnsConfig != nil {
  57. dnsServer := dns.NewCacheServer(vpoint.space, dnsConfig)
  58. vpoint.space.BindApp(dns.APP_ID, dnsServer)
  59. }
  60. routerConfig := pConfig.RouterConfig
  61. if routerConfig != nil {
  62. r, err := router.CreateRouter(routerConfig.Strategy, routerConfig.Settings, vpoint.space)
  63. if err != nil {
  64. log.Error("Failed to create router: ", err)
  65. return nil, ErrorBadConfiguration
  66. }
  67. vpoint.space.BindApp(router.APP_ID, r)
  68. vpoint.router = r
  69. }
  70. vpoint.space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(vpoint.space))
  71. ichConfig := pConfig.InboundConfig.Settings
  72. ich, err := proxyrepo.CreateInboundHandler(pConfig.InboundConfig.Protocol, vpoint.space, ichConfig)
  73. if err != nil {
  74. log.Error("Failed to create inbound connection handler: ", err)
  75. return nil, err
  76. }
  77. vpoint.ich = ich
  78. ochConfig := pConfig.OutboundConfig.Settings
  79. och, err := proxyrepo.CreateOutboundHandler(pConfig.OutboundConfig.Protocol, vpoint.space, ochConfig)
  80. if err != nil {
  81. log.Error("Failed to create outbound connection handler: ", err)
  82. return nil, err
  83. }
  84. vpoint.och = och
  85. outboundHandlerManager.SetDefaultHandler(och)
  86. vpoint.taggedIdh = make(map[string]InboundDetourHandler)
  87. detours := pConfig.InboundDetours
  88. if len(detours) > 0 {
  89. vpoint.idh = make([]InboundDetourHandler, len(detours))
  90. for idx, detourConfig := range detours {
  91. allocConfig := detourConfig.Allocation
  92. var detourHandler InboundDetourHandler
  93. switch allocConfig.Strategy {
  94. case AllocationStrategyAlways:
  95. dh, err := NewInboundDetourHandlerAlways(vpoint.space, detourConfig)
  96. if err != nil {
  97. log.Error("Point: Failed to create detour handler: ", err)
  98. return nil, ErrorBadConfiguration
  99. }
  100. detourHandler = dh
  101. case AllocationStrategyRandom:
  102. dh, err := NewInboundDetourHandlerDynamic(vpoint.space, detourConfig)
  103. if err != nil {
  104. log.Error("Point: Failed to create detour handler: ", err)
  105. return nil, ErrorBadConfiguration
  106. }
  107. detourHandler = dh
  108. default:
  109. log.Error("Point: Unknown allocation strategy: ", allocConfig.Strategy)
  110. return nil, ErrorBadConfiguration
  111. }
  112. vpoint.idh[idx] = detourHandler
  113. if len(detourConfig.Tag) > 0 {
  114. vpoint.taggedIdh[detourConfig.Tag] = detourHandler
  115. }
  116. }
  117. }
  118. outboundDetours := pConfig.OutboundDetours
  119. if len(outboundDetours) > 0 {
  120. vpoint.odh = make(map[string]proxy.OutboundHandler)
  121. for _, detourConfig := range outboundDetours {
  122. detourHandler, err := proxyrepo.CreateOutboundHandler(detourConfig.Protocol, vpoint.space, detourConfig.Settings)
  123. if err != nil {
  124. log.Error("Failed to create detour outbound connection handler: ", err)
  125. return nil, err
  126. }
  127. vpoint.odh[detourConfig.Tag] = detourHandler
  128. outboundHandlerManager.SetHandler(detourConfig.Tag, detourHandler)
  129. }
  130. }
  131. return vpoint, nil
  132. }
  133. func (this *Point) Close() {
  134. this.ich.Close()
  135. for _, idh := range this.idh {
  136. idh.Close()
  137. }
  138. }
  139. // Start starts the Point server, and return any error during the process.
  140. // In the case of any errors, the state of the server is unpredicatable.
  141. func (this *Point) Start() error {
  142. if this.port <= 0 {
  143. log.Error("Invalid port ", this.port)
  144. return ErrorBadConfiguration
  145. }
  146. err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
  147. err := this.ich.Listen(this.port)
  148. if err != nil {
  149. return err
  150. }
  151. log.Warning("Point server started on port ", this.port)
  152. return nil
  153. })
  154. if err != nil {
  155. return err
  156. }
  157. for _, detourHandler := range this.idh {
  158. err := detourHandler.Start()
  159. if err != nil {
  160. return err
  161. }
  162. }
  163. return nil
  164. }
  165. func (this *Point) GetHandler(tag string) (proxy.InboundHandler, int) {
  166. handler, found := this.taggedIdh[tag]
  167. if !found {
  168. log.Warning("Point: Unable to find an inbound handler with tag: ", tag)
  169. return nil, 0
  170. }
  171. return handler.GetConnectionHandler()
  172. }