Skip to content

Commit deaafa9

Browse files
committed
Release 2.10.0
2 parents 1d36308 + 3790c52 commit deaafa9

16 files changed

Lines changed: 1614 additions & 61 deletions

agent/agent.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"errors"
2020
"fmt"
2121
"math"
22+
"net"
23+
"net/url"
2224
"os"
2325
"os/exec"
2426
"path/filepath"
@@ -37,6 +39,7 @@ import (
3739
nats "github.com/nats-io/nats.go"
3840
"github.com/shirou/gopsutil/v3/cpu"
3941
"github.com/sirupsen/logrus"
42+
"github.com/ugorji/go/codec"
4043
trmm "github.com/wh1te909/trmm-shared"
4144
)
4245

@@ -54,6 +57,7 @@ type Agent struct {
5457
EXE string
5558
SystemDrive string
5659
WinTmpDir string
60+
UnixTmpDir string
5761
WinRunAsUserTmpDir string
5862
MeshInstaller string
5963
MeshSystemEXE string
@@ -266,6 +270,7 @@ func New(logger *logrus.Logger, version string) *Agent {
266270
SystemDrive: sd,
267271
WinTmpDir: winTempDir,
268272
WinRunAsUserTmpDir: winRunAsUserTmpDir,
273+
UnixTmpDir: ac.UnixTmpDir,
269274
MeshInstaller: "meshagent.exe",
270275
MeshSystemEXE: MeshSysExe,
271276
MeshSVC: meshSvcName,
@@ -309,6 +314,9 @@ type CmdOptions struct {
309314
IsExecutable bool
310315
Detached bool
311316
EnvVars []string
317+
Stream bool
318+
Nc *nats.Conn // nats connection
319+
CmdID string
312320
}
313321

314322
func (a *Agent) NewCMDOpts() *CmdOptions {
@@ -371,6 +379,9 @@ func (a *Agent) CmdV2(c *CmdOptions) CmdStatus {
371379
}
372380
fmt.Fprintln(&stdoutBuf, line)
373381
a.Logger.Debugln(line)
382+
if c.Stream {
383+
streamLineToNats(line, a.AgentID, c.CmdID, c.Nc)
384+
}
374385

375386
case line, open := <-envCmd.Stderr:
376387
if !open {
@@ -379,6 +390,9 @@ func (a *Agent) CmdV2(c *CmdOptions) CmdStatus {
379390
}
380391
fmt.Fprintln(&stderrBuf, line)
381392
a.Logger.Debugln(line)
393+
if c.Stream {
394+
streamLineToNats(line, a.AgentID, c.CmdID, c.Nc)
395+
}
382396
}
383397
}
384398
}()
@@ -427,9 +441,30 @@ func (a *Agent) CmdV2(c *CmdOptions) CmdStatus {
427441
Stderr: CleanString(stderrBuf.String()),
428442
}
429443
a.Logger.Debugf("%+v\n", ret)
444+
445+
if c.Stream {
446+
finalPayload := map[string]interface{}{
447+
"done": true,
448+
"exit_code": finalStatus.Exit,
449+
}
450+
var finalResp []byte
451+
retEnc := codec.NewEncoderBytes(&finalResp, new(codec.MsgpackHandle))
452+
_ = retEnc.Encode(finalPayload)
453+
subject := a.AgentID + ".cmdoutput." + c.CmdID
454+
_ = c.Nc.Publish(subject, finalResp)
455+
}
456+
430457
return ret
431458
}
432459

