point.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 := router.NewRouter(routerConfig, vpoint.space)
  61. vpoint.space.BindApp(router.APP_ID, r)
  62. vpoint.router = r
  63. }
  64. vpoint.space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(vpoint.space))
  65. ichConfig := pConfig.InboundConfig.Settings
  66. ich, err := proxyregistry.CreateInboundHandler(
  67. pConfig.InboundConfig.Protocol, vpoint.space, ichConfig, &proxy.InboundHandlerMeta{
  68. Tag: "system.inbound",
  69. Address: pConfig.InboundConfig.ListenOn,
  70. Port: vpoint.port,
  71. StreamSettings: pConfig.InboundConfig.StreamSettings,
  72. AllowPassiveConnection: pConfig.InboundConfig.AllowPassiveConnection,
  73. })
  74. if err != nil {
  75. log.Error("Failed to create inbound connection handler: ", err)
  76. return nil, err
  77. }
  78. vpoint.ich = ich
  79. ochConfig := pConfig.OutboundConfig.Settings
  80. och, err := proxyregistry.CreateOutboundHandler(
  81. pConfig.OutboundConfig.Protocol, vpoint.space, ochConfig, &proxy.OutboundHandlerMeta{
  82. Tag: "system.outbound",
  83. Address: pConfig.OutboundConfig.SendThrough,
  84. StreamSettings: pConfig.OutboundConfig.StreamSettings,
  85. })
  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, common.ErrBadConfiguration
  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, common.ErrBadConfiguration
  112. }
  113. detourHandler = dh
  114. default:
  115. log.Error("Point: Unknown allocation strategy: ", allocConfig.Strategy)
  116. return nil, common.ErrBadConfiguration
  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 := proxyregistry.CreateOutboundHandler(
  129. detourConfig.Protocol, vpoint.space, detourConfig.Settings, &proxy.OutboundHandlerMeta{
  130. Tag: detourConfig.Tag,
  131. Address: detourConfig.SendThrough,
  132. StreamSettings: detourConfig.StreamSettings,
  133. })
  134. if err != nil {
  135. log.Error("Point: Failed to create detour outbound connection handler: ", err)
  136. return nil, err
  137. }
  138. vpoint.odh[detourConfig.Tag] = detourHandler
  139. outboundHandlerManager.SetHandler(detourConfig.Tag, detourHandler)
  140. }
  141. }
  142. if err := vpoint.space.Initialize(); err != nil {
  143. return nil, err
  144. }
  145. return vpoint, nil
  146. }
  147. func (this *Point) Close() {
  148. this.ich.Close()
  149. for _, idh := range this.idh {
  150. idh.Close()
  151. }
  152. }
  153. // Start starts the Point server, and return any error during the process.
  154. // In the case of any errors, the state of the server is unpredicatable.
  155. func (this *Point) Start() error {
  156. if this.port <= 0 {
  157. log.Error("Point: Invalid port ", this.port)
  158. return common.ErrBadConfiguration
  159. }
  160. err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
  161. err := this.ich.Start()
  162. if err != nil {
  163. return err
  164. }
  165. log.Warning("Point: started on port ", this.port)
  166. return nil
  167. })
  168. if err != nil {
  169. return err
  170. }
  171. for _, detourHandler := range this.idh {
  172. err := detourHandler.Start()
  173. if err != nil {
  174. return err
  175. }
  176. }
  177. return nil
  178. }
  179. func (this *Point) GetHandler(tag string) (proxy.InboundHandler, int) {
  180. handler, found := this.taggedIdh[tag]
  181. if !found {
  182. log.Warning("Point: Unable to find an inbound handler with tag: ", tag)
  183. return nil, 0
  184. }
  185. return handler.GetConnectionHandler()
  186. }
  187. func (this *Point) Release() {
  188. }