2022-07-02 06:07:50 +00:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"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 14:55:10 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
2022-07-02 06:07:50 +00:00
|
|
|
F "github.com/sagernet/sing/common/format"
|
|
|
|
)
|
|
|
|
|
|
|
|
func New(ctx context.Context, router adapter.Router, logger log.Logger, index int, options option.Inbound) (adapter.Inbound, error) {
|
|
|
|
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 14:55:10 +00:00
|
|
|
return NewDirect(ctx, router, inboundLogger, options.Tag, common.PtrValueOrDefault(options.DirectOptions)), nil
|
2022-07-02 06:07:50 +00:00
|
|
|
case C.TypeSocks:
|
2022-07-02 14:55:10 +00:00
|
|
|
return NewSocks(ctx, router, inboundLogger, options.Tag, common.PtrValueOrDefault(options.SocksOptions)), nil
|
2022-07-02 06:07:50 +00:00
|
|
|
case C.TypeHTTP:
|
2022-07-02 14:55:10 +00:00
|
|
|
return NewHTTP(ctx, router, inboundLogger, options.Tag, common.PtrValueOrDefault(options.HTTPOptions)), nil
|
2022-07-02 06:07:50 +00:00
|
|
|
case C.TypeMixed:
|
2022-07-02 14:55:10 +00:00
|
|
|
return NewMixed(ctx, router, inboundLogger, options.Tag, common.PtrValueOrDefault(options.MixedOptions)), nil
|
2022-07-02 06:07:50 +00:00
|
|
|
case C.TypeShadowsocks:
|
2022-07-02 14:55:10 +00:00
|
|
|
return NewShadowsocks(ctx, router, inboundLogger, options.Tag, common.PtrValueOrDefault(options.ShadowsocksOptions))
|
2022-07-02 06:07:50 +00:00
|
|
|
default:
|
|
|
|
panic(F.ToString("unknown inbound type: ", options.Type))
|
|
|
|
}
|
|
|
|
}
|