-
Notifications
You must be signed in to change notification settings - Fork 4
/
source_ipfire.go
238 lines (217 loc) · 5.31 KB
/
source_ipfire.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package mmdbmeld
import (
"bufio"
"errors"
"fmt"
"io"
"net"
"net/textproto"
"os"
"strings"
)
// IPFireSource reads geoip data in the ipfire format.
type IPFireSource struct {
file string
reader *textproto.Reader
fieldMap map[string]string
types map[string]string
asOrgCache map[string]string
err error
}
// LoadIPFireSource returns a new IPFireSource.
func LoadIPFireSource(input DatabaseInput, types map[string]string) (*IPFireSource, error) {
file, err := os.Open(input.File)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
}
reader := textproto.NewReader(bufio.NewReader(file))
// Skip comment section.
for {
line, err := reader.ReadLine()
if err != nil {
continue
}
// Discard lines until the comments stop.
if !strings.HasPrefix(line, "#") {
break
}
}
return &IPFireSource{
file: input.File,
reader: reader,
fieldMap: input.FieldMap,
types: types,
asOrgCache: make(map[string]string),
}, nil
}
// Name returns an identifying name for the source.
func (ipf *IPFireSource) Name() string {
return ipf.file
}
// NextEntry returns the next entry of the source.
// If nil, nil is returned, stop reading and check Err().
func (ipf *IPFireSource) NextEntry() (*SourceEntry, error) {
// Check if there is an error, do not read if there is an error.
if ipf.err != nil {
return nil, nil //nolint:nilerr
}
// Read data sections.
for {
// Read next section.
data, err := ipf.reader.ReadMIMEHeader()
if err != nil {
ipf.err = err
return nil, nil //nolint:nilerr
}
// If the section is empty, continue to next.
if len(data) == 0 {
continue
}
// Parse data.
se, err := ipf.SourceEntryFromMimeHeader(data)
if err != nil {
return nil, fmt.Errorf("failed to parse ipfire entry: %w", err)
}
asNum, ok1 := se.Values[ipf.fieldMap["aut-num"]]
asOrg, ok2 := se.Values[ipf.fieldMap["name"]]
// Check for ASN->ASOrg mapping.
if se.Net == nil {
if ok1 && ok2 {
ipf.asOrgCache[asNum.Value] = asOrg.Value
}
continue
}
// Fill in ASOrg if missing.
if ok1 && !ok2 {
asOrgKey := ipf.fieldMap["name"]
if asOrgKey != "" {
asOrg := ipf.asOrgCache[asNum.Value]
if asOrg != "" {
se.Values[asOrgKey] = SourceValue{
Type: ipf.types[asOrgKey],
Value: asOrg,
}
}
}
}
return se, nil
}
}
// Err returns the processing error encountered by the source.
func (ipf *IPFireSource) Err() error {
switch {
case ipf.err == nil:
return nil
case errors.Is(ipf.err, io.EOF):
return nil
default:
return ipf.err
}
}
// SourceEntryFromMimeHeader parses an IPFire entry.
// The keys in the IPFire file are:
// - net
// - aut-num
// - name
// - country
// - is-anycast
// - is-satellite-provider
// - is-anonymous-proxy
// - drop
// .
func (ipf *IPFireSource) SourceEntryFromMimeHeader(data textproto.MIMEHeader) (*SourceEntry, error) {
se := &SourceEntry{
Values: make(map[string]SourceValue),
}
// Parse Network.
if netData := data.Get("net"); netData != "" {
_, ipNet, err := net.ParseCIDR(netData)
if err != nil {
return nil, fmt.Errorf("failed to parse net %s: %w", netData, err)
}
se.Net = ipNet
}
// Parse AS.
if fieldName, ok := ipf.fieldMap["aut-num"]; ok {
if asNum := data.Get("aut-num"); asNum != "" {
asNum := strings.TrimPrefix(asNum, "AS")
fieldType, ok := ipf.types[fieldName]
if ok && fieldType != "" && fieldType != "-" {
se.Values[fieldName] = SourceValue{
Type: fieldType,
Value: asNum,
}
}
}
}
// Parse AS Org.
if fieldName, ok := ipf.fieldMap["name"]; ok {
if asOrg := data.Get("name"); asOrg != "" {
fieldType, ok := ipf.types[fieldName]
if ok && fieldType != "" && fieldType != "-" {
se.Values[fieldName] = SourceValue{
Type: fieldType,
Value: asOrg,
}
}
}
}
// Parse country.
if fieldName, ok := ipf.fieldMap["country"]; ok {
if country := data.Get("country"); len(country) == 2 {
fieldType, ok := ipf.types[fieldName]
if ok && fieldType != "" && fieldType != "-" {
se.Values[fieldName] = SourceValue{
Type: fieldType,
Value: country,
}
}
}
}
// Parse flags.
if fieldName, ok := ipf.fieldMap["is-anycast"]; ok {
if flag := data.Get("is-anycast"); flag != "" {
fieldType, ok := ipf.types[fieldName]
if ok && fieldType != "" && fieldType != "-" {
se.Values[fieldName] = SourceValue{
Type: fieldType,
Value: "true",
}
}
}
}
if fieldName, ok := ipf.fieldMap["is-satellite-provider"]; ok {
if flag := data.Get("is-satellite-provider"); flag != "" {
fieldType, ok := ipf.types[fieldName]
if ok && fieldType != "" && fieldType != "-" {
se.Values[fieldName] = SourceValue{
Type: fieldType,
Value: "true",
}
}
}
}
if fieldName, ok := ipf.fieldMap["is-anonymous-proxy"]; ok {
if flag := data.Get("is-anonymous-proxy"); flag != "" {
fieldType, ok := ipf.types[fieldName]
if ok && fieldType != "" && fieldType != "-" {
se.Values[fieldName] = SourceValue{
Type: fieldType,
Value: "true",
}
}
}
}
if fieldName, ok := ipf.fieldMap["drop"]; ok {
if flag := data.Get("drop"); flag != "" {
fieldType, ok := ipf.types[fieldName]
if ok && fieldType != "" && fieldType != "-" {
se.Values[fieldName] = SourceValue{
Type: fieldType,
Value: "true",
}
}
}
}
return se, nil
}