2020-11-23 10:38:43 -05:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2021-06-04 19:42:53 -04:00
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
2020-12-04 09:32:55 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/main/commands/base"
|
2020-11-23 10:38:43 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// CmdTest tests config files
|
|
|
|
var CmdTest = &base.Command{
|
|
|
|
CustomFlags: true,
|
2020-11-28 09:06:03 -05:00
|
|
|
UsageLine: "{{.Exec}} test [-format=json] [-c config.json] [-d dir]",
|
2021-02-21 10:02:42 -05:00
|
|
|
Short: "test config files",
|
2020-11-23 10:38:43 -05:00
|
|
|
Long: `
|
|
|
|
Test config files, without launching V2Ray server.
|
|
|
|
|
2021-03-10 16:03:26 -05:00
|
|
|
{{.Exec}} will also use the config directory specified by environment
|
|
|
|
variable "v2ray.location.confdir". If no config found, it tries
|
|
|
|
to load config from one of below:
|
|
|
|
|
|
|
|
1. The default "config.json" in the current directory
|
|
|
|
2. The config file from ENV "v2ray.location.config"
|
|
|
|
3. The stdin if all failed above
|
|
|
|
|
2020-11-23 10:38:43 -05:00
|
|
|
Arguments:
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
-c, -config <file>
|
2020-11-28 09:06:03 -05:00
|
|
|
Config file for V2Ray. Multiple assign is accepted.
|
2020-11-23 10:38:43 -05:00
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
-d, -confdir <dir>
|
2021-03-10 16:03:26 -05:00
|
|
|
A directory with config files. Multiple assign is accepted.
|
2020-11-23 10:38:43 -05:00
|
|
|
|
2020-11-28 09:06:03 -05:00
|
|
|
-r
|
|
|
|
Load confdir recursively.
|
2020-11-23 10:38:43 -05:00
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
-format <format>
|
2021-03-10 16:03:26 -05:00
|
|
|
Format of config input. (default "auto")
|
2020-11-23 10:38:43 -05:00
|
|
|
|
2020-11-28 09:06:03 -05:00
|
|
|
Examples:
|
|
|
|
|
|
|
|
{{.Exec}} {{.LongName}} -c config.json
|
|
|
|
{{.Exec}} {{.LongName}} -d path/to/dir
|
|
|
|
|
|
|
|
Use "{{.Exec}} help format-loader" for more information about format.
|
|
|
|
`,
|
|
|
|
Run: executeTest,
|
2020-11-23 10:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func executeTest(cmd *base.Command, args []string) {
|
|
|
|
setConfigFlags(cmd)
|
|
|
|
cmd.Flag.Parse(args)
|
2020-11-28 09:06:03 -05:00
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
extension, err := core.GetLoaderExtensions(*configFormat)
|
2020-11-28 09:06:03 -05:00
|
|
|
if err != nil {
|
|
|
|
base.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(configDirs) > 0 {
|
|
|
|
dirReader := readConfDir
|
|
|
|
if *configDirRecursively {
|
|
|
|
dirReader = readConfDirRecursively
|
|
|
|
}
|
|
|
|
for _, d := range configDirs {
|
|
|
|
log.Println("Using confdir from arg:", d)
|
|
|
|
configFiles = append(configFiles, dirReader(d, extension)...)
|
|
|
|
}
|
2020-11-23 10:38:43 -05:00
|
|
|
}
|
|
|
|
printVersion()
|
2021-02-21 10:02:42 -05:00
|
|
|
_, err = startV2Ray()
|
2020-11-23 10:38:43 -05:00
|
|
|
if err != nil {
|
|
|
|
base.Fatalf("Test failed: %s", err)
|
|
|
|
}
|
|
|
|
fmt.Println("Configuration OK.")
|
|
|
|
}
|