-
Notifications
You must be signed in to change notification settings - Fork 2
/
shadow.js
59 lines (52 loc) · 1.82 KB
/
shadow.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
var request = require('request'),
fs = require('fs'),
path = require('path'),
cheerio = require('cheerio'),
schedule = require('node-schedule');
//每小时30分时执行一次更新操作
var rule = new schedule.RecurrenceRule();
rule.hour = [0,6,12,18,24];
var j = schedule.scheduleJob(rule, getPassword);
//运行时先执行一次
getPassword();
function getPassword() {
request('http://www.ishadowsocks.com/', function(err, res, body) {
var $ = cheerio.load(body);
var passwords = [];
var tmp = $('#free').text();
var usPatt = /A密码:\d+/;
var hkPatt = /B密码:\d+/;
var jpPatt = /C密码:\d+/;
var fileContentObj;
var fileContent;
if (tmp.match(usPatt)) {
var t = tmp.match(usPatt).toString().replace('A密码:', '');
passwords.push(t);
} else {
passwords.push('111');
}
if (tmp.match(hkPatt)) {
var t = tmp.match(hkPatt).toString().replace('B密码:', '');
passwords.push(t);
} else {
passwords.push('111');
}
if (tmp.match(jpPatt)) {
var t = tmp.match(jpPatt).toString().replace('C密码:', '');
passwords.push(t);
} else {
passwords.push('111');
}
fileContentObj = JSON.parse(fs.readFileSync('tpl.json', {
encoding: 'utf8'
}));
for (var i = 0, len = fileContentObj.configs.length; i < len; i++) {
fileContentObj.configs[i].password = passwords[i];
}
fileContent = JSON.stringify(fileContentObj);
fs.writeFile('gui-config.json', fileContent, function(err) {
if (err) throw err;
console.log(new Date()+'saved!');
})
})
}