mirror of
https://github.com/dragonheim/gagent.git
synced 2025-06-27 17:28:15 -07:00
Converted TCL list style and added hint parser.
This commit is contained in:
parent
de41740bac
commit
abba1972d1
6 changed files with 100 additions and 63 deletions
|
@ -6,6 +6,8 @@ import (
|
|||
ioutil "io/ioutil"
|
||||
log "log"
|
||||
os "os"
|
||||
regexp "regexp"
|
||||
strings "strings"
|
||||
sync "sync"
|
||||
time "time"
|
||||
|
||||
|
@ -32,12 +34,14 @@ func Main(wg *sync.WaitGroup, config gstructs.GagentConfig) {
|
|||
agent.ScriptCode, err = ioutil.ReadFile(config.File)
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] No such file or directory: %s", config.File)
|
||||
os.Exit(6)
|
||||
os.Exit(4)
|
||||
}
|
||||
agent.Shasum = fmt.Sprintf("%x", sha.Sum256(agent.ScriptCode))
|
||||
agent.Status = "loaded"
|
||||
log.Printf("[DEBUG] SHA256 of Agent file: %s", agent.Shasum)
|
||||
log.Printf("[INFO] SHA256 of Agent file: %s", agent.Shasum)
|
||||
log.Printf("[DEBUG] Agent file contents: \n%s\n", agent.ScriptCode)
|
||||
}
|
||||
agent.Hints = getTagsFromHints(agent)
|
||||
|
||||
for key := range config.Routers {
|
||||
/*
|
||||
|
@ -51,12 +55,27 @@ func Main(wg *sync.WaitGroup, config gstructs.GagentConfig) {
|
|||
|
||||
wg.Add(1)
|
||||
go sendAgent(wg, config.UUID, connectString, agent.ScriptCode)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse Agent file for GHINT data to populate the G'Agent hints
|
||||
*/
|
||||
func getTagsFromHints(agent gstructs.AgentDetails) []string {
|
||||
var tags []string
|
||||
re := regexp.MustCompile(`\s*set\s+GHINT\s*\[\s*split\s*"(?P<Hints>.+)"\s*\,\s*\]`)
|
||||
res := re.FindStringSubmatch(string(agent.ScriptCode))
|
||||
if len(res) < 1 {
|
||||
log.Printf("[ERROR] Agent is missing GHINT tags")
|
||||
os.Exit(4)
|
||||
}
|
||||
tags = strings.Split(res[1], ",")
|
||||
log.Printf("[DEBUG] G'Agent hints: %v\n", tags)
|
||||
|
||||
return tags
|
||||
}
|
||||
|
||||
func sendAgent(wg *sync.WaitGroup, uuid string, connectString string, agent []byte) {
|
||||
log.Printf("[DEBUG] Attempting to connect to %s\n", connectString)
|
||||
defer wg.Done()
|
||||
|
||||
var mu sync.Mutex
|
||||
|
@ -66,10 +85,23 @@ func sendAgent(wg *sync.WaitGroup, uuid string, connectString string, agent []by
|
|||
defer sock.Close()
|
||||
|
||||
sock.SetIdentity(uuid)
|
||||
sock.Connect(connectString)
|
||||
|
||||
log.Printf("[DEBUG] Attempting to connect to %s\n", connectString)
|
||||
err := sock.Connect(connectString)
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] Failed to connect to %s\n", connectString)
|
||||
os.Exit(10)
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Start sending agent...\n")
|
||||
sock.SendMessage(agent)
|
||||
log.Printf("[DEBUG] End sending agent...\n")
|
||||
status, err := sock.SendMessage(agent)
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] Failed to send agent to router\n")
|
||||
// os.Exit(11)
|
||||
return
|
||||
}
|
||||
log.Printf("[DEBUG] Agent send status: %d\n", status)
|
||||
mu.Unlock()
|
||||
time.Sleep(10 * time.Second)
|
||||
|
||||
}
|
||||
|
|
|
@ -2,19 +2,20 @@ package gstructs
|
|||
|
||||
// GagentConfig is the primary construct used by all modes
|
||||
type GagentConfig struct {
|
||||
Name string `hcl:"name,optional"`
|
||||
Mode string `hcl:"mode,attr"`
|
||||
UUID string `hcl:"uuid,optional"`
|
||||
ListenAddr string `hcl:"listenaddr,optional"`
|
||||
ClientPort int64 `hcl:"clientport,optional"`
|
||||
RouterPort int64 `hcl:"routerport,optional"`
|
||||
WorkerPort int64 `hcl:"workerport,optional"`
|
||||
Clients []*ClientDetails `hcl:"client,block"`
|
||||
Routers []*RouterDetails `hcl:"router,block"`
|
||||
Workers []*WorkerDetails `hcl:"worker,block"`
|
||||
Version string
|
||||
File string
|
||||
CMode bool
|
||||
Name string `hcl:"name,optional"`
|
||||
Mode string `hcl:"mode,attr"`
|
||||
UUID string `hcl:"uuid,optional"`
|
||||
MonitorPort int `hcl:"monitorport,optional"`
|
||||
ListenAddr string `hcl:"listenaddr,optional"`
|
||||
ClientPort int64 `hcl:"clientport,optional"`
|
||||
RouterPort int64 `hcl:"routerport,optional"`
|
||||
WorkerPort int64 `hcl:"workerport,optional"`
|
||||
Clients []*ClientDetails `hcl:"client,block"`
|
||||
Routers []*RouterDetails `hcl:"router,block"`
|
||||
Workers []*WorkerDetails `hcl:"worker,block"`
|
||||
Version string
|
||||
File string
|
||||
CMode bool
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -127,5 +128,5 @@ type AgentDetails struct {
|
|||
Shasum string `hcl:"shasum"`
|
||||
Status string `hcl:"status"`
|
||||
ScriptCode []byte
|
||||
Hints []*string
|
||||
Hints []string
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
)
|
||||
|
||||
func Main(wg *sync.WaitGroup, config gs.GagentConfig) {
|
||||
log.Printf("[INFO] setup mode\n")
|
||||
log.Printf("[INFO] Starting setup\n")
|
||||
defer wg.Done()
|
||||
|
||||
f := hclwrite.NewEmptyFile()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue