From aed33b9a95fadb44e60c0522185dcce09a849623 Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Sun, 26 Jun 2022 09:02:48 -0700 Subject: [PATCH 1/2] update for new exe format --- agent/agent.go | 8 ++++++++ agent/agent_windows.go | 2 +- agent/process.go | 7 +++++++ main.go | 2 +- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index f299863..5fb4dd8 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -384,6 +384,7 @@ func (a *Agent) CleanupAgentUpdates() { return } + // winagent-v* is deprecated files, err := filepath.Glob("winagent-v*.exe") if err == nil { for _, f := range files { @@ -391,6 +392,13 @@ func (a *Agent) CleanupAgentUpdates() { } } + agents, err := filepath.Glob("tacticalagent-v*.exe") + if err == nil { + for _, f := range agents { + os.Remove(f) + } + } + cderr = os.Chdir(os.Getenv("TMP")) if cderr != nil { a.Logger.Errorln(cderr) diff --git a/agent/agent_windows.go b/agent/agent_windows.go index 0912cd2..541184b 100644 --- a/agent/agent_windows.go +++ b/agent/agent_windows.go @@ -635,7 +635,7 @@ func (a *Agent) AgentUninstall(code string) { func (a *Agent) addDefenderExlusions() { code := ` Add-MpPreference -ExclusionPath 'C:\Program Files\TacticalAgent\*' -Add-MpPreference -ExclusionPath 'C:\Windows\Temp\winagent-v*.exe' +Add-MpPreference -ExclusionPath 'C:\Windows\Temp\tacticalagent-v*.exe' Add-MpPreference -ExclusionPath 'C:\Windows\Temp\trmm\*' Add-MpPreference -ExclusionPath 'C:\Program Files\Mesh Agent\*' ` diff --git a/agent/process.go b/agent/process.go index bd3e4ee..ae7c392 100644 --- a/agent/process.go +++ b/agent/process.go @@ -64,9 +64,16 @@ func (a *Agent) KillHungUpdates() { if err != nil { continue } + + // winagent-v* is deprecated if strings.Contains(p.Exe, "winagent-v") { a.Logger.Debugln("killing process", p.Exe) KillProc(int32(p.PID)) } + + if strings.Contains(p.Exe, "tacticalagent-v") { + a.Logger.Debugln("killing process", p.Exe) + KillProc(int32(p.PID)) + } } } diff --git a/main.go b/main.go index 5b3b35c..7bbface 100644 --- a/main.go +++ b/main.go @@ -197,6 +197,6 @@ func installUsage() { } func updateUsage() { - u := `Usage: tacticalrmm.exe -m update -updateurl https://example.com/winagent-vX.X.X.exe -inno winagent-vX.X.X.exe -updatever 1.1.1` + u := `Usage: tacticalrmm.exe -m update -updateurl https://example.com/tacticalagent-vX.X.X.exe -inno tacticalagent-vX.X.X.exe -updatever 1.1.1` fmt.Println(u) } From 8d2511129c96565096e0fd09bca8c9ee56054d97 Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Sun, 26 Jun 2022 15:34:00 -0700 Subject: [PATCH 2/2] support for nats websocket amidaware/tacticalrmm@42e1717455f6d3933266e9be9dd10e4fce2c7492 --- agent/agent.go | 18 ++++++++++++++++++ agent/agent_unix.go | 2 ++ agent/agent_windows.go | 4 ++++ agent/checkin.go | 4 +--- agent/install.go | 5 ----- agent/rpc.go | 3 +-- agent/svc.go | 4 +--- shared/types.go | 2 ++ 8 files changed, 29 insertions(+), 13 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 5fb4dd8..9e29645 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -66,6 +66,9 @@ type Agent struct { Platform string GoArch string ServiceConfig *service.Config + NatsServer string + NatsProxyPath string + NatsProxyPort string } const ( @@ -139,6 +142,17 @@ func New(logger *logrus.Logger, version string) *Agent { }, } + var natsProxyPath, natsProxyPort string + if ac.NatsProxyPath == "" { + natsProxyPath = "natsws" + } + + if ac.NatsProxyPort == "" { + natsProxyPort = "443" + } + + natsServer := fmt.Sprintf("wss://%s:%s", ac.APIURL, natsProxyPort) + return &Agent{ Hostname: info.Hostname, BaseURL: ac.BaseURL, @@ -163,6 +177,9 @@ func New(logger *logrus.Logger, version string) *Agent { Platform: runtime.GOOS, GoArch: runtime.GOARCH, ServiceConfig: svcConf, + NatsServer: natsServer, + NatsProxyPath: natsProxyPath, + NatsProxyPort: natsProxyPort, } } @@ -359,6 +376,7 @@ func (a *Agent) setupNatsOptions() []nats.Option { opts = append(opts, nats.RetryOnFailedConnect(true)) opts = append(opts, nats.MaxReconnects(-1)) opts = append(opts, nats.ReconnectBufSize(-1)) + opts = append(opts, nats.ProxyPath("natsws")) return opts } diff --git a/agent/agent_unix.go b/agent/agent_unix.go index ddd5bac..42630c7 100644 --- a/agent/agent_unix.go +++ b/agent/agent_unix.go @@ -154,6 +154,8 @@ func NewAgentConfig() *rmm.AgentConfig { Cert: viper.GetString("cert"), Proxy: viper.GetString("proxy"), CustomMeshDir: viper.GetString("meshdir"), + NatsProxyPath: viper.GetString("natsproxypath"), + NatsProxyPort: viper.GetString("natsproxyport"), } return ret } diff --git a/agent/agent_windows.go b/agent/agent_windows.go index 541184b..5b83535 100644 --- a/agent/agent_windows.go +++ b/agent/agent_windows.go @@ -61,6 +61,8 @@ func NewAgentConfig() *rmm.AgentConfig { cert, _, _ := k.GetStringValue("Cert") proxy, _, _ := k.GetStringValue("Proxy") customMeshDir, _, _ := k.GetStringValue("MeshDir") + natsProxyPath, _, _ := k.GetStringValue("NatsProxyPath") + natsProxyPort, _, _ := k.GetStringValue("NatsProxyPort") return &rmm.AgentConfig{ BaseURL: baseurl, @@ -72,6 +74,8 @@ func NewAgentConfig() *rmm.AgentConfig { Cert: cert, Proxy: proxy, CustomMeshDir: customMeshDir, + NatsProxyPath: natsProxyPath, + NatsProxyPort: natsProxyPort, } } diff --git a/agent/checkin.go b/agent/checkin.go index 1deebbd..9990198 100644 --- a/agent/checkin.go +++ b/agent/checkin.go @@ -12,7 +12,6 @@ https://license.tacticalrmm.com package agent import ( - "fmt" "runtime" "time" @@ -78,8 +77,7 @@ func (a *Agent) NatsMessage(nc *nats.Conn, mode string) { func (a *Agent) DoNatsCheckIn() { opts := a.setupNatsOptions() - server := fmt.Sprintf("tls://%s:4222", a.ApiURL) - nc, err := nats.Connect(server, opts...) + nc, err := nats.Connect(a.NatsServer, opts...) if err != nil { a.Logger.Errorln(err) return diff --git a/agent/install.go b/agent/install.go index db8fad3..a975581 100644 --- a/agent/install.go +++ b/agent/install.go @@ -82,11 +82,6 @@ func (a *Agent) Install(i *Installer) { a.Logger.Debugln("API:", i.SaltMaster) - terr := TestTCP(fmt.Sprintf("%s:4222", i.SaltMaster)) - if terr != nil { - a.installerMsg(fmt.Sprintf("ERROR: Either port 4222 TCP is not open on your RMM, or nats.service is not running.\n\n%s", terr.Error()), "error", i.Silent) - } - baseURL := u.Scheme + "://" + u.Host a.Logger.Debugln("Base URL:", baseURL) diff --git a/agent/rpc.go b/agent/rpc.go index 7d24d0b..e457ecd 100644 --- a/agent/rpc.go +++ b/agent/rpc.go @@ -54,8 +54,7 @@ func (a *Agent) RunRPC() { var wg sync.WaitGroup wg.Add(1) opts := a.setupNatsOptions() - server := fmt.Sprintf("tls://%s:4222", a.ApiURL) - nc, err := nats.Connect(server, opts...) + nc, err := nats.Connect(a.NatsServer, opts...) if err != nil { a.Logger.Fatalln("RunRPC() nats.Connect()", err) } diff --git a/agent/svc.go b/agent/svc.go index 6e21b15..e360ebf 100644 --- a/agent/svc.go +++ b/agent/svc.go @@ -12,7 +12,6 @@ https://license.tacticalrmm.com package agent import ( - "fmt" "sync" "time" @@ -38,8 +37,7 @@ func (a *Agent) AgentSvc() { time.Sleep(time.Duration(sleepDelay) * time.Second) opts := a.setupNatsOptions() - server := fmt.Sprintf("tls://%s:4222", a.ApiURL) - nc, err := nats.Connect(server, opts...) + nc, err := nats.Connect(a.NatsServer, opts...) if err != nil { a.Logger.Fatalln("AgentSvc() nats.Connect()", err) } diff --git a/shared/types.go b/shared/types.go index 64ae5be..683b85b 100644 --- a/shared/types.go +++ b/shared/types.go @@ -42,6 +42,8 @@ type AgentConfig struct { Cert string Proxy string CustomMeshDir string + NatsProxyPath string + NatsProxyPort string } type RunScriptResp struct {