welsonjs/lib/shadowsocks.js

62 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-07-25 09:10:43 +00:00
////////////////////////////////////////////////////////////////////////
// Shadowsocks API
////////////////////////////////////////////////////////////////////////
2020-11-05 08:51:09 +00:00
var CONFIG = require("lib/config");
2020-07-25 09:10:43 +00:00
var SHELL = require("lib/shell");
2020-11-13 08:44:58 +00:00
var binPath = "bin\\ss-local.exe";
var getRandomInt = function(min, max) {
2020-07-26 11:40:25 +00:00
var x = Math.random();
return min + Math.floor((max - min) * x);
};
2020-11-13 08:44:58 +00:00
var ShadowsocksObject = function() {
this.binPath
this.processID = 0;
this.listenPort = 0;
this.connect = function(host) {
this.listenPort = getRandomInt(49152, 65535);
var process = SHELL.createProcess([
binPath,
"-s",
host,
"-p",
2020-11-25 08:08:17 +00:00
CONFIG.getValue("SSPort"),
2020-11-13 08:44:58 +00:00
"-l",
this.listenPort,
"-k",
2020-11-25 08:08:17 +00:00
CONFIG.getValue("SSPassword"),
2020-11-13 08:44:58 +00:00
"-m",
2020-11-25 08:08:17 +00:00
CONFIG.getValue("SSCipher")
2020-11-13 08:44:58 +00:00
]);
sleep(1000);
try {
this.processID = process.ProcessID;
if (this.processID > 0) {
return this;
} else {
console.info("Retrying connect to shadowsocks...");
return this.connect();
}
} catch(e) {
console.info("Retrying connect to shadowsocks...");
return this.connect();
}
};
2020-10-20 02:41:26 +00:00
};
2020-11-15 04:31:35 +00:00
exports.connect = function(host) {
return (new ShadowsocksObject()).connect(host);
2020-10-20 02:41:26 +00:00
};
2020-11-09 10:06:34 +00:00
2020-11-15 04:31:35 +00:00
exports.VERSIONINFO = "Shadowsocks interface (shadowsocks.js) version 0.2";
2020-11-09 10:06:34 +00:00
exports.global = global;
exports.require = global.require;