V2bX/cmd/server.go

118 lines
2.4 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
log "github.com/sirupsen/logrus"
vCore "github.com/InazumaV/V2bX/core"
2023-07-29 07:27:15 -04:00
"github.com/InazumaV/V2bX/conf"
"github.com/InazumaV/V2bX/limiter"
"github.com/InazumaV/V2bX/node"
"github.com/spf13/cobra"
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-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-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-07-19 00:03:09 -04:00
dns := os.Getenv("XRAY_DNS_PATH")
2023-05-23 22:26:14 -04:00
if watch {
2023-07-19 00:03:09 -04:00
err = c.Watch(config, dns, 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-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
}
runtime.GC()
})
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
}
}