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 17:57:04 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
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) {
|
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
|
|
|
}
|
|
|
|
}
|