Add opt-in NPM and CrowdSec traffic map with selectable agent source
This commit is contained in:
23
agent.go
23
agent.go
@@ -144,9 +144,10 @@ type AgentUpdateManifest struct {
|
||||
}
|
||||
|
||||
type agentReportResponse struct {
|
||||
Accepted bool `json:"accepted"`
|
||||
Update *AgentUpdateManifest `json:"update,omitempty"`
|
||||
Commands []AgentCommand `json:"commands,omitempty"`
|
||||
Accepted bool `json:"accepted"`
|
||||
TrafficEnabled bool `json:"trafficEnabled"`
|
||||
Update *AgentUpdateManifest `json:"update,omitempty"`
|
||||
Commands []AgentCommand `json:"commands,omitempty"`
|
||||
}
|
||||
|
||||
var guestInventory struct {
|
||||
@@ -339,6 +340,15 @@ func runGuestAgent(opts agentOptions) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
var trafficStop func()
|
||||
stopTraffic := func() {
|
||||
if trafficStop != nil {
|
||||
trafficStop()
|
||||
trafficStop = nil
|
||||
}
|
||||
}
|
||||
defer stopTraffic()
|
||||
|
||||
client := &http.Client{Timeout: 12 * time.Second}
|
||||
for {
|
||||
report := collectAgentReport()
|
||||
@@ -356,6 +366,7 @@ func runGuestAgent(opts agentOptions) error {
|
||||
if response.StatusCode == http.StatusUnauthorized && opts.EnrollmentToken != "" {
|
||||
fresh, enrollmentErr := enrollRemote(config, opts.EnrollmentToken)
|
||||
if enrollmentErr == nil {
|
||||
stopTraffic()
|
||||
config = fresh
|
||||
if saveErr := saveAgentConfig(opts.ConfigPath, config); saveErr != nil {
|
||||
return saveErr
|
||||
@@ -372,6 +383,12 @@ func runGuestAgent(opts agentOptions) error {
|
||||
if response.StatusCode == http.StatusOK {
|
||||
var result agentReportResponse
|
||||
if json.Unmarshal(responseData, &result) == nil {
|
||||
if !result.TrafficEnabled {
|
||||
stopTraffic()
|
||||
} else if trafficStop == nil {
|
||||
trafficStop = startAgentTraffic(config, filepath.Dir(opts.ConfigPath))
|
||||
}
|
||||
|
||||
if len(report.ActionResults) > 0 {
|
||||
guestActionResults.Lock()
|
||||
guestActionResults.value = nil
|
||||
|
||||
Reference in New Issue
Block a user