sing-box/cmd/sing-box/cmd_run.go

204 lines
4.3 KiB
Go
Raw Normal View History

2022-07-04 08:45:32 +00:00
package main
import (
"context"
2022-08-19 07:42:29 +00:00
"io"
2022-07-04 08:45:32 +00:00
"os"
"os/signal"
2023-03-18 11:15:28 +00:00
"path/filepath"
2022-08-22 15:17:08 +00:00
runtimeDebug "runtime/debug"
2023-03-18 11:15:28 +00:00
"sort"
"strings"
2022-07-04 08:45:32 +00:00
"syscall"
2023-04-08 00:10:03 +00:00
"time"
2022-07-04 08:45:32 +00:00
"github.com/sagernet/sing-box"
2022-07-12 07:17:29 +00:00
"github.com/sagernet/sing-box/log"
2022-07-04 08:45:32 +00:00
"github.com/sagernet/sing-box/option"
2022-08-07 06:58:07 +00:00
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/json/badjson"
2022-07-06 07:01:09 +00:00
2022-07-04 08:45:32 +00:00
"github.com/spf13/cobra"
)
var commandRun = &cobra.Command{
Use: "run",
2022-07-04 09:08:28 +00:00
Short: "Run service",
2022-08-13 10:36:49 +00:00
Run: func(cmd *cobra.Command, args []string) {
err := run()
if err != nil {
log.Fatal(err)
}
},
2022-07-04 08:45:32 +00:00
}
2022-08-13 10:36:49 +00:00
func init() {
mainCommand.AddCommand(commandRun)
2022-08-07 06:58:07 +00:00
}
2023-03-18 11:15:28 +00:00
type OptionsEntry struct {
content []byte
path string
options option.Options
}
func readConfigAt(path string) (*OptionsEntry, error) {
2022-08-19 07:42:29 +00:00
var (
configContent []byte
err error
)
2023-03-18 11:15:28 +00:00
if path == "stdin" {
2022-08-19 07:42:29 +00:00
configContent, err = io.ReadAll(os.Stdin)
} else {
2023-03-18 11:15:28 +00:00
configContent, err = os.ReadFile(path)
2022-08-19 07:42:29 +00:00
}
2022-07-04 08:45:32 +00:00
if err != nil {
2023-03-18 11:15:28 +00:00
return nil, E.Cause(err, "read config at ", path)
2022-07-04 08:45:32 +00:00
}
var options option.Options
2022-09-07 05:19:57 +00:00
err = options.UnmarshalJSON(configContent)
2022-07-04 08:45:32 +00:00
if err != nil {
2023-03-18 11:15:28 +00: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 12:46:22 +00:00
if len(optionsList) == 1 {
return optionsList[0].options, nil
}
2023-03-18 11:15:28 +00:00
var mergedOptions option.Options
for _, options := range optionsList {
mergedOptions, err = badjson.Merge(options.options, mergedOptions)
2023-03-18 11:15:28 +00:00
if err != nil {
return option.Options{}, E.Cause(err, "merge config at ", options.path)
}
2022-08-19 07:42:29 +00:00
}
2023-03-18 11:15:28 +00:00
return mergedOptions, nil
2022-08-19 07:42:29 +00:00
}
2022-08-22 15:17:08 +00:00
func create() (*box.Box, context.CancelFunc, error) {
2023-03-18 11:15:28 +00:00
options, err := readConfigAndMerge()
2022-08-19 07:42:29 +00:00
if err != nil {
2022-08-22 15:17:08 +00:00
return nil, nil, err
2022-07-04 08:45:32 +00:00
}
if disableColor {
if options.Log == nil {
2022-07-19 14:16:49 +00:00
options.Log = &option.LogOptions{}
2022-07-04 08:45:32 +00:00
}
options.Log.DisableColor = true
}
ctx, cancel := context.WithCancel(context.Background())
2023-04-03 10:24:20 +00:00
instance, err := box.New(box.Options{
Context: ctx,
Options: options,
})
2022-07-04 08:45:32 +00:00
if err != nil {
2022-08-07 06:58:07 +00:00
cancel()
2022-08-22 15:17:08 +00:00
return nil, nil, E.Cause(err, "create service")
2022-07-04 08:45:32 +00:00
}
2022-11-25 10:16:55 +00: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 10:16:55 +00:00
go func() {
_, loaded := <-osSignals
if loaded {
cancel()
closeMonitor(startCtx)
2022-11-25 10:16:55 +00:00
}
}()
2022-07-07 13:47:21 +00:00
err = instance.Start()
finishStart()
2022-07-04 08:45:32 +00:00
if err != nil {
2022-08-07 06:58:07 +00:00
cancel()
2022-08-22 15:17:08 +00:00
return nil, nil, E.Cause(err, "start service")
}
return instance, cancel, nil
}
func run() error {
2022-08-23 15:22:49 +00:00
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
2022-11-25 10:16:55 +00:00
defer signal.Stop(osSignals)
2022-08-23 11:56:28 +00:00
for {
instance, cancel, err := create()
if err != nil {
return err
}
runtimeDebug.FreeOSMemory()
2022-08-23 15:22:49 +00: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 00:10:03 +00:00
closeCtx, closed := context.WithCancel(context.Background())
go closeMonitor(closeCtx)
2022-08-23 15:22:49 +00:00
instance.Close()
2023-04-08 00:10:03 +00:00
closed()
2022-08-23 15:22:49 +00:00
if osSignal != syscall.SIGHUP {
return nil
}
break
2022-08-23 11:56:28 +00:00
}
2022-08-12 04:13:57 +00:00
}
2022-07-04 08:45:32 +00:00
}
2023-04-08 00:10:03 +00:00
func closeMonitor(ctx context.Context) {
time.Sleep(3 * time.Second)
select {
case <-ctx.Done():
return
default:
}
log.Fatal("sing-box did not close!")
}