2016-10-15 18:46:30 -04:00
|
|
|
package main
|
|
|
|
|
2017-11-11 16:29:00 -05:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"v2ray.com/core"
|
|
|
|
"v2ray.com/core/common/platform"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2017-12-13 07:42:07 -05:00
|
|
|
cmd.SysProcAttr = getSysProcAttr()
|
2017-11-11 16:29:00 -05:00
|
|
|
|
|
|
|
stdoutReader, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stdoutReader.Close()
|
2017-11-16 18:51:04 -05:00
|
|
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := core.LoadConfig(core.ConfigFormat_Protobuf, stdoutReader)
|
|
|
|
|
|
|
|
cmd.Wait()
|
|
|
|
|
|
|
|
return config, err
|
2017-11-11 16:29:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
core.RegisterConfigLoader(core.ConfigFormat_JSON, func(input io.Reader) (*core.Config, error) {
|
|
|
|
config, err := jsonToProto(input)
|
|
|
|
if err != nil {
|
2017-11-19 15:44:49 -05:00
|
|
|
return nil, newError("failed to execute v2ctl to convert config file.").Base(err)
|
2017-11-11 16:29:00 -05:00
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
})
|
|
|
|
}
|