github.com/sagernet/sing-box@v1.9.0-rc.20/outbound/builder.go (about)

     1  package outbound
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/sagernet/sing-box/adapter"
     7  	C "github.com/sagernet/sing-box/constant"
     8  	"github.com/sagernet/sing-box/log"
     9  	"github.com/sagernet/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.TypeWireGuard:
    41  		return NewWireGuard(ctx, router, logger, tag, options.WireGuardOptions)
    42  	case C.TypeHysteria:
    43  		return NewHysteria(ctx, router, logger, tag, options.HysteriaOptions)
    44  	case C.TypeTor:
    45  		return NewTor(ctx, router, logger, tag, options.TorOptions)
    46  	case C.TypeSSH:
    47  		return NewSSH(ctx, router, logger, tag, options.SSHOptions)
    48  	case C.TypeShadowTLS:
    49  		return NewShadowTLS(ctx, router, logger, tag, options.ShadowTLSOptions)
    50  	case C.TypeShadowsocksR:
    51  		return NewShadowsocksR(ctx, router, logger, tag, options.ShadowsocksROptions)
    52  	case C.TypeVLESS:
    53  		return NewVLESS(ctx, router, logger, tag, options.VLESSOptions)
    54  	case C.TypeTUIC:
    55  		return NewTUIC(ctx, router, logger, tag, options.TUICOptions)
    56  	case C.TypeHysteria2:
    57  		return NewHysteria2(ctx, router, logger, tag, options.Hysteria2Options)
    58  	case C.TypeSelector:
    59  		return NewSelector(ctx, router, logger, tag, options.SelectorOptions)
    60  	case C.TypeURLTest:
    61  		return NewURLTest(ctx, router, logger, tag, options.URLTestOptions)
    62  	default:
    63  		return nil, E.New("unknown outbound type: ", options.Type)
    64  	}
    65  }