-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
387 lines (310 loc) · 11.6 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
'use strict'
const { Resolver } = require('node:dns').promises
const dns = new Resolver({ timeout: 25000, tries: 1 })
const net = require('node:net')
const os = require('node:os')
// npm modules
const ipaddr = require('ipaddr.js')
const sprintf = require('sprintf-js').sprintf
const tlds = require('haraka-tld')
const locallyBoundIPs = []
// export config, so config base path can be overloaded by tests
exports.config = require('haraka-config')
exports.long_to_ip = function (n) {
let d = n % 256
for (let i = 3; i > 0; i--) {
n = Math.floor(n / 256)
d = `${n % 256}.${d}`
}
return d
}
exports.dec_to_hex = function (d) {
return d.toString(16)
}
exports.hex_to_dec = function (h) {
return parseInt(h, 16)
}
exports.ip_to_long = function (ip) {
if (!net.isIPv4(ip)) return false
const d = ip.split('.')
return ((+d[0] * 256 + +d[1]) * 256 + +d[2]) * 256 + +d[3]
}
exports.octets_in_string = function (str, oct1, oct2) {
let oct1_idx
let oct2_idx
// test the largest of the two octets first
if (oct2.length >= oct1.length) {
oct2_idx = str.lastIndexOf(oct2)
if (oct2_idx === -1) return false
oct1_idx = (
str.substring(0, oct2_idx) + str.substring(oct2_idx + oct2.length)
).lastIndexOf(oct1)
if (oct1_idx === -1) return false
return true // both were found
}
oct1_idx = str.indexOf(oct1)
if (oct1_idx === -1) return false
oct2_idx = (
str.substring(0, oct1_idx) + str.substring(oct1_idx + oct1.length)
).lastIndexOf(oct2)
if (oct2_idx === -1) return false
return true
}
exports.is_ip_in_str = function (ip, str) {
if (!str) return false
if (!ip) return false
if (!net.isIPv4(ip)) return false // IPv4 only, for now
const host_part = tlds.split_hostname(str, 1)[0].toString()
const octets = ip.split('.')
// See if the 3rd and 4th octets appear in the string
if (this.octets_in_string(host_part, octets[2], octets[3])) {
return true
}
// then the 1st and 2nd octets
if (this.octets_in_string(host_part, octets[0], octets[1])) {
return true
}
// Whole IP in hex
let host_part_copy = host_part
const ip_hex = this.dec_to_hex(this.ip_to_long(ip))
for (let i = 0; i < 4; i++) {
const part = host_part_copy.indexOf(ip_hex.substring(i * 2, i * 2 + 2))
if (part === -1) break
if (i === 3) return true
host_part_copy =
host_part_copy.substring(0, part) + host_part_copy.substring(part + 2)
}
return false
}
const re_ipv4 = {
loopback: /^127\./,
link_local: /^169\.254\./,
private10: /^10\./, // 10/8
private192: /^192\.168\./, // 192.168/16
// 172.16/16 .. 172.31/16
private172: /^172\.(1[6-9]|2[0-9]|3[01])\./, // 172.16/12
// RFC 5735
testnet1: /^192\.0\.2\./, // 192.0.2.0/24
testnet2: /^198\.51\.100\./, // 198.51.100.0/24
testnet3: /^203\.0\.113\./, // 203.0.113.0/24
}
exports.is_private_ipv4 = function (ip) {
// RFC 1918, reserved as "private" IP space
if (re_ipv4.private10.test(ip)) return true
if (re_ipv4.private192.test(ip)) return true
if (re_ipv4.private172.test(ip)) return true
if (re_ipv4.testnet1.test(ip)) return true
if (re_ipv4.testnet2.test(ip)) return true
if (re_ipv4.testnet3.test(ip)) return true
return false
}
exports.on_local_interface = function (ip) {
if (locallyBoundIPs.length === 0) {
const ifList = os.networkInterfaces()
for (const ifName of Object.keys(ifList)) {
for (const addr of ifList[ifName]) {
locallyBoundIPs.push(addr.address)
}
}
}
return locallyBoundIPs.includes(ip)
}
exports.is_local_host = async function (dst_host) {
// Is the destination hostname/IP delivered to a hostname or IP
// that's local to _this_ mail server?
const local_ips = []
const dest_ips = []
try {
const public_ip = await this.get_public_ip()
if (public_ip) local_ips.push(public_ip)
const local_hostname = this.get_primary_host_name()
local_ips.push(...(await this.get_ips_by_host(local_hostname)))
if (net.isIP(dst_host)) {
// an IP address
dest_ips.push(dst_host)
} else {
// a hostname
if (dst_host === local_hostname) return true
dest_ips.push(...(await this.get_ips_by_host(dst_host)))
}
} catch (e) {
// console.error(e)
return false
}
for (const ip of dest_ips) {
if (this.is_local_ip(ip)) return true
if (local_ips.includes(ip)) return true
}
return false
}
exports.is_local_ip = function (ip) {
if (this.on_local_interface(ip)) return true
if (net.isIPv4(ip)) return this.is_local_ipv4(ip)
if (net.isIPv6(ip)) return this.is_local_ipv6(ip)
// console.error(`invalid IP address: ${ip}`);
return false
}
exports.is_local_ipv4 = function (ip) {
if ('0.0.0.0' === ip) return true // RFC 5735
// 127/8 (loopback) # RFC 1122
if (re_ipv4.loopback.test(ip)) return true
// link local: 169.254/16 RFC 3927
if (re_ipv4.link_local.test(ip)) return true
return false
}
const re_ipv6 = {
loopback: /^(0{1,4}:){7}0{0,3}1$/,
link_local: /^fe80::/i,
unique_local: /^f(c|d)[a-f0-9]{2}:/i,
}
exports.is_local_ipv6 = function (ip) {
if (ip === '::') return true // RFC 5735
if (ip === '::1') return true // RFC 4291
// 2 more IPv6 notations for ::1
// 0:0:0:0:0:0:0:1 or 0000:0000:0000:0000:0000:0000:0000:0001
if (re_ipv6.loopback.test(ip)) return true
// link local: fe80::/10, RFC 4862
if (re_ipv6.link_local.test(ip)) return true
// unique local (fc00::/7) -> fc00: - fd00:
if (re_ipv6.unique_local.test(ip)) return true
return false
}
exports.is_private_ip = function (ip) {
if (net.isIPv4(ip)) return this.is_local_ipv4(ip) || this.is_private_ipv4(ip)
if (net.isIPv6(ip)) return this.is_local_ipv6(ip)
return false
}
// backwards compatibility for non-public modules. Sunset: v3.0
exports.is_rfc1918 = exports.is_private_ip
exports.is_ip_literal = function (host) {
return exports.get_ipany_re('^\\[(IPv6:)?', '\\]$', '').test(host)
? true
: false
}
exports.is_ipv4_literal = function (host) {
return /^\[(\d{1,3}\.){3}\d{1,3}\]$/.test(host) ? true : false
}
exports.same_ipv4_network = function (ip, ipList) {
if (!ipList || !ipList.length) {
console.error('same_ipv4_network, no ip list!')
return false
}
if (!net.isIPv4(ip)) {
console.error('same_ipv4_network, IP is not IPv4!')
return false
}
const first3 = ip.split('.').slice(0, 3).join('.')
for (let i = 0; i < ipList.length; i++) {
if (!net.isIPv4(ipList[i])) {
console.error('same_ipv4_network, IP in list is not IPv4!')
continue
}
if (first3 === ipList[i].split('.').slice(0, 3).join('.')) return true
}
return false
}
exports.get_ipany_re = function (prefix = '', suffix = '', modifier = 'mg') {
return new RegExp(
prefix +
`(` + // capture group
`(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(?:(?:(?:(?:(?:(?:(?:[0-9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0-9]|2[0-4])?[0-9]))\\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,3}(?:(?:[0-9a-fA-F]{1,4})))?::(?:(?:[0-9a-fA-F]{1,4})):)(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,4}(?:(?:[0-9a-fA-F]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,5}(?:(?:[0-9a-fA-F]{1,4})))?::)(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,6}(?:(?:[0-9a-fA-F]{1,4})))?::))))` + // complex ipv4 + ipv6
`)` + // end capture
`${suffix}`,
modifier,
)
}
exports.get_ips_by_host = function (hostname, done) {
const ips = new Set()
const errors = []
return Promise.allSettled([
dns.resolve6(hostname),
dns.resolve4(hostname),
]).then((res) => {
res.filter((a) => a.status === 'rejected').map((a) => errors.push(a.reason))
res
.filter((a) => a.status === 'fulfilled')
.map((a) => a.value.map((ip) => ips.add(ip)))
if (done) done(errors, Array.from(ips))
return Array.from(ips)
})
}
exports.ipv6_reverse = function (ipv6) {
ipv6 = ipaddr.parse(ipv6)
return ipv6
.toNormalizedString()
.split(':')
.map(function (n) {
return sprintf('%04x', parseInt(n, 16))
})
.join('')
.split('')
.reverse()
.join('.')
}
exports.ipv6_bogus = function (ipv6) {
try {
const ipCheck = ipaddr.parse(ipv6)
if (ipCheck.range() !== 'unicast') return true
return false
} catch (e) {
// If we get an error from parsing, return true for bogus.
console.error(e)
return true
}
}
exports.ip_in_list = function (list, ip) {
if (list === undefined) return false
const isHostname = !net.isIP(ip)
const isArray = Array.isArray(list)
// Quick lookup
if (!isArray) {
if (ip in list) return true // domain or literal IP
if (isHostname) return false // skip CIDR match
}
// Iterate: arrays and CIDR matches
for (let item in list) {
if (isArray) {
item = list[item] // item is index
if (item === ip) return true // exact match
}
if (isHostname) continue // skip CIDR match
const cidr = item.split('/')
const c_net = cidr[0]
if (!net.isIP(c_net)) continue // bad config entry
if (net.isIPv4(ip) && net.isIPv6(c_net)) continue
if (net.isIPv6(ip) && net.isIPv4(c_net)) continue
const c_mask = parseInt(cidr[1], 10) || (net.isIPv6(c_net) ? 128 : 32)
if (ipaddr.parse(ip).match(ipaddr.parse(c_net), c_mask)) {
return true
}
}
return false
}
exports.get_primary_host_name = function () {
return exports.config.get('me') || os.hostname()
}
for (const l of ['get_mx', 'get_implicit_mx', 'resolve_mx_hosts']) {
exports[l] = require('./lib/get_mx')[l]
}
exports.get_public_ip = require('./lib/get_public_ip').get_public_ip
exports.get_public_ip_async = require('./lib/get_public_ip').get_public_ip_async
exports.HarakaMx = require('./lib/HarakaMx')
exports.add_line_processor = (socket) => {
const line_regexp = /^([^\n]*\n)/ // utils.line_regexp
let current_data = ''
socket.on('data', (data) => {
current_data += data
let results
while ((results = line_regexp.exec(current_data))) {
const this_line = results[1]
current_data = current_data.slice(this_line.length)
socket.emit('line', this_line)
}
})
socket.on('end', () => {
if (current_data.length) {
socket.emit('line', current_data)
}
current_data = ''
})
}