diff --git a/common/platform/others.go b/common/platform/others.go index 0bd7ee77d..e93f5af6b 100644 --- a/common/platform/others.go +++ b/common/platform/others.go @@ -4,6 +4,7 @@ package platform import ( "os" + "path/filepath" ) func ExpandEnv(s string) string { @@ -13,3 +14,9 @@ func ExpandEnv(s string) string { func LineSeparator() string { return "\n" } + +func GetToolLocation(file string) string { + const name = "v2ray.location.tool" + toolPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir) + return filepath.Join(toolPath, file) +} diff --git a/common/platform/platform.go b/common/platform/platform.go index 6e41897cd..fe9acdc79 100644 --- a/common/platform/platform.go +++ b/common/platform/platform.go @@ -45,14 +45,16 @@ func NormalizeEnvName(name string) string { return strings.Replace(strings.ToUpper(strings.TrimSpace(name)), ".", "_", -1) } +func getExecutableDir() string { + exec, err := os.Executable() + if err != nil { + return "" + } + return filepath.Dir(exec) +} + func GetAssetLocation(file string) string { const name = "v2ray.location.asset" - assetPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(func() string { - exec, err := os.Executable() - if err != nil { - return "" - } - return filepath.Dir(exec) - }) + assetPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir) return filepath.Join(assetPath, file) } diff --git a/common/platform/windows.go b/common/platform/windows.go index 7fc1f90d7..c311ef5c0 100644 --- a/common/platform/windows.go +++ b/common/platform/windows.go @@ -2,6 +2,8 @@ package platform +import "path/filepath" + func ExpandEnv(s string) string { // TODO return s @@ -10,3 +12,9 @@ func ExpandEnv(s string) string { func LineSeparator() string { return "\r\n" } + +func GetToolLocation(file string) string { + const name = "v2ray.location.tool" + toolPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir) + return filepath.Join(toolPath, file+".exe") +} diff --git a/main/config_json.go b/main/config_json.go index bf0093df4..157dee041 100644 --- a/main/config_json.go +++ b/main/config_json.go @@ -1,5 +1,41 @@ -// +build json - package main -import _ "v2ray.com/core/tools/conf" +import ( + "io" + "os" + "os/exec" + + "v2ray.com/core" + "v2ray.com/core/app/log" + "v2ray.com/core/common/platform" + jsonconf "v2ray.com/ext/tools/conf/serial" +) + +func jsonToProto(input io.Reader) (*core.Config, error) { + v2ctl := platform.GetToolLocation("v2ctl") + _, err := os.Stat(v2ctl) + if err != nil { + return nil, err + } + cmd := exec.Command(v2ctl, "config") + cmd.Stdin = input + cmd.Stderr = os.Stderr + + stdoutReader, err := cmd.StdoutPipe() + if err != nil { + return nil, err + } + defer stdoutReader.Close() + return core.LoadConfig(core.ConfigFormat_Protobuf, stdoutReader) +} + +func init() { + core.RegisterConfigLoader(core.ConfigFormat_JSON, func(input io.Reader) (*core.Config, error) { + config, err := jsonToProto(input) + if err != nil { + log.Trace(newError("failed to execute v2ctl to convert config file.").Base(err).AtWarning()) + return jsonconf.LoadJSONConfig(input) + } + return config, nil + }) +}