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

122 lines
2.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"
2022-08-22 15:17:08 +00:00
runtimeDebug "runtime/debug"
2022-07-04 08:45:32 +00:00
"syscall"
"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"
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
}
2022-08-19 07:42:29 +00:00
func readConfig() (option.Options, error) {
var (
configContent []byte
err error
)
if configPath == "stdin" {
configContent, err = io.ReadAll(os.Stdin)
} else {
configContent, err = os.ReadFile(configPath)
}
2022-07-04 08:45:32 +00:00
if err != nil {
2022-08-19 07:42:29 +00:00
return option.Options{}, E.Cause(err, "read config")
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 {
2022-08-19 07:42:29 +00:00
return option.Options{}, E.Cause(err, "decode config")
}
return options, nil
}
2022-08-22 15:17:08 +00:00
func create() (*box.Box, context.CancelFunc, error) {
2022-08-19 07:42:29 +00:00
options, err := readConfig()
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-02-28 11:02:27 +00:00
instance, err := box.New(ctx, options, nil)
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)
}()
go func() {
_, loaded := <-osSignals
if loaded {
cancel()
}
}()
2022-07-07 13:47:21 +00:00
err = instance.Start()
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()
instance.Close()
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
}