Minor cleanup and and got the client sending agents again.

This commit is contained in:
James Wells 2022-07-14 13:27:46 -07:00
parent 42623f63c6
commit 134f33cd6a
Signed by: jwells
GPG key ID: 73196D10B8E65666
7 changed files with 50 additions and 64 deletions

View file

@ -2,6 +2,7 @@ package client
import (
sha "crypto/sha256"
hex "encoding/hex"
fmt "fmt"
ioutil "io/ioutil"
log "log"
@ -40,9 +41,10 @@ func Main(wg *sync.WaitGroup, config gstructs.GagentConfig) {
log.Printf("[DEBUG] Agent file contents: \n----- -----\n%s\n----- -----\n", agent.ScriptCode)
}
agent.Client = config.UUID
agent.Shasum = fmt.Sprintf("%s", sha.Sum256(agent.ScriptCode))
tmpsum := sha.Sum256([]byte(agent.ScriptCode))
agent.Shasum = fmt.Sprintf("%v", hex.EncodeToString(tmpsum[:]))
log.Printf("[INFO] SHA256 of Agent file: %s", agent.Shasum)
agent.Status = "loaded"
agent.Status = 1
agent.Hints = getTagsFromHints(agent)
agent.Answer = nil

View file

@ -28,7 +28,8 @@ func NewConsulClient(addr string) (Client, error) {
if err != nil {
return nil, err
}
return &client{consul: c}, nil
return &client{c}, nil
//return &client{consul: c}, nil
}
// Register a service with consul local agent
@ -51,10 +52,10 @@ func (c *client) Service(service, tag string) ([]*consul.ServiceEntry, *consul.Q
passingOnly := true
addrs, meta, err := c.consul.Health().Service(service, tag, passingOnly, nil)
if len(addrs) == 0 && err == nil {
return nil, fmt.Errorf("service ( %s ) was not found", service)
return nil, nil, fmt.Errorf("service ( %s ) was not found", service)
}
if err != nil {
return nil, err
return nil, nil, err
}
return addrs, meta, nil
}

View file

@ -24,16 +24,14 @@ type GagentConfig struct {
*/
type ClientDetails struct {
/*
* Client name for display purposes in logs and
* diagnostics.
* Client name for display purposes in logs and diagnostics.
*/
ClientName string `hcl:",label"`
/*
* UUID String for the client node. This is used by
* the router to determine which MQ client to send
* the agent's results to. This attempts to keep the
* clients unique globally.
* UUID String for the client node. This is used by the router to
* determine which MQ client to send the agent's results to. This
* attempts to keep the clients unique globally.
*/
ClientID string `hcl:"clientid,optional"`
}
@ -43,52 +41,44 @@ type ClientDetails struct {
*/
type RouterDetails struct {
/*
* Router name for display purposes in logs and
* diagnostics
* Router name for display purposes in logs and diagnostics.
*/
RouterName string `hcl:",label"`
/*
* UUID String for the router node. This is used by
* the clients, routers, and workers to determine
* which MQ router to send the agent's requests to.
* This attempts to keep the routers unique globally.
* UUID String for the router node. This is used by the clients,
* routers, and workers to determine which MQ router to send the
* agent's requests to. This attempts to keep the routers unique
* globally.
*/
RouterID string `hcl:"routerid,attr"`
/*
* This is the IP address or hostname the router
* will listen on. The router will start up a 0MQ
* service that clients and workers will connect to.
* This is the IP address or hostname the router will listen on. The
* router will start up a 0MQ service that clients and workers will
* connect to.
*/
RouterAddr string `hcl:"address,attr"`
/*
* This is the is the port that the router listens
* on for clients. If not defined, it will default
* to 35571.
* G'Agent client will use this port to communicate with the routers.
*/
ClientPort int64 `hcl:"clientport,optional"`
/*
* This is the is the port that the router listens
* on for routers. If not defined, it will default
* to 35570.
* G'Agent router will use this port to communicate with other routers.
*/
RouterPort int64 `hcl:"routerport,optional"`
/*
* This is the is the port that the router listens
* on for clients. If not defined, it will default
* to 35572.
* G'Agent worker will use this port to communicate with the routers.
*/
WorkerPort int64 `hcl:"workerport,optional"`
/*
* These tags will be passed to the router upon
* connection. The router will then use these
* tags to help determine which worker / client
* to send the client's requests and results to.
* These tags will be passed to the router upon connection. The router
* will then use these tags to help determine which worker / client to
* send the client's requests and results to.
*/
RouterTags []string `hcl:"tags,optional"`
}
@ -98,33 +88,30 @@ type RouterDetails struct {
*/
type WorkerDetails struct {
/*
* Router name for display purposes in logs and
* diagnostics
* Router name for display purposes in logs and diagnostics.
*/
WorkerName string `hcl:",label"`
/*
* UUID String for the worker node. This is used
* by the router to determine which MQ client to
* send agents to. This attempts to keep the
* workers unique globally.
* UUID String for the worker node. This is used by the router to
* determine which MQ client to send agents to. This attempts to keep
* the workers unique globally.
*/
WorkerID string `hcl:"workerid,attr"`
/*
* These tags will be passed to the router upon
* connection. The router will then use these
* tags to help determine which worker / client
* to send the agent and it's results to.
* These tags will be passed to the router upon connection. The router
* will then use these tags to help determine which worker / client to
* send the agent and it's results to.
*/
WorkerTags []string `hcl:"tags,optional"`
}
type AgentDetails struct {
Client string `hcl:"client"`
Status int64 `hcl:"status"`
Shasum string `hcl:"shasum"`
Status string `hcl:"status"`
ScriptCode []byte
Hints []string
ScriptCode []byte
Answer []byte
}

View file

@ -57,7 +57,9 @@ func getAgent(wg *sync.WaitGroup, uuid string, connectString string) {
subscriber, _ := zmq.NewSocket(zmq.REP)
defer subscriber.Close()
_ = subscriber.Connect(connectString)
// _ = subscriber.Connect(connectString)
foo := subscriber.Connect(connectString)
log.Printf("[DEBUG] Connected to %s\n", foo)
msg, err := subscriber.Recv(0)
if err != nil {