From 5ae47d45c2a7c112c002b04faa6fa697daa31948 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Tue, 31 Dec 2019 11:45:19 +0800 Subject: [PATCH 1/5] multiconf with -confdir arg --- common/platform/ctlcmd/ctlcmd.go | 3 ++- common/platform/platform.go | 6 ++++++ infra/conf/v2ray.go | 14 ++++++++----- infra/control/command.go | 3 +++ infra/control/config.go | 2 +- main/main.go | 36 +++++++++++++++++++++++++++++++- 6 files changed, 56 insertions(+), 8 deletions(-) diff --git a/common/platform/ctlcmd/ctlcmd.go b/common/platform/ctlcmd/ctlcmd.go index adebbf7a0..dcb0a7249 100644 --- a/common/platform/ctlcmd/ctlcmd.go +++ b/common/platform/ctlcmd/ctlcmd.go @@ -4,6 +4,7 @@ import ( "io" "os" "os/exec" + "strings" "v2ray.com/core/common/buf" "v2ray.com/core/common/platform" @@ -42,7 +43,7 @@ func Run(args []string, input io.Reader) (buf.MultiBuffer, error) { // log stderr, info message if !errBuffer.IsEmpty() { - newError("v2ctl > \n", errBuffer.MultiBuffer.String()).AtInfo().WriteToLog() + newError(" \n", strings.TrimSpace(errBuffer.MultiBuffer.String())).AtInfo().WriteToLog() } return outBuffer.MultiBuffer, nil diff --git a/common/platform/platform.go b/common/platform/platform.go index 4fa253d9c..9e74ec22a 100644 --- a/common/platform/platform.go +++ b/common/platform/platform.go @@ -83,3 +83,9 @@ func GetConfigurationPath() string { configPath := NewEnvFlag(name).GetValue(getExecutableDir) return filepath.Join(configPath, "config.json") } + +func GetConfDirPath() string { + const name = "v2ray.location.confdir" + configPath := NewEnvFlag(name).GetValue(getExecutableDir) + return configPath +} diff --git a/infra/conf/v2ray.go b/infra/conf/v2ray.go index b87447231..56c1a2a49 100644 --- a/infra/conf/v2ray.go +++ b/infra/conf/v2ray.go @@ -2,6 +2,8 @@ package conf import ( "encoding/json" + "log" + "os" "strings" "v2ray.com/core" @@ -31,6 +33,8 @@ var ( "mtproto": func() interface{} { return new(MTProtoClientConfig) }, "dns": func() interface{} { return new(DnsOutboundConfig) }, }, "protocol", "settings") + + ctllog = log.New(os.Stderr, "v2ctl> ", 0) ) func toProtocolList(s []string) ([]proxyman.KnownProtocols, error) { @@ -361,10 +365,10 @@ func (c *Config) Override(o *Config, fn string) { if len(c.InboundConfigs) > 0 && len(o.InboundConfigs) == 1 { if idx := c.findInboundTag(o.InboundConfigs[0].Tag); idx > -1 { c.InboundConfigs[idx] = o.InboundConfigs[0] - newError("<", fn, "> updated inbound with tag: ", o.InboundConfigs[0].Tag).AtInfo().WriteToLog() + ctllog.Println("[", fn, "] updated inbound with tag: ", o.InboundConfigs[0].Tag) } else { c.InboundConfigs = append(c.InboundConfigs, o.InboundConfigs[0]) - newError("<", fn, "> appended inbound with tag: ", o.InboundConfigs[0].Tag).AtInfo().WriteToLog() + ctllog.Println("[", fn, "] appended inbound with tag: ", o.InboundConfigs[0].Tag) } } else { c.InboundConfigs = o.InboundConfigs @@ -376,14 +380,14 @@ func (c *Config) Override(o *Config, fn string) { if len(c.OutboundConfigs) > 0 && len(o.OutboundConfigs) == 1 { if idx := c.findOutboundTag(o.OutboundConfigs[0].Tag); idx > -1 { c.OutboundConfigs[idx] = o.OutboundConfigs[0] - newError("<", fn, "> updated outbound with tag: ", o.OutboundConfigs[0].Tag).AtInfo().WriteToLog() + ctllog.Println("[", fn, "] updated outbound with tag: ", o.OutboundConfigs[0].Tag) } else { if strings.Contains(strings.ToLower(fn), "tail") { c.OutboundConfigs = append(c.OutboundConfigs, o.OutboundConfigs[0]) - newError("<", fn, "> appended outbound with tag: ", o.OutboundConfigs[0].Tag).AtInfo().WriteToLog() + ctllog.Println("[", fn, "] appended outbound with tag: ", o.OutboundConfigs[0].Tag) } else { c.OutboundConfigs = append(o.OutboundConfigs, c.OutboundConfigs...) - newError("<", fn, "> prepended outbound with tag: ", o.OutboundConfigs[0].Tag).AtInfo().WriteToLog() + ctllog.Println("[", fn, "] prepended outbound with tag: ", o.OutboundConfigs[0].Tag) } } } else { diff --git a/infra/control/command.go b/infra/control/command.go index a261646ac..1edeac10c 100644 --- a/infra/control/command.go +++ b/infra/control/command.go @@ -2,6 +2,8 @@ package control import ( "fmt" + "log" + "os" "strings" ) @@ -18,6 +20,7 @@ type Command interface { var ( commandRegistry = make(map[string]Command) + ctllog = log.New(os.Stderr, "v2ctl> ", 0) ) func RegisterCommand(cmd Command) error { diff --git a/infra/control/config.go b/infra/control/config.go index 02d7cd5dd..8fca11b0b 100644 --- a/infra/control/config.go +++ b/infra/control/config.go @@ -37,7 +37,7 @@ func (c *ConfigCommand) Execute(args []string) error { conf := &conf.Config{} for _, arg := range args { - newError("Reading config: ", arg).AtInfo().WriteToLog() + ctllog.Println("Read config: ", arg) r, err := c.LoadArg(arg) common.Must(err) c, err := serial.DecodeJSONConfig(r) diff --git a/main/main.go b/main/main.go index 94ba7088d..2d6f2f803 100644 --- a/main/main.go +++ b/main/main.go @@ -5,8 +5,11 @@ package main import ( "flag" "fmt" + "io/ioutil" + "log" "os" "os/signal" + "path" "path/filepath" "runtime" "strings" @@ -20,6 +23,7 @@ import ( var ( 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.") test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.") format = flag.String("format", "json", "Format of input file.") @@ -31,7 +35,25 @@ func fileExists(file string) bool { 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 { + configFiles.Set(path.Join(dirPath, f.Name())) + } +} + func getConfigFilePath() (cmdarg.Arg, error) { + if dirExists(configDir) { + readConfDir(configDir) + } if len(configFiles) > 0 { return configFiles, nil } @@ -39,14 +61,25 @@ func getConfigFilePath() (cmdarg.Arg, error) { if workingDir, err := os.Getwd(); err == nil { configFile := filepath.Join(workingDir, "config.json") if fileExists(configFile) { + log.Println("Using default config: ", configFile) return cmdarg.Arg{configFile}, nil } } if configFile := platform.GetConfigurationPath(); fileExists(configFile) { + log.Println("Using config from env: ", configFile) 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 } @@ -87,7 +120,8 @@ func printVersion() { 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.Var(&configFiles, "c", "Short alias of -config") + flag.StringVar(&configDir, "confdir", "", "A dir with multiple json config") flag.Parse() printVersion() From c3f55dcd678f18a983c85f90a6f6a9adc1cced29 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Tue, 31 Dec 2019 13:11:34 +0800 Subject: [PATCH 2/5] filter .json in confDir --- infra/control/config.go | 4 +++- main/main.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/infra/control/config.go b/infra/control/config.go index 8fca11b0b..504975a8c 100644 --- a/infra/control/config.go +++ b/infra/control/config.go @@ -41,7 +41,9 @@ func (c *ConfigCommand) Execute(args []string) error { r, err := c.LoadArg(arg) common.Must(err) c, err := serial.DecodeJSONConfig(r) - common.Must(err) + if err != nil { + ctllog.Fatalln(err) + } conf.Override(c, arg) } diff --git a/main/main.go b/main/main.go index 2d6f2f803..298e97d12 100644 --- a/main/main.go +++ b/main/main.go @@ -46,7 +46,9 @@ func readConfDir(dirPath string) { log.Fatalln(err) } for _, f := range confs { - configFiles.Set(path.Join(dirPath, f.Name())) + if strings.HasSuffix(f.Name(), ".json") { + configFiles.Set(path.Join(dirPath, f.Name())) + } } } From e50afd6e440549262aa7363441ed572e050cb215 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Tue, 31 Dec 2019 13:29:25 +0800 Subject: [PATCH 3/5] trim err msg --- common/platform/ctlcmd/ctlcmd.go | 2 +- main/main.go | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/common/platform/ctlcmd/ctlcmd.go b/common/platform/ctlcmd/ctlcmd.go index dcb0a7249..1fad1e74e 100644 --- a/common/platform/ctlcmd/ctlcmd.go +++ b/common/platform/ctlcmd/ctlcmd.go @@ -36,7 +36,7 @@ func Run(args []string, input io.Reader) (buf.MultiBuffer, error) { if err := cmd.Wait(); err != nil { msg := "failed to execute v2ctl" if errBuffer.Len() > 0 { - msg += ": " + errBuffer.MultiBuffer.String() + msg += ": \n" + strings.TrimSpace(errBuffer.MultiBuffer.String()) } return nil, newError(msg).Base(err) } diff --git a/main/main.go b/main/main.go index 298e97d12..481b998c3 100644 --- a/main/main.go +++ b/main/main.go @@ -27,7 +27,6 @@ var ( 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", "json", "Format of input file.") - errNoConfig = newError("no valid config") ) func fileExists(file string) bool { @@ -134,11 +133,8 @@ func main() { server, err := startV2Ray() if err != nil { - fmt.Println(err.Error()) + fmt.Println(err) // Configuration error. Exit with a special value to prevent systemd from restarting. - if err == errNoConfig { - flag.PrintDefaults() - } os.Exit(23) } From 78b95d4bc219a7a22a4f08dd832e22ba3bf67883 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Tue, 31 Dec 2019 13:39:17 +0800 Subject: [PATCH 4/5] fix: default env empty --- common/platform/platform.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/platform/platform.go b/common/platform/platform.go index 9e74ec22a..6ccd97620 100644 --- a/common/platform/platform.go +++ b/common/platform/platform.go @@ -86,6 +86,6 @@ func GetConfigurationPath() string { func GetConfDirPath() string { const name = "v2ray.location.confdir" - configPath := NewEnvFlag(name).GetValue(getExecutableDir) + configPath := NewEnvFlag(name).GetValue(func() string { return "" }) return configPath } From 8ccc142e7e2c1fa2a926b707378f06f67585d0aa Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Tue, 31 Dec 2019 14:23:01 +0800 Subject: [PATCH 5/5] add comment to please codacy --- common/platform/platform.go | 1 + 1 file changed, 1 insertion(+) diff --git a/common/platform/platform.go b/common/platform/platform.go index 6ccd97620..b57a98fd7 100644 --- a/common/platform/platform.go +++ b/common/platform/platform.go @@ -84,6 +84,7 @@ func GetConfigurationPath() string { return filepath.Join(configPath, "config.json") } +// GetConfDirPath reads "v2ray.location.confdir" func GetConfDirPath() string { const name = "v2ray.location.confdir" configPath := NewEnvFlag(name).GetValue(func() string { return "" })