use external config converter first, and then fallback to the internal one

pull/703/merge
Darien Raymond 2017-11-11 22:29:00 +01:00
parent 901d13ca76
commit 268d7264e8
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 63 additions and 10 deletions

View File

@ -4,6 +4,7 @@ package platform
import ( import (
"os" "os"
"path/filepath"
) )
func ExpandEnv(s string) string { func ExpandEnv(s string) string {
@ -13,3 +14,9 @@ func ExpandEnv(s string) string {
func LineSeparator() string { func LineSeparator() string {
return "\n" 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)
}

View File

@ -45,14 +45,16 @@ func NormalizeEnvName(name string) string {
return strings.Replace(strings.ToUpper(strings.TrimSpace(name)), ".", "_", -1) 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 { func GetAssetLocation(file string) string {
const name = "v2ray.location.asset" const name = "v2ray.location.asset"
assetPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(func() string { assetPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir)
exec, err := os.Executable()
if err != nil {
return ""
}
return filepath.Dir(exec)
})
return filepath.Join(assetPath, file) return filepath.Join(assetPath, file)
} }

View File

@ -2,6 +2,8 @@
package platform package platform
import "path/filepath"
func ExpandEnv(s string) string { func ExpandEnv(s string) string {
// TODO // TODO
return s return s
@ -10,3 +12,9 @@ func ExpandEnv(s string) string {
func LineSeparator() string { func LineSeparator() string {
return "\r\n" 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")
}

View File

@ -1,5 +1,41 @@
// +build json
package main 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
})
}