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

185 lines
3.9 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"
"github.com/sagernet/sing-box"
2023-03-18 19:15:28 +08:00
"github.com/sagernet/sing-box/common/badjsonmerge"
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"
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
}
var options option.Options
2022-09-07 13:19:57 +08:00
err = options.UnmarshalJSON(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-03-18 19:15:28 +08:00
var mergedOptions option.Options
for _, options := range optionsList {
mergedOptions, err = badjsonmerge.MergeOptions(options.options, mergedOptions)
if err != nil {
return option.Options{}, E.Cause(err, "merge config at ", options.path)
}
2022-08-19 15:42:29 +08:00
}
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(context.Background())
2023-02-28 19:02:27 +08:00
instance, err := box.New(ctx, options, nil)
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)
}()
go func() {
_, loaded := <-osSignals
if loaded {
cancel()
}
}()
2022-07-07 21:47:21 +08:00
err = instance.Start()
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()
instance.Close()
if osSignal != syscall.SIGHUP {
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
}