-
Notifications
You must be signed in to change notification settings - Fork 39
/
utils.go
97 lines (76 loc) · 2.2 KB
/
utils.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
package main
import (
"encoding/hex"
"fmt"
"math"
"math/rand"
"strconv"
"strings"
"time"
)
func logf(format string, args ...interface{}) {
fmt.Printf(fmt.Sprintf("[%s] %s\n", time.Now().Format(time.StampMilli), format), args...)
}
func randStringN(n int) string {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
func interfaceName(left *Router, right *Router) string {
name := ""
if left.Configuration.ASN > 0 {
name = fmt.Sprintf("%d", left.Configuration.ASN)
} else {
name = fmt.Sprintf("%s", strings.TrimPrefix(left.Name, "ctn-"))
}
if right.Configuration.ASN > 0 {
name = fmt.Sprintf("%s-%d", name, right.Configuration.ASN)
} else {
name = fmt.Sprintf("%s-%s", name, strings.TrimPrefix(right.Name, "ctn-"))
}
return fmt.Sprintf("%s", name)
}
func delayGPS(source string, destination string) (int, error) {
src := strings.Split(source, "/")
lat1, err := strconv.ParseFloat(src[0], 64)
if err != nil {
return -1, err
}
lon1, err := strconv.ParseFloat(src[1], 64)
if err != nil {
return -1, err
}
dest := strings.Split(destination, "/")
lat2, err := strconv.ParseFloat(dest[0], 64)
if err != nil {
return -1, err
}
lon2, err := strconv.ParseFloat(dest[1], 64)
if err != nil {
return -1, err
}
radius := float64(6371)
dlat := (lat2 - lat1) / (180 / math.Pi)
dlon := (lon2 - lon1) / (180 / math.Pi)
a := math.Sin(dlat/2)*math.Sin(dlat/2) + math.Cos(lat1/(180*math.Pi))*math.Cos(lat2/(180*math.Pi))*math.Sin(dlon/2)*math.Sin(dlon/2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
d := radius * c
speed := float64(139)
return int(math.Floor(d/speed + 0.5)), nil
}
func macToLinkLocal(mac string) (string, error) {
if len(mac) != 17 {
return "", fmt.Errorf("Bad MAC address: %s", mac)
}
fields := strings.Split(mac, ":")
bytes, err := hex.DecodeString(fields[0])
if err != nil {
return "", fmt.Errorf("Bad MAC address: %s", mac)
}
bytes[0] = bytes[0] ^ 2
fields[0] = hex.EncodeToString(bytes)
return fmt.Sprintf("fe80::%s%s:%sff:fe%s:%s%s", fields[0], fields[1], fields[2], fields[3], fields[4], fields[5]), nil
}