inbound.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package inbound
  2. //go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg inbound -path App,Proxyman,Inbound
  3. import (
  4. "context"
  5. "v2ray.com/core"
  6. "v2ray.com/core/app/proxyman"
  7. "v2ray.com/core/common"
  8. )
  9. // Manager is to manage all inbound handlers.
  10. type Manager struct {
  11. handlers []core.InboundHandler
  12. taggedHandlers map[string]core.InboundHandler
  13. }
  14. func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error) {
  15. m := &Manager{
  16. taggedHandlers: make(map[string]core.InboundHandler),
  17. }
  18. v := core.FromContext(ctx)
  19. if v == nil {
  20. return nil, newError("V is not in context")
  21. }
  22. if err := v.RegisterFeature((*core.InboundHandlerManager)(nil), m); err != nil {
  23. return nil, newError("unable to register InboundHandlerManager").Base(err)
  24. }
  25. return m, nil
  26. }
  27. func (m *Manager) AddHandler(ctx context.Context, handler core.InboundHandler) error {
  28. m.handlers = append(m.handlers, handler)
  29. tag := handler.Tag()
  30. if len(tag) > 0 {
  31. m.taggedHandlers[tag] = handler
  32. }
  33. return nil
  34. }
  35. func (m *Manager) GetHandler(ctx context.Context, tag string) (core.InboundHandler, error) {
  36. handler, found := m.taggedHandlers[tag]
  37. if !found {
  38. return nil, newError("handler not found: ", tag)
  39. }
  40. return handler, nil
  41. }
  42. func (m *Manager) Start() error {
  43. for _, handler := range m.handlers {
  44. if err := handler.Start(); err != nil {
  45. return err
  46. }
  47. }
  48. return nil
  49. }
  50. func (m *Manager) Close() {
  51. for _, handler := range m.handlers {
  52. handler.Close()
  53. }
  54. }
  55. func NewHandler(ctx context.Context, config *core.InboundHandlerConfig) (core.InboundHandler, error) {
  56. rawReceiverSettings, err := config.ReceiverSettings.GetInstance()
  57. if err != nil {
  58. return nil, err
  59. }
  60. receiverSettings, ok := rawReceiverSettings.(*proxyman.ReceiverConfig)
  61. if !ok {
  62. return nil, newError("not a ReceiverConfig").AtError()
  63. }
  64. proxySettings, err := config.ProxySettings.GetInstance()
  65. if err != nil {
  66. return nil, err
  67. }
  68. tag := config.Tag
  69. allocStrategy := receiverSettings.AllocationStrategy
  70. if allocStrategy == nil || allocStrategy.Type == proxyman.AllocationStrategy_Always {
  71. return NewAlwaysOnInboundHandler(ctx, tag, receiverSettings, proxySettings)
  72. }
  73. if allocStrategy.Type == proxyman.AllocationStrategy_Random {
  74. return NewDynamicInboundHandler(ctx, tag, receiverSettings, proxySettings)
  75. }
  76. return nil, newError("unknown allocation strategy: ", receiverSettings.AllocationStrategy.Type).AtError()
  77. }
  78. func init() {
  79. common.Must(common.RegisterConfig((*proxyman.InboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  80. return New(ctx, config.(*proxyman.InboundConfig))
  81. }))
  82. common.Must(common.RegisterConfig((*core.InboundHandlerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  83. return NewHandler(ctx, config.(*core.InboundHandlerConfig))
  84. }))
  85. }