point.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. ich InboundConnectionHandler
  25. och OutboundConnectionHandler
  26. }
  27. // NewPoint returns a new Point server based on given configuration.
  28. // The server is not started at this point.
  29. func NewPoint(pConfig config.PointConfig) (*Point, error) {
  30. var vpoint = new(Point)
  31. vpoint.port = pConfig.Port()
  32. ichFactory, ok := inboundFactories[pConfig.InboundConfig().Protocol()]
  33. if !ok {
  34. panic(log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol()))
  35. }
  36. ichConfig := pConfig.InboundConfig().Settings(config.TypeInbound)
  37. ich, err := ichFactory.Create(vpoint, ichConfig)
  38. if err != nil {
  39. log.Error("Failed to create inbound connection handler: %v", err)
  40. return nil, err
  41. }
  42. vpoint.ich = ich
  43. ochFactory, ok := outboundFactories[pConfig.OutboundConfig().Protocol()]
  44. if !ok {
  45. panic(log.Error("Unknown outbound connection handler factory %s", pConfig.OutboundConfig().Protocol))
  46. }
  47. ochConfig := pConfig.OutboundConfig().Settings(config.TypeOutbound)
  48. och, err := ochFactory.Create(vpoint, ochConfig)
  49. if err != nil {
  50. log.Error("Failed to create outbound connection handler: %v", err)
  51. return nil, err
  52. }
  53. vpoint.och = och
  54. return vpoint, nil
  55. }
  56. type InboundConnectionHandlerFactory interface {
  57. Create(vp *Point, config interface{}) (InboundConnectionHandler, error)
  58. }
  59. type InboundConnectionHandler interface {
  60. Listen(port uint16) error
  61. }
  62. type OutboundConnectionHandlerFactory interface {
  63. Create(VP *Point, config interface{}) (OutboundConnectionHandler, error)
  64. }
  65. type OutboundConnectionHandler interface {
  66. Dispatch(firstPacket v2net.Packet, ray OutboundRay) error
  67. }
  68. // Start starts the Point server, and return any error during the process.
  69. // In the case of any errors, the state of the server is unpredicatable.
  70. func (vp *Point) Start() error {
  71. if vp.port <= 0 {
  72. return log.Error("Invalid port %d", vp.port)
  73. }
  74. err := vp.ich.Listen(vp.port)
  75. // TODO: handle error
  76. return err
  77. }
  78. func (p *Point) DispatchToOutbound(packet v2net.Packet) InboundRay {
  79. ray := NewRay()
  80. // TODO: handle error
  81. p.och.Dispatch(packet, ray)
  82. return ray
  83. }