mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-09-27 22:36:12 -04:00
132 lines
2.8 KiB
Go
132 lines
2.8 KiB
Go
package main
|
|
|
|
//go:generate errorgen
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"v2ray.com/core"
|
|
"v2ray.com/core/common/cmdarg"
|
|
"v2ray.com/core/common/platform"
|
|
_ "v2ray.com/core/main/distro/all"
|
|
)
|
|
|
|
var (
|
|
configFiles cmdarg.Arg // "Config file for V2Ray.", the option is customed type, parse in main
|
|
version = flag.Bool("version", false, "Show current version of V2Ray.")
|
|
test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
|
|
format = flag.String("format", "", "Format of input file.")
|
|
errNoConfig = newError("no valid config")
|
|
)
|
|
|
|
func fileExists(file string) bool {
|
|
info, err := os.Stat(file)
|
|
return err == nil && !info.IsDir()
|
|
}
|
|
|
|
func getConfigFilePath() cmdarg.Arg {
|
|
if len(configFiles) > 0 {
|
|
return configFiles
|
|
}
|
|
|
|
if workingDir, err := os.Getwd(); err == nil {
|
|
configFile := filepath.Join(workingDir, "config.json")
|
|
if fileExists(configFile) {
|
|
return cmdarg.Arg{configFile}
|
|
}
|
|
}
|
|
|
|
if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
|
|
return cmdarg.Arg{configFile}
|
|
}
|
|
|
|
return configFiles
|
|
}
|
|
|
|
func GetConfigFormat() string {
|
|
switch strings.ToLower(*format) {
|
|
case "pb", "protobuf":
|
|
return "protobuf"
|
|
default:
|
|
return "json"
|
|
}
|
|
}
|
|
|
|
func startV2Ray() (core.Server, error) {
|
|
configFiles := getConfigFilePath()
|
|
if len(configFiles) == 0 {
|
|
if *format == "" {
|
|
return nil, errNoConfig
|
|
}
|
|
configFiles = []string{"stdin:"}
|
|
}
|
|
|
|
config, err := core.LoadConfig(GetConfigFormat(), configFiles[0], configFiles)
|
|
if err != nil {
|
|
return nil, newError("failed to read config files: [", configFiles.String(), "]").Base(err)
|
|
}
|
|
|
|
server, err := core.New(config)
|
|
if err != nil {
|
|
return nil, newError("failed to create server").Base(err)
|
|
}
|
|
|
|
return server, nil
|
|
}
|
|
|
|
func printVersion() {
|
|
version := core.VersionStatement()
|
|
for _, s := range version {
|
|
fmt.Println(s)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
flag.Var(&configFiles, "config", "Config file for V2Ray. Multiple assign is accepted (only json). Latter ones overrides the former ones.")
|
|
flag.Var(&configFiles, "c", "short alias of -config")
|
|
flag.Parse()
|
|
|
|
printVersion()
|
|
|
|
if *version {
|
|
return
|
|
}
|
|
|
|
server, err := startV2Ray()
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
// Configuration error. Exit with a special value to prevent systemd from restarting.
|
|
if err == errNoConfig {
|
|
flag.PrintDefaults()
|
|
}
|
|
os.Exit(23)
|
|
}
|
|
|
|
if *test {
|
|
fmt.Println("Configuration OK.")
|
|
os.Exit(0)
|
|
}
|
|
|
|
if err := server.Start(); err != nil {
|
|
fmt.Println("Failed to start", err)
|
|
os.Exit(-1)
|
|
}
|
|
defer server.Close()
|
|
|
|
// Explicitly triggering GC to remove garbage from config loading.
|
|
runtime.GC()
|
|
|
|
{
|
|
osSignals := make(chan os.Signal, 1)
|
|
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
|
|
<-osSignals
|
|
}
|
|
}
|