welsonjs/shadow.js

261 lines
7.4 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 = {
2020-11-27 13:31:51 +00:00
LDPlayer: {},
NoxPlayer: {},
Chrome: {},
ProcessName: {}
2020-11-09 19:43:19 +00:00
};
var AppsMutex = [];
2020-11-15 04:31:35 +00:00
var AppsPID = [];
var getAvailablePID = function() {
2020-11-27 13:31:51 +00:00
var items = [];
2020-11-15 04:31:35 +00:00
var cmd = "tasklist | findstr .exe";
var result = SHELL.exec(cmd);
var lines = result.split(/\r?\n/);
for(var i = 0; i < lines.length; i++) {
2020-11-27 13:31:51 +00:00
var row = lines[i].split(/\s+/);
2020-11-15 04:31:35 +00:00
items.push(row[1]);
2020-11-27 13:31:51 +00:00
}
return items;
2020-11-15 04:31:35 +00:00
};
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++) {
2020-11-27 13:31:51 +00:00
try {
var name = items[i].getDOM().selectSingleNode("Name").text;
var uniqueId = items[i].getDOM().selectSingleNode("UniqueID").text;
var ipAddress = items[i].getDOM().selectSingleNode("IPAddress").text;
if (name in Apps) {
Apps[name][uniqueId] = ipAddress;
}
} 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-27 13:31:51 +00:00
var items = LDPlayer.getList();
for (var i = 0; i < items.length; i++) {
var pid = items[i].PIDVBox;
var title = items[i].title;
if (pid > 0 && AppsMutex.indexOf(pid) < 0) {
console.info("New launched LDPlayer: " + title);
AppsMutex.push(pid);
if (title in Apps.LDPlayer) {
while (!SYS.isAlivePID(ssPID)) {
var ss = SS.connect(Apps.LDPlayer[title]);
ssPort = ss.listenPort;
ssPID = ss.processID;
}
} else {
console.error("Not assigned static IP: " + title);
continue;
}
2020-11-09 19:43:19 +00:00
2020-11-15 04:31:35 +00:00
var process;
2020-11-27 13:31:51 +00:00
while (!SYS.isAlivePID(shadowPID)) {
2020-11-15 04:31:35 +00:00
process = SHELL.createProcess([
SYS.getCurrentScriptDirectory() + "/bin/shadow.exe",
"-c",
SYS.getCurrentScriptDirectory() + "/config.template.json",
"-s",
"socks://localhost:" + ssPort,
"-p",
pid
]);
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-15 04:31:35 +00:00
shadowPID = process.ProcessID;
}
AppsPID.push([pid, ssPID, shadowPID]);
2020-11-09 19:43:19 +00:00
2020-11-27 13:31:51 +00:00
console.info("Waiting new launched");
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-27 13:31:51 +00:00
}
}
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 2. NoxPlayer
var check_NoxPlayer = function() {
2020-11-15 04:31:35 +00:00
var ssPort, ssPID, shadowPID = 0;
2020-11-27 13:31:51 +00:00
var items = NoxPlayer.getList();
for (var i = 0; i < items.length; i++) {
var pid = items[i].PID;
2020-12-10 02:19:07 +00:00
var title = items[i].title;
2020-11-27 13:31:51 +00:00
if (pid > 0 && AppsMutex.indexOf(pid) < 0) {
2020-12-10 02:19:07 +00:00
console.info("New launched NoxPlayer: " + title);
2020-11-27 13:31:51 +00:00
AppsMutex.push(pid);
2020-12-10 02:19:07 +00:00
if (title in Apps.NoxPlayer) {
2020-11-27 13:31:51 +00:00
while (!SYS.isAlivePID(ssPID)) {
2020-12-10 02:19:07 +00:00
var ss = SS.connect(Apps.NoxPlayer[title]);
2020-11-27 13:31:51 +00:00
ssPort = ss.listenPort;
ssPID = ss.processID;
}
} else {
2020-12-10 02:19:07 +00:00
console.error("Not assigned static IP: " + title);
2020-11-27 13:31:51 +00:00
continue;
}
2020-11-09 19:43:19 +00:00
2020-11-15 04:31:35 +00:00
var process;
2020-11-27 13:31:51 +00:00
while (!SYS.isAlivePID(shadowPID)) {
2020-11-15 04:31:35 +00:00
process = SHELL.createProcess([
SYS.getCurrentScriptDirectory() + "/bin/shadow.exe",
"-c",
SYS.getCurrentScriptDirectory() + "/config.template.json",
"-s",
"socks://localhost:" + ssPort,
"-p",
pid
]);
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-15 04:31:35 +00:00
shadowPID = process.ProcessID;
}
AppsPID.push([pid, ssPID, shadowPID]);
2020-11-27 13:31:51 +00:00
console.info("Waiting new launched");
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-27 13:31:51 +00:00
}
}
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-27 13:31:51 +00:00
for (var uniqueId in Apps.Chrome) {
if (AppsMutex.indexOf("chrome_" + uniqueId) < 0) {
2020-11-20 08:44:50 +00:00
console.info("Creating Chrome Shoutcut: " + uniqueId);
2020-11-18 08:46:26 +00:00
2020-11-20 08:44:50 +00:00
// 바탕화면에 바로가기만 생성
2020-12-10 02:19:07 +00:00
var number = uniqueId.replace(/[^0-9]/g,'');
Chrome.create()
.setProfile(uniqueId, uniqueId)
.setInPrivate(true)
.createShoutcut("https://google.com")
;
2020-11-18 08:46:26 +00:00
AppsMutex.push("chrome_" + uniqueId);
2020-11-27 13:31:51 +00:00
}
}
2020-11-09 19:43:19 +00:00
};
2020-11-26 22:53:06 +00:00
// App 4. ProcessName
var check_ProcessName = function() {
for (var uniqueId in Apps.ProcessName) {
if (AppsMutex.indexOf("processName_" + uniqueId) < 0) {
var ssPort, ssPID, shadowPID = 0;
console.info("Running listener with process name: " + uniqueId);
2020-11-27 06:58:43 +00:00
AppsMutex.push("processName_" + uniqueId);
2020-11-26 22:53:06 +00:00
// 소켓 연결
2020-11-27 13:31:51 +00:00
while (!SYS.isAlivePID(ssPID)) {
var ss = SS.connect(Apps.ProcessName[uniqueId]);
ssPort = ss.listenPort;
ssPID = ss.processID;
}
2020-11-26 22:53:06 +00:00
// 프로세스 이름으로 실행
var process;
2020-11-27 13:31:51 +00:00
while (!SYS.isAlivePID(shadowPID)) {
2020-11-26 22:53:06 +00:00
process = SHELL.createProcess([
SYS.getCurrentScriptDirectory() + "/bin/shadow.exe",
"-c",
SYS.getCurrentScriptDirectory() + "/config.template.json",
"-s",
"socks://localhost:" + ssPort,
"-n",
uniqueId
]);
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-26 22:53:06 +00:00
shadowPID = process.ProcessID;
}
2020-11-27 06:58:43 +00:00
AppsPID.push([ssPID, shadowPID]);
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-26 22:53:06 +00:00
}
}
};
2020-11-27 06:58:43 +00:00
var check_Zombie = function() {
var alives = SYS.getPIDList();
var zombies = [];
// find dead processes
var _AppsPID = [];
for (var i = 0; i < AppsPID.length; i++) {
var v1 = AppsPID[i];
var isDead = false;
for (var k = 0; k < v1.length; k++) {
var v2 = v1[k];
if (alives.indexOf(v2) < 0) {
console.warn("Detected zombie: " + v2);
console.warn("Will be kill all related processes.");
zombies = zombies.concat(v1);
isDead = true;
break;
}
}
2020-11-15 04:31:35 +00:00
2020-11-27 06:58:43 +00:00
if (!isDead) {
_AppsPID.push(v1);
}
}
AppsPID = _AppsPID;
// kill zombie processes
var _zombies = [];
for (var i = 0; i < zombies.length; i++) {
var pid = zombies[i];
if (pid > 0) {
try {
SYS.killProcess(pid);
} catch (e) {
_zombies.push(pid);
console.error("shadow -> check_Zombie() -> ", e.message);
2020-11-15 04:31:35 +00:00
}
2020-11-27 06:58:43 +00:00
}
}
zombies = _zombies;
2020-11-15 04:31:35 +00:00
};
2020-11-09 19:43:19 +00:00
var main = function() {
2020-11-27 13:31:51 +00:00
console.info("Waiting new launched");
2020-11-05 07:50:05 +00:00
2020-11-27 13:31:51 +00:00
while (true) {
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-27 13:31:51 +00:00
check_LDPlayer();
2020-11-09 19:43:19 +00:00
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-27 13:31:51 +00:00
check_NoxPlayer();
2020-11-09 19:43:19 +00:00
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-18 08:46:26 +00:00
check_Chrome();
2020-11-26 22:53:06 +00:00
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-26 22:53:06 +00:00
check_ProcessName();
2020-11-27 06:58:43 +00:00
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-27 06:58:43 +00:00
check_Zombie();
2020-11-27 13:31:51 +00:00
}
2020-10-19 08:11:56 +00:00
};
2020-11-09 19:43:19 +00:00
exports.main = main;