Created
January 18, 2016 10:19
-
-
Save huzemin/24f89bbdfcfa61439c78 to your computer and use it in GitHub Desktop.
Get free shadowsocks server from ishadowsocks.com. [Node.js]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get free shadowsocks from ishadowsocks. | |
* the server password will change every 6 hours. | |
* ------------------------------------ | |
* @author huzemin8 <[email protected]> | |
*/ | |
var cheerio = require('cheerio'); | |
var fs = require('fs'); | |
var path = require('path'); | |
var superagent = require('superagent'); | |
function parseKey(str) { | |
var pair = str.split(':'); | |
return pair; | |
} | |
function getConfiguration() { | |
// 判断配置文件是否存在 | |
var config_file = path.resolve(__dirname,"gui-config.json"); | |
var config; | |
if(fs.exists(config_file)) { | |
config = fs.readFileSync(config_file, 'utf-8'); | |
config = JSON.parse(config); | |
} else { | |
// default configurate | |
config = { | |
"configs": [], | |
"strategy": null, | |
"index": 1, | |
"global": true, | |
"enabled": true, | |
"shareOverLan": false, | |
"isDefault": false, | |
"localPort": 1080, | |
"pacUrl": null, | |
"useOnlinePac": false, | |
"availabilityStatistics": false | |
} | |
} | |
superagent.get('http://www.ishadowsocks.com/').end(function(err, res){ | |
if(!err) { | |
if(res.ok) { | |
var $ = cheerio.load(res.text, {decodeEntities: false}); | |
var frees = $('#free .row .col-lg-4'); | |
var servers_config = []; | |
frees.each(function(index, f) { | |
var s = {}; | |
$(f).find('h4').each(function(id, el){ | |
var $el = $(el), | |
text = $el.text(), | |
pair = parseKey(text); | |
if(/服务器/.test(pair[0])) { | |
s['server'] = pair[1]; | |
} else if(/端口/.test(pair[0])) { | |
s['server_port'] = pair[1]; | |
} else if(/^\w密码$/.test(pair[0])) { | |
s['password'] = pair[1]; | |
} else if(/加密/.test(pair[0])) { | |
s['method'] = pair[1]; | |
} | |
}); | |
s['remarks'] = ''; | |
servers_config.push(s); | |
}); | |
// 替换配置 | |
config.configs = servers_config; | |
// 写入配置文件 | |
fs.writeFile(config_file, JSON.stringify(config, null, ' '), 'utf-8', function(err){ | |
if(!err) { | |
console.log('配置刷新成功!'); | |
} | |
}); | |
} else { | |
throw new Error("The status of Response isn't Ok."); | |
} | |
} | |
}); | |
} | |
console.log("Run..."); | |
getConfiguration(); | |
setInterval(function(){ | |
getConfiguration(); | |
}, 3600 * 6 * 1000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"superagent": "^1.6.1", | |
"cheerio": "^0.19.0" | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment