1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-06 18:28:00 -05:00
v2fly/common/platform/ctlcmd/ctlcmd.go

51 lines
1.1 KiB
Go
Raw Normal View History

2018-04-08 17:22:55 -04:00
package ctlcmd
import (
"io"
"os"
"os/exec"
2019-12-30 22:45:19 -05:00
"strings"
2018-04-08 17:22:55 -04:00
"v2ray.com/core/common/buf"
"v2ray.com/core/common/platform"
)
2018-09-30 17:08:41 -04:00
//go:generate errorgen
2018-04-08 17:22:55 -04:00
func Run(args []string, input io.Reader) (buf.MultiBuffer, error) {
v2ctl := platform.GetToolLocation("v2ctl")
if _, err := os.Stat(v2ctl); err != nil {
2018-04-13 05:25:41 -04:00
return nil, newError("v2ctl doesn't exist").Base(err)
2018-04-08 17:22:55 -04:00
}
2018-11-18 13:36:36 -05:00
var errBuffer buf.MultiBufferContainer
var outBuffer buf.MultiBufferContainer
2018-04-08 17:22:55 -04:00
cmd := exec.Command(v2ctl, args...)
2018-04-13 07:54:36 -04:00
cmd.Stderr = &errBuffer
cmd.Stdout = &outBuffer
2018-04-08 17:22:55 -04:00
cmd.SysProcAttr = getSysProcAttr()
if input != nil {
cmd.Stdin = input
}
if err := cmd.Start(); err != nil {
2018-04-13 05:25:41 -04:00
return nil, newError("failed to start v2ctl").Base(err)
2018-04-08 17:22:55 -04:00
}
2018-04-13 07:54:36 -04:00
if err := cmd.Wait(); err != nil {
msg := "failed to execute v2ctl"
if errBuffer.Len() > 0 {
2019-12-31 00:29:25 -05:00
msg += ": \n" + strings.TrimSpace(errBuffer.MultiBuffer.String())
2018-04-08 17:22:55 -04:00
}
2018-04-13 07:54:36 -04:00
return nil, newError(msg).Base(err)
2018-04-08 17:22:55 -04:00
}
2019-12-14 09:24:32 -05:00
// log stderr, info message
2019-12-14 08:43:47 -05:00
if !errBuffer.IsEmpty() {
2019-12-30 22:45:19 -05:00
newError("<v2ctl message> \n", strings.TrimSpace(errBuffer.MultiBuffer.String())).AtInfo().WriteToLog()
2019-12-14 08:43:47 -05:00
}
2018-04-08 17:22:55 -04:00
2018-11-18 13:36:36 -05:00
return outBuffer.MultiBuffer, nil
2018-04-08 17:22:55 -04:00
}