welsonjs/shadow.js

196 lines
5.1 KiB
JavaScript
Raw Normal View History

2020-10-19 08:11:56 +00:00
////////////////////////////////////////////////////////////////////////
2020-10-20 02:41:26 +00:00
// ShadowLoader
2020-10-19 08:11:56 +00:00
////////////////////////////////////////////////////////////////////////
var SS = require("lib/shadowsocks");
var SHELL = require("lib/shell");
2020-11-05 08:51:09 +00:00
var SYS = require("lib/system");
2020-11-09 19:43:19 +00:00
var XML = require("lib/xml");
2020-10-19 08:11:56 +00:00
var LDPlayer = require("lib/ldplayer");
var NoxPlayer = require("lib/noxplayer");
2020-11-09 19:43:19 +00:00
var Chrome = require("lib/chrome");
2020-10-19 08:11:56 +00:00
2020-11-09 19:43:19 +00:00
var Apps = {
LDPlayer: {},
NoxPlayer: {},
Chrome: {},
ProcessName: {}
};
var AppsMutex = [];
2020-11-15 04:31:35 +00:00
var AppsPID = [];
var getAvailablePID = function() {
var items = [];
var cmd = "tasklist | findstr .exe";
var result = SHELL.exec(cmd);
var lines = result.split(/\r?\n/);
for(var i = 0; i < lines.length; i++) {
var row = lines[i].split(/\s+/);
items.push(row[1]);
}
return items;
};
2020-11-09 19:45:54 +00:00
2020-11-09 19:43:19 +00:00
var items = XML.load("staticip.xml").select("/StaticIP/Item").toArray();
for (var i = 0; i < items.length; i++) {
try {
2020-11-09 20:45:13 +00:00
var name = items[i].getDOM().selectSingleNode("Name").text;
var uniqueId = items[i].getDOM().selectSingleNode("UniqueID").text;
var ipAddress = items[i].getDOM().selectSingleNode("IPAddress").text;
2020-11-09 19:43:19 +00:00
if (name in Apps) {
Apps[name][uniqueId] = ipAddress;
}
2020-11-18 08:46:26 +00:00
} catch (e) {}
2020-11-09 19:43:19 +00:00
}
2020-10-19 08:11:56 +00:00
2020-11-09 19:43:19 +00:00
// App 1. LDPlayer
var check_LDPlayer = function() {
2020-11-15 04:31:35 +00:00
var ssPort, ssPID, shadowPID = 0;
2020-11-09 19:43:19 +00:00
var items = LDPlayer.getList();
for (var i = 0; i < items.length; i++) {
2020-11-15 04:31:35 +00:00
var pid = items[i].PIDVBox;
2020-11-09 19:43:19 +00:00
var title = items[i].title;
if (pid > 0 && AppsMutex.indexOf(pid) < 0) {
console.info("New launched LDPlayer: " + title);
AppsMutex.push(pid);
2020-11-15 04:31:35 +00:00
2020-11-09 19:43:19 +00:00
if (title in Apps.LDPlayer) {
2020-11-18 08:46:26 +00:00
var ss = SS.connect(Apps.LDPlayer[title]);
2020-11-15 04:31:35 +00:00
ssPort = ss.listenPort;
ssPID = ss.processID;
2020-11-09 19:43:19 +00:00
} else {
console.error("Not assigned static IP: " + title);
continue;
}
2020-11-15 04:31:35 +00:00
var process;
while (!(shadowPID > 0)) {
process = SHELL.createProcess([
SYS.getCurrentScriptDirectory() + "/bin/shadow.exe",
"-c",
SYS.getCurrentScriptDirectory() + "/config.template.json",
"-s",
"socks://localhost:" + ssPort,
"-p",
pid
]);
sleep(1000);
shadowPID = process.ProcessID;
}
AppsPID.push([pid, ssPID, shadowPID]);
2020-11-09 19:43:19 +00:00
console.info("Waiting new launched");
sleep(3000);
}
}
};
2020-10-19 08:11:56 +00:00
2020-11-09 19:43:19 +00:00
// App 2. NoxPlayer
var check_NoxPlayer = function() {
2020-11-15 04:31:35 +00:00
var ssPort, ssPID, shadowPID = 0;
2020-11-09 19:43:19 +00:00
var items = NoxPlayer.getList();
for (var i = 0; i < items.length; i++) {
2020-11-15 04:31:35 +00:00
var pid = items[i].PID;
2020-11-09 19:43:19 +00:00
var hostname = items[i].hostname;
2020-11-09 20:06:08 +00:00
2020-11-09 19:43:19 +00:00
if (pid > 0 && AppsMutex.indexOf(pid) < 0) {
2020-11-09 20:45:13 +00:00
console.info("New launched NoxPlayer: " + hostname);
2020-11-09 19:43:19 +00:00
AppsMutex.push(pid);
2020-11-09 20:06:08 +00:00
if (hostname in Apps.NoxPlayer) {
2020-11-18 08:46:26 +00:00
var ss = SS.connect(Apps.NoxPlayer[hostname]);
2020-11-15 04:31:35 +00:00
ssPort = ss.listenPort;
ssPID = ss.processID;
2020-11-09 19:43:19 +00:00
} else {
console.error("Not assigned static IP: " + hostname);
continue;
}
2020-11-15 04:31:35 +00:00
var process;
while (!(shadowPID > 0)) {
process = SHELL.createProcess([
SYS.getCurrentScriptDirectory() + "/bin/shadow.exe",
"-c",
SYS.getCurrentScriptDirectory() + "/config.template.json",
"-s",
"socks://localhost:" + ssPort,
"-p",
pid
]);
sleep(1000);
shadowPID = process.ProcessID;
}
AppsPID.push([pid, ssPID, shadowPID]);
2020-11-09 19:43:19 +00:00
console.info("Waiting new launched");
sleep(3000);
}
}
2020-11-05 07:50:05 +00:00
};
2020-11-09 19:43:19 +00:00
// App 3. Chrome
var check_Chrome = function() {
2020-11-15 04:31:35 +00:00
var ssPort, ssPID;
2020-11-09 19:43:19 +00:00
for (var uniqueId in Apps.Chrome) {
if (AppsMutex.indexOf("chrome_" + uniqueId) < 0) {
console.info("Starting Google Chrome: " + uniqueId);
2020-11-18 08:46:26 +00:00
2020-11-15 04:31:35 +00:00
var ss = SS.connect(Apps.Chrome[uniqueId]);
ssPort = ss.listenPort;
ssPID = ss.processID;
2020-11-18 08:46:26 +00:00
console.info("Wait 10 seconds...")
sleep(10000);
Chrome.start("https://whatismyipaddress.com/", ssPort, uniqueId);
2020-11-09 19:43:19 +00:00
2020-11-18 08:46:26 +00:00
AppsPID.push([ssPID]);
AppsMutex.push("chrome_" + uniqueId);
2020-11-09 19:43:19 +00:00
}
}
};
2020-11-15 04:31:35 +00:00
// Check dead processes
var check_Exits = function() {
2020-11-18 08:46:26 +00:00
var alivePIDList = SYS.getProcessList().reduce(function(acc, process) {
acc.push(process.ProcessID);
}, []);
2020-11-15 04:31:35 +00:00
AppsPID.forEach(function(v1) {
v1.forEach(function(v2) {
2020-11-18 08:46:26 +00:00
if (alivePIDList.indexOf(v2) < 0) {
console.warn("Detected dead process: " + v2);
console.warn("Will be kill related processes.");
2020-11-15 04:31:35 +00:00
2020-11-18 08:46:26 +00:00
v1.forEach(function(v2) {
SYS.killProcess(v2);
});
2020-11-15 04:31:35 +00:00
return;
}
});
});
};
2020-11-09 19:43:19 +00:00
var main = function() {
console.info("Waiting new launched");
2020-11-05 07:50:05 +00:00
2020-11-09 19:43:19 +00:00
while (true) {
sleep(3000);
check_LDPlayer();
sleep(3000);
check_NoxPlayer();
sleep(3000);
2020-11-18 08:46:26 +00:00
check_Chrome();
2020-11-09 19:43:19 +00:00
}
2020-10-19 08:11:56 +00:00
};
2020-11-09 19:43:19 +00:00
exports.main = main;