Commit | Line | Data |
---|---|---|
8ac9f7ac D |
1 | /** |
2 | * DNS Blacklist support | |
3 | * | |
4 | * Check the client against a blacklist before connection to an IRC server | |
5 | */ | |
6 | ||
7 | var dns = require('dns'), | |
8 | kiwiModules = require('../server/modules'); | |
9 | ||
10 | ||
11 | // The available DNS zones to check against | |
12 | var bl_zones = { | |
13 | dronebl: '.dnsbl.dronebl.org' | |
14 | }; | |
15 | ||
16 | // The DNS zone we should use | |
17 | var current_bl = 'dronebl'; | |
18 | ||
19 | ||
20 | var module = new kiwiModules.Module('DNSBL'); | |
21 | ||
22 | module.on('irc connecting', function (event, event_data) { | |
23 | event.wait = true; | |
24 | ||
03a63ca4 | 25 | var client_addr = event_data.connection.state.client.websocket.meta.real_address; |
8ac9f7ac D |
26 | |
27 | isBlacklisted(client_addr, function(is_blocked) { | |
28 | if (is_blocked) { | |
1f1050e4 | 29 | var err = new Error('DNSBL blocked (' + client_addr + ')'); |
8ac9f7ac D |
30 | err.code = 'Blacklisted'; |
31 | ||
32 | event_data.connection.emit('error', err); | |
33 | event.preventDefault(); | |
34 | event.callback(); | |
35 | ||
36 | } else { | |
37 | event.callback(); | |
38 | } | |
39 | }); | |
40 | }); | |
41 | ||
42 | ||
43 | ||
44 | // The actual checking against the DNS blacklist | |
45 | function isBlacklisted(ip, callback) { | |
46 | var host_lookup = reverseIp(ip) + bl_zones[current_bl]; | |
47 | ||
48 | dns.resolve4(host_lookup, function(err, domain) { | |
49 | if (err) { | |
50 | // Not blacklisted | |
51 | callback(false); | |
52 | } else { | |
53 | // It is blacklisted | |
54 | callback(true); | |
55 | } | |
56 | }); | |
57 | } | |
58 | ||
59 | ||
60 | function reverseIp(ip) { | |
61 | return ip.split('.').reverse().join('.'); | |
62 | } |