-
Notifications
You must be signed in to change notification settings - Fork 233
/
telia.go
192 lines (180 loc) · 4.54 KB
/
telia.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
// Package lg provides looking glass methods for selected looking glasses
// Telia Carrier Looking Glass ASN 1299
package lg
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"sort"
)
// A Telia represents a telia looking glass request
type Telia struct {
Host string
IPv string
Node string
Nodes []string
}
var teliaDefaultNode = "Los Angeles"
// Set configures host and ip version
func (p *Telia) Set(host, version string) {
p.Host = host
p.IPv = version
if p.Node == "" {
p.Node = teliaDefaultNode
}
}
// GetDefaultNode returns telia default node
func (p *Telia) GetDefaultNode() string {
return teliaDefaultNode
}
// GetNodes returns all Telia nodes (US and International)
func (p *Telia) GetNodes() []string {
// Memory cache
if len(p.Nodes) > 1 {
return p.Nodes
}
var nodes []string
for node := range p.FetchNodes() {
nodes = append(nodes, node)
}
sort.Strings(nodes)
p.Nodes = nodes
return nodes
}
// ChangeNode set new requested node
func (p *Telia) ChangeNode(node string) {
var valid = false
// Validate
for _, n := range p.Nodes {
if node == n {
valid = true
break
}
}
if valid {
p.Node = node
} else {
p.Node = "NA"
println("Invalid node please press tab after node command to show the valid nodes")
}
}
// Ping tries to connect Telia's ping looking glass through HTTP
// Returns the result
func (p *Telia) Ping() (string, error) {
// Basic validate
if p.Node == "NA" || len(p.Host) < 5 {
print("Invalid node or host/ip address")
return "", errors.New("error")
}
resp, err := http.PostForm("http://looking-glass.telia.net/",
url.Values{"query": {"ping"}, "protocol": {p.IPv}, "addr": {p.Host}, "router": {p.Node}})
if err != nil {
println(err)
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
r, _ := regexp.Compile(`<CODE>(?s)(.*?)</CODE>`)
b := r.FindStringSubmatch(string(body))
if len(b) > 0 {
return b[1], nil
}
return "", errors.New("error")
}
// Trace gets traceroute information from Telia
func (p *Telia) Trace() chan string {
c := make(chan string)
resp, err := http.PostForm("http://looking-glass.telia.net/",
url.Values{"query": {"trace"}, "protocol": {p.IPv}, "addr": {p.Host}, "router": {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 informtion from Telia
func (p *Telia) BGP() chan string {
c := make(chan string)
resp, err := http.PostForm("http://looking-glass.telia.net/",
url.Values{"query": {"bgp"}, "protocol": {p.IPv}, "addr": {p.Host}, "router": {p.Node}})
if err != nil {
println(err)
}
go func() {
var (
parse = false
last string
)
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
l := scanner.Text()
l = sanitize(l)
if m, _ := regexp.MatchString("Telia Carrier", l); !parse && m {
parse = true
continue
}
if !parse || (l == last) {
continue
}
c <- l
last = l
}
close(c)
}()
return c
}
//FetchNodes returns all available nodes through HTTP
func (p *Telia) FetchNodes() map[string]string {
var nodes = make(map[string]string, 100)
resp, err := http.Get("http://looking-glass.telia.net/")
if err != nil {
println("error: telia looking glass unreachable (1) ")
return map[string]string{}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
println("error: telia looking glass unreachable (2)" + err.Error())
return map[string]string{}
}
r, _ := regexp.Compile(`(?i)<option value="(?s)([\w|\s|)(._-]+)"> (?s)([\w|\s|)(._-]+)`)
b := r.FindAllStringSubmatch(string(body), -1)
for _, v := range b {
nodes[v[1]] = v[2]
}
return nodes
}
//[GOOGLE (ARIN)" HREF="http://www.arin.net/cgi-bin/whois.pl?queryinput=15169" TARGET=_lookup>15169</A>] 1.261 ms 72.14.236.69 (72.14.236.69) [AS <A title="GOOGLE (ARIN)" HREF="http://www.arin.net/cgi-bin/whois.pl?queryinput=15169" TARGET=_lookup>15169</A>]
// replaceASNTrace
func replaceASNTrace(l string) string {
m, _ := regexp.MatchString(`\[AS\s+`, l)
if !m {
return l
}
r := regexp.MustCompile(`(?i)\[AS\s+<A\s+title="([a-z|\d|\s|\(\)_,-]+)"\s+HREF="[a-z|\/|:.-]+\?\w+=\d+"\s+\w+=_lookup>(\d+)</A>]`)
asn := r.FindStringSubmatch(l)
if len(asn) == 3 {
l = r.ReplaceAllString(l, fmt.Sprintf("[%s (%s)]", asn[1], asn[2]))
}
return l
}