Skip to content

Commit 6f33417

Browse files
authored
Refactor threads for concurrent execution
In the init() function, you create a thread, which runs runChecks this function, in turn, calls checkAtoms, checkFiles, and checkRegistry in sequence. so checkAtoms, checkFiles, and checkRegistry are called from both init() (within runChecks) and license (as individual threads). now you will get Multiple threads running the same functions, you are calling the same functions (checkAtoms, checkFiles, and checkRegistry) twice in parallel once from runChecks (which is called in init()) and once from license(), this leads to multiple threads working on the same task, potentially doing redundant work and introducing unnecessary complexity and overhead. it will consume more system resources (CPU, memory) than necessary. One more thing when you call runChecks from the init() function, it first calls checkAtoms, then checkFiles, and finally checkRegistry. the key point here is that runChecks doesn't return until checkAtoms finishes its work, since checkAtoms has an infinite loop (while(true)) it will never return control to runChecks, you are checking only for GlobalFindAtomA, which mean GetFileAttributesA and RegOpenKeyExA will never be called until you return from checkAtoms. One solution would be to move each of the checkAtoms, checkFiles, and checkRegistry functions into separate threads so they run concurrently, instead of running one after the other in runChecks, and no need to call them again in License() function since they gonna keep working till the program exit I Added a simple mechanism to interrupt the sleep if the user logs in before the 45 seconds have passed, the function waits in a loop, checking every second if the user has logged in, if the user logs in before the 45s timeout, the loop breaks, and the checks begin. if the user hasn’t logged in by the time the 45s pass, it simply proceeds with the checks.
1 parent e13dbdc commit 6f33417

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

auth.cpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ std::string signatureTimestamp;
7979
bool initialized;
8080
std::string API_PUBLIC_KEY = "5586b4bc69c7a4b487e4563a4cd96afd39140f919bd31cea7d1c6a1e8439422b";
8181
bool KeyAuth::api::debug = false;
82+
std::atomic<bool> LoggedIn(false);
8283

8384
void KeyAuth::api::init()
8485
{
85-
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)runChecks, 0, 0, 0);
86+
std::thread(runChecks).detach();
8687
seed = generate_random_number();
8788
std::atexit([]() { cleanUpSeedData(seed); });
8889
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)modify, 0, 0, 0);
@@ -294,6 +295,7 @@ void KeyAuth::api::login(std::string username, std::string password, std::string
294295
}
295296

296297
LI_FN(GlobalAddAtomA)(ownerid.c_str());
298+
LoggedIn.store(true);
297299
}
298300
else {
299301
LI_FN(exit)(12);
@@ -742,6 +744,7 @@ void KeyAuth::api::web_login()
742744
}
743745

744746
LI_FN(GlobalAddAtomA)(ownerid.c_str());
747+
LoggedIn.store(true);
745748
}
746749
else {
747750
LI_FN(exit)(12);
@@ -990,6 +993,7 @@ void KeyAuth::api::regstr(std::string username, std::string password, std::strin
990993
}
991994

992995
LI_FN(GlobalAddAtomA)(ownerid.c_str());
996+
LoggedIn.store(true);
993997
}
994998
else {
995999
LI_FN(exit)(12);
@@ -1062,11 +1066,6 @@ std::string generate_random_number() {
10621066
}
10631067

10641068
void KeyAuth::api::license(std::string key, std::string code) {
1065-
// Call threads to start in 15 seconds..
1066-
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)checkAtoms, 0, 0, 0);
1067-
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)checkFiles, 0, 0, 0);
1068-
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)checkRegistry, 0, 0, 0);
1069-
10701069
checkInit();
10711070

10721071
std::string hwid = utils::get_hwid();
@@ -1120,6 +1119,7 @@ void KeyAuth::api::license(std::string key, std::string code) {
11201119
}
11211120

11221121
LI_FN(GlobalAddAtomA)(ownerid.c_str());
1122+
LoggedIn.store(true);
11231123
}
11241124
else {
11251125
LI_FN(exit)(12);
@@ -1801,15 +1801,25 @@ auto check_section_integrity( const char *section_name, bool fix = false ) -> bo
18011801
}
18021802

18031803
void runChecks() {
1804-
Sleep(45000); // give people 1 minute to login. (because the functions we call already wait 15 seconds)
1805-
1806-
checkAtoms();
1807-
checkFiles();
1808-
checkRegistry();
1804+
// Wait before starting checks
1805+
int waitTime = 45000;
1806+
while (waitTime > 0) {
1807+
1808+
if (LoggedIn.load()) {
1809+
// If the user is logged in, proceed with the checks immediately
1810+
break;
1811+
}
1812+
std::this_thread::sleep_for(std::chrono::seconds(1));
1813+
waitTime -= 1000;
1814+
}
1815+
1816+
// Create separate threads for each check
1817+
std::thread(checkAtoms).detach();
1818+
std::thread(checkFiles).detach();
1819+
std::thread(checkRegistry).detach();
18091820
}
18101821

18111822
void checkAtoms() {
1812-
Sleep(15000); // enough time for API response, even on slower connections
18131823

18141824
while (true) {
18151825
if (LI_FN(GlobalFindAtomA)(seed.c_str()) == 0) {
@@ -1821,7 +1831,6 @@ void checkAtoms() {
18211831
}
18221832

18231833
void checkFiles() {
1824-
Sleep(15000); // enough time for API response, even on slower connections
18251834

18261835
while (true) {
18271836
std::string file_path = XorStr("C:\\ProgramData\\").c_str() + seed;
@@ -1835,8 +1844,7 @@ void checkFiles() {
18351844
}
18361845

18371846
void checkRegistry() {
1838-
Sleep(15000); // enough time for API response, even on slower connections
1839-
1847+
18401848
while (true) {
18411849
std::string regPath = XorStr("Software\\").c_str() + seed;
18421850
HKEY hKey;

0 commit comments

Comments
 (0)