Files
sing-box-extended-mirror/cmd/sing-box/cmd_run.go
T

213 lines
4.7 KiB
Go
Raw Normal View History

2022-07-04 16:45:32 +08:00
package main
import (
"context"
2022-08-19 15:42:29 +08:00
"io"
2022-07-04 16:45:32 +08:00
"os"
"os/signal"
2023-03-18 19:15:28 +08:00
"path/filepath"
2022-08-22 23:17:08 +08:00
runtimeDebug "runtime/debug"
2023-03-18 19:15:28 +08:00
"sort"
"strings"
2022-07-04 16:45:32 +08:00
"syscall"
2023-04-08 08:10:03 +08:00
"time"
2022-07-04 16:45:32 +08:00
"github.com/sagernet/sing-box"
2023-12-04 11:47:25 +08:00
C "github.com/sagernet/sing-box/constant"
2022-07-12 15:17:29 +08:00
"github.com/sagernet/sing-box/log"
2022-07-04 16:45:32 +08:00
"github.com/sagernet/sing-box/option"
2022-08-07 14:58:07 +08:00
E "github.com/sagernet/sing/common/exceptions"
2023-12-10 22:57:28 +08:00
"github.com/sagernet/sing/common/json"
"github.com/sagernet/sing/common/json/badjson"
2022-07-06 15:01:09 +08:00
2022-07-04 16:45:32 +08:00
"github.com/spf13/cobra"
)
var commandRun = &cobra.Command{
Use: "run",
2022-07-04 17:08:28 +08:00
Short: "Run service",
2022-08-13 18:36:49 +08:00
Run: func(cmd *cobra.Command, args []string) {
err := run()
if err != nil {
log.Fatal(err)
}
},
2022-07-04 16:45:32 +08:00
}
2022-08-13 18:36:49 +08:00
func init() {
mainCommand.AddCommand(commandRun)
2022-08-07 14:58:07 +08:00
}
2023-03-18 19:15:28 +08:00
type OptionsEntry struct {
content []byte
path string
options option.Options
}
func readConfigAt(path string) (*OptionsEntry, error) {
2022-08-19 15:42:29 +08:00
var (
configContent []byte
err error
)
2023-03-18 19:15:28 +08:00
if path == "stdin" {
2022-08-19 15:42:29 +08:00
configContent, err = io.ReadAll(os.Stdin)
} else {
2023-03-18 19:15:28 +08:00
configContent, err = os.ReadFile(path)
2022-08-19 15:42:29 +08:00
}
2022-07-04 16:45:32 +08:00
if err != nil {
2023-03-18 19:15:28 +08:00
return nil, E.Cause(err, "read config at ", path)
2022-07-04 16:45:32 +08:00
}
2024-11-02 00:39:02 +08:00
options, err := json.UnmarshalExtendedContext[option.Options](globalCtx, configContent)
2022-07-04 16:45:32 +08:00
if err != nil {
2023-03-18 19:15:28 +08:00
return nil, E.Cause(err, "decode config at ", path)
}
return &OptionsEntry{
content: configContent,
path: path,
options: options,
}, nil
}
func readConfig() ([]*OptionsEntry, error) {
var optionsList []*OptionsEntry
for _, path := range configPaths {
optionsEntry, err := readConfigAt(path)
if err != nil {
return nil, err
}
optionsList = append(optionsList, optionsEntry)
}
for _, directory := range configDirectories {
entries, err := os.ReadDir(directory)
if err != nil {
return nil, E.Cause(err, "read config directory at ", directory)
}
for _, entry := range entries {
if !strings.HasSuffix(entry.Name(), ".json") || entry.IsDir() {
continue
}
optionsEntry, err := readConfigAt(filepath.Join(directory, entry.Name()))
if err != nil {
return nil, err
}
optionsList = append(optionsList, optionsEntry)
}
}
sort.Slice(optionsList, func(i, j int) bool {
return optionsList[i].path < optionsList[j].path
})
return optionsList, nil
}
func readConfigAndMerge() (option.Options, error) {
optionsList, err := readConfig()
if err != nil {
return option.Options{}, err
}
2023-03-19 20:46:22 +08:00
if len(optionsList) == 1 {
return optionsList[0].options, nil
}
2023-12-10 22:57:28 +08:00
var mergedMessage json.RawMessage
2023-03-18 19:15:28 +08:00
for _, options := range optionsList {
2024-11-02 00:39:02 +08:00
mergedMessage, err = badjson.MergeJSON(globalCtx, options.options.RawMessage, mergedMessage, false)
2023-03-18 19:15:28 +08:00
if err != nil {
return option.Options{}, E.Cause(err, "merge config at ", options.path)
}
2022-08-19 15:42:29 +08:00
}
2023-12-10 22:57:28 +08:00
var mergedOptions option.Options
2024-11-02 00:39:02 +08:00
err = mergedOptions.UnmarshalJSONContext(globalCtx, mergedMessage)
2023-12-10 22:57:28 +08:00
if err != nil {
return option.Options{}, E.Cause(err, "unmarshal merged config")
}
2023-03-18 19:15:28 +08:00
return mergedOptions, nil
2022-08-19 15:42:29 +08:00
}
2022-08-22 23:17:08 +08:00
func create() (*box.Box, context.CancelFunc, error) {
2023-03-18 19:15:28 +08:00
options, err := readConfigAndMerge()
2022-08-19 15:42:29 +08:00
if err != nil {
2022-08-22 23:17:08 +08:00
return nil, nil, err
2022-07-04 16:45:32 +08:00
}
if disableColor {
if options.Log == nil {
2022-07-19 22:16:49 +08:00
options.Log = &option.LogOptions{}
2022-07-04 16:45:32 +08:00
}
options.Log.DisableColor = true
}
ctx, cancel := context.WithCancel(globalCtx)
2023-04-03 18:24:20 +08:00
instance, err := box.New(box.Options{
Context: ctx,
Options: options,
})
2022-07-04 16:45:32 +08:00
if err != nil {
2022-08-07 14:58:07 +08:00
cancel()
2022-08-22 23:17:08 +08:00
return nil, nil, E.Cause(err, "create service")
2022-07-04 16:45:32 +08:00
}
2022-11-25 18:16:55 +08:00
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
defer func() {
signal.Stop(osSignals)
close(osSignals)
}()
startCtx, finishStart := context.WithCancel(context.Background())
2022-11-25 18:16:55 +08:00
go func() {
_, loaded := <-osSignals
if loaded {
cancel()
closeMonitor(startCtx)
2022-11-25 18:16:55 +08:00
}
}()
2022-07-07 21:47:21 +08:00
err = instance.Start()
finishStart()
2022-07-04 16:45:32 +08:00
if err != nil {
2022-08-07 14:58:07 +08:00
cancel()
2022-08-22 23:17:08 +08:00
return nil, nil, E.Cause(err, "start service")
}
return instance, cancel, nil
}
func run() error {
2022-08-23 23:22:49 +08:00
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
2022-11-25 18:16:55 +08:00
defer signal.Stop(osSignals)
2022-08-23 19:56:28 +08:00
for {
instance, cancel, err := create()
if err != nil {
return err
}
runtimeDebug.FreeOSMemory()
2022-08-23 23:22:49 +08:00
for {
osSignal := <-osSignals
if osSignal == syscall.SIGHUP {
err = check()
if err != nil {
log.Error(E.Cause(err, "reload service"))
continue
}
}
cancel()
2023-04-08 08:10:03 +08:00
closeCtx, closed := context.WithCancel(context.Background())
go closeMonitor(closeCtx)
2024-08-10 16:47:44 +08:00
err = instance.Close()
2023-04-08 08:10:03 +08:00
closed()
2022-08-23 23:22:49 +08:00
if osSignal != syscall.SIGHUP {
2024-08-10 16:47:44 +08:00
if err != nil {
log.Error(E.Cause(err, "sing-box did not closed properly"))
}
2022-08-23 23:22:49 +08:00
return nil
}
break
2022-08-23 19:56:28 +08:00
}
2022-08-12 12:13:57 +08:00
}
2022-07-04 16:45:32 +08:00
}
2023-04-08 08:10:03 +08:00
func closeMonitor(ctx context.Context) {
2024-04-02 23:07:26 +08:00
time.Sleep(C.FatalStopTimeout)
2023-04-08 08:10:03 +08:00
select {
case <-ctx.Done():
return
default:
}
log.Fatal("sing-box did not close!")
}