2022-06-30 13:27:56 +00:00
|
|
|
package box
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-24 11:03:00 +00:00
|
|
|
"fmt"
|
2022-07-12 07:17:29 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2022-08-24 11:03:00 +00:00
|
|
|
"runtime/debug"
|
2022-07-04 11:34:45 +00:00
|
|
|
"time"
|
2022-06-30 13:27:56 +00:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-10-25 04:55:00 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2022-07-19 23:12:40 +00:00
|
|
|
"github.com/sagernet/sing-box/experimental"
|
2023-02-28 11:02:27 +00:00
|
|
|
"github.com/sagernet/sing-box/experimental/libbox/platform"
|
2022-07-03 12:59:25 +00:00
|
|
|
"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"
|
2022-07-03 15:23:18 +00:00
|
|
|
"github.com/sagernet/sing-box/outbound"
|
2022-07-03 12:59:25 +00:00
|
|
|
"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 {
|
2023-03-18 12:26:58 +00:00
|
|
|
createdAt time.Time
|
|
|
|
router adapter.Router
|
|
|
|
inbounds []adapter.Inbound
|
|
|
|
outbounds []adapter.Outbound
|
|
|
|
logFactory log.Factory
|
|
|
|
logger log.ContextLogger
|
|
|
|
logFile *os.File
|
|
|
|
preServices map[string]adapter.Service
|
|
|
|
postServices map[string]adapter.Service
|
|
|
|
done chan struct{}
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 11:02:27 +00:00
|
|
|
func New(ctx context.Context, options option.Options, platformInterface platform.Interface) (*Box, error) {
|
2022-07-04 11:34:45 +00:00
|
|
|
createdAt := time.Now()
|
2023-03-17 06:51:09 +00:00
|
|
|
|
|
|
|
experimentalOptions := common.PtrValueOrDefault(options.Experimental)
|
|
|
|
applyDebugOptions(common.PtrValueOrDefault(experimentalOptions.Debug))
|
2022-07-12 07:17:29 +00:00
|
|
|
|
2022-07-19 14:16:49 +00:00
|
|
|
var needClashAPI bool
|
2022-09-26 11:37:06 +00:00
|
|
|
var needV2RayAPI bool
|
2023-03-17 06:51:09 +00:00
|
|
|
if experimentalOptions.ClashAPI != nil && experimentalOptions.ClashAPI.ExternalController != "" {
|
|
|
|
needClashAPI = true
|
|
|
|
}
|
|
|
|
if experimentalOptions.V2RayAPI != nil && experimentalOptions.V2RayAPI.Listen != "" {
|
|
|
|
needV2RayAPI = true
|
2022-07-19 14:16:49 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 06:51:09 +00:00
|
|
|
logOptions := common.PtrValueOrDefault(options.Log)
|
|
|
|
|
2022-07-12 07:17:29 +00:00
|
|
|
var logFactory log.Factory
|
2022-07-19 14:16:49 +00:00
|
|
|
var observableLogFactory log.ObservableFactory
|
2022-07-12 07:17:29 +00:00
|
|
|
var logFile *os.File
|
2022-10-25 04:55:00 +00:00
|
|
|
var logWriter io.Writer
|
2022-07-12 07:17:29 +00:00
|
|
|
if logOptions.Disabled {
|
2022-07-20 01:41:44 +00:00
|
|
|
observableLogFactory = log.NewNOPFactory()
|
|
|
|
logFactory = observableLogFactory
|
2022-07-12 07:17:29 +00:00
|
|
|
} else {
|
|
|
|
switch logOptions.Output {
|
2022-10-25 04:55:00 +00:00
|
|
|
case "":
|
2023-02-28 11:02:27 +00:00
|
|
|
if platformInterface != nil {
|
2022-10-25 04:55:00 +00:00
|
|
|
logWriter = io.Discard
|
|
|
|
} else {
|
|
|
|
logWriter = os.Stdout
|
|
|
|
}
|
|
|
|
case "stderr":
|
2022-07-12 07:17:29 +00:00
|
|
|
logWriter = os.Stderr
|
|
|
|
case "stdout":
|
|
|
|
logWriter = os.Stdout
|
|
|
|
default:
|
|
|
|
var err error
|
2022-10-25 04:55:00 +00:00
|
|
|
logFile, err = os.OpenFile(C.BasePath(logOptions.Output), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
|
2022-07-12 07:17:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-18 23:50:19 +00:00
|
|
|
logWriter = logFile
|
2022-07-12 07:17:29 +00:00
|
|
|
}
|
2023-02-22 12:52:57 +00:00
|
|
|
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",
|
|
|
|
}
|
|
|
|
if needClashAPI {
|
2023-02-28 11:02:27 +00:00
|
|
|
observableLogFactory = log.NewObservableFactory(logFormatter, logWriter, platformInterface)
|
2023-02-22 12:52:57 +00:00
|
|
|
logFactory = observableLogFactory
|
|
|
|
} else {
|
2023-02-28 11:02:27 +00:00
|
|
|
logFactory = log.NewFactory(logFormatter, logWriter, platformInterface)
|
2023-02-22 12:52:57 +00:00
|
|
|
}
|
|
|
|
if logOptions.Level != "" {
|
|
|
|
logLevel, err := log.ParseLevel(logOptions.Level)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "parse log level")
|
|
|
|
}
|
|
|
|
logFactory.SetLevel(logLevel)
|
|
|
|
} else {
|
|
|
|
logFactory.SetLevel(log.LevelTrace)
|
2022-07-18 04:32:31 +00:00
|
|
|
}
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-07-12 07:17:29 +00:00
|
|
|
|
|
|
|
router, err := route.NewRouter(
|
|
|
|
ctx,
|
2022-11-28 05:10:56 +00:00
|
|
|
logFactory,
|
2022-07-12 07:17:29 +00:00
|
|
|
common.PtrValueOrDefault(options.Route),
|
|
|
|
common.PtrValueOrDefault(options.DNS),
|
2023-02-21 06:53:00 +00:00
|
|
|
common.PtrValueOrDefault(options.NTP),
|
2022-08-04 14:01:20 +00:00
|
|
|
options.Inbounds,
|
2023-02-28 11:02:27 +00:00
|
|
|
platformInterface,
|
2022-07-12 07:17:29 +00:00
|
|
|
)
|
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 {
|
2022-07-03 15:23:18 +00:00
|
|
|
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,
|
2023-02-28 11:02:27 +00:00
|
|
|
platformInterface,
|
2022-07-12 07:17:29 +00:00
|
|
|
)
|
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
|
|
|
}
|
2022-07-03 15:23:18 +00:00
|
|
|
inbounds = append(inbounds, in)
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
for i, outboundOptions := range options.Outbounds {
|
2022-07-03 15:23:18 +00:00
|
|
|
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(
|
2022-07-29 16:29:22 +00:00
|
|
|
ctx,
|
2022-07-12 07:17:29 +00:00
|
|
|
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
|
|
|
}
|
2022-07-03 15:23:18 +00:00
|
|
|
outbounds = append(outbounds, out)
|
2022-07-02 06:07:50 +00:00
|
|
|
}
|
2022-08-29 11:43:13 +00:00
|
|
|
err = router.Initialize(inbounds, outbounds, func() adapter.Outbound {
|
2022-07-29 16:29:22 +00:00
|
|
|
out, oErr := outbound.New(ctx, router, logFactory.NewLogger("outbound/direct"), option.Outbound{Type: "direct", Tag: "default"})
|
2022-07-03 15:23:18 +00:00
|
|
|
common.Must(oErr)
|
|
|
|
outbounds = append(outbounds, out)
|
|
|
|
return out
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2023-03-18 12:26:58 +00:00
|
|
|
preServices := make(map[string]adapter.Service)
|
|
|
|
postServices := make(map[string]adapter.Service)
|
2022-07-19 14:16:49 +00:00
|
|
|
if needClashAPI {
|
2023-03-18 12:26:58 +00:00
|
|
|
clashServer, err := experimental.NewClashServer(router, observableLogFactory, common.PtrValueOrDefault(options.Experimental.ClashAPI))
|
2022-07-19 23:12:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "create clash api server")
|
|
|
|
}
|
2022-09-10 06:09:47 +00:00
|
|
|
router.SetClashServer(clashServer)
|
2023-03-18 12:26:58 +00:00
|
|
|
preServices["clash api"] = clashServer
|
2022-07-19 14:16:49 +00:00
|
|
|
}
|
2022-09-26 11:37:06 +00:00
|
|
|
if needV2RayAPI {
|
2023-03-18 12:26:58 +00:00
|
|
|
v2rayServer, err := experimental.NewV2RayServer(logFactory.NewLogger("v2ray-api"), common.PtrValueOrDefault(options.Experimental.V2RayAPI))
|
2022-09-26 11:37:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "create v2ray api server")
|
|
|
|
}
|
|
|
|
router.SetV2RayServer(v2rayServer)
|
2023-03-18 12:26:58 +00:00
|
|
|
preServices["v2ray api"] = v2rayServer
|
2022-09-26 11:37:06 +00:00
|
|
|
}
|
2022-07-07 13:47:21 +00:00
|
|
|
return &Box{
|
2023-03-18 12:26:58 +00:00
|
|
|
router: router,
|
|
|
|
inbounds: inbounds,
|
|
|
|
outbounds: outbounds,
|
|
|
|
createdAt: createdAt,
|
|
|
|
logFactory: logFactory,
|
|
|
|
logger: logFactory.Logger(),
|
|
|
|
logFile: logFile,
|
|
|
|
preServices: preServices,
|
|
|
|
postServices: postServices,
|
|
|
|
done: make(chan struct{}),
|
2022-07-02 06:07:50 +00:00
|
|
|
}, nil
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-18 12:26:58 +00:00
|
|
|
func (s *Box) PreStart() error {
|
|
|
|
err := s.preStart()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: remove catch error
|
|
|
|
defer func() {
|
|
|
|
v := recover()
|
|
|
|
if v != nil {
|
|
|
|
log.Error(E.Cause(err, "origin error"))
|
|
|
|
debug.PrintStack()
|
|
|
|
panic("panic on early close: " + fmt.Sprint(v))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
s.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.logger.Info("sing-box pre-started (", F.Seconds(time.Since(s.createdAt).Seconds()), "s)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-07 13:47:21 +00:00
|
|
|
func (s *Box) Start() error {
|
2022-08-22 04:43:21 +00:00
|
|
|
err := s.start()
|
|
|
|
if err != nil {
|
2022-08-24 11:03:00 +00:00
|
|
|
// TODO: remove catch error
|
|
|
|
defer func() {
|
|
|
|
v := recover()
|
|
|
|
if v != nil {
|
|
|
|
log.Error(E.Cause(err, "origin error"))
|
|
|
|
debug.PrintStack()
|
|
|
|
panic("panic on early close: " + fmt.Sprint(v))
|
|
|
|
}
|
|
|
|
}()
|
2022-08-22 04:43:21 +00:00
|
|
|
s.Close()
|
2023-03-18 12:26:58 +00:00
|
|
|
return err
|
2022-08-22 04:43:21 +00:00
|
|
|
}
|
2023-03-18 12:26:58 +00:00
|
|
|
s.logger.Info("sing-box started (", F.Seconds(time.Since(s.createdAt).Seconds()), "s)")
|
|
|
|
return nil
|
2022-08-22 04:43:21 +00:00
|
|
|
}
|
|
|
|
|
2023-03-18 12:26:58 +00:00
|
|
|
func (s *Box) preStart() error {
|
|
|
|
for serviceName, service := range s.preServices {
|
2023-03-21 13:36:17 +00:00
|
|
|
s.logger.Trace("pre-starting ", serviceName)
|
2023-03-18 12:26:58 +00:00
|
|
|
err := adapter.PreStart(service)
|
2023-03-05 03:05:30 +00:00
|
|
|
if err != nil {
|
2023-03-18 12:26:58 +00:00
|
|
|
return E.Cause(err, "pre-start ", serviceName)
|
2023-03-05 03:05:30 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-16 15:37:51 +00:00
|
|
|
for i, out := range s.outbounds {
|
|
|
|
if starter, isStarter := out.(common.Starter); isStarter {
|
2023-03-21 13:36:17 +00:00
|
|
|
var tag string
|
|
|
|
if out.Tag() == "" {
|
|
|
|
tag = F.ToString(i)
|
|
|
|
} else {
|
|
|
|
tag = out.Tag()
|
|
|
|
}
|
|
|
|
s.logger.Trace("initializing outbound ", tag)
|
2022-08-20 01:13:00 +00:00
|
|
|
err := starter.Start()
|
2022-08-16 15:37:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, "initialize outbound/", out.Type(), "[", tag, "]")
|
|
|
|
}
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-18 12:26:58 +00:00
|
|
|
return s.router.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Box) start() error {
|
|
|
|
err := s.preStart()
|
2022-08-20 01:13:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-18 12:26:58 +00:00
|
|
|
for serviceName, service := range s.preServices {
|
2023-03-21 13:36:17 +00:00
|
|
|
s.logger.Trace("starting ", serviceName)
|
2023-03-18 12:26:58 +00:00
|
|
|
err = service.Start()
|
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, "start ", serviceName)
|
|
|
|
}
|
|
|
|
}
|
2022-08-20 01:13:00 +00:00
|
|
|
for i, in := range s.inbounds {
|
2023-03-21 13:36:17 +00:00
|
|
|
var tag string
|
|
|
|
if in.Tag() == "" {
|
|
|
|
tag = F.ToString(i)
|
|
|
|
} else {
|
|
|
|
tag = in.Tag()
|
|
|
|
}
|
|
|
|
s.logger.Trace("initializing inbound ", tag)
|
2022-08-20 01:13:00 +00:00
|
|
|
err = in.Start()
|
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, "initialize inbound/", in.Type(), "[", tag, "]")
|
|
|
|
}
|
|
|
|
}
|
2023-03-18 12:26:58 +00:00
|
|
|
for serviceName, service := range s.postServices {
|
2023-03-21 13:36:17 +00:00
|
|
|
s.logger.Trace("start ", serviceName)
|
2023-03-18 12:26:58 +00:00
|
|
|
err = service.Start()
|
|
|
|
if err != nil {
|
2023-03-21 13:36:17 +00:00
|
|
|
return E.Cause(err, "starting ", serviceName)
|
2023-03-18 12:26:58 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-04 11:34:45 +00:00
|
|
|
return nil
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 13:47:21 +00:00
|
|
|
func (s *Box) Close() error {
|
2022-08-12 04:13:57 +00:00
|
|
|
select {
|
|
|
|
case <-s.done:
|
|
|
|
return os.ErrClosed
|
|
|
|
default:
|
|
|
|
close(s.done)
|
|
|
|
}
|
2022-10-25 04:55:00 +00:00
|
|
|
var errors error
|
2023-03-18 12:26:58 +00:00
|
|
|
for serviceName, service := range s.postServices {
|
|
|
|
errors = E.Append(errors, service.Close(), func(err error) error {
|
2023-03-21 13:36:17 +00:00
|
|
|
s.logger.Trace("closing ", serviceName)
|
2023-03-18 12:26:58 +00:00
|
|
|
return E.Cause(err, "close ", serviceName)
|
|
|
|
})
|
|
|
|
}
|
2022-10-25 04:55:00 +00:00
|
|
|
for i, in := range s.inbounds {
|
2023-03-21 13:36:17 +00:00
|
|
|
var tag string
|
|
|
|
if in.Tag() == "" {
|
|
|
|
tag = F.ToString(i)
|
|
|
|
} else {
|
|
|
|
tag = in.Tag()
|
|
|
|
}
|
|
|
|
s.logger.Trace("closing inbound ", tag)
|
2022-10-25 04:55:00 +00:00
|
|
|
errors = E.Append(errors, in.Close(), func(err error) error {
|
|
|
|
return E.Cause(err, "close inbound/", in.Type(), "[", i, "]")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for i, out := range s.outbounds {
|
2023-03-21 13:36:17 +00:00
|
|
|
var tag string
|
|
|
|
if out.Tag() == "" {
|
|
|
|
tag = F.ToString(i)
|
|
|
|
} else {
|
|
|
|
tag = out.Tag()
|
|
|
|
}
|
|
|
|
s.logger.Trace("closing outbound ", tag)
|
2022-10-25 04:55:00 +00:00
|
|
|
errors = E.Append(errors, common.Close(out), func(err error) error {
|
|
|
|
return E.Cause(err, "close inbound/", out.Type(), "[", i, "]")
|
|
|
|
})
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2023-03-21 13:36:17 +00:00
|
|
|
s.logger.Trace("closing router")
|
2022-10-25 04:55:00 +00:00
|
|
|
if err := common.Close(s.router); err != nil {
|
|
|
|
errors = E.Append(errors, err, func(err error) error {
|
|
|
|
return E.Cause(err, "close router")
|
|
|
|
})
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2023-03-18 12:26:58 +00:00
|
|
|
for serviceName, service := range s.preServices {
|
2023-03-21 13:36:17 +00:00
|
|
|
s.logger.Trace("closing ", serviceName)
|
2023-03-18 12:26:58 +00:00
|
|
|
errors = E.Append(errors, service.Close(), func(err error) error {
|
|
|
|
return E.Cause(err, "close ", serviceName)
|
2022-10-25 04:55:00 +00:00
|
|
|
})
|
|
|
|
}
|
2023-03-21 13:36:17 +00:00
|
|
|
s.logger.Trace("closing logger")
|
2023-03-18 12:26:58 +00:00
|
|
|
if err := common.Close(s.logFactory); err != nil {
|
2022-10-25 04:55:00 +00:00
|
|
|
errors = E.Append(errors, err, func(err error) error {
|
2023-03-18 12:26:58 +00:00
|
|
|
return E.Cause(err, "close log factory")
|
2022-10-25 04:55:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if s.logFile != nil {
|
|
|
|
errors = E.Append(errors, s.logFile.Close(), func(err error) error {
|
|
|
|
return E.Cause(err, "close log file")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return errors
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-09-26 11:37:06 +00:00
|
|
|
|
|
|
|
func (s *Box) Router() adapter.Router {
|
|
|
|
return s.router
|
|
|
|
}
|