mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-09-07 04:24:23 -04:00
47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package ctlcmd
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
|
|
"v2ray.com/core/common/buf"
|
|
"v2ray.com/core/common/platform"
|
|
)
|
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg ctlcmd -path Command,Platform,CtlCmd
|
|
|
|
func Run(args []string, input io.Reader) (buf.MultiBuffer, error) {
|
|
v2ctl := platform.GetToolLocation("v2ctl")
|
|
if _, err := os.Stat(v2ctl); err != nil {
|
|
return nil, newError("v2ctl doesn't exist").Base(err)
|
|
}
|
|
|
|
errBuffer := buf.MultiBuffer{}
|
|
outBuffer := buf.MultiBuffer{}
|
|
|
|
cmd := exec.Command(v2ctl, args...)
|
|
cmd.Stderr = &errBuffer
|
|
cmd.Stdout = &outBuffer
|
|
cmd.SysProcAttr = getSysProcAttr()
|
|
if input != nil {
|
|
cmd.Stdin = input
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
return nil, newError("failed to start v2ctl").Base(err)
|
|
}
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
msg := "failed to execute v2ctl"
|
|
if errBuffer.Len() > 0 {
|
|
msg += ": " + errBuffer.String()
|
|
}
|
|
errBuffer.Release()
|
|
outBuffer.Release()
|
|
return nil, newError(msg).Base(err)
|
|
}
|
|
|
|
return outBuffer, nil
|
|
}
|