mao-service-discovery 1.6.0-commit-2023-07-24-04-37-15.0 → 1.6.0-commit-2023-07-24-06-22-14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MaoServerDiscovery +0 -0
- package/package.json +1 -1
- package/.github/dependabot.yml +0 -11
- package/.github/workflows/codeql-analysis.yml +0 -75
- package/.github/workflows/docker-image.yml +0 -24
- package/.github/workflows/docker-publish.yml +0 -94
- package/.github/workflows/go.yml +0 -29
- package/.github/workflows/go_static.yml +0 -29
- package/.github/workflows/npm-publish-release.yml +0 -36
- package/.github/workflows/npm-publish.yml +0 -32
- package/Dockerfile +0 -19
- package/LICENSE +0 -201
- package/MODULES.md +0 -39
- package/README.md +0 -54
- package/bak/index.html +0 -42
- package/bak/receiveProcessIcmpLoopV4V6.bak +0 -73
- package/bak/test/test-icmp-ka-module.go +0 -297
- package/bak/test/test-tech.go +0 -110
- package/bak/using_etcd/main.go +0 -165
- package/cmd/api/aux-data-module.go +0 -14
- package/cmd/api/config-module.go +0 -11
- package/cmd/api/email-module.go +0 -14
- package/cmd/api/gateway-module.go +0 -23
- package/cmd/api/grpc-ka-module.go +0 -29
- package/cmd/api/icmp-ka-module.go +0 -26
- package/cmd/api/restful-server-module.go +0 -13
- package/cmd/api/topo-module.go +0 -31
- package/cmd/api/wechat-module.go +0 -18
- package/cmd/general_client.go +0 -366
- package/cmd/lib/AuxDataProcessor/aux-data-processor-module.go +0 -64
- package/cmd/lib/AuxDataProcessor/env-temp-processor.go +0 -63
- package/cmd/lib/AuxDataProcessor/env-temp-processor_test.go +0 -30
- package/cmd/lib/Config/config-yaml-module.go +0 -303
- package/cmd/lib/Config/config-yaml-module_test.go +0 -308
- package/cmd/lib/Email/login-auth.go +0 -35
- package/cmd/lib/Email/login-auth_test.go +0 -13
- package/cmd/lib/Email/smtp-email-module.go +0 -229
- package/cmd/lib/Email/smtp-email-module_test.go +0 -12
- package/cmd/lib/GrpcKa/grpc-detect-module.go +0 -269
- package/cmd/lib/GrpcKa/grpc-detect-module_test.go +0 -26
- package/cmd/lib/IcmpKa/icmp-detect-module.go +0 -545
- package/cmd/lib/IcmpKa/icmp-detect-module_test.go +0 -23
- package/cmd/lib/InfluxDB/influxdb-util.go +0 -72
- package/cmd/lib/MaoCommon/gps.go +0 -15
- package/cmd/lib/MaoCommon/service-registry-util.go +0 -59
- package/cmd/lib/MaoCommon/service-registry.go +0 -14
- package/cmd/lib/MaoCommon/service-registry_test.go +0 -30
- package/cmd/lib/MaoEnhancedGolang/smtp.go +0 -438
- package/cmd/lib/Restful/restful-server-impl.go +0 -87
- package/cmd/lib/Soap/soap-tplink-util.go +0 -186
- package/cmd/lib/Soap/soap-tplink-util_test.go +0 -45
- package/cmd/lib/Soap/tplink-gateway-module.go +0 -200
- package/cmd/server.go +0 -327
- package/go.mod +0 -38
- package/go.sum +0 -222
- package/grpc.maojianwei.com/server/discovery/api/generate_api_from_protobuf.sh +0 -1
- package/grpc.maojianwei.com/server/discovery/api/mao-server-discovery.pb.go +0 -403
- package/grpc.maojianwei.com/server/discovery/api/mao-server-discovery.proto +0 -35
- package/grpc.maojianwei.com/server/discovery/api/mao-server-discovery_grpc.pb.go +0 -207
- package/incubator/OnosTopoShow/onos-topo-module.go +0 -347
- package/incubator/Wechat/wechat-message-module.go +0 -267
- package/incubator/Wechat/wechat-message-module_test.go +0 -108
- package/main.go +0 -383
- package/notebook/notebook.txt +0 -8
- package/resource/index-email.html +0 -67
- package/resource/index-icmp.html +0 -41
- package/resource/index-onos.html +0 -38
- package/resource/index-server.html +0 -57
- package/resource/index-wechat.html +0 -64
- package/resource/jquery-3.6.0.min.js +0 -2
- package/screenshot/2-cli-output.png +0 -0
- package/screenshot/2-cli-parameters.png +0 -0
- package/screenshot/2-json-format.png +0 -0
- package/screenshot/2-readable-format.png +0 -0
- package/screenshot/client-server-mode-1.png +0 -0
- package/screenshot/client-server-mode-2.png +0 -0
- package/screenshot/client-server-mode-production.png +0 -0
- package/screenshot/show_using_etcd.png +0 -0
- package/statically_linked_compilation.sh +0 -2
- package/util/mao_log.go +0 -42
- package/util/mao_log_test.go +0 -73
- package/util/mao_util.go +0 -53
|
@@ -1,545 +0,0 @@
|
|
|
1
|
-
package IcmpKa
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
MaoApi "MaoServerDiscovery/cmd/api"
|
|
5
|
-
"MaoServerDiscovery/cmd/lib/Config"
|
|
6
|
-
"MaoServerDiscovery/cmd/lib/MaoCommon"
|
|
7
|
-
"MaoServerDiscovery/util"
|
|
8
|
-
"fmt"
|
|
9
|
-
"github.com/gin-gonic/gin"
|
|
10
|
-
"golang.org/x/net/icmp"
|
|
11
|
-
"golang.org/x/net/ipv4"
|
|
12
|
-
"golang.org/x/net/ipv6"
|
|
13
|
-
"net"
|
|
14
|
-
"sort"
|
|
15
|
-
"strings"
|
|
16
|
-
"sync"
|
|
17
|
-
"time"
|
|
18
|
-
)
|
|
19
|
-
|
|
20
|
-
var (
|
|
21
|
-
//addServiceChan *chan string
|
|
22
|
-
//delServiceChan *chan string
|
|
23
|
-
//serviceMirror []*MaoIcmpService
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
const (
|
|
27
|
-
MODULE_NAME = "ICMP-Detect-module"
|
|
28
|
-
|
|
29
|
-
URL_CONFIG_HOMEPAGE = "/configIcmp"
|
|
30
|
-
URL_CONFIG_ADD_SERVICE_IP = "/addServiceIp"
|
|
31
|
-
URL_CONFIG_DEL_SERVICE_IP = "/delServiceIp"
|
|
32
|
-
URL_CONFIG_SHOW_SERVICE_IP = "/showServiceIP"
|
|
33
|
-
|
|
34
|
-
PROTO_ICMP = 1
|
|
35
|
-
PROTO_ICMP_V6 = 58
|
|
36
|
-
|
|
37
|
-
ICMP_DETECT_ID = 0x1994
|
|
38
|
-
ICMP_V6_DETECT_ID = 0x1996
|
|
39
|
-
|
|
40
|
-
SERVICE_LIST_CONFIG_PATH = "/icmp-ka/services"
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
type IcmpDetectModule struct {
|
|
44
|
-
connV4 *icmp.PacketConn
|
|
45
|
-
connV6 *icmp.PacketConn
|
|
46
|
-
serviceStore sync.Map // address_string -> Service object
|
|
47
|
-
|
|
48
|
-
AddChan chan string // need to be initiated when constructing
|
|
49
|
-
DelChan chan string // need to be initiated when constructing
|
|
50
|
-
|
|
51
|
-
// TODO - MAKE IT CONFIGURABLE
|
|
52
|
-
// configurable parameter
|
|
53
|
-
sendInterval uint32 // milliseconds
|
|
54
|
-
checkInterval uint32 // milliseconds
|
|
55
|
-
leaveTimeout uint32 // milliseconds
|
|
56
|
-
refreshShowingInterval uint32 //
|
|
57
|
-
|
|
58
|
-
// TODO - MAKE IT CONFIGURABLE
|
|
59
|
-
// tunable configurable parameter
|
|
60
|
-
receiveFreezePeriod uint32 // milliseconds - mitigate attack with malformed packets.
|
|
61
|
-
|
|
62
|
-
// only for web showing, i.e. external get operation
|
|
63
|
-
serviceMirror []*MaoApi.MaoIcmpService
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
func (m *IcmpDetectModule) sendIcmpLoop() {
|
|
67
|
-
round := 1
|
|
68
|
-
for {
|
|
69
|
-
util.MaoLogM(util.DEBUG, MODULE_NAME, "Detect Round %d", round)
|
|
70
|
-
m.serviceStore.Range(func(_, value interface{}) bool {
|
|
71
|
-
service := value.(*MaoApi.MaoIcmpService)
|
|
72
|
-
|
|
73
|
-
addr, err := net.ResolveIPAddr("ip", service.Address)
|
|
74
|
-
if err != nil {
|
|
75
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to ResolveIPAddr v4v6Addr: %s", err.Error())
|
|
76
|
-
return true // for continuous iteration
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
var msgType icmp.Type
|
|
80
|
-
var echoId int
|
|
81
|
-
var conn *icmp.PacketConn
|
|
82
|
-
if util.JudgeIPv6Addr(addr) {
|
|
83
|
-
msgType = ipv6.ICMPTypeEchoRequest
|
|
84
|
-
echoId = ICMP_V6_DETECT_ID
|
|
85
|
-
conn = m.connV6
|
|
86
|
-
} else {
|
|
87
|
-
msgType = ipv4.ICMPTypeEcho
|
|
88
|
-
echoId = ICMP_DETECT_ID
|
|
89
|
-
conn = m.connV4
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
// To build and send ICMP Request.
|
|
94
|
-
|
|
95
|
-
service.DetectCount++
|
|
96
|
-
icmpPayloadData := []byte(time.Now().String())
|
|
97
|
-
echoMsg := icmp.Echo{
|
|
98
|
-
ID: echoId,
|
|
99
|
-
Seq: int(service.DetectCount),
|
|
100
|
-
Data: icmpPayloadData,
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
icmpMsg := icmp.Message{
|
|
104
|
-
Type: msgType,
|
|
105
|
-
Code: 0,
|
|
106
|
-
//Checksum: 0,
|
|
107
|
-
Body: &echoMsg,
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// do le->be in the Marshal
|
|
111
|
-
icmpMsgByte, err := icmpMsg.Marshal(nil)
|
|
112
|
-
if err != nil {
|
|
113
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to marshal icmpMsg: %s", err.Error())
|
|
114
|
-
return true
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
service.RttOutboundTimestamp = time.Now()
|
|
118
|
-
_, err = conn.WriteTo(icmpMsgByte, addr)
|
|
119
|
-
if err != nil {
|
|
120
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to WriteTo connV6: %s", err.Error())
|
|
121
|
-
return true
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return true
|
|
125
|
-
})
|
|
126
|
-
time.Sleep(time.Duration(m.sendInterval) * time.Millisecond)
|
|
127
|
-
round++
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* For IPv6: PROTO_ICMP, m.connV4
|
|
133
|
-
* For IPv4: PROTO_ICMP_V6, m.connV6
|
|
134
|
-
*/
|
|
135
|
-
func (m *IcmpDetectModule) receiveProcessIcmpLoop(protoNum int, conn *icmp.PacketConn) {
|
|
136
|
-
recvBuf := make([]byte, 2000)
|
|
137
|
-
for {
|
|
138
|
-
count, addr, err := conn.ReadFrom(recvBuf)
|
|
139
|
-
lastseen := time.Now()
|
|
140
|
-
if err != nil {
|
|
141
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to recv ICMP, freeze %d ms, %s", m.receiveFreezePeriod, err.Error())
|
|
142
|
-
time.Sleep(time.Duration(m.receiveFreezePeriod) * time.Millisecond)
|
|
143
|
-
continue
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
msg, err := icmp.ParseMessage(protoNum, recvBuf)
|
|
147
|
-
if err != nil {
|
|
148
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to parse ICMP, freeze %d ms, %s", m.receiveFreezePeriod, err.Error())
|
|
149
|
-
time.Sleep(time.Duration(m.receiveFreezePeriod) * time.Millisecond)
|
|
150
|
-
continue
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
icmpEcho, ok := msg.Body.(*icmp.Echo)
|
|
154
|
-
if !ok {
|
|
155
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to convert *icmp.Echo, freeze %d ms", m.receiveFreezePeriod)
|
|
156
|
-
time.Sleep(time.Duration(m.receiveFreezePeriod) * time.Millisecond)
|
|
157
|
-
continue
|
|
158
|
-
}
|
|
159
|
-
util.MaoLogM(util.DEBUG, MODULE_NAME, "%v, %v = %v, %v, %v, %v, %v, %v", count, addr, msg.Type, msg.Code, msg.Checksum, icmpEcho.ID, icmpEcho.Seq, icmpEcho.Data)
|
|
160
|
-
|
|
161
|
-
var addrStr string
|
|
162
|
-
if protoNum == PROTO_ICMP_V6 {
|
|
163
|
-
addrStr = strings.Split(addr.String(), "%")[0] // for ipv6 link-local address, it is suffixed by % and interface name.
|
|
164
|
-
} else {
|
|
165
|
-
addrStr = addr.String()
|
|
166
|
-
}
|
|
167
|
-
value, ok := m.serviceStore.Load(addrStr)
|
|
168
|
-
if ok && value != nil {
|
|
169
|
-
service := value.(*MaoApi.MaoIcmpService)
|
|
170
|
-
service.LastSeen = lastseen
|
|
171
|
-
service.RttDuration = service.LastSeen.Sub(service.RttOutboundTimestamp)
|
|
172
|
-
service.ReportCount++
|
|
173
|
-
|
|
174
|
-
if !service.Alive {
|
|
175
|
-
service.Alive = true
|
|
176
|
-
|
|
177
|
-
emailModule := MaoCommon.ServiceRegistryGetEmailModule()
|
|
178
|
-
if emailModule == nil {
|
|
179
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to get EmailModule, can't send UP notification")
|
|
180
|
-
} else {
|
|
181
|
-
emailModule.SendEmail(&MaoApi.EmailMessage{
|
|
182
|
-
Subject: "ICMP UP notification",
|
|
183
|
-
Content: fmt.Sprintf("Service: %s\r\nUP Time: %s\r\nDetail: %v\r\n",
|
|
184
|
-
service.Address, time.Now().String(), service),
|
|
185
|
-
})
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
// TEMP: test wechat module
|
|
190
|
-
//wechatModule := MaoCommon.ServiceRegistryGetWechatModule()
|
|
191
|
-
//if wechatModule == nil {
|
|
192
|
-
// util.MaoLogM(util.WARN, MODULE_NAME, "Fail to get WechatModule, can't send UP notification")
|
|
193
|
-
//} else {
|
|
194
|
-
// wechatModule.SendWechatMessage(&MaoApi.WechatMessage{
|
|
195
|
-
// Title: "ICMP UP notification",
|
|
196
|
-
// ContentHttp: fmt.Sprintf("Service: %s\r\nUP Time: %s\r\nDetail: %v\r\n",
|
|
197
|
-
// service.Address, time.Now().String(), service),
|
|
198
|
-
// Url: "https://www.maojianwei.com/",
|
|
199
|
-
// })
|
|
200
|
-
//}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
func (m *IcmpDetectModule) controlLoop() {
|
|
207
|
-
checkTimer := time.NewTimer(time.Duration(m.checkInterval) * time.Millisecond)
|
|
208
|
-
for {
|
|
209
|
-
select {
|
|
210
|
-
case addService := <-m.AddChan:
|
|
211
|
-
if _, ok := m.serviceStore.Load(addService); !ok {
|
|
212
|
-
m.serviceStore.Store(addService, &MaoApi.MaoIcmpService{
|
|
213
|
-
Address: addService,
|
|
214
|
-
Alive: false,
|
|
215
|
-
LastSeen: time.Unix(0, 0),
|
|
216
|
-
DetectCount: 0,
|
|
217
|
-
ReportCount: 0,
|
|
218
|
-
RttDuration: 0,
|
|
219
|
-
RttOutboundTimestamp: time.Time{},
|
|
220
|
-
})
|
|
221
|
-
util.MaoLogM(util.DEBUG, MODULE_NAME, "Get new service %s", addService)
|
|
222
|
-
m.addNewServiceToConfig(addService)
|
|
223
|
-
}
|
|
224
|
-
case delService := <-m.DelChan:
|
|
225
|
-
m.serviceStore.Delete(delService)
|
|
226
|
-
util.MaoLogM(util.DEBUG, MODULE_NAME, "Del service %s", delService)
|
|
227
|
-
m.removeOldServiceFromConfig(delService)
|
|
228
|
-
|
|
229
|
-
topoModule := MaoCommon.ServiceRegistryGetTopoModule()
|
|
230
|
-
if topoModule == nil {
|
|
231
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to get TopoModule, can't send DELETE event")
|
|
232
|
-
} else {
|
|
233
|
-
topoModule.SendEvent(&MaoApi.TopoEvent{
|
|
234
|
-
EventType: MaoApi.SERVICE_DELETE,
|
|
235
|
-
EventSource: MaoApi.SOURCE_ICMP,
|
|
236
|
-
ServiceName: delService,
|
|
237
|
-
Timestamp: time.Now(),
|
|
238
|
-
})
|
|
239
|
-
}
|
|
240
|
-
case <-checkTimer.C:
|
|
241
|
-
// aliveness checking
|
|
242
|
-
m.serviceStore.Range(func(key, value interface{}) bool {
|
|
243
|
-
service := value.(*MaoApi.MaoIcmpService)
|
|
244
|
-
if service.Alive && time.Since(service.LastSeen) > time.Duration(m.leaveTimeout) * time.Millisecond {
|
|
245
|
-
service.Alive = false
|
|
246
|
-
|
|
247
|
-
emailModule := MaoCommon.ServiceRegistryGetEmailModule()
|
|
248
|
-
if emailModule == nil {
|
|
249
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to get EmailModule, can't send DOWN notification")
|
|
250
|
-
} else {
|
|
251
|
-
emailModule.SendEmail(&MaoApi.EmailMessage{
|
|
252
|
-
Subject: "ICMP DOWN notification",
|
|
253
|
-
Content: fmt.Sprintf("Service: %s\r\nDOWN Time: %s\r\nDetail: %v\r\n",
|
|
254
|
-
service.Address, time.Now().String(), service),
|
|
255
|
-
})
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
// TEMP: test wechat module
|
|
260
|
-
//wechatModule := MaoCommon.ServiceRegistryGetWechatModule()
|
|
261
|
-
//if wechatModule == nil {
|
|
262
|
-
// util.MaoLogM(util.WARN, MODULE_NAME, "Fail to get WechatModule, can't send DOWN notification")
|
|
263
|
-
//} else {
|
|
264
|
-
// wechatModule.SendWechatMessage(&MaoApi.WechatMessage{
|
|
265
|
-
// Title: "ICMP DOWN notification",
|
|
266
|
-
// ContentHttp: fmt.Sprintf("Service: %s\r\nDOWN Time: %s\r\nDetail: %v\r\n",
|
|
267
|
-
// service.Address, time.Now().String(), service),
|
|
268
|
-
// Url: "https://www.maojianwei.com/",
|
|
269
|
-
// })
|
|
270
|
-
//}
|
|
271
|
-
}
|
|
272
|
-
return true
|
|
273
|
-
})
|
|
274
|
-
checkTimer.Reset(time.Duration(m.checkInterval) * time.Millisecond)
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
func (m *IcmpDetectModule) refreshShowingService() {
|
|
280
|
-
for {
|
|
281
|
-
time.Sleep(time.Duration(m.refreshShowingInterval) * time.Millisecond)
|
|
282
|
-
servicesTmp := make([]*MaoApi.MaoIcmpService, 0)
|
|
283
|
-
m.serviceStore.Range(func(_, value interface{}) bool {
|
|
284
|
-
servicesTmp = append(servicesTmp, value.(*MaoApi.MaoIcmpService))
|
|
285
|
-
return true
|
|
286
|
-
})
|
|
287
|
-
m.serviceMirror = servicesTmp
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
func (m *IcmpDetectModule) getServiceConfig() (serviceList []string){
|
|
295
|
-
serviceList = make([]string, 0)
|
|
296
|
-
|
|
297
|
-
configModule := MaoCommon.ServiceRegistryGetConfigModule()
|
|
298
|
-
if configModule == nil {
|
|
299
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to get config module instance")
|
|
300
|
-
return nil
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
serviceObj, errCode := configModule.GetConfig(SERVICE_LIST_CONFIG_PATH)
|
|
304
|
-
if errCode != Config.ERR_CODE_SUCCESS {
|
|
305
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to get current services from config, errCode: %d", errCode)
|
|
306
|
-
return nil
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
serviceList, ok := serviceObj.([]string)
|
|
310
|
-
if !ok {
|
|
311
|
-
// the list is read from config file
|
|
312
|
-
serviceIntfList, ok := serviceObj.([]interface{})
|
|
313
|
-
if !ok {
|
|
314
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to parse serviceList config, []string and []interface{}")
|
|
315
|
-
return nil
|
|
316
|
-
}
|
|
317
|
-
serviceList = make([]string, 0)
|
|
318
|
-
for _, s := range serviceIntfList {
|
|
319
|
-
serviceList = append(serviceList, s.(string))
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
return serviceList
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
func (m *IcmpDetectModule) saveServiceConfig(serviceList []string) (success bool){
|
|
327
|
-
configModule := MaoCommon.ServiceRegistryGetConfigModule()
|
|
328
|
-
if configModule == nil {
|
|
329
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to get config module instance")
|
|
330
|
-
return false
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
_, errCode := configModule.PutConfig(SERVICE_LIST_CONFIG_PATH, serviceList)
|
|
334
|
-
if errCode != Config.ERR_CODE_SUCCESS {
|
|
335
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to put current services to config, errCode: %d", errCode)
|
|
336
|
-
return false
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
return true
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
func (m *IcmpDetectModule) addNewServiceToConfig(serviceAddr string) (success bool) {
|
|
343
|
-
currentServices := m.getServiceConfig()
|
|
344
|
-
if currentServices == nil {
|
|
345
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to get current services from config")
|
|
346
|
-
return false
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
for _, serviceExist := range currentServices {
|
|
350
|
-
if serviceExist == serviceAddr {
|
|
351
|
-
// Mainly for reading config during initialization phase.
|
|
352
|
-
return true
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
currentServices = append(currentServices, serviceAddr)
|
|
356
|
-
|
|
357
|
-
return m.saveServiceConfig(currentServices)
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
func (m *IcmpDetectModule) removeOldServiceFromConfig(serviceAddr string) (success bool) {
|
|
361
|
-
currentServices := m.getServiceConfig()
|
|
362
|
-
if currentServices == nil {
|
|
363
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to get current services from config")
|
|
364
|
-
return false
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
// assume that: the service address appears only once in the config
|
|
368
|
-
for index, s := range currentServices {
|
|
369
|
-
if s == serviceAddr {
|
|
370
|
-
currentServices = append(currentServices[:index], currentServices[index+1:]...)
|
|
371
|
-
m.saveServiceConfig(currentServices)
|
|
372
|
-
return true
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Can't find the service in the config, can't remove it, service: %s", serviceAddr)
|
|
377
|
-
return false
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
func (m *IcmpDetectModule) initConfigPath() (success bool, serviceConfig []string) {
|
|
381
|
-
services := m.getServiceConfig()
|
|
382
|
-
if services != nil {
|
|
383
|
-
return true, services
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
// the config doesn't exist, init it.
|
|
387
|
-
|
|
388
|
-
configModule := MaoCommon.ServiceRegistryGetConfigModule()
|
|
389
|
-
if configModule == nil {
|
|
390
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to get config module instance")
|
|
391
|
-
return false, nil
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
_, errCode := configModule.PutConfig(SERVICE_LIST_CONFIG_PATH, make([]string, 0))
|
|
395
|
-
if errCode != Config.ERR_CODE_SUCCESS {
|
|
396
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to put empty string array to config, errCode: %d", errCode)
|
|
397
|
-
return false, nil
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
return true, services
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
func (m *IcmpDetectModule) AddService(serviceIPv4v6 string) {
|
|
407
|
-
if net.ParseIP(serviceIPv4v6) != nil {
|
|
408
|
-
m.AddChan <- serviceIPv4v6
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
func (m *IcmpDetectModule) DelService(serviceIPv4v6 string) {
|
|
413
|
-
if net.ParseIP(serviceIPv4v6) != nil {
|
|
414
|
-
m.DelChan <- serviceIPv4v6
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
func (m *IcmpDetectModule) InitIcmpModule() bool {
|
|
421
|
-
var err error
|
|
422
|
-
m.connV4, err = icmp.ListenPacket("ip4:icmp", "0.0.0.0")
|
|
423
|
-
if err != nil {
|
|
424
|
-
util.MaoLogM(util.ERROR, MODULE_NAME, "Fail to listen ICMP, %s", err.Error())
|
|
425
|
-
return false
|
|
426
|
-
}
|
|
427
|
-
util.MaoLogM(util.INFO, MODULE_NAME, "Listen ICMP ok")
|
|
428
|
-
|
|
429
|
-
m.connV6, err = icmp.ListenPacket("ip6:ipv6-icmp", "::")
|
|
430
|
-
if err != nil {
|
|
431
|
-
util.MaoLogM(util.ERROR, MODULE_NAME, "Fail to listen ICMPv6, %s", err.Error())
|
|
432
|
-
return false
|
|
433
|
-
}
|
|
434
|
-
util.MaoLogM(util.INFO, MODULE_NAME, "Listen ICMPv6 ok")
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
m.AddChan = make(chan string, 50)
|
|
439
|
-
m.DelChan = make(chan string, 50)
|
|
440
|
-
|
|
441
|
-
if success, services := m.initConfigPath(); !success {
|
|
442
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to init config.")
|
|
443
|
-
} else {
|
|
444
|
-
for _, s := range services {
|
|
445
|
-
m.AddService(s)
|
|
446
|
-
}
|
|
447
|
-
util.MaoLogM(util.INFO, MODULE_NAME, "Services loaded from config: %s", services)
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
// configurable parameter
|
|
451
|
-
m.sendInterval = 500
|
|
452
|
-
m.checkInterval = 500
|
|
453
|
-
m.leaveTimeout = 2000
|
|
454
|
-
m.refreshShowingInterval = 1000
|
|
455
|
-
|
|
456
|
-
// tunable configurable parameter
|
|
457
|
-
m.receiveFreezePeriod = 10
|
|
458
|
-
m.serviceMirror = make([]*MaoApi.MaoIcmpService, 0)
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
go m.receiveProcessIcmpLoop(PROTO_ICMP, m.connV4)
|
|
462
|
-
go m.receiveProcessIcmpLoop(PROTO_ICMP_V6, m.connV6)
|
|
463
|
-
go m.sendIcmpLoop()
|
|
464
|
-
go m.controlLoop()
|
|
465
|
-
|
|
466
|
-
go m.refreshShowingService()
|
|
467
|
-
|
|
468
|
-
m.configRestControlInterface()
|
|
469
|
-
|
|
470
|
-
return true
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
func (m *IcmpDetectModule) GetServices() []*MaoApi.MaoIcmpService {
|
|
476
|
-
tmp := m.serviceMirror
|
|
477
|
-
sort.Slice(tmp, func(i, j int) bool {
|
|
478
|
-
return tmp[i].Address < tmp[j].Address
|
|
479
|
-
})
|
|
480
|
-
return tmp
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
func showConfigPage(c *gin.Context) {
|
|
484
|
-
c.HTML(200, "index-icmp.html", nil)
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
func (m *IcmpDetectModule) showServiceIps(c *gin.Context) {
|
|
488
|
-
c.JSON(200, m.GetServices())
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
func (m *IcmpDetectModule) processServiceIp(c *gin.Context) {
|
|
492
|
-
v4Ip, ok := c.GetPostForm("ipv4v6")
|
|
493
|
-
if ok {
|
|
494
|
-
v4IpArr := strings.Fields(v4Ip)
|
|
495
|
-
for _, s := range v4IpArr {
|
|
496
|
-
if c.FullPath() == URL_CONFIG_ADD_SERVICE_IP {
|
|
497
|
-
m.AddService(s)
|
|
498
|
-
} else {
|
|
499
|
-
m.DelService(s)
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
showConfigPage(c)
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
func (m *IcmpDetectModule) configRestControlInterface() {
|
|
509
|
-
restfulServer := MaoCommon.ServiceRegistryGetRestfulServerModule()
|
|
510
|
-
if restfulServer == nil {
|
|
511
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to get RestfulServerModule, unable to register restful apis.")
|
|
512
|
-
return
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
restfulServer.RegisterUiPage(URL_CONFIG_HOMEPAGE, showConfigPage)
|
|
516
|
-
restfulServer.RegisterGetApi(URL_CONFIG_SHOW_SERVICE_IP, m.showServiceIps)
|
|
517
|
-
|
|
518
|
-
restfulServer.RegisterPostApi(URL_CONFIG_ADD_SERVICE_IP, m.processServiceIp)
|
|
519
|
-
restfulServer.RegisterPostApi(URL_CONFIG_DEL_SERVICE_IP, m.processServiceIp)
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
//func main() {
|
|
523
|
-
// addServiceChan = make(chan string, 50)
|
|
524
|
-
// delServiceChan = make(chan string, 50)
|
|
525
|
-
//
|
|
526
|
-
// icmpDetectModule := &IcmpDetectModule{
|
|
527
|
-
// AddChan: &addServiceChan,
|
|
528
|
-
// DelChan: &delServiceChan,
|
|
529
|
-
// ControlPort: 2468,
|
|
530
|
-
// }
|
|
531
|
-
//
|
|
532
|
-
// icmpDetectModule.InitIcmpModule()
|
|
533
|
-
//
|
|
534
|
-
// go runRestControlInterface(icmpDetectModule.ControlPort)
|
|
535
|
-
//
|
|
536
|
-
// for {
|
|
537
|
-
// time.Sleep(1 * time.Second)
|
|
538
|
-
// newConfigService := []*MaoIcmpService{}
|
|
539
|
-
// icmpDetectModule.serviceStore.Range(func(_, value interface{}) bool {
|
|
540
|
-
// newConfigService = append(newConfigService, value.(*MaoIcmpService))
|
|
541
|
-
// return true
|
|
542
|
-
// })
|
|
543
|
-
// serviceMirror = newConfigService
|
|
544
|
-
// }
|
|
545
|
-
//}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
package IcmpKa
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"testing"
|
|
5
|
-
)
|
|
6
|
-
|
|
7
|
-
func TestIcmpDetectModule_InitIcmpModule(t *testing.T) {
|
|
8
|
-
icmpDetectModule := &IcmpDetectModule{
|
|
9
|
-
//AddChan: nil,
|
|
10
|
-
//DelChan: nil,
|
|
11
|
-
}
|
|
12
|
-
//icmpDetectModule.InitIcmpModule()
|
|
13
|
-
|
|
14
|
-
if icmpDetectModule.sendInterval == 0 ||
|
|
15
|
-
icmpDetectModule.checkInterval == 0 ||
|
|
16
|
-
icmpDetectModule.leaveTimeout == 0 ||
|
|
17
|
-
icmpDetectModule.refreshShowingInterval == 0 ||
|
|
18
|
-
icmpDetectModule.receiveFreezePeriod == 0 ||
|
|
19
|
-
icmpDetectModule.serviceMirror == nil {
|
|
20
|
-
|
|
21
|
-
//t.Fatalf("Icmp-KA-Module is not fully initiated.")
|
|
22
|
-
}
|
|
23
|
-
}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
package InfluxDB
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"MaoServerDiscovery/util"
|
|
5
|
-
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
|
6
|
-
influxdb2Api "github.com/influxdata/influxdb-client-go/v2/api"
|
|
7
|
-
"time"
|
|
8
|
-
)
|
|
9
|
-
|
|
10
|
-
const (
|
|
11
|
-
MODULE_NAME = "InfluxDB-Util"
|
|
12
|
-
)
|
|
13
|
-
|
|
14
|
-
var (
|
|
15
|
-
config_influxdbUrl = ""
|
|
16
|
-
config_influxdbToken = ""
|
|
17
|
-
config_influxdbOrgBucket = ""
|
|
18
|
-
)
|
|
19
|
-
|
|
20
|
-
//a.influxdbUrl = influxdbUrl
|
|
21
|
-
//a.influxdbToken = influxdbToken
|
|
22
|
-
//a.influxdbOrgBucket = influxdbOrgBucket
|
|
23
|
-
|
|
24
|
-
func nat66UploadInfluxdb(writeAPI *influxdb2Api.WriteAPI, v6In uint64, v6Out uint64) {
|
|
25
|
-
// write point asynchronously
|
|
26
|
-
(*writeAPI).WritePoint(
|
|
27
|
-
influxdb2.NewPointWithMeasurement("NAT66_Gateway").
|
|
28
|
-
AddTag("Geo", "Beijing-HQ").
|
|
29
|
-
AddField("v6In", v6In).
|
|
30
|
-
AddField("v6Out", v6Out).
|
|
31
|
-
SetTime(time.Now()))
|
|
32
|
-
// Not flush writes, avoid blocking my thread, then the lib's thread will block itself.
|
|
33
|
-
//(*writeAPI).Flush()
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
//
|
|
37
|
-
func EnvTempUploadInfluxdb(geo string, timestamp time.Time, envTemperature float32) {
|
|
38
|
-
client, writeAPI := CreateClientAndWriteAPI()
|
|
39
|
-
if writeAPI == nil {
|
|
40
|
-
if time.Now().Second() % 10 == 0 {
|
|
41
|
-
// suppress the number of logs
|
|
42
|
-
util.MaoLogM(util.WARN, MODULE_NAME, "Fail to upload env temp, InfluxDB API URLs haven't configured yet.")
|
|
43
|
-
}
|
|
44
|
-
return
|
|
45
|
-
}
|
|
46
|
-
defer (*client).Close()
|
|
47
|
-
|
|
48
|
-
// write point asynchronously
|
|
49
|
-
(*writeAPI).WritePoint(
|
|
50
|
-
influxdb2.NewPointWithMeasurement("Temperature").
|
|
51
|
-
AddTag("Geo", geo).
|
|
52
|
-
AddField("env", envTemperature).
|
|
53
|
-
SetTime(timestamp))
|
|
54
|
-
// Not flush writes, avoid blocking my thread, then the lib's thread will block itself.
|
|
55
|
-
//(*writeAPI).Flush()
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
func CreateClientAndWriteAPI() (*influxdb2.Client, *influxdb2Api.WriteAPI) {
|
|
59
|
-
if config_influxdbUrl == "" {
|
|
60
|
-
return nil, nil
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
influxdbClient := influxdb2.NewClient(config_influxdbUrl, config_influxdbToken)
|
|
64
|
-
influxdbWriteAPI := influxdbClient.WriteAPI(config_influxdbOrgBucket, config_influxdbOrgBucket)
|
|
65
|
-
return &influxdbClient, &influxdbWriteAPI
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
func ConfigInfluxdbUtils(influxdbUrl, influxdbToken, influxdbOrgBucket string) {
|
|
69
|
-
config_influxdbUrl = influxdbUrl
|
|
70
|
-
config_influxdbToken = influxdbToken
|
|
71
|
-
config_influxdbOrgBucket = influxdbOrgBucket
|
|
72
|
-
}
|