2018-02-17 15:22:51 -05:00
|
|
|
package json
|
|
|
|
|
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg json -path Main,Json
|
2016-10-15 18:46:30 -04:00
|
|
|
|
2017-11-11 16:29:00 -05:00
|
|
|
import (
|
2018-02-12 05:40:42 -05:00
|
|
|
"context"
|
2017-11-11 16:29:00 -05:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"v2ray.com/core"
|
2018-02-12 05:40:42 -05:00
|
|
|
"v2ray.com/core/common"
|
2017-11-11 16:29:00 -05:00
|
|
|
"v2ray.com/core/common/platform"
|
2018-02-12 05:40:42 -05:00
|
|
|
"v2ray.com/core/common/signal"
|
2017-11-11 16:29:00 -05:00
|
|
|
)
|
|
|
|
|
2018-02-12 05:40:42 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-11-11 16:29:00 -05:00
|
|
|
func jsonToProto(input io.Reader) (*core.Config, error) {
|
|
|
|
v2ctl := platform.GetToolLocation("v2ctl")
|
2018-02-12 05:40:42 -05:00
|
|
|
if _, err := os.Stat(v2ctl); err != nil {
|
2017-11-11 16:29:00 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cmd := exec.Command(v2ctl, "config")
|
|
|
|
cmd.Stdin = input
|
2018-02-12 05:40:42 -05:00
|
|
|
cmd.Stderr = &logWriter{}
|
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
|
|
|
|
}
|
|
|
|
|
2018-02-12 05:40:42 -05:00
|
|
|
var config *core.Config
|
|
|
|
|
|
|
|
loadTask := signal.ExecuteAsync(func() error {
|
2018-02-14 17:57:40 -05:00
|
|
|
c, err := core.LoadConfig("protobuf", "", stdoutReader)
|
2018-02-12 05:40:42 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config = c
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
waitTask := signal.ExecuteAsync(func() error {
|
|
|
|
return cmd.Wait()
|
|
|
|
})
|
2017-11-16 18:51:04 -05:00
|
|
|
|
2018-02-12 05:40:42 -05:00
|
|
|
if err := signal.ErrorOrFinish2(context.Background(), loadTask, waitTask); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-16 18:51:04 -05:00
|
|
|
|
2018-02-12 05:40:42 -05:00
|
|
|
return config, nil
|
2017-11-11 16:29:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-02-14 17:57:40 -05:00
|
|
|
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
|
|
|
|
},
|
2018-02-12 05:40:42 -05:00
|
|
|
}))
|
2017-11-11 16:29:00 -05:00
|
|
|
}
|