Add: Download the nu and deno binaries from GitHub
Signed-off-by: David Randall <David@NiceGuyIT.biz>
This commit is contained in:
parent
6ba3272dc0
commit
87e1b29ef6
8 changed files with 592 additions and 11 deletions
|
|
@ -19,6 +19,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
|
@ -156,9 +157,41 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int,
|
|||
case "cmd":
|
||||
exe = tmpfn.Name()
|
||||
case "nushell":
|
||||
exe = tmpfn.Name()
|
||||
exe = a.NuBin
|
||||
cmdArgs = []string{"--no-config-file", tmpfn.Name()}
|
||||
if !trmm.FileExists(a.NuBin) {
|
||||
a.Logger.Errorln("RunScript(): Executable does not exist. Install Nu and try again:", a.NuBin)
|
||||
err := errors.New("File Not Found: " + a.NuBin)
|
||||
return "", err.Error(), 85, err
|
||||
}
|
||||
case "deno":
|
||||
exe = tmpfn.Name()
|
||||
exe = a.DenoBin
|
||||
cmdArgs = []string{"run", "--no-prompt"}
|
||||
if !trmm.FileExists(a.DenoBin) {
|
||||
a.Logger.Errorln("RunScript(): Executable does not exist. Install deno and try again:", a.DenoBin)
|
||||
err := errors.New("File Not Found: " + a.DenoBin)
|
||||
return "", err.Error(), 85, err
|
||||
}
|
||||
|
||||
// Search the environment variables for DENO_PERMISSIONS and use that to set permissions for the script.
|
||||
// https://docs.deno.com/runtime/manual/basics/permissions#permissions-list
|
||||
// DENO_PERMISSIONS is not an official environment variable.
|
||||
// https://docs.deno.com/runtime/manual/basics/env_variables
|
||||
for i, v := range envVars {
|
||||
if strings.HasPrefix(v, "DENO_PERMISSIONS=") {
|
||||
permissions := strings.Split(v, "=")[1]
|
||||
cmdArgs = append(cmdArgs, strings.Split(permissions, " ")...)
|
||||
// Remove the DENO_PERMISSIONS variable from the environment variables slice.
|
||||
// It's possible more variables may exist with the same prefix.
|
||||
envVars = append(envVars[:i], envVars[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Can't append a variadic slice after a string arg.
|
||||
// https://pkg.go.dev/builtin#append
|
||||
cmdArgs = append(cmdArgs, tmpfn.Name())
|
||||
|
||||
}
|
||||
|
||||
if len(args) > 0 {
|
||||
|
|
@ -846,6 +879,204 @@ func (a *Agent) GetPython(force bool) {
|
|||
}
|
||||
}
|
||||
|
||||
// GetNushell will download nushell from GitHub and install (copy) it to ProgramDir\bin, where ProgramDir is
|
||||
// initialized to C:\Program Files\TacticalAgent
|
||||
func (a *Agent) GetNushell(force bool) {
|
||||
if trmm.FileExists(a.NuBin) {
|
||||
if force {
|
||||
err := os.Remove(a.NuBin)
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetNushell(): Error removing nu binary:", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
programBinDir := path.Join(a.ProgramDir, "bin")
|
||||
if !trmm.FileExists(programBinDir) {
|
||||
err := os.MkdirAll(programBinDir, 0755)
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetNushell(): Error creating Program Files bin folder:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
assetName string
|
||||
url string
|
||||
)
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
switch runtime.GOARCH {
|
||||
case "amd64":
|
||||
// https://github.com/nushell/nushell/releases/download/0.87.0/nu-0.87.0-x86_64-windows-msvc-full.zip
|
||||
assetName = fmt.Sprintf("nu-%s-x86_64-windows-msvc-full.zip", nuVersion)
|
||||
case "arm64":
|
||||
// https://github.com/nushell/nushell/releases/download/0.87.0/nu-0.87.0-aarch64-windows-msvc-full.zip
|
||||
assetName = fmt.Sprintf("nu-%s-aarch64-windows-msvc-full.zip", nuVersion)
|
||||
default:
|
||||
a.Logger.Debugln("GetNushell(): Unsupported architecture and OS:", runtime.GOARCH, runtime.GOOS)
|
||||
return
|
||||
}
|
||||
default:
|
||||
a.Logger.Debugln("GetNushell(): Unsupported OS:", runtime.GOOS)
|
||||
return
|
||||
}
|
||||
url = fmt.Sprintf("https://github.com/nushell/nushell/releases/download/%s/%s", nuVersion, assetName)
|
||||
a.Logger.Debugln("GetNushell(): Nu download url:", url)
|
||||
|
||||
tmpDir, err := os.MkdirTemp("", "trmm")
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetNushell(): Error creating temp directory:", err)
|
||||
return
|
||||
}
|
||||
defer func(path string) {
|
||||
err := os.RemoveAll(path)
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetNushell(): Error removing temp directory:", err)
|
||||
}
|
||||
}(tmpDir)
|
||||
|
||||
tmpAssetName := filepath.Join(tmpDir, assetName)
|
||||
a.Logger.Debugln("GetNushell(): tmpAssetName:", tmpAssetName)
|
||||
|
||||
rClient := resty.New()
|
||||
rClient.SetTimeout(20 * time.Minute)
|
||||
rClient.SetRetryCount(10)
|
||||
rClient.SetRetryWaitTime(1 * time.Minute)
|
||||
rClient.SetRetryMaxWaitTime(15 * time.Minute)
|
||||
if len(a.Proxy) > 0 {
|
||||
rClient.SetProxy(a.Proxy)
|
||||
}
|
||||
|
||||
r, err := rClient.R().SetOutput(tmpAssetName).Get(url)
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetNushell(): Unable to download nu from github.", err)
|
||||
return
|
||||
}
|
||||
if r.IsError() {
|
||||
a.Logger.Errorln("GetNushell(): Unable to download nu from github. Status code", r.StatusCode())
|
||||
return
|
||||
}
|
||||
|
||||
err = Unzip(tmpAssetName, tmpDir)
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetNushell(): Failed to unzip downloaded zip file:", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = copyFile(path.Join(tmpDir, "nu.exe"), a.NuBin)
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetNushell(): Failed to copy nu.exe file to install dir:", err)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// GetDeno will download deno from GitHub and install (copy) it to nixAgentBinDir
|
||||
func (a *Agent) GetDeno(force bool) {
|
||||
if trmm.FileExists(a.DenoBin) {
|
||||
if force {
|
||||
err := os.Remove(a.DenoBin)
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetDeno(): Error removing deno binary:", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
programBinDir := path.Join(a.ProgramDir, "bin")
|
||||
if !trmm.FileExists(programBinDir) {
|
||||
err := os.MkdirAll(programBinDir, 0755)
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetDeno(): Error creating Program Files bin folder:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
assetName string
|
||||
url string
|
||||
)
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
switch runtime.GOARCH {
|
||||
case "amd64":
|
||||
// https://github.com/denoland/deno/releases/download/v1.38.2/deno-x86_64-pc-windows-msvc.zip
|
||||
assetName = fmt.Sprintf("deno-x86_64-pc-windows-msvc.zip")
|
||||
default:
|
||||
a.Logger.Debugln("GetDeno(): Unsupported architecture and OS:", runtime.GOARCH, runtime.GOOS)
|
||||
return
|
||||
}
|
||||
default:
|
||||
a.Logger.Debugln("GetDeno(): Unsupported OS:", runtime.GOOS)
|
||||
return
|
||||
}
|
||||
url = fmt.Sprintf("https://github.com/denoland/deno/releases/download/%s/%s", denoVersion, assetName)
|
||||
a.Logger.Debugln("GetDeno(): Deno download url:", url)
|
||||
|
||||
tmpDir, err := os.MkdirTemp("", "trmm")
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetDeno(): Error creating temp directory:", err)
|
||||
return
|
||||
}
|
||||
defer func(path string) {
|
||||
err := os.RemoveAll(path)
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetDeno(): Error removing temp directory:", err)
|
||||
}
|
||||
}(tmpDir)
|
||||
|
||||
tmpAssetName := filepath.Join(tmpDir, assetName)
|
||||
a.Logger.Debugln("GetDeno(): tmpAssetName:", tmpAssetName)
|
||||
|
||||
if !trmm.FileExists(nixAgentBinDir) {
|
||||
err = os.MkdirAll(nixAgentBinDir, 0755)
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetDeno(): Error creating nixAgentBinDir:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
rClient := resty.New()
|
||||
rClient.SetTimeout(20 * time.Minute)
|
||||
rClient.SetRetryCount(10)
|
||||
rClient.SetRetryWaitTime(1 * time.Minute)
|
||||
rClient.SetRetryMaxWaitTime(15 * time.Minute)
|
||||
if len(a.Proxy) > 0 {
|
||||
rClient.SetProxy(a.Proxy)
|
||||
}
|
||||
|
||||
r, err := rClient.R().SetOutput(tmpAssetName).Get(url)
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetDeno(): Unable to download deno from github.", err)
|
||||
return
|
||||
}
|
||||
if r.IsError() {
|
||||
a.Logger.Errorln("GetDeno(): Unable to download deno from github. Status code", r.StatusCode())
|
||||
return
|
||||
}
|
||||
|
||||
err = Unzip(tmpAssetName, tmpDir)
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetDeno(): Failed to unzip downloaded zip file:", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = copyFile(path.Join(tmpDir, "deno.exe"), a.DenoBin)
|
||||
if err != nil {
|
||||
a.Logger.Errorln("GetDeno(): Failed to copy deno.exe file to install dir:", err)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (a *Agent) RecoverMesh() {
|
||||
a.Logger.Infoln("Attempting mesh recovery")
|
||||
defer CMD("net", []string{"start", a.MeshSVC}, 60, false)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue