2023-05-23 22:26:14 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"runtime"
|
|
|
|
"syscall"
|
2023-06-07 13:18:56 -04:00
|
|
|
|
2023-07-29 07:27:15 -04:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
vCore "github.com/InazumaV/V2bX/core"
|
2023-06-07 13:18:56 -04:00
|
|
|
|
2023-07-29 07:27:15 -04:00
|
|
|
"github.com/InazumaV/V2bX/conf"
|
|
|
|
"github.com/InazumaV/V2bX/limiter"
|
|
|
|
"github.com/InazumaV/V2bX/node"
|
2023-06-07 13:18:56 -04:00
|
|
|
"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",
|
|
|
|
"/etc/V2bX/config.yml", "config file path")
|
|
|
|
serverCommand.PersistentFlags().
|
|
|
|
BoolVarP(&watch, "watch", "w",
|
|
|
|
true, "watch file path change")
|
|
|
|
command.AddCommand(&serverCommand)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serverHandle(_ *cobra.Command, _ []string) {
|
2023-05-29 16:04:05 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
limiter.Init()
|
2023-06-29 23:07:27 -04:00
|
|
|
log.Info("Start V2bX...")
|
2023-06-07 13:18:56 -04:00
|
|
|
vc, err := vCore.NewCore(&c.CoreConfig)
|
|
|
|
if err != nil {
|
2023-06-29 23:07:27 -04:00
|
|
|
log.WithField("err", err).Error("new core failed")
|
|
|
|
return
|
2023-06-07 13:18:56 -04:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2023-06-07 13:18:56 -04:00
|
|
|
defer vc.Close()
|
2023-05-23 22:26:14 -04:00
|
|
|
nodes := node.New()
|
2023-06-07 13:18:56 -04:00
|
|
|
err = nodes.Start(c.NodesConfig, 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()
|
2023-06-07 13:18:56 -04:00
|
|
|
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-06-07 13:18:56 -04:00
|
|
|
vc, err = vCore.NewCore(&c.CoreConfig)
|
|
|
|
if err != nil {
|
2023-06-29 23:07:27 -04:00
|
|
|
log.WithField("err", err).Error("New core failed")
|
|
|
|
return
|
2023-06-07 13:18:56 -04:00
|
|
|
}
|
|
|
|
err = vc.Start()
|
|
|
|
if err != nil {
|
2023-06-29 23:07:27 -04:00
|
|
|
log.WithField("err", err).Error("Start core failed")
|
|
|
|
return
|
2023-06-07 13:18:56 -04:00
|
|
|
}
|
|
|
|
err = nodes.Start(c.NodesConfig, 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
|
|
|
|
}
|
|
|
|
}
|