github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/outbound/builder.go (about) 1 package outbound 2 3 import ( 4 "context" 5 6 "github.com/inazumav/sing-box/adapter" 7 C "github.com/inazumav/sing-box/constant" 8 "github.com/inazumav/sing-box/log" 9 "github.com/inazumav/sing-box/option" 10 E "github.com/sagernet/sing/common/exceptions" 11 ) 12 13 func New(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.Outbound) (adapter.Outbound, error) { 14 var metadata *adapter.InboundContext 15 if tag != "" { 16 ctx, metadata = adapter.AppendContext(ctx) 17 metadata.Outbound = tag 18 } 19 if options.Type == "" { 20 return nil, E.New("missing outbound type") 21 } 22 ctx = ContextWithTag(ctx, tag) 23 switch options.Type { 24 case C.TypeDirect: 25 return NewDirect(router, logger, tag, options.DirectOptions) 26 case C.TypeBlock: 27 return NewBlock(logger, tag), nil 28 case C.TypeDNS: 29 return NewDNS(router, tag), nil 30 case C.TypeSOCKS: 31 return NewSocks(router, logger, tag, options.SocksOptions) 32 case C.TypeHTTP: 33 return NewHTTP(ctx, router, logger, tag, options.HTTPOptions) 34 case C.TypeShadowsocks: 35 return NewShadowsocks(ctx, router, logger, tag, options.ShadowsocksOptions) 36 case C.TypeVMess: 37 return NewVMess(ctx, router, logger, tag, options.VMessOptions) 38 case C.TypeTrojan: 39 return NewTrojan(ctx, router, logger, tag, options.TrojanOptions) 40 case C.TypeHysteria: 41 return NewHysteria(ctx, router, logger, tag, options.HysteriaOptions) 42 case C.TypeTor: 43 return NewTor(ctx, router, logger, tag, options.TorOptions) 44 case C.TypeSSH: 45 return NewSSH(ctx, router, logger, tag, options.SSHOptions) 46 case C.TypeShadowTLS: 47 return NewShadowTLS(ctx, router, logger, tag, options.ShadowTLSOptions) 48 case C.TypeShadowsocksR: 49 return NewShadowsocksR(ctx, router, logger, tag, options.ShadowsocksROptions) 50 case C.TypeVLESS: 51 return NewVLESS(ctx, router, logger, tag, options.VLESSOptions) 52 case C.TypeTUIC: 53 return NewTUIC(ctx, router, logger, tag, options.TUICOptions) 54 case C.TypeHysteria2: 55 return NewHysteria2(ctx, router, logger, tag, options.Hysteria2Options) 56 case C.TypeSelector: 57 return NewSelector(router, logger, tag, options.SelectorOptions) 58 case C.TypeURLTest: 59 return NewURLTest(ctx, router, logger, tag, options.URLTestOptions) 60 default: 61 return nil, E.New("unknown outbound type: ", options.Type) 62 } 63 }