-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir.go
151 lines (131 loc) · 3.09 KB
/
ir.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
)
var IRMutex sync.Mutex
func incidentResponse() {
go func() {
serverName, err := os.Hostname()
if err != nil {
h.Error("error getting hostname: %v", err)
}
path, err := getMyPath()
if err != nil {
h.Error("error getting path: %v", err)
}
for {
IRMutex.Lock()
cnf := getConfig()
actions, err := getCommands(
AGENTMANAGERPROTO+
"://"+
cnf.Server+
":"+
strconv.Itoa(AGENTMANAGERPORT)+
GETCOMMANDSENDPOINT+
fmt.Sprintf("?agentName=%s", serverName),
cnf.AgentID,
cnf.AgentKey,
cnf.SkipCertValidation,
)
var commands []struct {
ID int64 `json:"id"`
Command string `json:"command"`
}
if err == nil {
json.Unmarshal(actions, &commands)
} else {
h.Error("Error getting commands: %v", err)
}
for _, c := range commands {
cmd := strings.Split(c.Command, " ")
var response string
h.Debug("Executing command: %v", cmd)
if len(cmd) > 1 {
response, _ = execute(cmd[0], path, cmd[1:]...)
} else {
response, _ = execute(cmd[0], path)
}
err := commandResponse(
AGENTMANAGERPROTO+
"://"+
cnf.Server+
":"+
strconv.Itoa(AGENTMANAGERPORT)+
COMMANDSRESPONSEENDPOINT+
fmt.Sprintf("?agentName=%s", serverName),
cnf.AgentID,
cnf.AgentKey,
c.ID,
response,
cnf.SkipCertValidation,
)
if err != nil {
h.Error("Error sending command response: %v", err)
}
}
IRMutex.Unlock()
time.Sleep(1 * time.Second)
}
}()
}
func getCommands(endPoint, agentId, key string, insecure bool) ([]byte, error) {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: insecure}
req, err := http.NewRequest("GET", endPoint, nil)
if err != nil {
return nil, err
}
req.Header.Add("Agent-Id", agentId)
req.Header.Add("Agent-Key", key)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("%s", string(body[:]))
}
return body, nil
}
func commandResponse(endPoint, agentId, key string, id int64, response string, insecure bool) error {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: insecure}
result, err := json.Marshal(jobResult{JobId: id, Result: response})
if err != nil {
return err
}
payload := strings.NewReader(string(result))
h.Debug("Command execution result: %s", string(result))
req, err := http.NewRequest("POST", endPoint, payload)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Agent-Id", agentId)
req.Header.Add("Agent-Key", key)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode != 200 {
return fmt.Errorf("%s", string(body[:]))
}
return nil
}