point.go 6.4 KB

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