sing-box/box.go

159 lines
3.8 KiB
Go
Raw Normal View History

2022-06-30 13:27:56 +00:00
package box
import (
"context"
2022-07-12 07:17:29 +00:00
"io"
"os"
2022-07-04 11:34:45 +00:00
"time"
2022-06-30 13:27:56 +00:00
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/inbound"
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"
"github.com/sagernet/sing-box/outbound"
"github.com/sagernet/sing-box/route"
2022-07-08 15:03:57 +00:00
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
2022-06-30 13:27:56 +00:00
)
2022-07-07 13:47:21 +00:00
var _ adapter.Service = (*Box)(nil)
2022-06-30 13:27:56 +00:00
2022-07-07 13:47:21 +00:00
type Box struct {
2022-07-12 07:17:29 +00:00
createdAt time.Time
router adapter.Router
inbounds []adapter.Inbound
outbounds []adapter.Outbound
logFactory log.Factory
logger log.ContextLogger
logFile *os.File
2022-06-30 13:27:56 +00:00
}
2022-07-07 13:47:21 +00:00
func New(ctx context.Context, options option.Options) (*Box, error) {
2022-07-04 11:34:45 +00:00
createdAt := time.Now()
2022-07-12 07:17:29 +00:00
logOptions := common.PtrValueOrDefault(options.Log)
var logFactory log.Factory
var logFile *os.File
if logOptions.Disabled {
logFactory = log.NewNOPFactory()
} else {
var logWriter io.Writer
switch logOptions.Output {
case "", "stderr":
logWriter = os.Stderr
case "stdout":
logWriter = os.Stdout
default:
var err error
logFile, err = os.OpenFile(logOptions.Output, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return nil, err
}
}
logFormatter := log.Formatter{
BaseTime: createdAt,
DisableColors: logOptions.DisableColor || logFile != nil,
DisableTimestamp: !logOptions.Timestamp && logFile != nil,
FullTimestamp: logOptions.Timestamp,
TimestampFormat: "-0700 2006-01-02 15:04:05",
}
logFactory = log.NewFactory(logFormatter, logWriter)
2022-06-30 13:27:56 +00:00
}
2022-07-12 07:17:29 +00:00
router, err := route.NewRouter(
ctx,
logFactory.NewLogger("router"),
logFactory.NewLogger("dns"),
common.PtrValueOrDefault(options.Route),
common.PtrValueOrDefault(options.DNS),
)
2022-07-02 06:07:50 +00:00
if err != nil {
2022-07-02 17:57:04 +00:00
return nil, E.Cause(err, "parse route options")
2022-06-30 13:27:56 +00:00
}
2022-07-02 14:55:10 +00:00
inbounds := make([]adapter.Inbound, 0, len(options.Inbounds))
outbounds := make([]adapter.Outbound, 0, len(options.Outbounds))
for i, inboundOptions := range options.Inbounds {
var in adapter.Inbound
2022-07-12 07:17:29 +00:00
var tag string
if inboundOptions.Tag != "" {
tag = inboundOptions.Tag
} else {
tag = F.ToString(i)
}
in, err = inbound.New(
ctx,
router,
logFactory.NewLogger(F.ToString("inbound/", inboundOptions.Type, "[", tag, "]")),
inboundOptions,
)
2022-07-02 14:55:10 +00:00
if err != nil {
2022-07-02 17:57:04 +00:00
return nil, E.Cause(err, "parse inbound[", i, "]")
2022-06-30 13:27:56 +00:00
}
inbounds = append(inbounds, in)
2022-06-30 13:27:56 +00:00
}
for i, outboundOptions := range options.Outbounds {
var out adapter.Outbound
2022-07-12 07:17:29 +00:00
var tag string
if outboundOptions.Tag != "" {
tag = outboundOptions.Tag
} else {
tag = F.ToString(i)
}
out, err = outbound.New(
router,
logFactory.NewLogger(F.ToString("outbound/", outboundOptions.Type, "[", tag, "]")),
outboundOptions)
2022-06-30 13:27:56 +00:00
if err != nil {
2022-07-02 17:57:04 +00:00
return nil, E.Cause(err, "parse outbound[", i, "]")
2022-06-30 13:27:56 +00:00
}
outbounds = append(outbounds, out)
2022-07-02 06:07:50 +00:00
}
err = router.Initialize(outbounds, func() adapter.Outbound {
2022-07-12 07:17:29 +00:00
out, oErr := outbound.New(router, logFactory.NewLogger("outbound/direct"), option.Outbound{Type: "direct", Tag: "default"})
common.Must(oErr)
outbounds = append(outbounds, out)
return out
})
if err != nil {
return nil, err
2022-06-30 13:27:56 +00:00
}
2022-07-07 13:47:21 +00:00
return &Box{
2022-07-12 07:17:29 +00:00
router: router,
inbounds: inbounds,
outbounds: outbounds,
createdAt: createdAt,
logFactory: logFactory,
logger: logFactory.NewLogger(""),
logFile: logFile,
2022-07-02 06:07:50 +00:00
}, nil
2022-06-30 13:27:56 +00:00
}
2022-07-07 13:47:21 +00:00
func (s *Box) Start() error {
2022-07-12 07:17:29 +00:00
err := s.router.Start()
2022-07-04 11:34:45 +00:00
if err != nil {
return err
}
2022-07-02 06:07:50 +00:00
for _, in := range s.inbounds {
2022-07-02 17:57:04 +00:00
err = in.Start()
2022-06-30 13:27:56 +00:00
if err != nil {
return err
}
}
2022-07-04 11:34:45 +00:00
s.logger.Info("sing-box started (", F.Seconds(time.Since(s.createdAt).Seconds()), "s)")
return nil
2022-06-30 13:27:56 +00:00
}
2022-07-07 13:47:21 +00:00
func (s *Box) 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 17:57:04 +00:00
return common.Close(
s.router,
2022-07-12 07:17:29 +00:00
common.PtrOrNil(s.logFile),
2022-07-02 17:57:04 +00:00
)
2022-06-30 13:27:56 +00:00
}