2022-06-01 13:35:41 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-07-28 11:00:05 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/conf"
|
2022-08-12 06:02:06 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/core"
|
2023-05-15 21:15:29 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/limiter"
|
2022-07-28 11:00:05 -04:00
|
|
|
"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)
|
2022-07-28 11:00:05 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Panicf("can't unmarshal config file: %s \n", err)
|
|
|
|
}
|
2023-05-15 21:15:29 -04:00
|
|
|
limiter.Init()
|
2022-10-09 20:59:00 -04:00
|
|
|
log.Println("Start V2bX...")
|
2022-09-12 23:08:20 -04:00
|
|
|
x := core.New(config)
|
2022-10-09 20:59:00 -04:00
|
|
|
err = x.Start()
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Failed to start core: %s", err)
|
|
|
|
}
|
2022-07-28 11:00:05 -04:00
|
|
|
defer x.Close()
|
2022-10-09 23:31:15 -04:00
|
|
|
nodes := node.New()
|
|
|
|
err = nodes.Start(config.NodesConfig, x)
|
2022-07-28 11:00:05 -04:00
|
|
|
if err != nil {
|
2022-10-09 20:59:00 -04:00
|
|
|
log.Panicf("run nodes error: %s", err)
|
2022-07-28 11:00:05 -04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
2023-05-15 21:15:29 -04:00
|
|
|
// clear memory
|
2022-06-01 13:35:41 -04:00
|
|
|
runtime.GC()
|
2023-05-15 21:15:29 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|