-
Notifications
You must be signed in to change notification settings - Fork 232
/
cogent.go
196 lines (184 loc) · 4.5 KB
/
cogent.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
// Package lg provides looking glass methods for selected looking glasses
// Cogent Carrier Looking Glass ASN 174
package lg
import (
"bufio"
"errors"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"sort"
"strings"
)
// A Cogent represents a telia looking glass request
type Cogent struct {
Host string
IPv string
Node string
Nodes []string
}
var (
cogentNodes = map[string]string{}
cogentBGPNodes = map[string]string{}
cogentDefaultNode = "US - Los Angeles"
)
// Set configures host and ip version
func (p *Cogent) Set(host, version string) {
p.Host = host
p.IPv = version
if p.Node == "" {
p.Node = cogentDefaultNode
}
}
// GetDefaultNode returns telia default node
func (p *Cogent) GetDefaultNode() string {
return cogentDefaultNode
}
// GetNodes returns all Cogent nodes (US and International)
func (p *Cogent) GetNodes() []string {
// Memory cache
if len(p.Nodes) > 1 {
return p.Nodes
}
cogentNodes, cogentBGPNodes = p.FetchNodes()
var nodes []string
for node := range cogentNodes {
nodes = append(nodes, node)
}
sort.Strings(nodes)
p.Nodes = nodes
return nodes
}
// ChangeNode set new requested node
func (p *Cogent) ChangeNode(node string) bool {
// Validate
for _, n := range p.Nodes {
if node == n {
p.Node = node
return true
}
}
return false
}
// Ping tries to connect Cogent's ping looking glass through HTTP
// Returns the result
func (p *Cogent) Ping() (string, error) {
// Basic validate
if p.Node == "NA" || len(p.Host) < 5 {
print("Invalid node or host/ip address")
return "", errors.New("error")
}
var cmd = "P4"
if p.IPv == "ipv6" {
cmd = "P6"
}
resp, err := http.PostForm("http://www.cogentco.com/lookingglass.php",
url.Values{"FKT": {"go!"}, "CMD": {cmd}, "DST": {p.Host}, "LOC": {cogentNodes[p.Node]}})
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", errors.New("error: level3 looking glass is not available")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
r, _ := regexp.Compile(`<pre>(?s)(.*?)</pre>`)
b := r.FindStringSubmatch(string(body))
if len(b) > 0 {
return b[1], nil
}
return "", errors.New("error")
}
// Trace gets traceroute information from Cogent
func (p *Cogent) Trace() chan string {
c := make(chan string)
var cmd = "T4"
if p.IPv == "ipv6" {
cmd = "T6"
}
resp, err := http.PostForm("http://www.cogentco.com/lookingglass.php",
url.Values{"FKT": {"go!"}, "CMD": {cmd}, "DST": {p.Host}, "LOC": {cogentNodes[p.Node]}})
if err != nil {
println(err)
}
go func() {
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
l := scanner.Text()
m, _ := regexp.MatchString(`^(traceroute|\s*\d{1,2})`, l)
if m {
l = replaceASNTrace(l)
c <- l
}
}
close(c)
}()
return c
}
// BGP gets bgp information from cogent
func (p *Cogent) BGP() chan string {
c := make(chan string)
if _, ok := cogentBGPNodes[p.Node]; !ok {
println("current node doesn't support bgp, please select one of the below nodes:")
go func() {
for n := range cogentBGPNodes {
println(n)
}
close(c)
}()
return c
}
resp, err := http.PostForm("http://www.cogentco.com/lookingglass.php",
url.Values{"FKT": {"go!"}, "CMD": {"BGP"}, "DST": {p.Host}, "LOC": {cogentBGPNodes[p.Node]}})
if err != nil {
println(err)
}
go func() {
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
l := scanner.Text()
c <- l
}
close(c)
}()
return c
}
//FetchNodes returns all available nodes through HTTP
func (p *Cogent) FetchNodes() (map[string]string, map[string]string) {
var (
nodes = make(map[string]string, 100)
bgpNodes = make(map[string]string, 50)
)
resp, err := http.Get("http://www.cogentco.com/lookingglass.php")
if err != nil {
println("error: cogent looking glass unreachable (1)")
return map[string]string{}, map[string]string{}
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
println("error: cogent looking glass unreachable (2)" + err.Error())
return map[string]string{}, map[string]string{}
}
body := string(b)
// ping, trace nodes
i := strings.Index(body, "default:")
r, _ := regexp.Compile(`(?is)Option\("([\w|,|\s|-]+)","([\w|\d]+)"`)
f := r.FindAllStringSubmatch(body[i:], -1)
for _, v := range f {
nodes[v[1]] = v[2]
}
// bgp nodes
r, _ = regexp.Compile(`(?is)Option\("([\w|,|\s|-]+)","([\w|\d]+)"`)
f = r.FindAllStringSubmatch(body[:i], -1)
for _, v := range f {
bgpNodes[v[1]] = v[2]
}
return nodes, bgpNodes
}