Add sub-command "-dump" to "run". (#2854)

* Add MarshalToJson().

* Add cmd arg -dump for printing out merged multiple json configs.

---------

Co-authored-by: nobody <nobody@nowhere.mars>
This commit is contained in:
nobody 2023-12-30 00:16:48 +08:00 committed by GitHub
parent 006cf491e5
commit 44bb83033f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 476 additions and 10 deletions

View file

@ -12,8 +12,10 @@ import (
"runtime/debug"
"strings"
"syscall"
"time"
"github.com/xtls/xray-core/common/cmdarg"
clog "github.com/xtls/xray-core/common/log"
"github.com/xtls/xray-core/common/platform"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/main/commands/base"
@ -34,7 +36,9 @@ The -format=json flag sets the format of config files.
Default "auto".
The -test flag tells Xray to test config files only,
without launching the server
without launching the server.
The -dump flag tells Xray to print the merged config.
`,
}
@ -45,6 +49,7 @@ func init() {
var (
configFiles cmdarg.Arg // "Config file for Xray.", the option is customed type, parse in main
configDir string
dump = cmdRun.Flag.Bool("dump", false, "Dump merged config only, without launching Xray server.")
test = cmdRun.Flag.Bool("test", false, "Test config file only, without launching Xray server.")
format = cmdRun.Flag.String("format", "auto", "Format of input file.")
@ -61,6 +66,12 @@ var (
)
func executeRun(cmd *base.Command, args []string) {
if *dump {
clog.ReplaceWithSeverityLogger(clog.Severity_Warning)
errCode := dumpConfig()
os.Exit(errCode)
}
printVersion()
server, err := startXray()
if err != nil {
@ -97,6 +108,18 @@ func executeRun(cmd *base.Command, args []string) {
}
}
func dumpConfig() int {
files := getConfigFilePath(false)
if config, err := core.GetMergedConfig(files); err != nil {
fmt.Println(err)
time.Sleep(1 * time.Second)
return 23
} else {
fmt.Print(config)
}
return 0
}
func fileExists(file string) bool {
info, err := os.Stat(file)
return err == nil && !info.IsDir()
@ -139,12 +162,16 @@ func readConfDir(dirPath string) {
}
}
func getConfigFilePath() cmdarg.Arg {
func getConfigFilePath(verbose bool) cmdarg.Arg {
if dirExists(configDir) {
log.Println("Using confdir from arg:", configDir)
if verbose {
log.Println("Using confdir from arg:", configDir)
}
readConfDir(configDir)
} else if envConfDir := platform.GetConfDirPath(); dirExists(envConfDir) {
log.Println("Using confdir from env:", envConfDir)
if verbose {
log.Println("Using confdir from env:", envConfDir)
}
readConfDir(envConfDir)
}
@ -155,17 +182,23 @@ func getConfigFilePath() cmdarg.Arg {
if workingDir, err := os.Getwd(); err == nil {
configFile := filepath.Join(workingDir, "config.json")
if fileExists(configFile) {
log.Println("Using default config: ", configFile)
if verbose {
log.Println("Using default config: ", configFile)
}
return cmdarg.Arg{configFile}
}
}
if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
log.Println("Using config from env: ", configFile)
if verbose {
log.Println("Using config from env: ", configFile)
}
return cmdarg.Arg{configFile}
}
log.Println("Using config from STDIN")
if verbose {
log.Println("Using config from STDIN")
}
return cmdarg.Arg{"stdin:"}
}
@ -178,7 +211,7 @@ func getConfigFormat() string {
}
func startXray() (core.Server, error) {
configFiles := getConfigFilePath()
configFiles := getConfigFilePath(true)
// config, err := core.LoadConfig(getConfigFormat(), configFiles[0], configFiles)