point.go 2.9 KB

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