-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (51 loc) · 1.79 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
'use strict';
// add timestamps in front of log messages
require('console-stamp')(console, 'yyyy/mm/dd HH:MM:ss');
const config = require('./config/config');
const IP_MAP = require('./cml_ip_map.json');
const Promise = require('bluebird');
const table2json = require('tabletojson');
const request = require('request-promise');
const iconv = require('iconv-lite');
const loginFacebook = Promise.promisify(require('facebook-chat-api'));
const fs = Promise.promisifyAll(require('fs'));
const log = require('./log.json');
const option = {
method: 'POST',
uri: 'http://cert.ntu.edu.tw/Module/Index/ip.php',
encoding: null,
form: {
ip1: 140,
ip2: 112,
ip3: 29,
isset: 'ok'
}
};
let msg = {};
request(option)
.then(buffer => {
const decoded = iconv.decode(new Buffer(buffer), 'big5');
const table = table2json.convert(decoded)[1];
const banned = checkIP(table);
if (!banned.length) {
console.log("All IP are fine, flush log.");
fs.writeFileSync(`${__dirname}/log.json`, '[]');
process.exit(0);
}
msg.body = JSON.stringify(banned, null, '\t').replace(/[\{\},\[\]\t"]/g, '');
if (JSON.stringify(log) == JSON.stringify(banned)) {
console.log('Already warned.');
process.exit(0);
} else {
fs.writeFileSync(`${__dirname}/log.json`, JSON.stringify(banned));
}
return loginFacebook(config.account);
})
.then(api => api.sendMessage(msg, config.notify_channel_id))
.then(() => console.log(msg.body))
.catch(err => console.error(err));
function checkIP(list) {
return list
.filter(item => !!IP_MAP[item['違規IP']] )
.map(item => Object.assign(item, { '主機': IP_MAP[item['違規IP']] }));
}