sing-box/service.go

126 lines
4.0 KiB
Go
Raw Normal View History

2022-06-30 13:27:56 +00:00
package box
import (
"context"
"github.com/sagernet/sing-box/adapter"
2022-07-01 11:34:02 +00:00
"github.com/sagernet/sing-box/adapter/inbound"
"github.com/sagernet/sing-box/adapter/outbound"
"github.com/sagernet/sing-box/adapter/route"
2022-06-30 13:27:56 +00:00
"github.com/sagernet/sing-box/config"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
"github.com/sirupsen/logrus"
)
var _ adapter.Service = (*Service)(nil)
type Service struct {
logger *logrus.Logger
inbounds []adapter.Inbound
outbounds []adapter.Outbound
router *route.Router
}
func NewService(ctx context.Context, options *config.Config) (service *Service, err error) {
logger := logrus.New()
logger.SetLevel(logrus.TraceLevel)
logger.Formatter.(*logrus.TextFormatter).ForceColors = true
logger.AddHook(new(log.Hook))
if options.Log != nil {
if options.Log.Level != "" {
logger.Level, err = logrus.ParseLevel(options.Log.Level)
if err != nil {
return
}
}
}
service = &Service{
logger: logger,
router: route.NewRouter(logrus.NewEntry(logger).WithFields(logrus.Fields{"prefix": "router: "})),
}
if len(options.Inbounds) > 0 {
for i, inboundOptions := range options.Inbounds {
var prefix string
if inboundOptions.Tag != "" {
prefix = inboundOptions.Tag
} else {
prefix = F.ToString(i)
}
prefix = F.ToString("inbound/", inboundOptions.Type, "[", prefix, "]: ")
inboundLogger := logrus.NewEntry(logger).WithFields(logrus.Fields{"prefix": prefix})
2022-07-01 11:34:02 +00:00
var inboundService adapter.Inbound
2022-06-30 13:27:56 +00:00
switch inboundOptions.Type {
case C.TypeDirect:
2022-07-01 11:34:02 +00:00
inboundService = inbound.NewDirect(ctx, service.router, inboundLogger, inboundOptions.Tag, inboundOptions.DirectOptions)
2022-06-30 13:27:56 +00:00
case C.TypeSocks:
2022-07-01 11:34:02 +00:00
inboundService = inbound.NewSocks(ctx, service.router, inboundLogger, inboundOptions.Tag, inboundOptions.SocksOptions)
2022-06-30 13:27:56 +00:00
case C.TypeHTTP:
2022-07-01 11:34:02 +00:00
inboundService = inbound.NewHTTP(ctx, service.router, inboundLogger, inboundOptions.Tag, inboundOptions.HTTPOptions)
2022-06-30 13:27:56 +00:00
case C.TypeMixed:
2022-07-01 11:34:02 +00:00
inboundService = inbound.NewMixed(ctx, service.router, inboundLogger, inboundOptions.Tag, inboundOptions.MixedOptions)
2022-06-30 13:27:56 +00:00
case C.TypeShadowsocks:
2022-07-01 11:34:02 +00:00
inboundService, err = inbound.NewShadowsocks(ctx, service.router, inboundLogger, inboundOptions.Tag, inboundOptions.ShadowsocksOptions)
2022-06-30 13:27:56 +00:00
default:
err = E.New("unknown inbound type: " + inboundOptions.Type)
}
if err != nil {
return
}
2022-07-01 11:34:02 +00:00
service.inbounds = append(service.inbounds, inboundService)
2022-06-30 13:27:56 +00:00
}
}
for i, outboundOptions := range options.Outbounds {
var prefix string
if outboundOptions.Tag != "" {
prefix = outboundOptions.Tag
} else {
prefix = F.ToString(i)
}
prefix = F.ToString("outbound/", outboundOptions.Type, "[", prefix, "]: ")
outboundLogger := logrus.NewEntry(logger).WithFields(logrus.Fields{"prefix": prefix})
2022-07-01 11:34:02 +00:00
var outboundHandler adapter.Outbound
2022-06-30 13:27:56 +00:00
switch outboundOptions.Type {
case C.TypeDirect:
2022-07-01 11:34:02 +00:00
outboundHandler = outbound.NewDirect(service.router, outboundLogger, outboundOptions.Tag, outboundOptions.DirectOptions)
2022-06-30 13:27:56 +00:00
case C.TypeShadowsocks:
2022-07-01 11:34:02 +00:00
outboundHandler, err = outbound.NewShadowsocks(service.router, outboundLogger, outboundOptions.Tag, outboundOptions.ShadowsocksOptions)
2022-06-30 13:27:56 +00:00
default:
err = E.New("unknown outbound type: " + outboundOptions.Type)
}
if err != nil {
return
}
2022-07-01 11:34:02 +00:00
service.outbounds = append(service.outbounds, outboundHandler)
service.router.AddOutbound(outboundHandler)
2022-06-30 13:27:56 +00:00
}
if len(service.outbounds) == 0 {
2022-07-01 11:34:02 +00:00
service.outbounds = append(service.outbounds, outbound.NewDirect(nil, logger, "direct", &config.DirectOutboundOptions{}))
2022-06-30 13:27:56 +00:00
service.router.AddOutbound(service.outbounds[0])
}
return
}
func (s *Service) Start() error {
for _, inbound := range s.inbounds {
err := inbound.Start()
if err != nil {
return err
}
}
return nil
}
func (s *Service) Close() error {
for _, inbound := range s.inbounds {
inbound.Close()
}
for _, outbound := range s.outbounds {
common.Close(outbound)
}
return nil
}