-
Notifications
You must be signed in to change notification settings - Fork 7
/
runner.go
266 lines (218 loc) · 4.35 KB
/
runner.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
package gomtr
import (
"bufio"
"errors"
"fmt"
"github.com/gogather/safemap"
"io"
"os/exec"
"strconv"
"strings"
"time"
)
const maxttls = 50
// service
type MtrService struct {
taskQueue *safemap.SafeMap
flag int64
index int64
in io.WriteCloser
out io.ReadCloser
outChan chan string
mtrPacketPath string
startAt string
}
// NewMtrService new a mtr service
// path - mtr-packet executable path
func NewMtrService(path string) *MtrService {
return &MtrService{
taskQueue: safemap.New(),
flag: 102400,
index: 0,
in: nil,
out: nil,
outChan: make(chan string, 1000),
mtrPacketPath: path,
}
}
// Start start service and wait mtr-packet stdio
func (ms *MtrService) Start() {
go ms.startup()
time.Sleep(time.Second)
}
func (ms *MtrService) startup() {
cmd := exec.Command(ms.mtrPacketPath)
var e error
ms.out, e = cmd.StdoutPipe()
if e != nil {
fmt.Println(e)
}
ms.in, e = cmd.StdinPipe()
if e != nil {
fmt.Println(e)
}
err, e := cmd.StderrPipe()
if e != nil {
fmt.Println(e)
}
// start sub process
if e := cmd.Start(); nil != e {
fmt.Printf("ERROR: %v\n", e)
}
// read data and put into result chan
go func() {
for {
// read lines
bio := bufio.NewReader(ms.out)
for {
output, isPrefix, err := bio.ReadLine()
if err != nil {
break
}
if string(output) != "" {
ms.outChan <- string(output)
}
if isPrefix {
break
}
}
}
}()
// get result from chan and parse
go func() {
for {
select {
case result := <-ms.outChan:
{
ms.parseTTLData(result)
}
}
}
}()
// error output
go func() {
for {
var readBytes []byte = make([]byte, 100)
err.Read(readBytes)
time.Sleep(time.Second)
}
}()
ms.startAt = fmt.Sprintf("Start: %s", getMtrStartTime())
// wait sub process
if e := cmd.Wait(); nil != e {
fmt.Printf("ERROR: %v\n", e)
}
}
// Request send a task request
// ip - the test ip
// c - repeat time, such as mtr tool argument c
// callback - just callback after task ready
func (ms *MtrService) Request(ip string, c int, callback func(interface{})) {
ms.index++
taskID := ms.index
if ms.index > ms.flag {
ms.index = 1
}
if c <= 0 {
c = 1
}
task := &MtrTask{
id: taskID,
callback: callback,
c: c,
ttlData: safemap.New(),
target: ip,
}
ms.taskQueue.Put(fmt.Sprintf("%d", taskID), task)
task.send(ms.in, taskID, ip, c)
}
func (ms *MtrService) GetServiceStartupTime() string {
return ms.startAt
}
func (ms *MtrService) parseTTLData(data string) {
segs := strings.Split(data, "\n")
for i := 0; i < len(segs); i++ {
item := strings.TrimSpace(segs[i])
if len(item) > 0 {
ms.parseTTLDatum(item)
}
}
}
func (ms *MtrService) parseTTLDatum(data string) {
hasNewline := strings.Contains(data, "\n")
if hasNewline {
fmt.Println(hasNewline)
}
segments := strings.Split(data, " ")
var ttlData *TTLData
var fullID int64
var ttlTime int64
var ttlerr error
var status string
var ipType string
var ip string
if len(segments) <= 0 {
return
}
if len(segments) > 0 {
idInt, err := strconv.Atoi(segments[0])
if err != nil {
idInt = 0
}
fullID = int64(idInt)
}
if len(segments) > 1 {
switch segments[1] {
case "command-parse-error", "no-reply", "probes-exhausted", "network-down", "permission-denied", "no-route", "invalid-argument", "feature-support":
{
ttlerr = errors.New(segments[1])
break
}
case "ttl-expired":
{
status = segments[1]
break
}
case "reply":
{
status = segments[1]
break
}
}
}
if len(segments) > 2 {
ipType = segments[2]
}
if len(segments) > 3 {
ip = segments[3]
}
if len(segments) > 5 {
ttlTimeInt, err := strconv.Atoi(segments[5])
if err != nil {
ttlTimeInt = 0
} else {
ttlTime = int64(ttlTimeInt)
}
}
ttlData = &TTLData{
TTLID: getTTLID(fullID),
ipType: ipType,
ip: ip,
err: ttlerr,
status: status,
raw: data,
time: ttlTime,
receivedTime: time.Now(),
}
// store
taskID := fmt.Sprintf("%d", getRealID(fullID))
taskRaw, ok := ms.taskQueue.Get(taskID)
var task *MtrTask = nil
if ok && taskRaw != nil {
task = taskRaw.(*MtrTask)
ttlID := getTTLID(fullID)
task.save(ttlID, ttlData)
} else {
return
}
}