-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
356 lines (299 loc) · 8.91 KB
/
index.js
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// determine the ASN of the connecting IP
const dns = require('node:dns').promises
const fs = require('node:fs/promises')
const path = require('node:path')
let test_ip = '66.128.51.163'
const providers = []
let conf_providers = [
'origin.asn.cymru.com',
'asn.routeviews.org',
'asn.rspamd.com',
]
exports.register = async function () {
this.registered = false
this.load_asn_ini()
await this.test_and_register_dns_providers()
await this.test_and_register_geoip()
if (this.cfg.header.asn) {
this.register_hook('data_post', 'add_header_asn')
}
if (this.cfg.header.provider) {
this.register_hook('data_post', 'add_header_provider')
}
}
exports.test_and_register_dns_providers = async function () {
if (!this.cfg.protocols.dns) return // disabled in config
for (const zone of conf_providers) {
try {
const res = await this.get_dns_results(zone, test_ip)
if (!res) {
this.logerror(this, `${zone} failed`)
continue
}
this.logdebug(this, `${zone} succeeded`)
if (!providers.includes(zone)) providers.push(zone)
if (this.registered) continue
this.registered = true
this.register_hook('lookup_rdns', 'lookup_via_dns')
} catch (err) {
this.logerror(this, `zone ${zone} encountered ${err.message}`)
}
}
return providers
}
exports.load_asn_ini = function () {
const plugin = this
plugin.cfg = plugin.config.get(
'asn.ini',
{
booleans: [
'+header.asn',
'-header.provider',
'+protocols.dns',
'+protocols.geoip',
],
},
function () {
plugin.load_asn_ini()
},
)
const c = plugin.cfg
if (c.main.providers !== undefined) {
if (c.main.providers === '') {
conf_providers = []
} else {
conf_providers = c.main.providers.split(/[\s,;]+/)
}
}
if (c.main.test_ip) test_ip = c.main.test_ip
}
exports.get_dns_results = async function (zone, ip) {
const query = `${ip.split('.').reverse().join('.')}.${zone}`
const timeout = (prom, time, exception) => {
let timer
return Promise.race([
prom,
new Promise((_r, rej) => (timer = setTimeout(rej, time, exception))),
]).finally(() => clearTimeout(timer))
}
try {
const addrs = await timeout(
dns.resolveTxt(query),
(this.cfg.main.timeout || 4) * 1000,
new Error(`${zone} timeout`),
)
if (!addrs || !addrs[0]) {
this.logerror(this, `no results for ${query}`)
return
}
const first = addrs[0]
this.logdebug(this, `${zone} answers: ${first}`)
return this.get_result(zone, first)
} catch (err) {
this.logerror(this, `error: ${err} running: ${query}`)
}
}
exports.get_result = function (zone, first) {
switch (zone) {
case 'origin.asn.cymru.com':
return this.parse_cymru(first.join(''))
case 'asn.routeviews.org':
return this.parse_routeviews(first)
case 'asn.rspamd.com':
return this.parse_rspamd(first.join(''))
case 'origin.asn.spameatingmonkey.net':
return this.parse_monkey(first.join(''))
}
this.logerror(this, `unrecognized ASN provider: ${zone}`)
return
}
exports.lookup_via_dns = function (next, connection) {
if (connection.remote.is_private) return next()
const promises = []
for (const zone of providers) {
promises.push(
new Promise((resolve) => {
// connection.logdebug(plugin, `zone: ${zone}`);
try {
this.get_dns_results(zone, connection.remote.ip).then((r) => {
if (!r) return resolve()
const results = { emit: true }
// store asn & net from any source
if (r.asn) results.asn = r.asn
if (r.net) results.net = r.net
// store provider specific results
switch (zone) {
case 'origin.asn.cymru.com':
results.cymru = r
break
case 'asn.routeviews.org':
results.routeviews = r
break
case 'origin.asn.spameatingmonkey.net':
results.monkey = r
break
case 'asn.rspamd.com':
results.rspamd = r
break
}
connection.results.add(this, results)
resolve(results)
})
} catch (err) {
connection.results.add(this, { err })
resolve()
}
}),
)
}
Promise.all(promises).then(next)
}
exports.parse_routeviews = function (thing) {
let labels
if (typeof thing === 'string' && /,/.test(thing)) {
labels = thing.split(',')
return { asn: labels[0], net: `${labels[1]}/${labels[2]}` }
}
// this is a correct result (node >= 0.10.26)
// 99.177.75.208.asn.routeviews.org. IN TXT "40431" "208.75.176.0" "21"
if (Array.isArray(thing)) {
labels = thing
} else {
// this is what node (< 0.10.26) returns
// 99.177.75.208.asn.routeviews.org. IN TXT "40431208.75.176.021"
labels = thing.split(/ /)
}
if (labels.length !== 3) {
this.logerror(
this,
`result length not 3: ${labels.length} string="${thing}"`,
)
return
}
return { asn: labels[0], net: `${labels[1]}/${labels[2]}` }
}
exports.parse_cymru = function (str) {
const r = str.split(/\s+\|\s*/)
// 99.177.75.208.origin.asn.cymru.com. 14350 IN TXT
// "40431 | 208.75.176.0/21 | US | arin | 2007-03-02"
// "10290 | 12.129.48.0/24 | US | arin |"
if (r.length < 4) {
this.logerror(this, `cymru: bad result length ${r.length} string="${str}"`)
return
}
return { asn: r[0], net: r[1], country: r[2], assignor: r[3], date: r[4] }
}
exports.parse_monkey = function (str) {
const plugin = this
const r = str.split(/\s+\|\s+/)
// "74.125.44.0/23 | AS15169 | Google Inc. | 2000-03-30"
// "74.125.0.0/16 | AS15169 | Google Inc. | 2000-03-30 | US"
if (r.length < 3) {
plugin.logerror(
plugin,
`monkey: bad result length ${r.length} string="${str}"`,
)
return
}
return {
asn: r[1].substring(2),
net: r[0],
org: r[2],
date: r[3],
country: r[4],
}
}
exports.parse_rspamd = function (str) {
const plugin = this
const r = str.split(/\s*\|\s*/)
// 8.8.8.8.asn.rspamd.com. 14350 IN TXT
// "15169|8.8.8.0/24|US|arin|"
if (r.length < 4) {
plugin.logerror(
plugin,
`rspamd: bad result length ${r.length} string="${str}"`,
)
return
}
return { asn: r[0], net: r[1], country: r[2], assignor: r[3], date: r[4] }
}
exports.add_header_asn = function (next, connection) {
const asn = connection.results.get('asn')
if (!asn?.asn) return next()
if (!connection.transaction) return next()
if (asn.net) {
connection.transaction.add_header('X-Haraka-ASN', `${asn.asn} ${asn.net}`)
} else {
connection.transaction.add_header('X-Haraka-ASN', `${asn.asn}`)
}
if (asn.org) {
connection.transaction.add_header('X-Haraka-ASN-Org', `${asn.org}`)
}
next()
}
exports.add_header_provider = function (next, connection) {
const asn = connection.results.get('asn')
if (!asn?.asn) return next()
for (const p in asn) {
if (!asn[p].asn) continue // ignore non-object results
const name = `X-Haraka-ASN-${p.toUpperCase()}`
const values = []
for (const k in asn[p]) {
values.push(`${k}=${asn[p][k]}`)
}
if (values.length === 0) continue
connection.transaction.add_header(name, values.join(' '))
}
next()
}
exports.test_and_register_geoip = async function () {
if (!this.cfg.protocols.geoip) return // disabled in config
try {
this.maxmind = require('maxmind')
if (await this.load_dbs()) {
this.register_hook('connect', 'lookup_via_maxmind')
}
} catch (e) {
this.logerror(e)
this.logerror(
"unable to load maxmind, try\n\n\t'npm install -g [email protected]'\n\n",
)
}
}
exports.load_dbs = async function () {
this.dbsLoaded = 0
const dbdir = this.cfg.main.dbdir || '/usr/local/share/GeoIP/'
const dbPath = path.join(dbdir, `GeoLite2-ASN.mmdb`)
try {
await fs.access(dbPath)
this.lookup = await this.maxmind.open(dbPath, {
// this causes tests to hang, which is why mocha runs with --exit
watchForUpdates: true,
cache: {
max: 1000, // max items in cache
maxAge: 1000 * 60 * 60, // life time in milliseconds
},
})
this.loginfo(`loaded maxmind db ${dbPath}`)
this.dbsLoaded++
} catch (e) {
console.error(e)
this.loginfo(`missing [access to] DB ${dbPath}`)
}
return this.dbsLoaded
}
exports.lookup_via_maxmind = function (next, connection) {
if (!this.maxmind || !this.dbsLoaded) return next()
const asn = this.lookup.get(connection.remote.ip)
if (asn?.autonomous_system_number || asn?.autonomous_system_organization) {
connection.results.add(this, {
...(asn.autonomous_system_number
? { asn: asn.autonomous_system_number }
: {}),
...(asn.autonomous_system_organization
? { org: asn.autonomous_system_organization }
: {}),
})
}
next()
}