|
@@ -5,11 +5,8 @@ package main
|
|
|
import (
|
|
import (
|
|
|
"flag"
|
|
"flag"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
- "io/ioutil"
|
|
|
|
|
- "log"
|
|
|
|
|
"os"
|
|
"os"
|
|
|
"os/signal"
|
|
"os/signal"
|
|
|
- "path"
|
|
|
|
|
"path/filepath"
|
|
"path/filepath"
|
|
|
"runtime"
|
|
"runtime"
|
|
|
"strings"
|
|
"strings"
|
|
@@ -23,10 +20,10 @@ import (
|
|
|
|
|
|
|
|
var (
|
|
var (
|
|
|
configFiles cmdarg.Arg // "Config file for V2Ray.", the option is customed type, parse in main
|
|
configFiles cmdarg.Arg // "Config file for V2Ray.", the option is customed type, parse in main
|
|
|
- configDir string
|
|
|
|
|
version = flag.Bool("version", false, "Show current version of V2Ray.")
|
|
version = flag.Bool("version", false, "Show current version of V2Ray.")
|
|
|
test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
|
|
test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
|
|
|
format = flag.String("format", "json", "Format of input file.")
|
|
format = flag.String("format", "json", "Format of input file.")
|
|
|
|
|
+ errNoConfig = newError("no valid config")
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
func fileExists(file string) bool {
|
|
func fileExists(file string) bool {
|
|
@@ -34,27 +31,7 @@ func fileExists(file string) bool {
|
|
|
return err == nil && !info.IsDir()
|
|
return err == nil && !info.IsDir()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func dirExists(file string) bool {
|
|
|
|
|
- info, err := os.Stat(file)
|
|
|
|
|
- return err == nil && info.IsDir()
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func readConfDir(dirPath string) {
|
|
|
|
|
- confs, err := ioutil.ReadDir(dirPath)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- log.Fatalln(err)
|
|
|
|
|
- }
|
|
|
|
|
- for _, f := range confs {
|
|
|
|
|
- if strings.HasSuffix(f.Name(), ".json") {
|
|
|
|
|
- configFiles.Set(path.Join(dirPath, f.Name()))
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
func getConfigFilePath() (cmdarg.Arg, error) {
|
|
func getConfigFilePath() (cmdarg.Arg, error) {
|
|
|
- if dirExists(configDir) {
|
|
|
|
|
- readConfDir(configDir)
|
|
|
|
|
- }
|
|
|
|
|
if len(configFiles) > 0 {
|
|
if len(configFiles) > 0 {
|
|
|
return configFiles, nil
|
|
return configFiles, nil
|
|
|
}
|
|
}
|
|
@@ -62,25 +39,14 @@ func getConfigFilePath() (cmdarg.Arg, error) {
|
|
|
if workingDir, err := os.Getwd(); err == nil {
|
|
if workingDir, err := os.Getwd(); err == nil {
|
|
|
configFile := filepath.Join(workingDir, "config.json")
|
|
configFile := filepath.Join(workingDir, "config.json")
|
|
|
if fileExists(configFile) {
|
|
if fileExists(configFile) {
|
|
|
- log.Println("Using default config: ", configFile)
|
|
|
|
|
return cmdarg.Arg{configFile}, nil
|
|
return cmdarg.Arg{configFile}, nil
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
|
|
if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
|
|
|
- log.Println("Using config from env: ", configFile)
|
|
|
|
|
return cmdarg.Arg{configFile}, nil
|
|
return cmdarg.Arg{configFile}, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if envConfDir := platform.GetConfDirPath(); dirExists(envConfDir) {
|
|
|
|
|
- log.Println("Using confdir from env: ", envConfDir)
|
|
|
|
|
- readConfDir(envConfDir)
|
|
|
|
|
- if len(configFiles) > 0 {
|
|
|
|
|
- return configFiles, nil
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- log.Println("Using config from STDIN")
|
|
|
|
|
return cmdarg.Arg{"stdin:"}, nil
|
|
return cmdarg.Arg{"stdin:"}, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -121,8 +87,7 @@ func printVersion() {
|
|
|
|
|
|
|
|
func main() {
|
|
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, "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.StringVar(&configDir, "confdir", "", "A dir with multiple json config")
|
|
|
|
|
|
|
+ flag.Var(&configFiles, "c", "short alias of -config")
|
|
|
flag.Parse()
|
|
flag.Parse()
|
|
|
|
|
|
|
|
printVersion()
|
|
printVersion()
|
|
@@ -133,8 +98,11 @@ func main() {
|
|
|
|
|
|
|
|
server, err := startV2Ray()
|
|
server, err := startV2Ray()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
|
|
|
|
+ fmt.Println(err.Error())
|
|
|
// Configuration error. Exit with a special value to prevent systemd from restarting.
|
|
// Configuration error. Exit with a special value to prevent systemd from restarting.
|
|
|
|
|
+ if err == errNoConfig {
|
|
|
|
|
+ flag.PrintDefaults()
|
|
|
|
|
+ }
|
|
|
os.Exit(23)
|
|
os.Exit(23)
|
|
|
}
|
|
}
|
|
|
|
|
|