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/log"
|
2022-07-02 06:07:50 +00:00
|
|
|
"github.com/sagernet/sing-box/option"
|
2022-06-30 13:27:56 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ adapter.Service = (*Service)(nil)
|
|
|
|
|
|
|
|
type Service struct {
|
2022-07-02 06:07:50 +00:00
|
|
|
router adapter.Router
|
|
|
|
logger log.Logger
|
2022-06-30 13:27:56 +00:00
|
|
|
inbounds []adapter.Inbound
|
|
|
|
outbounds []adapter.Outbound
|
|
|
|
}
|
|
|
|
|
2022-07-02 06:07:50 +00:00
|
|
|
func NewService(ctx context.Context, options *option.Options) (*Service, error) {
|
|
|
|
var logOptions option.LogOption
|
2022-06-30 13:27:56 +00:00
|
|
|
if options.Log != nil {
|
2022-07-02 06:07:50 +00:00
|
|
|
logOptions = *options.Log
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-07-02 06:07:50 +00:00
|
|
|
logger, err := log.NewLogger(logOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-07-02 06:07:50 +00:00
|
|
|
router := route.NewRouter(logger)
|
|
|
|
var inbounds []adapter.Inbound
|
|
|
|
var outbounds []adapter.Outbound
|
2022-06-30 13:27:56 +00:00
|
|
|
if len(options.Inbounds) > 0 {
|
|
|
|
for i, inboundOptions := range options.Inbounds {
|
2022-07-01 11:34:02 +00:00
|
|
|
var inboundService adapter.Inbound
|
2022-07-02 06:07:50 +00:00
|
|
|
inboundService, err = inbound.New(ctx, router, logger, i, inboundOptions)
|
2022-06-30 13:27:56 +00:00
|
|
|
if err != nil {
|
2022-07-02 06:07:50 +00:00
|
|
|
return nil, err
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-07-02 06:07:50 +00:00
|
|
|
inbounds = append(inbounds, inboundService)
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, outboundOptions := range options.Outbounds {
|
2022-07-02 06:07:50 +00:00
|
|
|
var outboundService adapter.Outbound
|
|
|
|
outboundService, err = outbound.New(router, logger, i, outboundOptions)
|
2022-06-30 13:27:56 +00:00
|
|
|
if err != nil {
|
2022-07-02 06:07:50 +00:00
|
|
|
return nil, err
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-07-02 06:07:50 +00:00
|
|
|
outbounds = append(outbounds, outboundService)
|
|
|
|
}
|
|
|
|
if len(outbounds) == 0 {
|
|
|
|
outbounds = append(outbounds, outbound.NewDirect(nil, logger, "direct", &option.DirectOutboundOptions{}))
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-07-02 06:07:50 +00:00
|
|
|
router.UpdateOutbounds(outbounds)
|
|
|
|
err = router.UpdateRules(options.Routes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-07-02 06:07:50 +00:00
|
|
|
return &Service{
|
|
|
|
router: router,
|
|
|
|
logger: logger,
|
|
|
|
inbounds: inbounds,
|
|
|
|
outbounds: outbounds,
|
|
|
|
}, nil
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Start() error {
|
2022-07-02 06:07:50 +00:00
|
|
|
for _, in := range s.inbounds {
|
|
|
|
err := in.Start()
|
2022-06-30 13:27:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Close() error {
|
2022-07-02 06:07:50 +00:00
|
|
|
for _, in := range s.inbounds {
|
|
|
|
in.Close()
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-07-02 06:07:50 +00:00
|
|
|
for _, out := range s.outbounds {
|
|
|
|
common.Close(out)
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-07-02 06:07:50 +00:00
|
|
|
s.logger.Close()
|
|
|
|
s.router.Close()
|
2022-06-30 13:27:56 +00:00
|
|
|
return nil
|
|
|
|
}
|