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

54 lines
1.1 KiB
Go
Raw Normal View History

2022-07-04 16:45:32 +08:00
package main
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/option"
2022-07-06 15:01:09 +08:00
"github.com/goccy/go-json"
2022-07-04 16:45:32 +08:00
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var commandRun = &cobra.Command{
Use: "run",
2022-07-04 17:08:28 +08:00
Short: "Run service",
2022-07-04 16:45:32 +08:00
Run: run,
}
func run(cmd *cobra.Command, args []string) {
configContent, err := os.ReadFile(configPath)
if err != nil {
logrus.Fatal("read config: ", err)
}
var options option.Options
err = json.Unmarshal(configContent, &options)
if err != nil {
logrus.Fatal("decode config: ", err)
}
if disableColor {
if options.Log == nil {
options.Log = &option.LogOption{}
}
options.Log.DisableColor = true
}
ctx, cancel := context.WithCancel(context.Background())
2022-07-07 21:47:21 +08:00
instance, err := box.New(ctx, options)
2022-07-04 16:45:32 +08:00
if err != nil {
logrus.Fatal("create service: ", err)
}
2022-07-07 21:47:21 +08:00
err = instance.Start()
2022-07-04 16:45:32 +08:00
if err != nil {
logrus.Fatal("start service: ", err)
}
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
<-osSignals
cancel()
2022-07-07 21:47:21 +08:00
instance.Close()
2022-07-04 16:45:32 +08:00
}