sing-box/inbound/builder.go

42 lines
1.3 KiB
Go
Raw Normal View History

2022-07-02 06:07:50 +00:00
package inbound
import (
"context"
2022-07-06 07:01:09 +00:00
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
2022-07-02 06:07:50 +00:00
"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"
)
func New(ctx context.Context, router adapter.Router, logger log.Logger, index int, options option.Inbound) (adapter.Inbound, error) {
2022-07-02 17:57:04 +00:00
if common.IsEmptyByEquals(options) {
return nil, E.New("empty inbound config")
}
2022-07-02 06:07:50 +00:00
var tag string
if options.Tag != "" {
tag = options.Tag
} else {
tag = F.ToString(index)
}
inboundLogger := logger.WithPrefix(F.ToString("inbound/", options.Type, "[", tag, "]: "))
switch options.Type {
case C.TypeDirect:
2022-07-02 17:57:04 +00:00
return NewDirect(ctx, router, inboundLogger, options.Tag, options.DirectOptions), nil
2022-07-02 06:07:50 +00:00
case C.TypeSocks:
2022-07-02 17:57:04 +00:00
return NewSocks(ctx, router, inboundLogger, options.Tag, options.SocksOptions), nil
2022-07-02 06:07:50 +00:00
case C.TypeHTTP:
2022-07-02 17:57:04 +00:00
return NewHTTP(ctx, router, inboundLogger, options.Tag, options.HTTPOptions), nil
2022-07-02 06:07:50 +00:00
case C.TypeMixed:
2022-07-02 17:57:04 +00:00
return NewMixed(ctx, router, inboundLogger, options.Tag, options.MixedOptions), nil
2022-07-02 06:07:50 +00:00
case C.TypeShadowsocks:
2022-07-02 17:57:04 +00:00
return NewShadowsocks(ctx, router, inboundLogger, options.Tag, options.ShadowsocksOptions)
2022-07-02 06:07:50 +00:00
default:
2022-07-02 17:57:04 +00:00
return nil, E.New("unknown inbound type: ", options.Type)
2022-07-02 06:07:50 +00:00
}
}