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
package/bak/index.html
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8">
|
|
5
|
-
<title>Title</title>
|
|
6
|
-
</head>
|
|
7
|
-
<body>
|
|
8
|
-
<!--enctype="application/json"-->
|
|
9
|
-
<form action="/addServiceIp" method="post">
|
|
10
|
-
IPv4<br/>
|
|
11
|
-
<textarea rows="6" cols="50" name="ipv4"></textarea><br/>
|
|
12
|
-
<br/>
|
|
13
|
-
IPv6<br/>
|
|
14
|
-
<textarea rows="6" cols="50" name="ipv6"></textarea><br/>
|
|
15
|
-
<input type="submit" value="Submit" />
|
|
16
|
-
</form>
|
|
17
|
-
<form action="/delServiceIp" method="post">
|
|
18
|
-
IPv4 Status<br/>
|
|
19
|
-
<textarea rows="6" cols="50" name="ipv4"></textarea><br/>
|
|
20
|
-
<br/>
|
|
21
|
-
IPv6 Status<br/>
|
|
22
|
-
<textarea rows="6" cols="50" name="ipv6"></textarea><br/>
|
|
23
|
-
<input type="submit" value="Submit" />
|
|
24
|
-
</form>
|
|
25
|
-
|
|
26
|
-
<div id="services"></div>
|
|
27
|
-
<script src="/static/jquery-3.6.0.min.js" type="text/javascript"></script>
|
|
28
|
-
<script>
|
|
29
|
-
$.get("/showServiceIP",function (response, status, xhr) {
|
|
30
|
-
services = ""
|
|
31
|
-
$.each(response, function(index, item) {
|
|
32
|
-
services += "<form action=\"/delServiceIp\" method=\"post\">"
|
|
33
|
-
services += "<input type=\"submit\" value=\"Submit\" />"
|
|
34
|
-
services += "<input type=\"text\" name='service' value='" + item["Address"] + "'/>"
|
|
35
|
-
services += "</form>"
|
|
36
|
-
})
|
|
37
|
-
$("#services").html(services)
|
|
38
|
-
})
|
|
39
|
-
</script>
|
|
40
|
-
|
|
41
|
-
</body>
|
|
42
|
-
</html>
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
//func (m *IcmpDetectModule) receiveProcessIcmpLoopV4() {
|
|
2
|
-
// freeze_period := 500 // ms
|
|
3
|
-
// recvBuf := make([]byte, 2000)
|
|
4
|
-
// for {
|
|
5
|
-
// count, addr, err := m.connV4.ReadFrom(recvBuf)
|
|
6
|
-
// lastseen := time.Now()
|
|
7
|
-
// if err != nil {
|
|
8
|
-
// util.MaoLog(util.WARN, fmt.Sprintf("Fail to recv ICMP, freeze %d ms, %s", freeze_period, err.Error()))
|
|
9
|
-
// time.Sleep(time.Duration(freeze_period) * time.Millisecond)
|
|
10
|
-
// continue
|
|
11
|
-
// }
|
|
12
|
-
//
|
|
13
|
-
// msg, err := icmp.ParseMessage(PROTO_ICMP, recvBuf)
|
|
14
|
-
// if err != nil {
|
|
15
|
-
// util.MaoLog(util.WARN, fmt.Sprintf("Fail to parse ICMP, %s", err.Error()))
|
|
16
|
-
// continue
|
|
17
|
-
// }
|
|
18
|
-
//
|
|
19
|
-
// icmpEcho, ok := msg.Body.(*icmp.Echo)
|
|
20
|
-
// if !ok {
|
|
21
|
-
// continue
|
|
22
|
-
// }
|
|
23
|
-
// util.MaoLog(util.DEBUG, fmt.Sprintf("%v, %v = %v, %v, %v, %v, %v, %v", count, addr, msg.Type, msg.Code, msg.Checksum, icmpEcho.ID, icmpEcho.Seq, icmpEcho.Data))
|
|
24
|
-
//
|
|
25
|
-
// value, ok := m.serviceStore.Load(addr.String())
|
|
26
|
-
// if ok && value != nil {
|
|
27
|
-
// service := value.(*MaoIcmpService)
|
|
28
|
-
// service.Alive = true
|
|
29
|
-
// service.LastSeen = lastseen
|
|
30
|
-
// service.RttDuration = service.LastSeen.Sub(service.RttOutboundTimestamp).Nanoseconds()
|
|
31
|
-
// service.ReportCount++
|
|
32
|
-
// }
|
|
33
|
-
// }
|
|
34
|
-
//}
|
|
35
|
-
//func (m *IcmpDetectModule) receiveProcessIcmpLoopV6() {
|
|
36
|
-
//
|
|
37
|
-
// //m.connV4.ReadFrom()
|
|
38
|
-
// //
|
|
39
|
-
// //icmp.ParseMessage(PROTO_ICMP_V6, recvBuf)
|
|
40
|
-
//
|
|
41
|
-
// freeze_period := 500 // ms
|
|
42
|
-
// recvBuf := make([]byte, 2000)
|
|
43
|
-
// for {
|
|
44
|
-
// count, addr, err := m.connV6.ReadFrom(recvBuf)
|
|
45
|
-
// lastseen := time.Now()
|
|
46
|
-
// if err != nil {
|
|
47
|
-
// util.MaoLog(util.WARN, fmt.Sprintf("Fail to recv ICMP, freeze %d ms, %s", freeze_period, err.Error()))
|
|
48
|
-
// time.Sleep(time.Duration(freeze_period) * time.Millisecond)
|
|
49
|
-
// continue
|
|
50
|
-
// }
|
|
51
|
-
//
|
|
52
|
-
// msg, err := icmp.ParseMessage(PROTO_ICMP_V6, recvBuf)
|
|
53
|
-
// if err != nil {
|
|
54
|
-
// util.MaoLog(util.WARN, fmt.Sprintf("Fail to parse ICMP, %s", err.Error()))
|
|
55
|
-
// continue
|
|
56
|
-
// }
|
|
57
|
-
//
|
|
58
|
-
// icmpEcho, ok := msg.Body.(*icmp.Echo)
|
|
59
|
-
// if !ok {
|
|
60
|
-
// continue
|
|
61
|
-
// }
|
|
62
|
-
// util.MaoLog(util.DEBUG, fmt.Sprintf("%v, %v = %v, %v, %v, %v, %v, %v", count, addr, msg.Type, msg.Code, msg.Checksum, icmpEcho.ID, icmpEcho.Seq, icmpEcho.Data))
|
|
63
|
-
//
|
|
64
|
-
// value, ok := m.serviceStore.Load(addrStr)
|
|
65
|
-
// if ok && value != nil {
|
|
66
|
-
// service := value.(*MaoIcmpService)
|
|
67
|
-
// service.Alive = true
|
|
68
|
-
// service.LastSeen = lastseen
|
|
69
|
-
// service.RttDuration = service.LastSeen.Sub(service.RttOutboundTimestamp).Nanoseconds()
|
|
70
|
-
// service.ReportCount++
|
|
71
|
-
// }
|
|
72
|
-
// }
|
|
73
|
-
//}
|
|
@@ -1,297 +0,0 @@
|
|
|
1
|
-
package main
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"MaoServerDiscovery/util"
|
|
5
|
-
"fmt"
|
|
6
|
-
"github.com/gin-gonic/gin"
|
|
7
|
-
"golang.org/x/net/icmp"
|
|
8
|
-
"golang.org/x/net/ipv4"
|
|
9
|
-
"golang.org/x/net/ipv6"
|
|
10
|
-
"net"
|
|
11
|
-
"sort"
|
|
12
|
-
"strings"
|
|
13
|
-
"sync"
|
|
14
|
-
"time"
|
|
15
|
-
)
|
|
16
|
-
|
|
17
|
-
var (
|
|
18
|
-
addServiceChan chan string
|
|
19
|
-
delServiceChan chan string
|
|
20
|
-
configService []*MaoIcmpService
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
const (
|
|
24
|
-
URL_CONFIG_HOMEPAGE string = "/"
|
|
25
|
-
URL_CONFIG_ADD_SERVICE_IP string = "/addServiceIp"
|
|
26
|
-
URL_CONFIG_DEL_SERVICE_IP string = "/delServiceIp"
|
|
27
|
-
URL_CONFIG_SHOW_SERVICE_IP string = "/showServiceIP"
|
|
28
|
-
|
|
29
|
-
PROTO_ICMP = 1
|
|
30
|
-
PROTO_ICMP_V6 = 58
|
|
31
|
-
|
|
32
|
-
ICMP_DETECT_ID = 0x1994
|
|
33
|
-
ICMP_V6_DETECT_ID = 0x1996
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
type MaoIcmpService struct {
|
|
37
|
-
Address string
|
|
38
|
-
|
|
39
|
-
Alive bool
|
|
40
|
-
LastSeen time.Time
|
|
41
|
-
|
|
42
|
-
DetectCount uint64
|
|
43
|
-
ReportCount uint64
|
|
44
|
-
|
|
45
|
-
RttDuration int64
|
|
46
|
-
RttOutboundTimestamp time.Time
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
type IcmpDetectModule struct {
|
|
50
|
-
connV4 *icmp.PacketConn
|
|
51
|
-
connV6 *icmp.PacketConn
|
|
52
|
-
serviceStore sync.Map // address_string -> Service object
|
|
53
|
-
|
|
54
|
-
controlPort uint32 // Todo: can be moved out
|
|
55
|
-
addChan *chan string
|
|
56
|
-
delChan *chan string
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
func (m *IcmpDetectModule) sendIcmpLoop() {
|
|
60
|
-
round := 1
|
|
61
|
-
for {
|
|
62
|
-
util.MaoLog(util.DEBUG, fmt.Sprintf("Detect Round %d", round))
|
|
63
|
-
m.serviceStore.Range(func(_, value interface{}) bool {
|
|
64
|
-
service := value.(*MaoIcmpService)
|
|
65
|
-
|
|
66
|
-
addr, err := net.ResolveIPAddr("ip", service.Address)
|
|
67
|
-
if err != nil {
|
|
68
|
-
util.MaoLog(util.WARN, fmt.Sprintf("Fail to ResolveIPAddr v4v6Addr: %s", err.Error()))
|
|
69
|
-
return true // for continuous iteration
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
var msgType icmp.Type
|
|
73
|
-
var echoId int
|
|
74
|
-
var conn *icmp.PacketConn
|
|
75
|
-
if util.JudgeIPv6Addr(addr) {
|
|
76
|
-
msgType = ipv6.ICMPTypeEchoRequest
|
|
77
|
-
echoId = ICMP_V6_DETECT_ID
|
|
78
|
-
conn = m.connV6
|
|
79
|
-
} else {
|
|
80
|
-
msgType = ipv4.ICMPTypeEcho
|
|
81
|
-
echoId = ICMP_DETECT_ID
|
|
82
|
-
conn = m.connV4
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
// To build and send ICMP Request.
|
|
87
|
-
|
|
88
|
-
service.DetectCount++
|
|
89
|
-
icmpPayloadData := []byte(time.Now().String())
|
|
90
|
-
echoMsg := icmp.Echo{
|
|
91
|
-
ID: echoId,
|
|
92
|
-
Seq: int(service.DetectCount),
|
|
93
|
-
Data: icmpPayloadData,
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
icmpMsg := icmp.Message{
|
|
97
|
-
Type: msgType,
|
|
98
|
-
Code: 0,
|
|
99
|
-
//Checksum: 0,
|
|
100
|
-
Body: &echoMsg,
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// do le->be in the Marshal
|
|
104
|
-
icmpMsgByte, err := icmpMsg.Marshal(nil)
|
|
105
|
-
if err != nil {
|
|
106
|
-
util.MaoLog(util.WARN, fmt.Sprintf("Fail to marshal icmpMsg: %s", err.Error()))
|
|
107
|
-
return true
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
service.RttOutboundTimestamp = time.Now()
|
|
111
|
-
_, err = conn.WriteTo(icmpMsgByte, addr)
|
|
112
|
-
if err != nil {
|
|
113
|
-
util.MaoLog(util.WARN, fmt.Sprintf("Fail to WriteTo connV6: %s", err.Error()))
|
|
114
|
-
return true
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return true
|
|
118
|
-
})
|
|
119
|
-
time.Sleep(500 * time.Millisecond)
|
|
120
|
-
round++
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* For IPv6: PROTO_ICMP, m.connV4
|
|
126
|
-
* For IPv4: PROTO_ICMP_V6, m.connV6
|
|
127
|
-
*/
|
|
128
|
-
func (m *IcmpDetectModule) receiveProcessIcmpLoop(protoNum int, conn *icmp.PacketConn) {
|
|
129
|
-
freeze_period := 500 // ms
|
|
130
|
-
recvBuf := make([]byte, 2000)
|
|
131
|
-
for {
|
|
132
|
-
count, addr, err := conn.ReadFrom(recvBuf)
|
|
133
|
-
lastseen := time.Now()
|
|
134
|
-
if err != nil {
|
|
135
|
-
util.MaoLog(util.WARN, fmt.Sprintf("Fail to recv ICMP, freeze %d ms, %s", freeze_period, err.Error()))
|
|
136
|
-
time.Sleep(time.Duration(freeze_period) * time.Millisecond)
|
|
137
|
-
continue
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
msg, err := icmp.ParseMessage(protoNum, recvBuf)
|
|
141
|
-
if err != nil {
|
|
142
|
-
util.MaoLog(util.WARN, fmt.Sprintf("Fail to parse ICMP, %s", err.Error()))
|
|
143
|
-
continue
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
icmpEcho, ok := msg.Body.(*icmp.Echo)
|
|
147
|
-
if !ok {
|
|
148
|
-
continue
|
|
149
|
-
}
|
|
150
|
-
util.MaoLog(util.DEBUG, fmt.Sprintf("%v, %v = %v, %v, %v, %v, %v, %v", count, addr, msg.Type, msg.Code, msg.Checksum, icmpEcho.ID, icmpEcho.Seq, icmpEcho.Data))
|
|
151
|
-
|
|
152
|
-
var addrStr string
|
|
153
|
-
if protoNum == PROTO_ICMP_V6 {
|
|
154
|
-
addrStr = strings.Split(addr.String(), "%")[0] // for ipv6 link-local address, it is suffixed by % and interface name.
|
|
155
|
-
} else {
|
|
156
|
-
addrStr = addr.String()
|
|
157
|
-
}
|
|
158
|
-
value, ok := m.serviceStore.Load(addrStr)
|
|
159
|
-
if ok && value != nil {
|
|
160
|
-
service := value.(*MaoIcmpService)
|
|
161
|
-
service.Alive = true
|
|
162
|
-
service.LastSeen = lastseen
|
|
163
|
-
service.RttDuration = service.LastSeen.Sub(service.RttOutboundTimestamp).Nanoseconds()
|
|
164
|
-
service.ReportCount++
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
func (m *IcmpDetectModule) controlLoop() {
|
|
170
|
-
checkPeriod := 1 * time.Second
|
|
171
|
-
checkTimer := time.NewTimer(checkPeriod)
|
|
172
|
-
for {
|
|
173
|
-
select {
|
|
174
|
-
case addService := <-*m.addChan:
|
|
175
|
-
if _, ok := m.serviceStore.Load(addService); !ok {
|
|
176
|
-
m.serviceStore.Store(addService, &MaoIcmpService{
|
|
177
|
-
Address: addService,
|
|
178
|
-
Alive: false,
|
|
179
|
-
LastSeen: time.Unix(0, 0),
|
|
180
|
-
DetectCount: 0,
|
|
181
|
-
ReportCount: 0,
|
|
182
|
-
RttDuration: 0,
|
|
183
|
-
RttOutboundTimestamp: time.Time{},
|
|
184
|
-
})
|
|
185
|
-
util.MaoLog(util.DEBUG, fmt.Sprintf("Get new service %s", addService))
|
|
186
|
-
}
|
|
187
|
-
case delService := <-*m.delChan:
|
|
188
|
-
m.serviceStore.Delete(delService)
|
|
189
|
-
util.MaoLog(util.DEBUG, fmt.Sprintf("Del service %s", delService))
|
|
190
|
-
case <-checkTimer.C:
|
|
191
|
-
m.serviceStore.Range(func(key, value interface{}) bool {
|
|
192
|
-
service := value.(*MaoIcmpService)
|
|
193
|
-
if service.Alive && time.Since(service.LastSeen) > 3*time.Second {
|
|
194
|
-
service.Alive = false
|
|
195
|
-
}
|
|
196
|
-
return true
|
|
197
|
-
})
|
|
198
|
-
checkTimer.Reset(checkPeriod)
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
func (m *IcmpDetectModule) InitIcmpModule() bool {
|
|
204
|
-
var err error
|
|
205
|
-
m.connV4, err = icmp.ListenPacket("ip4:icmp", "0.0.0.0")
|
|
206
|
-
if err != nil {
|
|
207
|
-
util.MaoLog(util.ERROR, fmt.Sprintf("Fail to listen ICMP, %s", err.Error()))
|
|
208
|
-
return false
|
|
209
|
-
}
|
|
210
|
-
util.MaoLog(util.INFO, "Listen ICMP ok")
|
|
211
|
-
|
|
212
|
-
m.connV6, err = icmp.ListenPacket("ip6:ipv6-icmp", "::")
|
|
213
|
-
if err != nil {
|
|
214
|
-
util.MaoLog(util.ERROR, fmt.Sprintf("Fail to listen ICMPv6, %s", err.Error()))
|
|
215
|
-
return false
|
|
216
|
-
}
|
|
217
|
-
util.MaoLog(util.INFO, "Listen ICMPv6 ok")
|
|
218
|
-
|
|
219
|
-
go m.receiveProcessIcmpLoop(PROTO_ICMP, m.connV4)
|
|
220
|
-
go m.receiveProcessIcmpLoop(PROTO_ICMP_V6, m.connV6)
|
|
221
|
-
go m.sendIcmpLoop()
|
|
222
|
-
go m.controlLoop()
|
|
223
|
-
|
|
224
|
-
return true
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
func showServiceIp(c *gin.Context) {
|
|
228
|
-
tmp := configService
|
|
229
|
-
sort.Slice(tmp, func(i, j int) bool {
|
|
230
|
-
return tmp[i].Address < tmp[j].Address
|
|
231
|
-
})
|
|
232
|
-
c.JSON(200, tmp)
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
func showConfigPage(c *gin.Context) {
|
|
236
|
-
c.HTML(200, "index-icmp.html", nil)
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
func processServiceIp(c *gin.Context) {
|
|
240
|
-
v4Ip, ok := c.GetPostForm("ipv4v6")
|
|
241
|
-
if ok {
|
|
242
|
-
v4IpArr := strings.Fields(v4Ip)
|
|
243
|
-
for _, s := range v4IpArr {
|
|
244
|
-
ip := net.ParseIP(s)
|
|
245
|
-
if ip != nil {
|
|
246
|
-
if c.FullPath() == URL_CONFIG_ADD_SERVICE_IP {
|
|
247
|
-
addServiceChan <- s
|
|
248
|
-
} else {
|
|
249
|
-
delServiceChan <- s
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
c.HTML(200, "index-icmp.html", nil)
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
func runRestControlInterface(controlPort uint32) {
|
|
258
|
-
gin.SetMode(gin.ReleaseMode)
|
|
259
|
-
restful := gin.Default()
|
|
260
|
-
restful.LoadHTMLFiles("resource/index-icmp.html")
|
|
261
|
-
restful.Static("/static/", "resource")
|
|
262
|
-
|
|
263
|
-
restful.GET(URL_CONFIG_HOMEPAGE, showConfigPage)
|
|
264
|
-
restful.GET(URL_CONFIG_SHOW_SERVICE_IP, showServiceIp)
|
|
265
|
-
restful.POST(URL_CONFIG_ADD_SERVICE_IP, processServiceIp)
|
|
266
|
-
restful.POST(URL_CONFIG_DEL_SERVICE_IP, processServiceIp)
|
|
267
|
-
|
|
268
|
-
err := restful.Run(fmt.Sprintf("[::]:%d", controlPort))
|
|
269
|
-
if err != nil {
|
|
270
|
-
util.MaoLog(util.ERROR, fmt.Sprintf("Fail to run config server, %s", err.Error()))
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
func main() {
|
|
275
|
-
addServiceChan = make(chan string, 50)
|
|
276
|
-
delServiceChan = make(chan string, 50)
|
|
277
|
-
|
|
278
|
-
icmpDetectModule := &IcmpDetectModule{
|
|
279
|
-
addChan: &addServiceChan,
|
|
280
|
-
delChan: &delServiceChan,
|
|
281
|
-
controlPort: 2468,
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
icmpDetectModule.InitIcmpModule()
|
|
285
|
-
|
|
286
|
-
go runRestControlInterface(icmpDetectModule.controlPort)
|
|
287
|
-
|
|
288
|
-
for {
|
|
289
|
-
time.Sleep(1 * time.Second)
|
|
290
|
-
newConfigService := []*MaoIcmpService{}
|
|
291
|
-
icmpDetectModule.serviceStore.Range(func(_, value interface{}) bool {
|
|
292
|
-
newConfigService = append(newConfigService, value.(*MaoIcmpService))
|
|
293
|
-
return true
|
|
294
|
-
})
|
|
295
|
-
configService = newConfigService
|
|
296
|
-
}
|
|
297
|
-
}
|
package/bak/test/test-tech.go
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
package main
|
|
3
|
-
//
|
|
4
|
-
//import (
|
|
5
|
-
// "github.com/gin-gonic/gin"
|
|
6
|
-
// "golang.org/x/net/icmp"
|
|
7
|
-
// "golang.org/x/net/ipv4"
|
|
8
|
-
// "log"
|
|
9
|
-
// "net"
|
|
10
|
-
// "strings"
|
|
11
|
-
// "time"
|
|
12
|
-
//)
|
|
13
|
-
//
|
|
14
|
-
//var (
|
|
15
|
-
// v4Services = map[string]bool{}
|
|
16
|
-
// v6Services = map[string]bool{}
|
|
17
|
-
//)
|
|
18
|
-
//
|
|
19
|
-
//func receiveV4Ping() {
|
|
20
|
-
//
|
|
21
|
-
//}
|
|
22
|
-
//
|
|
23
|
-
//func sendV4Ping(connV4 *icmp.PacketConn) {
|
|
24
|
-
// //conn packetConn
|
|
25
|
-
// //conn.SetFlagTTL()
|
|
26
|
-
//
|
|
27
|
-
// icmpPayloadData := append([]byte(time.Now().String()))
|
|
28
|
-
// echoMsg := icmp.Echo{
|
|
29
|
-
// ID: 0xabcd,
|
|
30
|
-
// Seq: 0x1994,
|
|
31
|
-
// Data: icmpPayloadData,
|
|
32
|
-
// }
|
|
33
|
-
//
|
|
34
|
-
// icmpMsg := icmp.Message{
|
|
35
|
-
// Type: ipv4.ICMPTypeEcho,
|
|
36
|
-
// Code: 0,
|
|
37
|
-
// //Checksum: 0,
|
|
38
|
-
// Body: &echoMsg,
|
|
39
|
-
// }
|
|
40
|
-
//
|
|
41
|
-
// // do le->be in the Marshal
|
|
42
|
-
// icmpMsgByte, err := icmpMsg.Marshal(nil)
|
|
43
|
-
// if err != nil {
|
|
44
|
-
// log.Printf("Fail to marshal icmpMsg: %s", err.Error())
|
|
45
|
-
// }
|
|
46
|
-
//
|
|
47
|
-
// v4Addr, err := net.ResolveIPAddr("ip", "192.168.1.2")
|
|
48
|
-
// if err != nil {
|
|
49
|
-
// log.Printf("Fail to ResolveIPAddr v4Addr: %s", err.Error())
|
|
50
|
-
// }
|
|
51
|
-
//
|
|
52
|
-
// count, err := connV4.WriteTo(icmpMsgByte, v4Addr)
|
|
53
|
-
// if err != nil {
|
|
54
|
-
// log.Printf("Fail to WriteTo connV4: %s", err.Error())
|
|
55
|
-
// }
|
|
56
|
-
// log.Printf("WriteTo connV4 %d: %s: %s --- %v \n--- %v", count, v4Addr.String(), v4Addr.Network(), icmpMsgByte, icmpMsg)
|
|
57
|
-
//}
|
|
58
|
-
//
|
|
59
|
-
//func showConfigPage(c *gin.Context) {
|
|
60
|
-
// c.HTML(200, "index-icmp.html", nil)
|
|
61
|
-
//}
|
|
62
|
-
//
|
|
63
|
-
//func addServiceIp(c *gin.Context) {
|
|
64
|
-
// v4Ip, ok := c.GetPostForm("ipv4")
|
|
65
|
-
// if ok {
|
|
66
|
-
// v4IpArr := strings.Fields(v4Ip)
|
|
67
|
-
// log.Printf("\n%v", v4IpArr)
|
|
68
|
-
// }
|
|
69
|
-
// v6Ip, ok := c.GetPostForm("ipv6")
|
|
70
|
-
// if ok {
|
|
71
|
-
// v6IpArr := strings.Fields(v6Ip)
|
|
72
|
-
// log.Printf("\n%v", v6IpArr)
|
|
73
|
-
// }
|
|
74
|
-
//}
|
|
75
|
-
//
|
|
76
|
-
//func startRestServer() {
|
|
77
|
-
// gin.SetMode(gin.ReleaseMode)
|
|
78
|
-
// restful := gin.Default()
|
|
79
|
-
// restful.LoadHTMLFiles("index-icmp.html")
|
|
80
|
-
// restful.GET("/", showConfigPage)
|
|
81
|
-
// restful.POST("/addServiceIp", addServiceIp)
|
|
82
|
-
// err := restful.Run("[::]:9876")
|
|
83
|
-
// if err != nil {
|
|
84
|
-
// log.Printf("qingdao %s", err.Error())
|
|
85
|
-
// }
|
|
86
|
-
//}
|
|
87
|
-
//
|
|
88
|
-
//func main() {
|
|
89
|
-
// go receiveV4Ping()
|
|
90
|
-
// startRestServer()
|
|
91
|
-
// return
|
|
92
|
-
//
|
|
93
|
-
//
|
|
94
|
-
//
|
|
95
|
-
// connV4, err := icmp.ListenPacket("ip4:icmp", "0.0.0.0")
|
|
96
|
-
// if err != nil {
|
|
97
|
-
// log.Printf("Listen for v4 error: %s", err.Error())
|
|
98
|
-
// }
|
|
99
|
-
// log.Printf("v4 conn: %v", connV4)
|
|
100
|
-
// connV6, err := icmp.ListenPacket("ip6:ipv6-icmp", "::")
|
|
101
|
-
// if err != nil {
|
|
102
|
-
// log.Printf("Listen for v6 error: %s", err.Error())
|
|
103
|
-
// }
|
|
104
|
-
// log.Printf("v6 conn: %v", connV6)
|
|
105
|
-
//
|
|
106
|
-
// for {
|
|
107
|
-
// sendV4Ping(connV4)
|
|
108
|
-
// time.Sleep(1000 * time.Millisecond)
|
|
109
|
-
// }
|
|
110
|
-
//}
|
package/bak/using_etcd/main.go
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
package main
|
|
2
|
-
|
|
3
|
-
func main() {
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
//
|
|
9
|
-
//import (
|
|
10
|
-
// "MaoServerDiscovery/util"
|
|
11
|
-
// "context"
|
|
12
|
-
// "fmt"
|
|
13
|
-
// etcd "go.etcd.io/etcd/client/v3"
|
|
14
|
-
// "time"
|
|
15
|
-
//)
|
|
16
|
-
//
|
|
17
|
-
//const (
|
|
18
|
-
// KEEP_ALIVE_TTL = 10 // Second
|
|
19
|
-
//)
|
|
20
|
-
//
|
|
21
|
-
//var (
|
|
22
|
-
// last_hostname, last_addrStr string
|
|
23
|
-
//)
|
|
24
|
-
//
|
|
25
|
-
//func putKV(client *etcd.Client, lease etcd.LeaseID, key, value string) {
|
|
26
|
-
// util.MaoLog(util.DEBUG, fmt.Sprintf("To put { %s -> %s }", key, value))
|
|
27
|
-
// ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
28
|
-
// putResp, err := client.Put(ctx, key, value, etcd.WithLease(lease))
|
|
29
|
-
// cancel()
|
|
30
|
-
// if err != nil {
|
|
31
|
-
// util.MaoLog(util.WARN, fmt.Sprintf("Fail to put { %s -> %s }, Resp: %s, err: %s", key, value, putResp, err))
|
|
32
|
-
// return
|
|
33
|
-
// }
|
|
34
|
-
//
|
|
35
|
-
// util.MaoLog(util.INFO, fmt.Sprintf("Success to put { %s -> %s }", key, value))
|
|
36
|
-
//}
|
|
37
|
-
//
|
|
38
|
-
//func watchKeyPrefix(client *etcd.Client, key string) {
|
|
39
|
-
//
|
|
40
|
-
// // Mao: If using this, it will close channel automatically.
|
|
41
|
-
// // ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
42
|
-
// ctx := context.Background()
|
|
43
|
-
//
|
|
44
|
-
// util.MaoLog(util.INFO, fmt.Sprintf("To watch: %s", key))
|
|
45
|
-
// watchChannel := client.Watch(ctx, key, etcd.WithPrefix())
|
|
46
|
-
// for watchResp := range watchChannel {
|
|
47
|
-
// util.MaoLog(util.DEBUG, fmt.Sprintf("New WatchResponse"))
|
|
48
|
-
// for _, ev := range watchResp.Events {
|
|
49
|
-
// util.MaoLog(util.INFO, fmt.Sprintf("New change, Type: %s, { %s -> %s }", ev.Type, ev.Kv.Key, ev.Kv.Value)) // "ev.PrevKv.Key nil", "ev.PrevKv.Value nil"
|
|
50
|
-
// }
|
|
51
|
-
// }
|
|
52
|
-
//
|
|
53
|
-
// util.MaoLog(util.INFO, fmt.Sprintf("Finish watch"))
|
|
54
|
-
//}
|
|
55
|
-
//
|
|
56
|
-
//// useless
|
|
57
|
-
//func showLease(client *etcd.Client) {
|
|
58
|
-
// leasesResp, err := client.Leases(context.Background())
|
|
59
|
-
// if err != nil {
|
|
60
|
-
// util.MaoLog(util.ERROR, fmt.Sprintf("Fail to get leases, err: %s", err))
|
|
61
|
-
// }
|
|
62
|
-
//
|
|
63
|
-
// util.MaoLog(util.INFO, fmt.Sprintf("Found %d leases", len(leasesResp.Leases)))
|
|
64
|
-
// for _, leaseStatus := range leasesResp.Leases {
|
|
65
|
-
// util.MaoLog(util.INFO, fmt.Sprintf("Lease id: %d", leaseStatus.ID))
|
|
66
|
-
// }
|
|
67
|
-
//}
|
|
68
|
-
//
|
|
69
|
-
//func keepAliveLease(client *etcd.Client, id etcd.LeaseID) {
|
|
70
|
-
// util.MaoLog(util.INFO, fmt.Sprintf("Start keep-alive for lease %d ...", id))
|
|
71
|
-
// kaRespChan, err := client.KeepAlive(context.Background(), id)
|
|
72
|
-
// if err != nil {
|
|
73
|
-
// util.MaoLog(util.WARN, fmt.Sprintf("Fail to start keep-alive for lease %d", id))
|
|
74
|
-
// return
|
|
75
|
-
// }
|
|
76
|
-
//
|
|
77
|
-
// for kaResp := range kaRespChan {
|
|
78
|
-
// util.MaoLog(util.DEBUG, fmt.Sprintf("KA - ID: %d, TTL: %d", kaResp.ID, kaResp.TTL))
|
|
79
|
-
// }
|
|
80
|
-
//}
|
|
81
|
-
//
|
|
82
|
-
//func grantAndKeepAliveLease(client *etcd.Client) (etcd.LeaseID, error) {
|
|
83
|
-
//
|
|
84
|
-
// lgResp, err := client.Grant(context.Background(), KEEP_ALIVE_TTL)
|
|
85
|
-
// if err != nil {
|
|
86
|
-
// util.MaoLog(util.ERROR, fmt.Sprintf("Fail to grant a new lease, err: %s", err))
|
|
87
|
-
// return -1, err
|
|
88
|
-
// }
|
|
89
|
-
//
|
|
90
|
-
// util.MaoLog(util.INFO, fmt.Sprintf("Granted lease ID: %d, TTL: %d", lgResp.ID, lgResp.TTL))
|
|
91
|
-
// go keepAliveLease(client, lgResp.ID)
|
|
92
|
-
//
|
|
93
|
-
// return lgResp.ID, nil
|
|
94
|
-
//}
|
|
95
|
-
//
|
|
96
|
-
//func createClient() (*etcd.Client, error) {
|
|
97
|
-
// client, err := etcd.New(etcd.Config{
|
|
98
|
-
// Endpoints: []string{"pi-dpdk.maojianwei.com:2379"},
|
|
99
|
-
// DialTimeout: 3 * time.Second,
|
|
100
|
-
// })
|
|
101
|
-
// if err != nil {
|
|
102
|
-
// util.MaoLog(util.ERROR, fmt.Sprintf("Fail to generate client, err: %s", err))
|
|
103
|
-
// return nil, err
|
|
104
|
-
// }
|
|
105
|
-
// return client, nil
|
|
106
|
-
// //defer client.Close()
|
|
107
|
-
//
|
|
108
|
-
//}
|
|
109
|
-
//
|
|
110
|
-
//func announceNodeInfo(client *etcd.Client, lease etcd.LeaseID, hostname string, addrs []string) {
|
|
111
|
-
// addrStr := ""
|
|
112
|
-
// for _, addr := range addrs {
|
|
113
|
-
// addrStr = fmt.Sprintf("%s%s,", addrStr, addr)
|
|
114
|
-
// }
|
|
115
|
-
//
|
|
116
|
-
// addrBytes := []byte(addrStr)
|
|
117
|
-
// addrBytes = addrBytes[:len(addrBytes)-1]
|
|
118
|
-
//
|
|
119
|
-
// addrStr = string(addrBytes)
|
|
120
|
-
//
|
|
121
|
-
// if last_hostname != hostname || last_addrStr != addrStr {
|
|
122
|
-
// putKV(client, lease, fmt.Sprintf("/node/%s/addrs", hostname), addrStr)
|
|
123
|
-
// }
|
|
124
|
-
// putKV(client, lease, fmt.Sprintf("/node/%s/lastseen", hostname), time.Now().String())
|
|
125
|
-
//
|
|
126
|
-
// last_hostname = hostname
|
|
127
|
-
// last_addrStr = addrStr
|
|
128
|
-
//}
|
|
129
|
-
//
|
|
130
|
-
//func main() {
|
|
131
|
-
// client, err := createClient()
|
|
132
|
-
// if err != nil {
|
|
133
|
-
// return
|
|
134
|
-
// }
|
|
135
|
-
// defer func(client *etcd.Client) {
|
|
136
|
-
// err := client.Close()
|
|
137
|
-
// if err != nil {
|
|
138
|
-
// util.MaoLog(util.WARN, fmt.Sprintf("Fail to close client, err: %s", err))
|
|
139
|
-
// }
|
|
140
|
-
// }(client)
|
|
141
|
-
//
|
|
142
|
-
// lease, err := grantAndKeepAliveLease(client)
|
|
143
|
-
// if err != nil {
|
|
144
|
-
// return
|
|
145
|
-
// }
|
|
146
|
-
//
|
|
147
|
-
// go watchKeyPrefix(client, "/node")
|
|
148
|
-
// time.Sleep(1 * time.Second)
|
|
149
|
-
//
|
|
150
|
-
// for {
|
|
151
|
-
// hostname, err := util.GetHostname()
|
|
152
|
-
// if err != nil {
|
|
153
|
-
// return
|
|
154
|
-
// }
|
|
155
|
-
//
|
|
156
|
-
// addrs, err := util.GetUnicastIp()
|
|
157
|
-
// if err != nil {
|
|
158
|
-
// return
|
|
159
|
-
// }
|
|
160
|
-
//
|
|
161
|
-
// announceNodeInfo(client, lease, hostname, addrs)
|
|
162
|
-
//
|
|
163
|
-
// time.Sleep(5 * time.Second)
|
|
164
|
-
// }
|
|
165
|
-
//}
|