diff --git a/README.md b/README.md index 4d68653..d07fec8 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,6 @@ https://github.com/amidaware/tacticalrmm #### building the agent - linux ``` -go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo -go generate env CGO_ENABLED=0 GOOS= GOARCH= go build -ldflags "-s -w -X 'main.version=v2.0.4'" example: env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -X 'main.version=v2.0.4' -o build/output/rmmagent" ``` diff --git a/agent/system/system_unix.go b/agent/system/system_unix.go index f147e21..2746fef 100644 --- a/agent/system/system_unix.go +++ b/agent/system/system_unix.go @@ -7,7 +7,6 @@ import ( "bufio" "fmt" "os" - "path/filepath" "runtime" "strings" "syscall" @@ -287,21 +286,4 @@ func CMDShell(shell string, cmdArgs []string, command string, timeout int, detac func CMD(exe string, args []string, timeout int, detached bool) (output [2]string, e error) { return [2]string{"", ""}, nil -} - -func GetPythonBin() string { - opts := NewCMDOpts() - opts.Command = "which python" - out := CmdV2(opts) - return out.Stdout -} - -func GetProgramDirectory() string { - pd := filepath.Join(os.Getenv("ProgramFiles"), ProgFilesName) - return pd -} - -func GetProgramBin() string { - exe := filepath.Join(GetProgramDirectory(), winExeName) - return exe } \ No newline at end of file diff --git a/agent/tactical/shared/shared_test.go b/agent/tactical/shared/shared_test.go new file mode 100644 index 0000000..856fd72 --- /dev/null +++ b/agent/tactical/shared/shared_test.go @@ -0,0 +1,16 @@ +package shared_test + +import ( + "testing" + + "github.com/amidaware/rmmagent/agent/tactical/shared" +) + +func TestGetPythonBin(t *testing.T) { + pybin := shared.GetPythonBin() + if pybin == "" { + t.Errorf("expected path, got %s", pybin) + } + + t.Logf("result: %s", pybin) +} \ No newline at end of file diff --git a/agent/tactical/shared/shared_unix.go b/agent/tactical/shared/shared_unix.go index 2b8a792..7cb4c98 100644 --- a/agent/tactical/shared/shared_unix.go +++ b/agent/tactical/shared/shared_unix.go @@ -3,6 +3,34 @@ package shared +import ( + "os/exec" + "path/filepath" + "strings" +) + +const ( + binName = "tacticalagent" +) + func GetPython(force bool) {} -func RunMigrations() {} \ No newline at end of file +func RunMigrations() {} + +func GetPythonBin() string { + pybin, err := exec.Command("python", "-c", "import sys; print(sys.executable)").Output() + if err != nil { + return "" + } + + return strings.TrimSuffix(string(pybin), "\n") +} + +func GetProgramDirectory() string { + return "/usr/local/bin" +} + +func GetProgramBin() string { + bin := filepath.Join(GetProgramDirectory(), binName) + return bin +} \ No newline at end of file diff --git a/agent/tactical/structs.go b/agent/tactical/structs.go index 1f03ba5..e85ac07 100644 --- a/agent/tactical/structs.go +++ b/agent/tactical/structs.go @@ -1 +1,13 @@ package tactical + +type AgentConfig struct { + BaseURL string + AgentID string + APIURL string + Token string + AgentPK string + PK int + Cert string + Proxy string + CustomMeshDir string +} \ No newline at end of file diff --git a/agent/tactical/tactical_test.go b/agent/tactical/tactical_test.go index 348e178..c3870a7 100644 --- a/agent/tactical/tactical_test.go +++ b/agent/tactical/tactical_test.go @@ -8,5 +8,9 @@ import ( func TestGetVersion(t *testing.T) { version := tactical.GetVersion() + if version == "" { + t.Errorf("expected version, got empty version") + } + t.Logf("got version %s", version) } diff --git a/agent/tactical/tactical_linux.go b/agent/tactical/tactical_unix.go similarity index 86% rename from agent/tactical/tactical_linux.go rename to agent/tactical/tactical_unix.go index 88da0c9..5fa9d56 100644 --- a/agent/tactical/tactical_linux.go +++ b/agent/tactical/tactical_unix.go @@ -1,15 +1,20 @@ +//go:build !windows +// +build !windows + package tactical import ( "os" + "os/exec" + "regexp" "strconv" "strings" "time" "github.com/amidaware/rmmagent/agent/system" "github.com/amidaware/rmmagent/agent/tactical/mesh" + "github.com/amidaware/rmmagent/agent/tactical/shared" "github.com/amidaware/rmmagent/agent/utils" - "github.com/amidaware/rmmagent/shared" "github.com/go-resty/resty/v2" "github.com/spf13/viper" "github.com/wh1te909/trmm-shared" @@ -19,7 +24,7 @@ func GetMeshBinary() string { return "/opt/tacticalmesh/meshagent" } -func NewAgentConfig() *shared.AgentConfig { +func NewAgentConfig() *AgentConfig { viper.SetConfigName("tacticalagent") viper.SetConfigType("json") viper.AddConfigPath("/etc/") @@ -28,13 +33,13 @@ func NewAgentConfig() *shared.AgentConfig { err := viper.ReadInConfig() if err != nil { - return &shared.AgentConfig{} + return &AgentConfig{} } agentpk := viper.GetString("agentpk") pk, _ := strconv.Atoi(agentpk) - ret := &shared.AgentConfig{ + ret := &AgentConfig{ BaseURL: viper.GetString("baseurl"), AgentID: viper.GetString("agentid"), APIURL: viper.GetString("apiurl"), @@ -49,7 +54,7 @@ func NewAgentConfig() *shared.AgentConfig { return ret } -func AgentUpdate(url, inno, version string) bool { +func AgentUpdate(url string, inno string) bool { self, err := os.Executable() if err != nil { return false @@ -61,15 +66,12 @@ func AgentUpdate(url, inno, version string) bool { } defer os.Remove(f.Name()) - //logger.Infof("Agent updating from %s to %s", a.Version, version) - //logger.Infoln("Downloading agent update from", url) - rClient := resty.New() rClient.SetCloseConnection(true) rClient.SetTimeout(15 * time.Minute) - if shared.DEBUG { - rClient.SetDebug(true) - } + //if shared.DEBUG { + //rClient.SetDebug(true) + //} config := NewAgentConfig() if len(config.Proxy) > 0 { @@ -184,4 +186,15 @@ func installMesh(meshbin, exe, proxy string) (string, error) { return "not implemented", nil } -func SendSoftware() {} \ No newline at end of file +func SendSoftware() {} + +func GetVersion() string { + version, err := exec.Command(shared.GetProgramBin(), "-version").Output() + if err != nil { + return "" + } + + re := regexp.MustCompile(`Tactical RMM Agent: v([0-9]\.[0-9]\.[0-9])`) + match := re.FindStringSubmatch(string(version)) + return match[1] +} \ No newline at end of file diff --git a/agent/tactical/tactical_linux_test.go b/agent/tactical/tactical_unix_test.go similarity index 73% rename from agent/tactical/tactical_linux_test.go rename to agent/tactical/tactical_unix_test.go index a9bd446..8f5f7af 100644 --- a/agent/tactical/tactical_linux_test.go +++ b/agent/tactical/tactical_unix_test.go @@ -1,11 +1,13 @@ -package tactical +package tactical_test import ( "testing" + + "github.com/amidaware/rmmagent/agent/tactical" ) func TestNewAgentConfig(t *testing.T) { - config := NewAgentConfig() + config := tactical.NewAgentConfig() if config.BaseURL == "" { t.Fatal("Could not get config") } @@ -15,8 +17,8 @@ func TestNewAgentConfig(t *testing.T) { func TestAgentUpdate(t *testing.T) { url := "https://github.com/redanthrax/rmmagent/releases/download/v2.0.4/linuxagent" - result := AgentUpdate(url, "", "v2.0.4") - if(!result) { + result := tactical.AgentUpdate(url, "") + if !result { t.Fatal("Agent update resulted in false") } @@ -24,7 +26,7 @@ func TestAgentUpdate(t *testing.T) { } func TestAgentUninstall(t *testing.T) { - result := AgentUninstall("foo") + result := tactical.AgentUninstall("foo") if !result { t.Fatal("Agent uninstall resulted in error") } @@ -33,11 +35,10 @@ func TestAgentUninstall(t *testing.T) { } func TestNixMeshNodeID(t *testing.T) { - nodeid := NixMeshNodeID() + nodeid := tactical.NixMeshNodeID() if nodeid == "" { t.Fatal("Unable to get mesh node id") } t.Logf("MeshNodeID: %s", nodeid) } -