2020-11-23 10:38:43 -05:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"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]",
|
2020-11-23 10:38:43 -05:00
|
|
|
Short: "Test config files",
|
|
|
|
Long: `
|
|
|
|
Test config files, without launching V2Ray server.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
2020-11-28 09:06:03 -05:00
|
|
|
-c, -config
|
|
|
|
Config file for V2Ray. Multiple assign is accepted.
|
2020-11-23 10:38:43 -05:00
|
|
|
|
2020-11-28 09:06:03 -05:00
|
|
|
-d, -confdir
|
|
|
|
A dir 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
|
|
|
|
2020-11-28 09:06:03 -05:00
|
|
|
-format
|
2020-11-23 10:38:43 -05:00
|
|
|
Format of input files. (default "json")
|
|
|
|
|
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
|
|
|
|
|
|
|
extension, err := getLoaderExtension()
|
|
|
|
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
|
|
|
}
|
|
|
|
if len(configFiles) == 0 {
|
2020-11-28 09:06:03 -05:00
|
|
|
if len(configDirs) == 0 {
|
|
|
|
cmd.Flag.Usage()
|
|
|
|
base.SetExitStatus(1)
|
|
|
|
base.Exit()
|
|
|
|
}
|
|
|
|
base.Fatalf("no config file found with extension: %s", extension)
|
2020-11-23 10:38:43 -05:00
|
|
|
}
|
|
|
|
printVersion()
|
2020-11-28 09:06:03 -05:00
|
|
|
_, err = startV2RayTesting()
|
2020-11-23 10:38:43 -05:00
|
|
|
if err != nil {
|
|
|
|
base.Fatalf("Test failed: %s", err)
|
|
|
|
}
|
|
|
|
fmt.Println("Configuration OK.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func startV2RayTesting() (core.Server, error) {
|
2020-11-28 09:06:03 -05:00
|
|
|
config, err := core.LoadConfig(*configFormat, configFiles[0], configFiles)
|
2020-11-23 10:38:43 -05:00
|
|
|
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
|
|
|
|
}
|