inbound.go 2.6 KB

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