point.go 2.9 KB

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