sing-box/adapter/outbound/builder.go

35 lines
1.1 KiB
Go
Raw Normal View History

2022-07-02 14:07:50 +08:00
package outbound
import (
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
2022-07-02 22:55:10 +08:00
"github.com/sagernet/sing/common"
2022-07-03 01:57:04 +08:00
E "github.com/sagernet/sing/common/exceptions"
2022-07-02 14:07:50 +08:00
F "github.com/sagernet/sing/common/format"
)
func New(router adapter.Router, logger log.Logger, index int, options option.Outbound) (adapter.Outbound, error) {
2022-07-03 01:57:04 +08:00
if common.IsEmpty(options) {
return nil, E.New("empty outbound config")
}
2022-07-02 14:07:50 +08:00
var tag string
if options.Tag != "" {
tag = options.Tag
} else {
tag = F.ToString(index)
}
outboundLogger := logger.WithPrefix(F.ToString("outbound/", options.Type, "[", tag, "]: "))
switch options.Type {
case C.TypeDirect:
2022-07-03 01:57:04 +08:00
return NewDirect(router, outboundLogger, options.Tag, options.DirectOptions), nil
2022-07-03 13:14:49 +08:00
case C.TypeSocks:
return NewSocks(router, outboundLogger, options.Tag, options.SocksOptions)
2022-07-02 14:07:50 +08:00
case C.TypeShadowsocks:
2022-07-03 01:57:04 +08:00
return NewShadowsocks(router, outboundLogger, options.Tag, options.ShadowsocksOptions)
2022-07-02 14:07:50 +08:00
default:
2022-07-03 01:57:04 +08:00
return nil, E.New("unknown outbound type: ", options.Type)
2022-07-02 14:07:50 +08:00
}
}