-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
163 lines (145 loc) · 3.93 KB
/
main.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
/*
* ip as-path regex builder
* Jörg Kost, [email protected], [email protected]
*
*/
package main
import (
"./numberRange"
"encoding/csv"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"sort"
"strconv"
"strings"
"sync"
)
var NicToASN map[string][]string
var fmtAsPathACL = "ip as-path access-list %s %s %s$\n"
/* Command line parameters */
var flagPermitOrDeny int
var flagAclTitle, flagFilename, flagNicRegion, asnFile string
var flagSummaryOnly bool
var flagNICsParsed []string
var PermitOrDenyArr [2]string = [2]string{"deny", "permit"}
var asnList []string = []string{
"http://www.iana.org/assignments/as-numbers/as-numbers-2.csv",
"http://www.iana.org/assignments/as-numbers/as-numbers-1.csv"}
func init() {
NicToASN = make(map[string][]string)
flag.StringVar(&flagNicRegion, "region", "", "Comma separated list with region for generated prefix")
flag.StringVar(&flagAclTitle, "acltitle", "region-summary", "Title for generated as-path list")
flag.BoolVar(&flagSummaryOnly, "summary", false, "Print summary of downloaded lists only")
flag.IntVar(&flagPermitOrDeny, "permitOrDeny", 1, "Deny = 0, Permit = 1")
flag.StringVar(&flagFilename, "filename", "", "Output file, else stdout")
flag.StringVar(&asnFile, "custom", "", "file with custom ASNs for list-generation")
flag.Parse()
flagNICsParsed = strings.Split(flagNicRegion, ",")
}
func main() {
var wg sync.WaitGroup
var mt sync.Mutex
if asnFile != "" {
f, err := os.Open(asnFile)
if err != nil {
log.Fatalf("Cant open as number file %s: %s", asnFile, err)
}
mapAsnToNic(f)
defer f.Close()
}
if asnFile == "" {
for _, v := range asnList {
wg.Add(1)
go func(asnURI string) {
defer wg.Done()
// log.Printf("downloading %s\n", asnURI)
resp, err := http.Get(asnURI)
if err != nil {
log.Fatal("Cant open as numbers from IANA")
}
mt.Lock()
mapAsnToNic(resp.Body)
mt.Unlock()
resp.Body.Close()
}(v)
}
}
wg.Wait()
if flagSummaryOnly == true || len(flagNICsParsed) == 0 {
printSummary()
} else {
if flagFilename == "" {
generatePrefixList(os.Stdout)
} else {
outputStream, err := os.Create(flagFilename)
if err != nil {
log.Fatal(err)
}
generatePrefixList(outputStream)
outputStream.Close()
}
}
}
func printSummary() {
for k := range NicToASN {
log.Printf("%s [%d table entries]\n", k, len(NicToASN[k]))
}
}
func generatePrefixList(outputStream io.Writer) {
var prefixLists []string
for _, nic := range flagNICsParsed {
for _, v := range NicToASN[nic] {
if strings.Contains(v, "-") {
rangeSplit := strings.Split(v, "-")
start, err := strconv.Atoi(rangeSplit[0])
if err != nil {
panic(err)
}
end, err := strconv.Atoi(rangeSplit[1])
if err != nil {
panic(err)
}
if start == end {
prefixLists = append(prefixLists, fmt.Sprintf(fmtAsPathACL, flagAclTitle, PermitOrDenyArr[flagPermitOrDeny],
"_"+strconv.Itoa(start)))
} else {
prefixLists = append(prefixLists, fmt.Sprintf(fmtAsPathACL, flagAclTitle, PermitOrDenyArr[flagPermitOrDeny],
numberRange.GetRegex(start, end)))
}
} else {
prefixLists = append(prefixLists, fmt.Sprintf(fmtAsPathACL, flagAclTitle, PermitOrDenyArr[flagPermitOrDeny], "_"+v))
}
}
}
sort.Slice(prefixLists,
func(i, j int) bool {
return prefixLists[i] < prefixLists[j]
})
for _, v := range prefixLists {
fmt.Fprint(outputStream, v)
}
}
func mapAsnToNic(ASN io.ReadCloser) {
r := csv.NewReader(ASN)
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal( err)
}
if strings.HasPrefix(record[1], "Assigned by ") {
nic := strings.TrimPrefix(record[1], "Assigned by ")
NicToASN[nic] = append(NicToASN[nic], record[0])
} else if strings.HasPrefix(record[1], "Reserved") || strings.HasPrefix(record[1], "Unallocated") {
NicToASN["bogons"] = append(NicToASN["bogons"], record[0])
} else {
NicToASN["custom"] = append(NicToASN["custom"], record[0])
}
}
}