-
Notifications
You must be signed in to change notification settings - Fork 1
/
traceroute.go
60 lines (52 loc) · 1.27 KB
/
traceroute.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
package controlsvc
import (
"context"
"fmt"
"strconv"
)
type (
TracerouteCommandType struct{}
TracerouteCommand struct {
target string
}
)
func (t *TracerouteCommandType) InitFromString(params string) (ControlCommand, error) {
if params == "" {
return nil, fmt.Errorf("no traceroute target")
}
c := &TracerouteCommand{
target: params,
}
return c, nil
}
func (t *TracerouteCommandType) InitFromJSON(config map[string]interface{}) (ControlCommand, error) {
target, ok := config["target"]
if !ok {
return nil, fmt.Errorf("no traceroute target")
}
targetStr, ok := target.(string)
if !ok {
return nil, fmt.Errorf("traceroute target must be string")
}
c := &TracerouteCommand{
target: targetStr,
}
return c, nil
}
func (c *TracerouteCommand) ControlFunc(ctx context.Context, nc NetceptorForControlCommand, _ ControlFuncOperations) (map[string]interface{}, error) {
cfr := make(map[string]interface{})
results := nc.Traceroute(ctx, c.target)
i := 0
for res := range results {
thisResult := make(map[string]interface{})
thisResult["From"] = res.From
thisResult["Time"] = res.Time
thisResult["TimeStr"] = fmt.Sprint(res.Time)
if res.Err != nil {
thisResult["Error"] = res.Err.Error()
}
cfr[strconv.Itoa(i)] = thisResult
i++
}
return cfr, nil
}