point.go 5.9 KB

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