2022-06-01 13:35:41 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-08-16 01:04:33 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/api/panel"
|
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"
|
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-06-01 13:35:41 -04:00
|
|
|
printVersion = flag.Bool("version", false, "show version")
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-09-12 23:11:01 -04:00
|
|
|
version = "v0.0.7_beta"
|
2022-06-04 04:59:10 -04:00
|
|
|
codename = "V2bX"
|
2022-08-12 06:02:06 -04:00
|
|
|
intro = "A V2board backend based on Xray-core"
|
2022-06-01 13:35:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func showVersion() {
|
|
|
|
fmt.Printf("%s %s (%s) \n", codename, version, intro)
|
|
|
|
}
|
|
|
|
|
2022-08-12 06:02:06 -04:00
|
|
|
func startNodes(nodes []*conf.NodeConfig, core *core.Core) error {
|
2022-09-12 23:08:20 -04:00
|
|
|
for i := range nodes {
|
2022-08-16 01:04:33 -04:00
|
|
|
var apiClient = panel.New(nodes[i].ApiConfig)
|
2022-07-28 11:00:05 -04:00
|
|
|
// Register controller service
|
|
|
|
err := node.New(core, apiClient, nodes[i].ControllerConfig).Start()
|
|
|
|
if err != nil {
|
2022-10-09 20:59:00 -04:00
|
|
|
return err
|
2022-07-28 11:00:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
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-09-12 23:08:20 -04:00
|
|
|
err = startNodes(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-06-01 13:35:41 -04:00
|
|
|
//Explicitly triggering GC to remove garbage from config loading.
|
|
|
|
runtime.GC()
|
|
|
|
// Running backend
|
|
|
|
{
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|