point.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package core
  2. import (
  3. "github.com/v2ray/v2ray-core/common/log"
  4. v2net "github.com/v2ray/v2ray-core/common/net"
  5. )
  6. var (
  7. inboundFactories = make(map[string]InboundConnectionHandlerFactory)
  8. outboundFactories = make(map[string]OutboundConnectionHandlerFactory)
  9. )
  10. func RegisterInboundConnectionHandlerFactory(name string, factory InboundConnectionHandlerFactory) error {
  11. // TODO check name
  12. inboundFactories[name] = factory
  13. return nil
  14. }
  15. func RegisterOutboundConnectionHandlerFactory(name string, factory OutboundConnectionHandlerFactory) error {
  16. // TODO check name
  17. outboundFactories[name] = factory
  18. return nil
  19. }
  20. // Point is an single server in V2Ray system.
  21. type Point struct {
  22. port uint16
  23. ichFactory InboundConnectionHandlerFactory
  24. ichConfig []byte
  25. ochFactory OutboundConnectionHandlerFactory
  26. ochConfig []byte
  27. }
  28. // NewPoint returns a new Point server based on given configuration.
  29. // The server is not started at this point.
  30. func NewPoint(config PointConfig) (*Point, error) {
  31. var vpoint = new(Point)
  32. vpoint.port = config.Port()
  33. ichFactory, ok := inboundFactories[config.InboundConfig().Protocol()]
  34. if !ok {
  35. panic(log.Error("Unknown inbound connection handler factory %s", config.InboundConfig().Protocol()))
  36. }
  37. vpoint.ichFactory = ichFactory
  38. vpoint.ichConfig = config.InboundConfig().Content()
  39. ochFactory, ok := outboundFactories[config.OutboundConfig().Protocol()]
  40. if !ok {
  41. panic(log.Error("Unknown outbound connection handler factory %s", config.OutboundConfig().Protocol))
  42. }
  43. vpoint.ochFactory = ochFactory
  44. vpoint.ochConfig = config.OutboundConfig().Content()
  45. return vpoint, nil
  46. }
  47. type InboundConnectionHandlerFactory interface {
  48. Create(vp *Point, config []byte) (InboundConnectionHandler, error)
  49. }
  50. type InboundConnectionHandler interface {
  51. Listen(port uint16) error
  52. }
  53. type OutboundConnectionHandlerFactory interface {
  54. Initialize(config []byte) error
  55. Create(VP *Point, firstPacket v2net.Packet) (OutboundConnectionHandler, error)
  56. }
  57. type OutboundConnectionHandler interface {
  58. Start(ray OutboundRay) error
  59. }
  60. // Start starts the Point server, and return any error during the process.
  61. // In the case of any errors, the state of the server is unpredicatable.
  62. func (vp *Point) Start() error {
  63. if vp.port <= 0 {
  64. return log.Error("Invalid port %d", vp.port)
  65. }
  66. vp.ochFactory.Initialize(vp.ochConfig)
  67. inboundConnectionHandler, err := vp.ichFactory.Create(vp, vp.ichConfig)
  68. if err != nil {
  69. return err
  70. }
  71. err = inboundConnectionHandler.Listen(vp.port)
  72. return nil
  73. }
  74. func (p *Point) DispatchToOutbound(packet v2net.Packet) InboundRay {
  75. ray := NewRay()
  76. // TODO: handle error
  77. och, _ := p.ochFactory.Create(p, packet)
  78. _ = och.Start(ray)
  79. return ray
  80. }
  81. func (p *Point) DispatchToInbound(packet v2net.Packet) {
  82. return
  83. }