point.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package point
  2. import (
  3. "github.com/v2ray/v2ray-core/app/point/config"
  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/transport/ray"
  9. )
  10. // Point is an single server in V2Ray system.
  11. type Point struct {
  12. port uint16
  13. ich connhandler.InboundConnectionHandler
  14. och connhandler.OutboundConnectionHandler
  15. idh []*InboundDetourHandler
  16. }
  17. // NewPoint returns a new Point server based on given configuration.
  18. // The server is not started at this point.
  19. func NewPoint(pConfig config.PointConfig) (*Point, error) {
  20. var vpoint = new(Point)
  21. vpoint.port = pConfig.Port()
  22. ichFactory := connhandler.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol())
  23. if ichFactory == nil {
  24. log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol())
  25. return nil, config.BadConfiguration
  26. }
  27. ichConfig := pConfig.InboundConfig().Settings()
  28. ich, err := ichFactory.Create(vpoint, ichConfig)
  29. if err != nil {
  30. log.Error("Failed to create inbound connection handler: %v", err)
  31. return nil, err
  32. }
  33. vpoint.ich = ich
  34. ochFactory := connhandler.GetOutboundConnectionHandlerFactory(pConfig.OutboundConfig().Protocol())
  35. if ochFactory == nil {
  36. log.Error("Unknown outbound connection handler factory %s", pConfig.OutboundConfig().Protocol())
  37. return nil, config.BadConfiguration
  38. }
  39. ochConfig := pConfig.OutboundConfig().Settings()
  40. och, err := ochFactory.Create(ochConfig)
  41. if err != nil {
  42. log.Error("Failed to create outbound connection handler: %v", err)
  43. return nil, err
  44. }
  45. vpoint.och = och
  46. detours := pConfig.InboundDetours()
  47. if len(detours) > 0 {
  48. vpoint.idh = make([]*InboundDetourHandler, len(detours))
  49. for idx, detourConfig := range detours {
  50. detourHandler := &InboundDetourHandler{
  51. point: vpoint,
  52. config: detourConfig,
  53. }
  54. err := detourHandler.Initialize()
  55. if err != nil {
  56. return nil, err
  57. }
  58. vpoint.idh[idx] = detourHandler
  59. }
  60. }
  61. return vpoint, nil
  62. }
  63. // Start starts the Point server, and return any error during the process.
  64. // In the case of any errors, the state of the server is unpredicatable.
  65. func (vp *Point) Start() error {
  66. if vp.port <= 0 {
  67. log.Error("Invalid port %d", vp.port)
  68. return config.BadConfiguration
  69. }
  70. err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
  71. err := vp.ich.Listen(vp.port)
  72. if err != nil {
  73. return err
  74. }
  75. log.Warning("Point server started on port %d", vp.port)
  76. return nil
  77. })
  78. if err != nil {
  79. return err
  80. }
  81. for _, detourHandler := range vp.idh {
  82. err := detourHandler.Start()
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. return nil
  88. }
  89. func (p *Point) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
  90. direct := ray.NewRay()
  91. go p.och.Dispatch(packet, direct)
  92. return direct
  93. }