2 Commits

Author SHA1 Message Date
zero@thinky 0f9697f6f3 fix(proto): package path 2026-04-03 23:56:28 +03:00
zero@thinky f4bcee658d feat(agent): init; add commander 2026-04-03 23:37:03 +03:00
5 changed files with 68 additions and 2 deletions
+65
View File
@@ -0,0 +1,65 @@
package commander
import (
"bytes"
"errors"
"io"
"os/exec"
"gitea.d3m0k1d.ru/d3m0k1d/HellreigN/proto"
"golang.org/x/sync/errgroup"
)
type Commander struct {
}
func (*Commander) Execute(command *proto.Command) (*proto.FinishedCommand, error) {
cmd := exec.Command(command.Command[0], command.Command[1:]...)
var (
stdin io.WriteCloser
err error
)
if command.Stdin != nil {
stdin, err = cmd.StdinPipe()
if err != nil {
return nil, err
}
}
stdout, err1 := cmd.StdoutPipe()
stderr, err2 := cmd.StderrPipe()
if err := errors.Join(err1, err2); err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}
if command.Stdin != nil {
io.WriteString(stdin, *command.Stdin)
if err := stdin.Close(); err != nil {
return nil, err
}
}
eg := new(errgroup.Group)
stdoutbuf := new(bytes.Buffer)
stderrbuf := new(bytes.Buffer)
eg.Go(func() error {
_, err := io.Copy(stdoutbuf, stdout)
return err
})
eg.Go(func() error {
_, err := io.Copy(stderrbuf, stderr)
return err
})
if err := cmd.Wait(); err != nil {
return nil, err
}
if err := eg.Wait(); err != nil {
return nil, err
}
return &proto.FinishedCommand{
Id: command.Id,
Status: int32(cmd.ProcessState.ExitCode()),
Stdout: stdoutbuf.String(),
Stderr: stderrbuf.String(),
}, nil
}
+1
View File
@@ -0,0 +1 @@
package main
+1 -1
View File
@@ -2,7 +2,7 @@ syntax = "proto3";
package chat;
option go_package="gitea.d3m0k1d.ru/d3m0k1d/HellreigN/proto";
option go_package="gitea.d3m0k1d.ru/d3m0k1d/HellreigN/proto/proto";
service Collector {
rpc Stream(stream CollectorRequest) returns (CollectorResponse);
@@ -250,7 +250,7 @@ const file_hellreign_proto_rawDesc = "" +
"\tCollector\x12;\n" +
"\x06Stream\x12\x16.chat.CollectorRequest\x1a\x17.chat.CollectorResponse(\x012?\n" +
"\tCommander\x122\n" +
"\x06Stream\x12\r.chat.Command\x1a\x15.chat.FinishedCommand(\x010\x01B*Z(gitea.d3m0k1d.ru/d3m0k1d/HellreigN/protob\x06proto3"
"\x06Stream\x12\r.chat.Command\x1a\x15.chat.FinishedCommand(\x010\x01B0Z.gitea.d3m0k1d.ru/d3m0k1d/HellreigN/proto/protob\x06proto3"
var (
file_hellreign_proto_rawDescOnce sync.Once