builder.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package inbound
  2. import (
  3. "context"
  4. "github.com/sagernet/sing-box/adapter"
  5. C "github.com/sagernet/sing-box/constant"
  6. "github.com/sagernet/sing-box/log"
  7. "github.com/sagernet/sing-box/option"
  8. "github.com/sagernet/sing/common"
  9. E "github.com/sagernet/sing/common/exceptions"
  10. F "github.com/sagernet/sing/common/format"
  11. )
  12. func New(ctx context.Context, router adapter.Router, logger log.Logger, index int, options option.Inbound) (adapter.Inbound, error) {
  13. if common.IsEmptyByEquals(options) {
  14. return nil, E.New("empty inbound config")
  15. }
  16. var tag string
  17. if options.Tag != "" {
  18. tag = options.Tag
  19. } else {
  20. tag = F.ToString(index)
  21. }
  22. inboundLogger := logger.WithPrefix(F.ToString("inbound/", options.Type, "[", tag, "]: "))
  23. switch options.Type {
  24. case C.TypeDirect:
  25. return NewDirect(ctx, router, inboundLogger, options.Tag, options.DirectOptions), nil
  26. case C.TypeSocks:
  27. return NewSocks(ctx, router, inboundLogger, options.Tag, options.SocksOptions), nil
  28. case C.TypeHTTP:
  29. return NewHTTP(ctx, router, inboundLogger, options.Tag, options.HTTPOptions), nil
  30. case C.TypeMixed:
  31. return NewMixed(ctx, router, inboundLogger, options.Tag, options.MixedOptions), nil
  32. case C.TypeShadowsocks:
  33. return NewShadowsocks(ctx, router, inboundLogger, options.Tag, options.ShadowsocksOptions)
  34. case C.TypeTun:
  35. return NewTun(ctx, router, inboundLogger, options.Tag, options.TunOptions)
  36. default:
  37. return nil, E.New("unknown inbound type: ", options.Type)
  38. }
  39. }