v2ray.go 5.9 KB

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