2022-06-30 21:27:56 +08:00
|
|
|
package box
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-04 19:34:45 +08:00
|
|
|
"time"
|
2022-06-30 21:27:56 +08:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-07-03 20:59:25 +08:00
|
|
|
"github.com/sagernet/sing-box/inbound"
|
2022-06-30 21:27:56 +08:00
|
|
|
"github.com/sagernet/sing-box/log"
|
2022-07-02 14:07:50 +08:00
|
|
|
"github.com/sagernet/sing-box/option"
|
2022-07-03 23:23:18 +08:00
|
|
|
"github.com/sagernet/sing-box/outbound"
|
2022-07-03 20:59:25 +08:00
|
|
|
"github.com/sagernet/sing-box/route"
|
2022-07-08 23:03:57 +08:00
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
F "github.com/sagernet/sing/common/format"
|
2022-06-30 21:27:56 +08:00
|
|
|
)
|
|
|
|
|
2022-07-07 21:47:21 +08:00
|
|
|
var _ adapter.Service = (*Box)(nil)
|
2022-06-30 21:27:56 +08:00
|
|
|
|
2022-07-07 21:47:21 +08:00
|
|
|
type Box struct {
|
2022-07-02 14:07:50 +08:00
|
|
|
router adapter.Router
|
|
|
|
logger log.Logger
|
2022-06-30 21:27:56 +08:00
|
|
|
inbounds []adapter.Inbound
|
|
|
|
outbounds []adapter.Outbound
|
2022-07-04 19:34:45 +08:00
|
|
|
createdAt time.Time
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
|
|
|
|
2022-07-07 21:47:21 +08:00
|
|
|
func New(ctx context.Context, options option.Options) (*Box, error) {
|
2022-07-04 19:34:45 +08:00
|
|
|
createdAt := time.Now()
|
2022-07-02 22:55:10 +08:00
|
|
|
logger, err := log.NewLogger(common.PtrValueOrDefault(options.Log))
|
|
|
|
if err != nil {
|
2022-07-03 01:57:04 +08:00
|
|
|
return nil, E.Cause(err, "parse log options")
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
2022-07-07 21:47:21 +08:00
|
|
|
router, err := route.NewRouter(ctx, logger, common.PtrValueOrDefault(options.Route), common.PtrValueOrDefault(options.DNS))
|
2022-07-02 14:07:50 +08:00
|
|
|
if err != nil {
|
2022-07-03 01:57:04 +08:00
|
|
|
return nil, E.Cause(err, "parse route options")
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
2022-07-02 22:55:10 +08:00
|
|
|
inbounds := make([]adapter.Inbound, 0, len(options.Inbounds))
|
|
|
|
outbounds := make([]adapter.Outbound, 0, len(options.Outbounds))
|
|
|
|
for i, inboundOptions := range options.Inbounds {
|
2022-07-03 23:23:18 +08:00
|
|
|
var in adapter.Inbound
|
|
|
|
in, err = inbound.New(ctx, router, logger, i, inboundOptions)
|
2022-07-02 22:55:10 +08:00
|
|
|
if err != nil {
|
2022-07-03 01:57:04 +08:00
|
|
|
return nil, E.Cause(err, "parse inbound[", i, "]")
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
2022-07-03 23:23:18 +08:00
|
|
|
inbounds = append(inbounds, in)
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
|
|
|
for i, outboundOptions := range options.Outbounds {
|
2022-07-03 23:23:18 +08:00
|
|
|
var out adapter.Outbound
|
|
|
|
out, err = outbound.New(router, logger, i, outboundOptions)
|
2022-06-30 21:27:56 +08:00
|
|
|
if err != nil {
|
2022-07-03 01:57:04 +08:00
|
|
|
return nil, E.Cause(err, "parse outbound[", i, "]")
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
2022-07-03 23:23:18 +08:00
|
|
|
outbounds = append(outbounds, out)
|
2022-07-02 14:07:50 +08:00
|
|
|
}
|
2022-07-03 23:23:18 +08:00
|
|
|
err = router.Initialize(outbounds, func() adapter.Outbound {
|
|
|
|
out, oErr := outbound.New(router, logger, 0, option.Outbound{Type: "direct", Tag: "default"})
|
|
|
|
common.Must(oErr)
|
|
|
|
outbounds = append(outbounds, out)
|
|
|
|
return out
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
2022-07-07 21:47:21 +08:00
|
|
|
return &Box{
|
2022-07-02 14:07:50 +08:00
|
|
|
router: router,
|
|
|
|
logger: logger,
|
|
|
|
inbounds: inbounds,
|
|
|
|
outbounds: outbounds,
|
2022-07-04 19:34:45 +08:00
|
|
|
createdAt: createdAt,
|
2022-07-02 14:07:50 +08:00
|
|
|
}, nil
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
|
|
|
|
2022-07-07 21:47:21 +08:00
|
|
|
func (s *Box) Start() error {
|
2022-07-03 01:57:04 +08:00
|
|
|
err := s.logger.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-04 19:34:45 +08:00
|
|
|
err = s.router.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-02 14:07:50 +08:00
|
|
|
for _, in := range s.inbounds {
|
2022-07-03 01:57:04 +08:00
|
|
|
err = in.Start()
|
2022-06-30 21:27:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-07-04 19:34:45 +08:00
|
|
|
s.logger.Info("sing-box started (", F.Seconds(time.Since(s.createdAt).Seconds()), "s)")
|
|
|
|
return nil
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
|
|
|
|
2022-07-07 21:47:21 +08:00
|
|
|
func (s *Box) Close() error {
|
2022-07-02 14:07:50 +08:00
|
|
|
for _, in := range s.inbounds {
|
|
|
|
in.Close()
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
2022-07-02 14:07:50 +08:00
|
|
|
for _, out := range s.outbounds {
|
|
|
|
common.Close(out)
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
2022-07-03 01:57:04 +08:00
|
|
|
return common.Close(
|
|
|
|
s.router,
|
|
|
|
s.logger,
|
|
|
|
)
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|