point.go 6.7 KB

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