point.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package core
  2. import (
  3. "github.com/v2ray/v2ray-core/common/log"
  4. v2net "github.com/v2ray/v2ray-core/common/net"
  5. "github.com/v2ray/v2ray-core/config"
  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 interface{}
  26. ochFactory OutboundConnectionHandlerFactory
  27. ochConfig interface{}
  28. }
  29. // NewPoint returns a new Point server based on given configuration.
  30. // The server is not started at this point.
  31. func NewPoint(pConfig config.PointConfig) (*Point, error) {
  32. var vpoint = new(Point)
  33. vpoint.port = pConfig.Port()
  34. ichFactory, ok := inboundFactories[pConfig.InboundConfig().Protocol()]
  35. if !ok {
  36. panic(log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol()))
  37. }
  38. vpoint.ichFactory = ichFactory
  39. vpoint.ichConfig = pConfig.InboundConfig().Settings(config.TypeInbound)
  40. ochFactory, ok := outboundFactories[pConfig.OutboundConfig().Protocol()]
  41. if !ok {
  42. panic(log.Error("Unknown outbound connection handler factory %s", pConfig.OutboundConfig().Protocol))
  43. }
  44. vpoint.ochFactory = ochFactory
  45. vpoint.ochConfig = pConfig.OutboundConfig().Settings(config.TypeOutbound)
  46. return vpoint, nil
  47. }
  48. type InboundConnectionHandlerFactory interface {
  49. Create(vp *Point, config interface{}) (InboundConnectionHandler, error)
  50. }
  51. type InboundConnectionHandler interface {
  52. Listen(port uint16) error
  53. }
  54. type OutboundConnectionHandlerFactory interface {
  55. Create(VP *Point, config interface{}, 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. inboundConnectionHandler, err := vp.ichFactory.Create(vp, vp.ichConfig)
  67. if err != nil {
  68. return err
  69. }
  70. err = inboundConnectionHandler.Listen(vp.port)
  71. return nil
  72. }
  73. func (p *Point) DispatchToOutbound(packet v2net.Packet) InboundRay {
  74. ray := NewRay()
  75. // TODO: handle error
  76. och, _ := p.ochFactory.Create(p, p.ochConfig, packet)
  77. _ = och.Start(ray)
  78. return ray
  79. }