v2fly/common/platform/ctlcmd/ctlcmd.go

51 lines
1.2 KiB
Go
Raw Normal View History

2018-04-08 21:22:55 +00:00
package ctlcmd
import (
"io"
"os"
"os/exec"
2020-01-03 01:26:48 +00:00
"strings"
2018-04-08 21:22:55 +00:00
2021-02-16 20:31:50 +00:00
"github.com/v2fly/v2ray-core/v4/common/buf"
"github.com/v2fly/v2ray-core/v4/common/platform"
2018-04-08 21:22:55 +00:00
)
2021-02-16 20:31:50 +00:00
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
2018-04-08 21:22:55 +00: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 09:25:41 +00:00
return nil, newError("v2ctl doesn't exist").Base(err)
2018-04-08 21:22:55 +00:00
}
2018-11-18 18:36:36 +00:00
var errBuffer buf.MultiBufferContainer
var outBuffer buf.MultiBufferContainer
2018-04-08 21:22:55 +00:00
cmd := exec.Command(v2ctl, args...)
2018-04-13 11:54:36 +00:00
cmd.Stderr = &errBuffer
cmd.Stdout = &outBuffer
2018-04-08 21:22:55 +00:00
cmd.SysProcAttr = getSysProcAttr()
if input != nil {
cmd.Stdin = input
}
if err := cmd.Start(); err != nil {
2018-04-13 09:25:41 +00:00
return nil, newError("failed to start v2ctl").Base(err)
2018-04-08 21:22:55 +00:00
}
2018-04-13 11:54:36 +00:00
if err := cmd.Wait(); err != nil {
msg := "failed to execute v2ctl"
if errBuffer.Len() > 0 {
2020-01-03 01:26:48 +00:00
msg += ": \n" + strings.TrimSpace(errBuffer.MultiBuffer.String())
2018-04-08 21:22:55 +00:00
}
2018-04-13 11:54:36 +00:00
return nil, newError(msg).Base(err)
2018-04-08 21:22:55 +00:00
}
2019-12-14 14:24:32 +00:00
2020-01-03 01:26:48 +00:00
// log stderr, info message
if !errBuffer.IsEmpty() {
newError("<v2ctl message> \n", strings.TrimSpace(errBuffer.MultiBuffer.String())).AtInfo().WriteToLog()
}
2018-11-18 18:36:36 +00:00
return outBuffer.MultiBuffer, nil
2018-04-08 21:22:55 +00:00
}