point.go 6.6 KB

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