460+
func streamLineToNats(line string, agentID string, cmdID string, nc *nats.Conn) {
461+
var resp []byte
462+
ret := codec.NewEncoderBytes(&resp, new(codec.MsgpackHandle))
463+
_ = ret.Encode(line)
464+
subject := agentID + ".cmdoutput." + cmdID
465+
_ = nc.Publish(subject, resp)
466+
}
467+
433468
func (a *Agent) GetCPULoadAvg() int {
434469
fallback := false
435470
pyCode := `
@@ -521,6 +556,33 @@ func (a *Agent) setupNatsOptions() []nats.Option {
521556
opts = append(opts, nats.ReconnectBufSize(-1))
522557
opts = append(opts, nats.ProxyPath(a.NatsProxyPath))
523558
opts = append(opts, nats.ReconnectJitter(500*time.Millisecond, 4*time.Second))
559+
560+
if a.Proxy != "" {
561+
proxyURL, err := url.Parse(a.Proxy)
562+
if err != nil {
563+
a.Logger.Errorf("setupNatsOptions(): failed to parse proxy URL: %v", err)
564+
} else {
565+
baseDialer := &net.Dialer{
566+
Timeout: 10 * time.Second,
567+
KeepAlive: 30 * time.Second,
568+
}
569+
570+
var dialFn func(network, addr string) (net.Conn, error)
571+
572+
switch proxyURL.Scheme {
573+
case "http", "https":
574+
dialFn = newHTTPConnectDialer(proxyURL, baseDialer)
575+
default:
576+
a.Logger.Errorf("setupNatsOptions(): unsupported proxy scheme: %s", proxyURL.Scheme)
577+
}
578+
579+
if dialFn != nil {
580+
cd := &customDialer{dialer: dialFn}
581+
opts = append(opts, nats.SetCustomDialer(cd))
582+
}
583+
}
584+
}
585+
524586
opts = append(opts, nats.DisconnectErrHandler(func(nc *nats.Conn, err error) {
525587
a.Logger.Debugln("NATS disconnected:", err)
526588
a.Logger.Debugf("%+v\n", nc.Statistics)
@@ -710,7 +772,7 @@ func (a *Agent) RunTask(id int) error {
710772

711773
switch runtime.GOOS {
712774
case "windows":
713-
out, err := CMDShell(action.Shell, []string{}, action.Command, action.Timeout, false, action.RunAsUser)
775+
out, err := CMDShell(action.Shell, []string{}, action.Command, action.Timeout, false, action.RunAsUser, false, nil, nil, nil)
714776
if err != nil {
715777
a.Logger.Debugln(err)
716778
}

agent/agent_unix.go

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/shirou/gopsutil/v3/cpu"
3636
"github.com/shirou/gopsutil/v3/disk"
3737
psHost "github.com/shirou/gopsutil/v3/host"
38+
nats "github.com/nats-io/nats.go"
3839
"github.com/spf13/viper"
3940
trmm "github.com/wh1te909/trmm-shared"
4041
"golang.org/x/text/cases"
@@ -165,6 +166,7 @@ func NewAgentConfig() *rmm.AgentConfig {
165166
NatsStandardPort: viper.GetString("natsstandardport"),
166167
NatsPingInterval: viper.GetInt("natspinginterval"),
167168
Insecure: viper.GetString("insecure"),
169+
UnixTmpDir: viper.GetString("tmpdir"),
168170
}
169171
return ret
170172
}
@@ -173,7 +175,7 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int,
173175
code = removeWinNewLines(code)
174176
content := []byte(code)
175177

176-
f, err := createNixTmpFile(shell)
178+
f, err := createNixTmpFile(a.UnixTmpDir, shell)
177179
if err != nil {
178180
a.Logger.Errorln("RunScript createNixTmpFile()", err)
179181
return "", err.Error(), 85, err
@@ -368,7 +370,7 @@ func (a *Agent) AgentUpdate(url, inno, version string) error {
368370
}
369371

370372
func (a *Agent) AgentUninstall(code string) {
371-
f, err := createNixTmpFile()
373+
f, err := createNixTmpFile(a.UnixTmpDir)
372374
if err != nil {
373375
a.Logger.Errorln("AgentUninstall createNixTmpFile():", err)
374376
return
@@ -665,20 +667,20 @@ func (a *Agent) InstallNushell(force bool) {
665667
case "darwin":
666668
switch runtime.GOARCH {
667669
case "arm64":
668-
// https://github.com/nushell/nushell/releases/download/0.87.0/nu-0.87.0-aarch64-darwin-full.tar.gz
669-
assetName = fmt.Sprintf("nu-%s-aarch64-darwin-full.tar.gz", conf.InstallNushellVersion)
670+
// https://github.com/nushell/nushell/releases/download/0.106.1/nu-0.106.1-aarch64-apple-darwin.tar.gz
671+
assetName = fmt.Sprintf("nu-%s-aarch64-apple-darwin.tar.gz", conf.InstallNushellVersion)
670672
default:
671673
a.Logger.Debugln("InstallNushell(): Unsupported architecture and OS:", runtime.GOARCH, runtime.GOOS)
672674
return
673675
}
674676
case "linux":
675677
switch runtime.GOARCH {
676678
case "amd64":
677-
// https://github.com/nushell/nushell/releases/download/0.87.0/nu-0.87.0-x86_64-linux-musl-full.tar.gz
678-
assetName = fmt.Sprintf("nu-%s-x86_64-linux-musl-full.tar.gz", conf.InstallNushellVersion)
679+
// https://github.com/nushell/nushell/releases/download/0.106.1/nu-0.106.1-x86_64-unknown-linux-musl.tar.gz
680+
assetName = fmt.Sprintf("nu-%s-x86_64-unknown-linux-musl.tar.gz", conf.InstallNushellVersion)
679681
case "arm64":
680-
// https://github.com/nushell/nushell/releases/download/0.87.0/nu-0.87.0-aarch64-linux-gnu-full.tar.gz
681-
assetName = fmt.Sprintf("nu-%s-aarch64-linux-gnu-full.tar.gz", conf.InstallNushellVersion)
682+
// https://github.com/nushell/nushell/releases/download/0.106.1/nu-0.106.1-aarch64-unknown-linux-musl.tar.gz
683+
assetName = fmt.Sprintf("nu-%s-aarch64-unknown-linux-musl.tar.gz", conf.InstallNushellVersion)
682684
default:
683685
a.Logger.Debugln("InstallNushell(): Unsupported architecture and OS:", runtime.GOARCH, runtime.GOOS)
684686
return
@@ -953,10 +955,42 @@ func (a *Agent) installMesh(meshbin, exe, proxy string) (string, error) {
953955
return "not implemented", nil
954956
}
955957

956-
func CMDShell(shell string, cmdArgs []string, command string, timeout int, detached bool, runasuser bool) (output [2]string, e error) {
958+
func CMDShell(shell string, cmdArgs []string, command string, timeout int, detached bool, runasuser bool, stream bool, agentID *string, cmdID *string, nc *nats.Conn) (output [2]string, e error) {
957959
return [2]string{"", ""}, nil
958960
}
959961

962+
func BrowseRegistry(path string, page int, pageSize int) ([]map[string]interface{}, []map[string]interface{}, bool, error) {
963+
return nil, nil, false, errors.New("registry access is only supported on Windows")
964+
}
965+
966+
func CreateRegistryKey(path string) error {
967+
return errors.New("registry key creation is only supported on Windows")
968+
}
969+
970+
func DeleteRegistryKey(path string) error {
971+
return errors.New("deleting registry keys is only supported on Windows")
972+
}
973+
974+
func RenameRegistryKey(oldPath, newPath string) error {
975+
return errors.New("renaming registry keys is only supported on Windows")
976+
}
977+
978+
func CreateRegistryValue(path string, name string, valType string, data interface{}) (map[string]interface{}, error) {
979+
return nil, errors.New("creating registry values is only supported on Windows")
980+
}
981+
982+
func DeleteRegistryValue(path string, name string) error {
983+
return errors.New("deleting registry values is only supported on Windows")
984+
}
985+
986+
func RenameRegistryValue(path, oldName, newName string) (string, error) {
987+
return "", errors.New("renaming registry values is only supported on Windows")
988+
}
989+
990+
func ModifyRegistryValue(path string, name string, valType string, data interface{}) (map[string]interface{}, error) {
991+
return nil, errors.New("modifying registry values is only supported on Windows")
992+
}
993+
960994
func CMD(exe string, args []string, timeout int, detached bool) (output [2]string, e error) {
961995
return [2]string{"", ""}, nil
962996
}

0 commit comments

Comments
 (0)