81 lines
2.0 KiB
D
81 lines
2.0 KiB
D
module angel.utils.utils;
|
|
|
|
// Internal imports
|
|
import angel.utils.logging;
|
|
// External imports
|
|
import std.stdio;
|
|
import std.process;
|
|
import std.format;
|
|
import core.thread.osthread;
|
|
import core.sys.windows.windows;
|
|
import std.conv : to;
|
|
import std.range;
|
|
import std.array;
|
|
import std.string;
|
|
import std.random;
|
|
|
|
class Utils {
|
|
public static string generateRandomString(size_t length) {
|
|
string characters = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
auto rnd = Random();
|
|
|
|
auto randomChars = generate(() => characters[uniform(0, characters.length, rnd)]).take(length).array;
|
|
|
|
return to!string(randomChars);
|
|
}
|
|
|
|
public static void execute(string command) {
|
|
STARTUPINFOA si;
|
|
PROCESS_INFORMATION pi;
|
|
|
|
si.cb = STARTUPINFO.sizeof;
|
|
si.dwFlags = STARTF_USESHOWWINDOW;
|
|
si.wShowWindow = SW_HIDE;
|
|
|
|
if (!CreateProcessA(
|
|
null,
|
|
cast(char*)command.ptr,
|
|
null,
|
|
null,
|
|
false,
|
|
0,
|
|
null,
|
|
null,
|
|
&si,
|
|
&pi
|
|
)) {
|
|
Logger.log(LogLevel.Error, format("Failed to create proc: %s", GetLastError()));
|
|
return;
|
|
}
|
|
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
|
CloseHandle(pi.hProcess);
|
|
CloseHandle(pi.hThread);
|
|
}
|
|
|
|
private static void dieproc(string proc_name) {
|
|
Logger.log(LogLevel.Debug, format("Attempting to kill proc: %s", proc_name));
|
|
string command = format("cmd.exe /C taskkill /F /IM \"%s\"", proc_name);
|
|
|
|
execute(command);
|
|
}
|
|
|
|
public static void killproc(string[] ulist) {
|
|
Logger.log(LogLevel.Debug, format("Attempting to kill procs: %s", ulist));
|
|
|
|
Thread[] threads;
|
|
|
|
foreach (proc; ulist) {
|
|
auto t = new Thread(() => dieproc(proc));
|
|
threads ~= t;
|
|
t.start();
|
|
continue;
|
|
}
|
|
|
|
foreach (t; threads) {
|
|
joinLowLevelThread(t.id);
|
|
}
|
|
|
|
Logger.log(LogLevel.Debug, "All procs killed.");
|
|
}
|
|
} |