mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
1.7 KiB
82 lines
1.7 KiB
7 years ago
|
package json
|
||
|
|
||
|
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg json -path Main,Json
|
||
8 years ago
|
|
||
7 years ago
|
import (
|
||
7 years ago
|
"context"
|
||
7 years ago
|
"io"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
|
||
|
"v2ray.com/core"
|
||
7 years ago
|
"v2ray.com/core/common"
|
||
7 years ago
|
"v2ray.com/core/common/platform"
|
||
7 years ago
|
"v2ray.com/core/common/signal"
|
||
7 years ago
|
)
|
||
|
|
||
7 years ago
|
type logWriter struct{}
|
||
|
|
||
|
func (*logWriter) Write(b []byte) (int, error) {
|
||
|
n, err := os.Stderr.Write(b)
|
||
|
if err == nil {
|
||
|
os.Stderr.WriteString(platform.LineSeparator())
|
||
|
}
|
||
|
return n, err
|
||
|
}
|
||
|
|
||
7 years ago
|
func jsonToProto(input io.Reader) (*core.Config, error) {
|
||
|
v2ctl := platform.GetToolLocation("v2ctl")
|
||
7 years ago
|
if _, err := os.Stat(v2ctl); err != nil {
|
||
7 years ago
|
return nil, err
|
||
|
}
|
||
|
cmd := exec.Command(v2ctl, "config")
|
||
|
cmd.Stdin = input
|
||
7 years ago
|
cmd.Stderr = &logWriter{}
|
||
7 years ago
|
cmd.SysProcAttr = getSysProcAttr()
|
||
7 years ago
|
|
||
|
stdoutReader, err := cmd.StdoutPipe()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer stdoutReader.Close()
|
||
7 years ago
|
|
||
|
if err := cmd.Start(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
7 years ago
|
var config *core.Config
|
||
|
|
||
|
loadTask := signal.ExecuteAsync(func() error {
|
||
7 years ago
|
c, err := core.LoadConfig("protobuf", "", stdoutReader)
|
||
7 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
config = c
|
||
|
return nil
|
||
|
})
|
||
|
|
||
|
waitTask := signal.ExecuteAsync(func() error {
|
||
|
return cmd.Wait()
|
||
|
})
|
||
7 years ago
|
|
||
7 years ago
|
if err := signal.ErrorOrFinish2(context.Background(), loadTask, waitTask); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
return config, nil
|
||
7 years ago
|
}
|
||
|
|
||
|
func init() {
|
||
7 years ago
|
common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
|
||
|
Name: "JSON",
|
||
|
Extension: []string{"json"},
|
||
|
Loader: func(input io.Reader) (*core.Config, error) {
|
||
|
config, err := jsonToProto(input)
|
||
|
if err != nil {
|
||
|
return nil, newError("failed to execute v2ctl to convert config file.").Base(err).AtWarning()
|
||
|
}
|
||
|
return config, nil
|
||
|
},
|
||
7 years ago
|
}))
|
||
7 years ago
|
}
|