-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
114 lines (97 loc) · 2.81 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
package main
import (
"github.com/rspamd/goasn/cachedir"
"github.com/rspamd/goasn/download"
"github.com/rspamd/goasn/iana"
"github.com/rspamd/goasn/ir"
"github.com/rspamd/goasn/log"
"github.com/rspamd/goasn/mrt"
"github.com/rspamd/goasn/sources"
"github.com/rspamd/goasn/zonefile"
"github.com/asergeyev/nradix"
flag "github.com/spf13/pflag"
"go.uber.org/zap"
)
const (
APP_NAME = "goasn"
)
var (
debug bool
downloadASN bool
downloadBGP bool
rejectFile string
zoneV4 string
zoneV6 string
)
func main() {
appCacheDir, err := cachedir.MakeCacheDir(APP_NAME)
if err != nil {
log.Logger.Fatal("failed to create cache directory",
zap.Error(err))
}
toRefresh := make([]string, 0)
if downloadASN {
toRefresh = append(toRefresh, sources.GetASNSources()...)
}
if downloadBGP {
toRefresh = append(toRefresh, sources.BGP_LATEST)
}
if !download.RefreshSources(appCacheDir, toRefresh) {
log.Logger.Warn("some sources failed to download")
}
IRDataFiles := sources.MustBasenames(sources.GetRIRASN())
asnToIRInfo, err := ir.ReadIRData(appCacheDir, IRDataFiles)
if err != nil {
log.Logger.Fatal("failed to read ASN info", zap.Error(err))
}
ianaASN, err := iana.ReadIANAASN(appCacheDir)
if err != nil {
log.Logger.Fatal("failed to read IANA ASN info", zap.Error(err))
}
var reservedV4 *nradix.Tree
var reservedV6 *nradix.Tree
if zoneV4 != "" {
var err error
reservedV4, err = iana.GetReservedIP4(appCacheDir)
if err != nil {
log.Logger.Fatal("failed to read IANA IP4 info", zap.Error(err))
}
} else {
reservedV4 = nradix.NewTree(0)
}
if zoneV6 != "" {
var err error
reservedV6, err = iana.GetReservedIP6(appCacheDir)
if err != nil {
log.Logger.Fatal("failed to read IANA IP6 info", zap.Error(err))
}
} else {
reservedV6 = nradix.NewTree(0)
}
bgpInfo := mrt.ASNFromBGP(appCacheDir, ianaASN, rejectFile, reservedV4, reservedV6)
if bgpInfo.Err != nil {
log.Logger.Fatal("failed to process MRT", zap.Error(bgpInfo.Err))
}
if bgpInfo.ParseErrorCount > 0 {
log.Logger.Error("MRT parsing errors occurred",
zap.Int("count", bgpInfo.ParseErrorCount),
zap.Any("errors", bgpInfo.ParseErrors))
}
err = zonefile.GenerateZones(asnToIRInfo, bgpInfo.V4, zoneV4, bgpInfo.V6, zoneV6)
if err != nil {
log.Logger.Fatal("failed to generate zone", zap.Error(err))
}
}
func init() {
flag.BoolVar(&debug, "debug", false, "enable debug logging")
flag.BoolVar(&downloadASN, "download-asn", false, "download RIR data")
flag.BoolVar(&downloadBGP, "download-bgp", false, "download MRT data")
flag.StringVar(&rejectFile, "reject", "", "path to write unparseable entries to")
flag.StringVar(&zoneV4, "file-v4", "", "path to V4 zonefile")
flag.StringVar(&zoneV6, "file-v6", "", "path to V6 zonefile")
flag.Parse()
err := log.SetupLogger(debug)
if err != nil {
panic(err)
}
}