2020-11-25 11:01:53 +00:00
|
|
|
package ctlcmd
|
|
|
|
|
|
|
|
import (
|
2024-06-29 18:32:57 +00:00
|
|
|
"context"
|
2020-11-25 11:01:53 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common/buf"
|
2024-06-29 18:32:57 +00:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common/platform"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Run(args []string, input io.Reader) (buf.MultiBuffer, error) {
|
|
|
|
xctl := platform.GetToolLocation("xctl")
|
|
|
|
if _, err := os.Stat(xctl); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return nil, errors.New("xctl doesn't exist").Base(err)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var errBuffer buf.MultiBufferContainer
|
|
|
|
var outBuffer buf.MultiBufferContainer
|
|
|
|
|
|
|
|
cmd := exec.Command(xctl, args...)
|
|
|
|
cmd.Stderr = &errBuffer
|
|
|
|
cmd.Stdout = &outBuffer
|
|
|
|
cmd.SysProcAttr = getSysProcAttr()
|
|
|
|
if input != nil {
|
|
|
|
cmd.Stdin = input
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return nil, errors.New("failed to start xctl").Base(err)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
msg := "failed to execute xctl"
|
|
|
|
if errBuffer.Len() > 0 {
|
|
|
|
msg += ": \n" + strings.TrimSpace(errBuffer.MultiBuffer.String())
|
|
|
|
}
|
2024-06-29 18:32:57 +00:00
|
|
|
return nil, errors.New(msg).Base(err)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// log stderr, info message
|
|
|
|
if !errBuffer.IsEmpty() {
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogInfo(context.Background(), "<xctl message> \n", strings.TrimSpace(errBuffer.MultiBuffer.String()))
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return outBuffer.MultiBuffer, nil
|
|
|
|
}
|