forked from ysrc/yulong-hids-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
285 lines (265 loc) · 6.95 KB
/
agent.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package client
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"runtime"
"strings"
"sync"
"time"
"yulong-hids/agent/collect"
"yulong-hids/agent/common"
"yulong-hids/agent/monitor"
"github.com/smallnest/rpcx/client"
"github.com/smallnest/rpcx/share"
)
var err error
type dataInfo struct {
IP string // 客户端的IP地址
Type string // 传输的数据类型
System string // 操作系统
Data []map[string]string // 数据内容
}
// Agent agent客户端结构
type Agent struct {
ServerNetLoc string // 服务端地址 IP:PORT
Client client.XClient // RPC 客户端
ServerList []string // 存活服务端集群列表
PutData dataInfo // 要传输的数据
Reply int // RPC Server 响应结果
Mutex *sync.Mutex // 安全操作锁
IsDebug bool // 是否开启debug模式,debug模式打印传输内容和报错信息
ctx context.Context
}
var httpClient = &http.Client{
Timeout: time.Second * 10,
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
}
func (a *Agent) init() {
a.ServerList, err = a.getServerList()
if err != nil {
a.log("GetServerList error:", err)
panic(1)
}
a.ctx = context.WithValue(context.Background(), share.ReqMetaDataKey, make(map[string]string))
a.log("Available server node:", a.ServerList)
if len(a.ServerList) == 0 {
time.Sleep(time.Second * 30)
a.log("No server node available")
panic(1)
}
a.newClient()
if common.LocalIP == "" {
a.log("Can not get local address")
panic(1)
}
a.Mutex = new(sync.Mutex)
err := a.Client.Call(a.ctx, "GetInfo", &common.ServerInfo, &common.Config)
if err != nil {
a.log("RPC Client Call Error:", err.Error())
panic(1)
}
a.log("Common Client Config:", common.Config)
}
// Run 启动agent
func (a *Agent) Run() {
// agent 初始化
// 请求Web API,获取Server地址,初始化RPC客户端,获取客户端IP等
a.init()
// 每隔一段时间更新初始化配置
a.configRefresh()
// 开启各个监控流程 文件监控,网络监控,进程监控
a.monitor()
// 每隔一段时间获取系统信息
// 监听端口,服务信息,用户信息,开机启动项,计划任务,登录信息,进程列表等
a.getInfo()
}
func (a *Agent) newClient() {
var servers []*client.KVPair
for _, server := range a.ServerList {
common.ServerIPList = append(common.ServerIPList, strings.Split(server, ":")[0])
s := client.KVPair{Key: server}
servers = append(servers, &s)
if common.LocalIP == "" {
a.setLocalIP(server)
common.ServerInfo = collect.GetComInfo()
a.log("Host Information:", common.ServerInfo)
}
}
conf := &tls.Config{
InsecureSkipVerify: true,
}
option := client.DefaultOption
option.TLSConfig = conf
serverd := client.NewMultipleServersDiscovery(servers)
a.Client = client.NewXClient("Watcher", FAILMODE, client.RandomSelect, serverd, option)
a.Client.Auth(AUTH_TOKEN)
}
func (a Agent) getServerList() ([]string, error) {
var serlist []string
var url string
if TESTMODE {
url = "http://" + a.ServerNetLoc + SERVER_API
} else {
url = "https://" + a.ServerNetLoc + SERVER_API
}
a.log("Web API:", url)
request, _ := http.NewRequest("GET", url, nil)
request.Close = true
resp, err := httpClient.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(result), &serlist)
if err != nil {
return nil, err
}
return serlist, nil
}
func (a Agent) setLocalIP(ip string) {
conn, err := net.Dial("tcp", ip)
if err != nil {
a.log("Net.Dial:", ip)
a.log("Error:", err)
panic(1)
}
defer conn.Close()
common.LocalIP = strings.Split(conn.LocalAddr().String(), ":")[0]
}
func (a *Agent) configRefresh() {
ticker := time.NewTicker(time.Second * time.Duration(CONFIGR_REF_INTERVAL))
go func() {
for _ = range ticker.C {
ch := make(chan bool)
go func() {
err = a.Client.Call(a.ctx, "GetInfo", &common.ServerInfo, &common.Config)
if err != nil {
a.log("RPC Client Call:", err.Error())
return
}
ch <- true
}()
// Server集群列表获取
select {
case <-ch:
serverList, err := a.getServerList()
if err != nil {
a.log("RPC Client Call:", err.Error())
break
}
if len(serverList) == 0 {
a.log("No server node available")
break
}
if len(serverList) == len(a.ServerList) {
for i, server := range serverList {
// TODO 可能会产生问题
if server != a.ServerList[i] {
a.ServerList = serverList
// 防止正在传输重置client导致数据丢失
a.Mutex.Lock()
a.Client.Close()
a.newClient()
a.Mutex.Unlock()
break
}
}
} else {
a.log("Server nodes from old to new:", a.ServerList, "->", serverList)
a.ServerList = serverList
a.Mutex.Lock()
a.Client.Close()
a.newClient()
a.Mutex.Unlock()
}
case <-time.NewTicker(time.Second * 3).C:
break
}
}
}()
}
func (a *Agent) monitor() {
resultChan := make(chan map[string]string, 16)
go monitor.StartNetSniff(resultChan)
go monitor.StartProcessMonitor(resultChan)
go monitor.StartFileMonitor(resultChan)
go func(result chan map[string]string) {
var resultdata []map[string]string
var data map[string]string
for {
data = <-result
data["time"] = fmt.Sprintf("%d", time.Now().Unix())
a.log("Monitor data: ", data)
source := data["source"]
delete(data, "source")
a.Mutex.Lock()
a.PutData = dataInfo{common.LocalIP, source, runtime.GOOS, append(resultdata, data)}
a.put()
a.Mutex.Unlock()
}
}(resultChan)
}
func (a *Agent) getInfo() {
historyCache := make(map[string][]map[string]string)
for {
if len(common.Config.MonitorPath) == 0 {
time.Sleep(time.Second)
a.log("Failed to get the configuration information")
continue
}
allData := collect.GetAllInfo()
for k, v := range allData {
if len(v) == 0 || a.mapComparison(v, historyCache[k]) {
a.log("GetInfo Data:", k, "No change")
continue
} else {
a.Mutex.Lock()
a.PutData = dataInfo{common.LocalIP, k, runtime.GOOS, v}
a.put()
a.Mutex.Unlock()
if k != "service" {
a.log("Data details:", k, a.PutData)
}
historyCache[k] = v
}
}
if common.Config.Cycle == 0 {
common.Config.Cycle = 1
}
time.Sleep(time.Second * time.Duration(common.Config.Cycle) * 60)
}
}
func (a Agent) put() {
_, err := a.Client.Go(a.ctx, "PutInfo", &a.PutData, &a.Reply, nil)
if err != nil {
a.log("PutInfo error:", err.Error())
}
}
func (a Agent) mapComparison(new []map[string]string, old []map[string]string) bool {
if len(new) == len(old) {
for i, v := range new {
for k, value := range v {
if value != old[i][k] {
return false
}
}
}
return true
}
return false
}
func (a Agent) log(info ...interface{}) {
if a.IsDebug {
log.Println(info...)
}
}