V2bX/cmd/server.go

132 lines
2.8 KiB
Go
Raw Normal View History

2023-05-23 22:26:14 -04:00
package cmd
import (
"os"
"os/signal"
"runtime"
"syscall"
2023-07-29 07:27:15 -04:00
"github.com/InazumaV/V2bX/conf"
2023-08-21 06:50:31 -04:00
vCore "github.com/InazumaV/V2bX/core"
2023-07-29 07:27:15 -04:00
"github.com/InazumaV/V2bX/limiter"
"github.com/InazumaV/V2bX/node"
2023-08-21 06:50:31 -04:00
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
2023-08-21 06:50:31 -04:00
"gopkg.in/natefinch/lumberjack.v2"
2023-05-23 22:26:14 -04:00
)
var (
config string
watch bool
)
var serverCommand = cobra.Command{
Use: "server",
Short: "Run V2bX server",
Run: serverHandle,
Args: cobra.NoArgs,
}
func init() {
serverCommand.PersistentFlags().
StringVarP(&config, "config", "c",
2023-08-20 03:13:52 -04:00
"/etc/V2bX/config.json", "config file path")
2023-05-23 22:26:14 -04:00
serverCommand.PersistentFlags().
BoolVarP(&watch, "watch", "w",
true, "watch file path change")
command.AddCommand(&serverCommand)
}
func serverHandle(_ *cobra.Command, _ []string) {
showVersion()
2023-05-23 22:26:14 -04:00
c := conf.New()
err := c.LoadFromPath(config)
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithField("err", err).Error("Load config file failed")
return
2023-05-23 22:26:14 -04:00
}
2023-08-20 03:13:52 -04:00
switch c.LogConfig.Level {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
}
2023-08-21 06:50:31 -04:00
if c.LogConfig.Output != "" {
w := &lumberjack.Logger{
Filename: c.LogConfig.Output,
MaxSize: 100,
MaxBackups: 3,
MaxAge: 28,
Compress: true,
}
log.SetOutput(w)
}
2023-05-23 22:26:14 -04:00
limiter.Init()
2023-06-29 23:07:27 -04:00
log.Info("Start V2bX...")
2023-08-20 03:13:52 -04:00
vc, err := vCore.NewCore(c.CoresConfig)
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithField("err", err).Error("new core failed")
return
}
err = vc.Start()
2023-05-23 22:26:14 -04:00
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithField("err", err).Error("Start core failed")
return
2023-05-23 22:26:14 -04:00
}
defer vc.Close()
2023-09-02 05:45:32 -04:00
log.Info("Core ", vc.Type(), " started")
2023-05-23 22:26:14 -04:00
nodes := node.New()
2023-08-20 03:13:52 -04:00
err = nodes.Start(c.NodeConfig, vc)
2023-05-23 22:26:14 -04:00
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithField("err", err).Error("Run nodes failed")
2023-05-23 22:26:14 -04:00
return
}
2023-09-02 05:45:32 -04:00
log.Info("Nodes started")
2023-09-21 00:34:03 -04:00
xdns := os.Getenv("XRAY_DNS_PATH")
sdns := os.Getenv("SING_DNS_PATH")
2023-05-23 22:26:14 -04:00
if watch {
2023-09-21 00:34:03 -04:00
err = c.Watch(config, xdns, sdns, func() {
2023-05-23 22:26:14 -04:00
nodes.Close()
err = vc.Close()
2023-05-23 22:26:14 -04:00
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithField("err", err).Error("Restart node failed")
return
2023-05-23 22:26:14 -04:00
}
2023-08-20 03:13:52 -04:00
vc, err = vCore.NewCore(c.CoresConfig)
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithField("err", err).Error("New core failed")
return
}
err = vc.Start()
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithField("err", err).Error("Start core failed")
return
}
2023-09-02 05:45:32 -04:00
log.Info("Core ", vc.Type(), " restarted")
2023-08-20 03:13:52 -04:00
err = nodes.Start(c.NodeConfig, vc)
2023-05-23 22:26:14 -04:00
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithField("err", err).Error("Run nodes failed")
return
2023-05-23 22:26:14 -04:00
}
2023-09-02 05:45:32 -04:00
log.Info("Nodes restarted")
2024-01-07 10:58:21 -05:00
runtime.GC()
2023-05-23 22:26:14 -04:00
})
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithField("err", err).Error("start watch failed")
return
2023-05-23 22:26:14 -04:00
}
}
// clear memory
runtime.GC()
// wait exit signal
{
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM)
<-osSignals
}
}