-
Notifications
You must be signed in to change notification settings - Fork 17
/
probe.go
60 lines (47 loc) · 1.17 KB
/
probe.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
// SPDX-License-Identifier: LGPL-3.0-or-later
package probe
import (
"encoding/json"
"strconv"
)
const ipv6 int = 6
// Probe holds information about a single Atlas probe
type Probe struct {
ID int `json:"id"`
Asn4 int `json:"asn_v4"`
Asn6 int `json:"asn_v6"`
CountryCode string `json:"country_code"`
Geometry struct {
Coordinates []float64 `json:"coordinates"`
} `json:"geometry"`
}
// FromJSON parses json and returns a probe
func FromJSON(body []byte) (*Probe, error) {
var p Probe
err := json.Unmarshal(body, &p)
if err != nil {
return nil, err
}
return &p, nil
}
// ASNForIPVersion return the ASN for the given IP Version
func (p *Probe) ASNForIPVersion(v int) int {
if v == ipv6 {
return p.Asn6
}
return p.Asn4
}
// Longitude of the geo location of the probe
func (p *Probe) Longitude() string {
if len(p.Geometry.Coordinates) == 0 {
return ""
}
return strconv.FormatFloat(p.Geometry.Coordinates[0], 'f', 4, 64)
}
// Latitude of the geo location of the probe
func (p *Probe) Latitude() string {
if len(p.Geometry.Coordinates) == 0 {
return ""
}
return strconv.FormatFloat(p.Geometry.Coordinates[1], 'f', 4, 64)
}