point.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package core
  2. import (
  3. "github.com/v2ray/v2ray-core/log"
  4. v2net "github.com/v2ray/v2ray-core/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. Create(VP *Point, config []byte, dest v2net.Address) (OutboundConnectionHandler, error)
  55. }
  56. type OutboundConnectionHandler interface {
  57. Start(ray OutboundRay) error
  58. }
  59. // Start starts the Point server, and return any error during the process.
  60. // In the case of any errors, the state of the server is unpredicatable.
  61. func (vp *Point) Start() error {
  62. if vp.port <= 0 {
  63. return log.Error("Invalid port %d", vp.port)
  64. }
  65. inboundConnectionHandler, err := vp.ichFactory.Create(vp, vp.ichConfig)
  66. if err != nil {
  67. return err
  68. }
  69. err = inboundConnectionHandler.Listen(vp.port)
  70. return nil
  71. }
  72. func (vp *Point) NewInboundConnectionAccepted(destination v2net.Address) InboundRay {
  73. ray := NewRay()
  74. // TODO: handle error
  75. och, _ := vp.ochFactory.Create(vp, vp.ochConfig, destination)
  76. _ = och.Start(ray)
  77. return ray
  78. }