point.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package point
  2. import (
  3. "github.com/v2ray/v2ray-core/app/router"
  4. "github.com/v2ray/v2ray-core/common/log"
  5. v2net "github.com/v2ray/v2ray-core/common/net"
  6. "github.com/v2ray/v2ray-core/common/retry"
  7. "github.com/v2ray/v2ray-core/proxy/common/connhandler"
  8. "github.com/v2ray/v2ray-core/shell/point/config"
  9. "github.com/v2ray/v2ray-core/transport/ray"
  10. )
  11. // Point is an single server in V2Ray system.
  12. type Point struct {
  13. port v2net.Port
  14. ich connhandler.InboundConnectionHandler
  15. och connhandler.OutboundConnectionHandler
  16. idh []*InboundDetourHandler
  17. odh map[string]connhandler.OutboundConnectionHandler
  18. router router.Router
  19. }
  20. // NewPoint returns a new Point server based on given configuration.
  21. // The server is not started at this point.
  22. func NewPoint(pConfig config.PointConfig) (*Point, error) {
  23. var vpoint = new(Point)
  24. vpoint.port = pConfig.Port()
  25. ichFactory := connhandler.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol())
  26. if ichFactory == nil {
  27. log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol())
  28. return nil, config.BadConfiguration
  29. }
  30. ichConfig := pConfig.InboundConfig().Settings()
  31. ich, err := ichFactory.Create(vpoint, ichConfig)
  32. if err != nil {
  33. log.Error("Failed to create inbound connection handler: %v", err)
  34. return nil, err
  35. }
  36. vpoint.ich = ich
  37. ochFactory := connhandler.GetOutboundConnectionHandlerFactory(pConfig.OutboundConfig().Protocol())
  38. if ochFactory == nil {
  39. log.Error("Unknown outbound connection handler factory %s", pConfig.OutboundConfig().Protocol())
  40. return nil, config.BadConfiguration
  41. }
  42. ochConfig := pConfig.OutboundConfig().Settings()
  43. och, err := ochFactory.Create(ochConfig)
  44. if err != nil {
  45. log.Error("Failed to create outbound connection handler: %v", err)
  46. return nil, err
  47. }
  48. vpoint.och = och
  49. detours := pConfig.InboundDetours()
  50. if len(detours) > 0 {
  51. vpoint.idh = make([]*InboundDetourHandler, len(detours))
  52. for idx, detourConfig := range detours {
  53. detourHandler := &InboundDetourHandler{
  54. point: vpoint,
  55. config: detourConfig,
  56. }
  57. err := detourHandler.Initialize()
  58. if err != nil {
  59. return nil, err
  60. }
  61. vpoint.idh[idx] = detourHandler
  62. }
  63. }
  64. outboundDetours := pConfig.OutboundDetours()
  65. if len(outboundDetours) > 0 {
  66. vpoint.odh = make(map[string]connhandler.OutboundConnectionHandler)
  67. for _, detourConfig := range outboundDetours {
  68. detourFactory := connhandler.GetOutboundConnectionHandlerFactory(detourConfig.Protocol())
  69. if detourFactory == nil {
  70. log.Error("Unknown detour outbound connection handler factory %s", detourConfig.Protocol())
  71. return nil, config.BadConfiguration
  72. }
  73. detourHandler, err := detourFactory.Create(detourConfig.Settings())
  74. if err != nil {
  75. log.Error("Failed to create detour outbound connection handler: %v", err)
  76. return nil, err
  77. }
  78. vpoint.odh[detourConfig.Tag()] = detourHandler
  79. }
  80. }
  81. routerConfig := pConfig.RouterConfig()
  82. if routerConfig != nil {
  83. r, err := router.CreateRouter(routerConfig.Strategy(), routerConfig.Settings())
  84. if err != nil {
  85. log.Error("Failed to create router: %v", err)
  86. return nil, config.BadConfiguration
  87. }
  88. vpoint.router = r
  89. }
  90. return vpoint, nil
  91. }
  92. // Start starts the Point server, and return any error during the process.
  93. // In the case of any errors, the state of the server is unpredicatable.
  94. func (this *Point) Start() error {
  95. if this.port <= 0 {
  96. log.Error("Invalid port %d", this.port)
  97. return config.BadConfiguration
  98. }
  99. err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
  100. err := this.ich.Listen(this.port)
  101. if err != nil {
  102. return err
  103. }
  104. log.Warning("Point server started on port %d", this.port)
  105. return nil
  106. })
  107. if err != nil {
  108. return err
  109. }
  110. for _, detourHandler := range this.idh {
  111. err := detourHandler.Start()
  112. if err != nil {
  113. return err
  114. }
  115. }
  116. return nil
  117. }
  118. func (this *Point) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
  119. direct := ray.NewRay()
  120. dest := packet.Destination()
  121. if this.router != nil {
  122. tag, err := this.router.TakeDetour(dest)
  123. if err == nil {
  124. handler, found := this.odh[tag]
  125. if found {
  126. go handler.Dispatch(packet, direct)
  127. return direct
  128. }
  129. }
  130. }
  131. go this.och.Dispatch(packet, direct)
  132. return direct
  133. }