Commit | Line | Data |
---|---|---|
121aeb3a D |
1 | /** |
2 | * Open proxy checking | |
3 | * | |
4 | * Look for common open proxy ports from the client | |
5 | */ | |
6 | ||
58b38d1c D |
7 | var util = require('util'), |
8 | kiwiModules = require('../server/modules'); | |
9 | ||
10 | ||
11 | var module = new kiwiModules.Module('proxychecker'); | |
12 | ||
13 | module.on('irc connecting', function (event, event_data) { | |
14 | event.wait = true; | |
15 | ||
16 | var client_addr = event_data.connection.state.client.websocket.meta.real_address; | |
17 | ||
18 | checkForOpenProxies(client_addr, function(is_proxy, host, port) { | |
19 | if (is_proxy) { | |
20 | var err = new Error(util.format('Proxy detected on %s:%d', client_addr, port)); | |
21 | err.code = 'Blocked proxy'; | |
22 | ||
23 | event_data.connection.emit('error', err); | |
24 | event.preventDefault(); | |
25 | event.callback(); | |
26 | ||
27 | } else { | |
28 | event.callback(); | |
29 | } | |
30 | }); | |
31 | }); | |
32 | ||
33 | ||
34 | ||
35 | function checkForOpenProxies(host, callback) { | |
36 | var net = require('net'); | |
37 | ||
38 | var ports = [80,8080,81,1080,6588,8000]; | |
39 | var ports_completed = 0; | |
40 | ||
41 | var callback_called = false; | |
42 | ||
43 | var portFailed = function() { | |
44 | ports_completed++; | |
45 | this.removeAllListeners(); | |
46 | this.destroy(); | |
47 | ||
48 | if (!callback_called && ports_completed >= ports.length) { | |
49 | callback_called = true; | |
50 | callback(false); | |
51 | } | |
52 | }; | |
53 | ||
54 | var portConnected = function() { | |
55 | var remote_port = this.remotePort; | |
56 | ||
57 | ports_completed++; | |
58 | this.removeAllListeners(); | |
59 | this.destroy(); | |
60 | ||
61 | if (!callback_called) { | |
62 | callback_called = true; | |
63 | callback(true, host, remote_port); | |
64 | } | |
65 | }; | |
66 | ||
102223ce D |
67 | var portTimeout = function() { |
68 | ports_completed++; | |
69 | this.removeAllListeners(); | |
70 | this.destroy(); | |
71 | ||
72 | if (!callback_called && ports_completed >= ports.length) { | |
73 | callback_called = true; | |
74 | callback(false); | |
75 | } | |
76 | }; | |
77 | ||
58b38d1c D |
78 | for (var idx=0; idx< ports.length; idx++) { |
79 | net.connect({port: ports[idx], host: host}) | |
80 | .on('connect', portConnected) | |
81 | .on('error', portFailed) | |
102223ce D |
82 | .on('close', portFailed) |
83 | .on('timeout', portTimeout) | |
84 | .setTimeout(5000); | |
58b38d1c | 85 | } |
55d0f0b1 | 86 | } |