-
Notifications
You must be signed in to change notification settings - Fork 3
/
go_traceroute.go
216 lines (193 loc) · 5.44 KB
/
go_traceroute.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"bufio"
"fmt"
//"io"
"net"
"os"
"os/exec"
"regexp"
"strings"
//"time"
)
func main() {
fmt.Printf("About to Run Traceroutes \n")
readFile("oix-full-snapshot-latest.dat")
//oix-full-snapshot-latest.dat
//time.Sleep(10 * time.Second)
ipChan := make(chan []string)
//asChan := make(chan []string)
waitChan := make(chan int)
maxPerIteration := 100;
iteration := 4000
waitCount := maxPerIteration // * iteraion
currCount := 0
var tablePos int = len(routingTableList)
for j := 0; j < iteration; j++ {
for i := tablePos; i > tablePos - maxPerIteration; i-- {
go runTraceroute(routingTableList[i - 1], ipChan, waitChan)
//fmt.Printf("%s\n", routingTableList[i - 1])
}
for i := 0; i < waitCount - 10; i++ {
fmt.Printf("%d\n", currCount)
traceRouteRaw := <- ipChan
fmt.Printf("%q\n", traceRouteRaw)
go makeAsPath(traceRouteRaw)
//fmt.Printf("%q\n", <- asChan)
currCount += <- waitChan
}
//time.Sleep(10 * time.Second)
tablePos = tablePos - maxPerIteration
}
for i := 0; i < iteration * 10; i++ {
fmt.Printf("%d\n", currCount)
traceRouteRaw := <- ipChan
fmt.Printf("%q\n", traceRouteRaw)
go makeAsPath(traceRouteRaw)
//fmt.Printf("%q\n", <- asChan)
currCount += <- waitChan
}
close(ipChan)
close(waitChan)
//fmt.Printf("%q\n", routingTableList)
fmt.Printf("Traceroutes Done \n")
}
var routingTableList = make([]string, 1)
var prevAsn string
func processResults(c chan []string) {}
func makeAsPath(traceRouteRaw []string) {
var asPath = make([]string, 0)
prevAsn := ""
for i := 0; i < len(traceRouteRaw); i++ {
tempAsString := strings.Split(traceRouteRaw[i], ",")
asn := tempAsString[2]
if asn == prevAsn {
//do nothing
} else {
asPath = append(asPath, asn)
}
prevAsn = asn
}
fmt.Printf("%q\n", asPath)
//asChan <- asPath
return
}
func runTraceroute(targetHost string, ipChan chan []string, waitChan chan int) {
app := "traceroute"
arg0 := "-f" //-f 2
arg1 := "1" //first ttl
arg2 := "-m"
arg3 := "25" //max ttl
arg4 := "-q"
arg5 := "3" //number of tries per hop
arg6 := "-w"
arg7 := "2" //wait time (in seconds)
arg8 := "-n" //no dns resolution
arg9 := "64" //packet size, bytes
cmd := exec.Command(app, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, targetHost, arg9) //execute the commands above, with the host passed
out, err := cmd.Output() //output the results of the command
if err != nil {
fmt.Printf(err.Error())
return
}
//fmt.Printf("%s\n", string(out))
traceResult := findIPaddr(string(out))
//fmt.Printf("%q\n", traceResult)
var ipArray = make([]string, 0)
dnsPtr := ""
asnResult := ""
for i := 2; i < len(traceResult); i++ {
currIpAddr := traceResult[i][0]
reverseDNSaddr, _ := net.LookupAddr(currIpAddr)
if len(reverseDNSaddr) > 0 {
//fmt.Printf("%s\n", reverseDNSaddr[0])
dnsPtr = reverseDNSaddr[0] //if it has a PTR, put it in the PTR array section
} else {
dnsPtr = "noPTR" //everything that does not have a PTR listed
}
reverseIPaddrResult := reverseIPaddr(currIpAddr) //call the function to reverse the IP address
dnsQueryAddress := reverseIPaddrResult + ".origin.asn.cymru.com" //query team cymru database for IP to ASN
ipToAsnLookup, _ := net.LookupTXT(dnsQueryAddress) //get TXT record result
if len(ipToAsnLookup) > 0 {
asnIndex := strings.Index(ipToAsnLookup[0], " ") //if so, parse out the ASN from the result
queryString := ipToAsnLookup[0]
asnResult = queryString[0:asnIndex]
} else {
asnResult = "noASN"
}
ipRowString := currIpAddr + "," + dnsPtr + "," + asnResult
ipArray = append(ipArray, ipRowString)
}
//fmt.Printf("%d\n", len(traceIpArray))
//fmt.Printf("%q\n", ipArray)
ipChan <- ipArray
waitChan <- 1
return
}
func reverseIPaddr(ip string) string {
ipOctetArray := strings.Split(ip, ".")
var reversedOctetArray [4]string
count := 0
for i := range ipOctetArray {
reversedOctetArray[count] = ipOctetArray[len(ipOctetArray)-1-i]
count++
}
reversedIPaddr := reversedOctetArray[0] + "." + reversedOctetArray[1] + "." + reversedOctetArray[2] + "." + reversedOctetArray[3]
return reversedIPaddr
}
func findIPaddr(inputText string) [][]string {
re, err := regexp.Compile(`[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+`) //basic IP match in regex. All IPS in traceroute should be valid
if err != nil {
fmt.Printf(err.Error())
}
ipArray := re.FindAllStringSubmatch(inputText, -1)
return ipArray
}
var prevIpAddr string
func buildIpArray(ipAddr string) []string {
if ipAddr == prevIpAddr {
return routingTableList
}
prevIpAddr = ipAddr
//fmt.Printf("%s\n", ipAddr)
routingTableList = append(routingTableList, ipAddr)
//fmt.Printf("%q\n", routingTableList)
return routingTableList
}
func parseFileLine(fileLine string) {
ipArray := findIPaddr(fileLine)
if len(ipArray) > 0 {
ipString := ipArray[0][0]
//fmt.Printf("%s\n", ipString)
buildIpArray(ipString)
} else {
fmt.Printf("err_empty_array\n")
}
}
func readFile(fileName string) {
fi, err := os.Open(fileName)
if err != nil {
fmt.Printf(err.Error())
}
scanner := bufio.NewScanner(fi)
for scanner.Scan() {
parseFileLine(scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Printf(err.Error())
}
}
/*
func writeLines(line string, path string) {
f, err := os.OpenFile(path, os.O_APPEND, 0666)
if err != nil {
fmt.Printf(err.Error())
}
_, err = io.WriteString(f, line)
if err != nil {
fmt.Printf(err.Error())
}
f.Close()
return
}
*/