-
Notifications
You must be signed in to change notification settings - Fork 10
/
connect.go
64 lines (53 loc) · 1.45 KB
/
connect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package commands
import (
"fmt"
"strings"
"github.com/AbGuthrie/goquery/v2/config"
"github.com/AbGuthrie/goquery/v2/hosts"
"github.com/AbGuthrie/goquery/v2/models"
"github.com/AbGuthrie/goquery/v2/utils"
prompt "github.com/c-bata/go-prompt"
)
func connect(api models.GoQueryAPI, config *config.Config, cmdline string) error {
args := strings.Split(cmdline, " ") // Separate command and arguments
if len(args) == 1 {
return fmt.Errorf("Host UUID required")
}
uuid := args[1]
host, err := api.CheckHost(uuid)
if err != nil {
return err
}
// All is good, update hosts state
if err := hosts.Register(host); err != nil {
return fmt.Errorf("Error connecting to host: %s", err)
}
fmt.Printf("Verified Host(%s) Exists.\n", uuid)
results, err := utils.ScheduleQueryAndWait(
api,
host.UUID,
"select name from osquery_registry where registry = 'table' and active = 1",
)
if err != nil {
return err
}
tables := make([]string, 0)
for _, row := range results {
// Probably unneeded guard against bad osquery/api data
if table, ok := row["name"]; ok {
tables = append(tables, table)
}
}
hosts.SetHostTables(host.UUID, tables)
return nil
}
func connectHelp() string {
return "Connect to a host with UUID"
}
func connectSuggest(cmdline string) []prompt.Suggest {
prompts := []prompt.Suggest{}
for _, host := range hosts.GetCurrentHosts() {
prompts = append(prompts, prompt.Suggest{host.UUID, host.ComputerName})
}
return prompts
}