forked from mehrdadrad/mylg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkpn.go
184 lines (171 loc) · 3.98 KB
/
kpn.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
// Package lg provides looking glass methods for selected looking glasses
// KPN Looking Glass ASN 1299
package lg
import (
"bufio"
"errors"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/signal"
"regexp"
"sort"
)
// A KPN represents a KPN looking glass request
type KPN struct {
Host string
IPv string
Node string
Nodes []string
}
var KPNDefaultNode = "Amsterdam (NL)"
// Set configures host and ip version
func (p *KPN) Set(host, version string) {
p.Host = host
p.IPv = version
if p.Node == "" {
p.Node = KPNDefaultNode
}
}
// GetDefaultNode returns KPN default node
func (p *KPN) GetDefaultNode() string {
return KPNDefaultNode
}
// GetNodes returns all KPN nodes (US and International)
func (p *KPN) 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 *KPN) ChangeNode(node string) bool {
// Validate
for _, n := range p.Nodes {
if node == n {
p.Node = node
return true
}
}
return false
}
// Ping tries to connect KPN's ping looking glass through HTTP
// Returns the result
func (p *KPN) 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://lg.eurorings.net/index.cgi",
url.Values{"query": {"ping"}, "protocol": {p.IPv}, "addr": {p.Host}, "router": {p.Node}})
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", errors.New("error: KPN looking glass is not available")
}
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 KPN
func (p *KPN) Trace() chan string {
c := make(chan string)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
resp, err := http.PostForm("http://lg.eurorings.net/index.cgi",
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)
LOOP:
for scanner.Scan() {
l := scanner.Text()
m, _ := regexp.MatchString(`^(traceroute|\s*\d{1,2})`, l)
if m {
l = replaceASNTrace(l)
select {
case <-sigCh:
break LOOP
case c <- l:
}
}
}
signal.Stop(sigCh)
close(c)
}()
return c
}
// BGP gets bgp information from KPN
func (p *KPN) BGP() chan string {
c := make(chan string)
resp, err := http.PostForm("http://lg.eurorings.net/index.cgi",
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("Location:", 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 *KPN) FetchNodes() map[string]string {
var nodes = make(map[string]string, 100)
resp, err := http.Get("http://lg.eurorings.net/index.cgi")
if err != nil {
println("error: KPN looking glass unreachable (1) ")
return map[string]string{}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
println("error: KPN 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
}