V2bX/main.go

87 lines
1.9 KiB
Go
Raw Normal View History

2022-06-01 13:35:41 -04:00
package main
import (
"flag"
"fmt"
"github.com/Yuzuki616/V2bX/conf"
"github.com/Yuzuki616/V2bX/core"
"github.com/Yuzuki616/V2bX/limiter"
"github.com/Yuzuki616/V2bX/node"
2022-06-01 13:35:41 -04:00
"log"
"os"
"os/signal"
"runtime"
"syscall"
)
var (
2022-09-12 23:08:20 -04:00
configFile = flag.String("config", "/etc/V2bX/config.yml", "Config file for V2bX.")
2022-10-11 04:03:29 -04:00
watch = flag.Bool("watch", true, "Watch config file for changes.")
2022-06-01 13:35:41 -04:00
printVersion = flag.Bool("version", false, "show version")
)
var (
2023-05-22 09:41:03 -04:00
version = "TempVersion" //use ldflags replace
codename = "V2bX"
intro = "A V2board backend based on Xray-core"
warnColor = "\033[0;31m"
2022-06-01 13:35:41 -04:00
)
func showVersion() {
fmt.Printf("%s %s (%s) \n", codename, version, intro)
2023-05-22 09:41:03 -04:00
// Warning
2023-05-22 22:02:47 -04:00
fmt.Printf("%sThis version need V2board version >= 1.7.0.\n", warnColor)
fmt.Printf("%sThis version changed config file. Please check config file before running.\n", warnColor)
2022-06-01 13:35:41 -04:00
}
func main() {
flag.Parse()
showVersion()
if *printVersion {
return
}
2022-09-12 23:08:20 -04:00
config := conf.New()
err := config.LoadFromPath(*configFile)
if err != nil {
log.Panicf("can't unmarshal config file: %s \n", err)
}
limiter.Init()
log.Println("Start V2bX...")
2022-09-12 23:08:20 -04:00
x := core.New(config)
err = x.Start()
if err != nil {
log.Panicf("Failed to start core: %s", err)
}
defer x.Close()
2022-10-09 23:31:15 -04:00
nodes := node.New()
err = nodes.Start(config.NodesConfig, x)
if err != nil {
log.Panicf("run nodes error: %s", err)
}
2022-10-11 04:03:29 -04:00
if *watch {
err = config.Watch(*configFile, func() {
nodes.Close()
err = x.Restart(config)
if err != nil {
log.Panicf("Failed to restart core: %s", err)
}
err = nodes.Start(config.NodesConfig, x)
if err != nil {
log.Panicf("run nodes error: %s", err)
}
runtime.GC()
})
2022-10-09 23:31:15 -04:00
if err != nil {
2022-10-11 04:03:29 -04:00
log.Panicf("watch config file error: %s", err)
2022-10-09 23:31:15 -04:00
}
}
// clear memory
2022-06-01 13:35:41 -04:00
runtime.GC()
// wait exit signal
2022-06-01 13:35:41 -04:00
{
osSignals := make(chan os.Signal, 1)
2022-09-04 08:32:56 -04:00
signal.Notify(osSignals, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM)
2022-06-01 13:35:41 -04:00
<-osSignals
}
}