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

51 lines
1.2 KiB
Go
Raw Normal View History

2018-04-08 17:22:55 -04:00
package ctlcmd
import (
"io"
"os"
"os/exec"
2020-01-02 20:26:48 -05:00
"strings"
2018-04-08 17:22:55 -04:00
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common/buf"
"github.com/v2fly/v2ray-core/v4/common/platform"
2018-04-08 17:22:55 -04:00
)
2021-02-16 15:31:50 -05:00
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/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 {
2020-01-02 20:26:48 -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
2020-01-02 20:26:48 -05:00
// log stderr, info message
if !errBuffer.IsEmpty() {
newError("<v2ctl message> \n", strings.TrimSpace(errBuffer.MultiBuffer.String())).AtInfo().WriteToLog()
}
2018-11-18 13:36:36 -05:00
return outBuffer.MultiBuffer, nil
2018-04-08 17:22:55 -04:00
}