#import "SystemManager.h" #import "../client/IOCPClient.h" #import #import #import #import #include // memcpy / strlen / memset #include SystemManager::SystemManager(IOCPClient* client) : m_client(client) { // 与 Windows/Linux 端一致:构造时立即发送进程列表。服务端把这条首包当作 // 「系统子连接已就绪」,kill_process / list_processes 都在等它。 sendProcessList(); } void SystemManager::OnReceive(PBYTE szBuffer, ULONG ulLength) { if (!szBuffer || ulLength == 0) return; switch (szBuffer[0]) { case COMMAND_PSLIST: sendProcessList(); break; case COMMAND_KILLPROCESS: if (ulLength > 1) { killProcesses(szBuffer + 1, ulLength - 1); // Refresh list after kill sendProcessList(); } break; case COMMAND_WSLIST: sendWindowsList(); break; default: Mprintf("[SystemManager] Unknown sub-command: %d\n", (int)szBuffer[0]); break; } } static std::vector getAllPids() { std::vector pids; // Get number of processes int count = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0); if (count <= 0) return pids; // Allocate buffer for PIDs (double the size: processes may spawn in between) std::vector buffer(count * 2); count = proc_listpids(PROC_ALL_PIDS, 0, buffer.data(), (int)(buffer.size() * sizeof(pid_t))); if (count <= 0) return pids; int numPids = count / sizeof(pid_t); for (int i = 0; i < numPids; i++) { if (buffer[i] > 0) { pids.push_back(buffer[i]); } } return pids; } static std::string getProcessName(pid_t pid) { char name[PROC_PIDPATHINFO_MAXSIZE]; memset(name, 0, sizeof(name)); struct proc_bsdinfo info; if (proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, sizeof(info)) > 0) { return std::string(info.pbi_name); } return ""; } static std::string getProcessPath(pid_t pid) { char path[PROC_PIDPATHINFO_MAXSIZE]; memset(path, 0, sizeof(path)); if (proc_pidpath(pid, path, sizeof(path)) > 0) { return std::string(path); } return ""; } void SystemManager::sendProcessList() { if (!m_client) return; std::vector buf; buf.reserve(64 * 1024); // Token header buf.push_back(TOKEN_PSLIST); // Architecture string #if defined(__arm64__) || defined(__aarch64__) const char* arch = "arm64"; #else const char* arch = "x64"; #endif std::vector pids = getAllPids(); size_t sent = 0; for (pid_t pid : pids) { if (pid <= 0) continue; std::string name = getProcessName(pid); if (name.empty()) continue; std::string path = getProcessPath(pid); if (path.empty()) { path = "[" + name + "]"; } // Format: "processname:arch" std::string exeFile = name + ":" + arch; // Write PID (4 bytes, DWORD) uint32_t dwPid = (uint32_t)pid; const uint8_t* p = (const uint8_t*)&dwPid; buf.insert(buf.end(), p, p + sizeof(uint32_t)); // Write exeFile (null-terminated) buf.insert(buf.end(), exeFile.begin(), exeFile.end()); buf.push_back(0); // Write fullPath (null-terminated) buf.insert(buf.end(), path.begin(), path.end()); buf.push_back(0); ++sent; } m_client->Send2Server((char*)buf.data(), (ULONG)buf.size()); Mprintf("[SystemManager] SendProcessList: %zu bytes, %zu processes\n", buf.size(), sent); } void SystemManager::killProcesses(const uint8_t* data, size_t size) { // Each PID is 4 bytes (DWORD),与 Windows/Linux 端一致 for (size_t i = 0; i + sizeof(uint32_t) <= size; i += sizeof(uint32_t)) { uint32_t dwPid; memcpy(&dwPid, data + i, sizeof(dwPid)); pid_t pid = (pid_t)dwPid; // Don't allow killing kernel/launchd(launchd 的 pid 是 1) if (pid <= 1) continue; // Don't allow killing ourselves if (pid == getpid()) continue; int ret = kill(pid, SIGKILL); Mprintf("[SystemManager] kill(%d, SIGKILL) = %d\n", (int)pid, ret); } usleep(100000); // 100ms 等待进程退出,之后的列表才不会还把死进程列出来 } void SystemManager::sendWindowsList() { if (!m_client) return; std::vector buf; buf.push_back(TOKEN_WSLIST); // macOS 没有 HWND。这里列「有可见窗口的应用」,句柄位置放 PID 充当伪句柄, // 与 Windows 的 TOKEN_WSLIST 同构(uint64 句柄 + NUL 结尾标题)。 NSArray* apps = [[NSWorkspace sharedWorkspace] runningApplications]; for (NSRunningApplication* app in apps) { // Only include apps with windows if (app.activationPolicy != NSApplicationActivationPolicyRegular) { continue; } NSString* name = app.localizedName; if (!name) continue; pid_t pid = app.processIdentifier; uint64_t hwnd = (uint64_t)pid; const uint8_t* p = (const uint8_t*)&hwnd; buf.insert(buf.end(), p, p + sizeof(uint64_t)); const char* utf8Name = [name UTF8String]; buf.insert(buf.end(), utf8Name, utf8Name + strlen(utf8Name)); buf.push_back(0); } m_client->Send2Server((char*)buf.data(), (ULONG)buf.size()); Mprintf("[SystemManager] SendWindowsList: %zu bytes\n", buf.size()); }