当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 0

碧海青天/daemon
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
daemon.hpp 9.54 KB
一键复制 编辑 原始数据 按行查看 历史
碧海青天 提交于 2019-05-30 18:03 . no message
#pragma once
#pragma warning(disable: 4312)
#pragma warning(disable: 26444)
#pragma warning(disable: 28253)
#ifndef DAEMON_HPP
#define DAEMON_HPP
#include <list>
#include <ctime>
#include <fstream>
#include <windows.h>
#include <tlhelp32.h>
#include "code.hpp"
#include "Yaml.hpp"
using namespace std;
struct BASEINFO
{
DWORD dwPid;
DWORD dwPpid;
wstring wsName;
wstring wsPname;
};
struct PROCINFO {
DWORD dwPid;
DWORD dwPpid;
wstring wsName;
SYSTEMTIME stCreate;
};
wstring wsLog = L"daemon.log";
void Log(wstring wstr)
{
SYSTEMTIME stLocal;
GetLocalTime(&stLocal);
WCHAR wszTime[MAX_PATH];
wsprintf(wszTime, L"[%02d:%02d:%02d.%03d] ", stLocal.wHour, stLocal.wMinute, stLocal.wSecond, stLocal.wMilliseconds);
wstr = wstring(wszTime) + wstr;
wcout << wstr << endl;
wofstream wfout(wsLog, ios::app);
wfout << wstr << endl;
wfout.close();
}
inline void EnableDebugPrivilege()
{
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
throw runtime_error("open process token fail");
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tp.Privileges[0].Luid))
throw runtime_error("look up privilege value fail");
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL))
throw runtime_error("adjust token privileges fail");
CloseHandle(hToken);
}
inline void InitBaseInfo(BASEINFO &baseInfo, list<PROCINFO> &procInfoList)
{
baseInfo.dwPid = GetCurrentProcessId();
for (list<PROCINFO>::iterator iter = procInfoList.begin(); iter != procInfoList.end(); iter++)
if ((*iter).dwPid == baseInfo.dwPid)
{
baseInfo.dwPpid = (*iter).dwPpid;
baseInfo.wsName = (*iter).wsName;
wsLog = baseInfo.wsName + L".log";
wstringReplace(wsLog, L".exe", L"");
}
for (list<PROCINFO>::iterator iter = procInfoList.begin(); iter != procInfoList.end(); iter++)
if ((*iter).dwPid == baseInfo.dwPpid)
baseInfo.wsPname = (*iter).wsName;
if (!baseInfo.dwPid || !baseInfo.dwPpid || !baseInfo.wsName.length() || !baseInfo.wsPname.length())
throw runtime_error("init base info fail");
}
inline SYSTEMTIME ft2st(FILETIME ft)
{
SYSTEMTIME stUTC, stLocal;
FileTimeToSystemTime(&ft, &stUTC);
SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
return stLocal;
}
inline time_t st2timet(SYSTEMTIME st)
{
tm timet = { 0 };
timet.tm_year = st.wYear - 1900;
timet.tm_mon = st.wMonth - 1;
timet.tm_mday = st.wDay;
timet.tm_hour = st.wHour;
timet.tm_min = st.wMinute;
timet.tm_sec = st.wSecond;
timet.tm_isdst = -1;
return mktime(&timet);
}
inline SYSTEMTIME GetLastWriteTime(string sFileName) {
OFSTRUCT buffer = { 0 };
HFILE hFile = OpenFile(sFileName.c_str(), &buffer, OF_READ);
if (hFile == HFILE_ERROR)
throw runtime_error("open file fail");
FILETIME ftCreate, ftAccess, ftWrite;
if (!GetFileTime((HANDLE)hFile, &ftCreate, &ftAccess, &ftWrite))
throw runtime_error("get file time fail");
SYSTEMTIME stWrite = ft2st(ftWrite);
CloseHandle((HANDLE)hFile);
return stWrite;
}
inline list<PROCINFO> EnumProcInfo(bool enumCreateTime = false)
{
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap == INVALID_HANDLE_VALUE)
throw runtime_error("create tool help 32 snapshot fail");
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnap, &pe32))
throw runtime_error("process 32 first fail");
list<PROCINFO> procInfoList;
do
{
PROCINFO procInfo = { 0 };
procInfo.dwPid = pe32.th32ProcessID;
procInfo.dwPpid = pe32.th32ParentProcessID;
procInfo.wsName = pe32.szExeFile;
wstring2lower(procInfo.wsName);
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procInfo.dwPid);
if (hProc)
{
FILETIME ftCreate, ftExit, ftKernel, ftUser;
if (GetProcessTimes(hProc, &ftCreate, &ftExit, &ftKernel, &ftUser))
procInfo.stCreate = ft2st(ftCreate);
CloseHandle(hProc);
}
procInfoList.push_back(procInfo);
} while (Process32Next(hSnap, &pe32));
CloseHandle(hSnap);
return procInfoList;
}
inline bool IsProcessAlive(DWORD dwPid)
{
bool flag = false;
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid);
if (hProc)
{
flag = true;
CloseHandle(hProc);
}
return flag;
}
inline DWORD StartupProcess(wstring wsCmd, DWORD dwCreationFlags = CREATE_NO_WINDOW)
{
WCHAR wszCmd[MAX_PATH];
wcscpy(wszCmd, wsCmd.c_str());
PROCESS_INFORMATION pi = { 0 };
STARTUPINFO si = { 0 };;
si.cb = sizeof(si);
if (!CreateProcess(NULL, wszCmd, NULL, NULL, FALSE, dwCreationFlags, NULL, NULL, &si, &pi))
return NULL;
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return pi.dwProcessId;
}
inline bool ShutdownProcess(DWORD dwProcId)
{
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcId);
if (hProc)
{
BOOL flag = TerminateProcess(hProc, 0);
CloseHandle(hProc);
if (flag)
return true;
}
return false;
}
inline void PostSendMessage(wstring wsClass, wstring wsTitle, UINT Msg, WPARAM wParam = NULL)
{
HWND hWnd = FindWindow(wsClass.c_str(), wsTitle.c_str());
if (hWnd) {
PostMessage(hWnd, Msg, wParam, NULL);
SendMessage(hWnd, Msg, wParam, NULL);
}
}
inline void KeepAlive(Yaml::Node& conf, list<PROCINFO> &procInfoList)
{
if (conf.IsSequence() && procInfoList.size())
for (int i = 0; i < conf.Size(); i++)
{
bool exist = false;
wstring wsName = ansi2unicode(conf[i]["name"].As<string>());
wstring2lower(wsName);
for (list<PROCINFO>::iterator iter = procInfoList.begin(); iter != procInfoList.end(); iter++)
if ((*iter).wsName == wsName)
exist = true;
if (!exist)
{
wstring wsCmd = ansi2unicode(conf[i]["command"].As<string>()).c_str();
Log(L"keep alive item not exist, " + wsName + L", try execute command, " + wsCmd);
if (!StartupProcess(wsCmd))
Log(L"command execute fail, something was wrong");
}
}
}
inline void TimingShutdown(Yaml::Node& conf, list<PROCINFO>& procInfoList)
{
if (conf.IsSequence() && procInfoList.size())
{
SYSTEMTIME stLocal;
GetLocalTime(&stLocal);
wstring wsLocal = to_wstring(stLocal.wHour) + L":" + to_wstring(stLocal.wMinute);
for (int i = 0; i < conf.Size(); i++)
if (wsLocal == ansi2unicode(conf[i]["timing"].As<string>()))
{
wstring wsName = ansi2unicode(conf[i]["name"].As<string>());
wstring2lower(wsName);
for (list<PROCINFO>::iterator iter = procInfoList.begin(); iter != procInfoList.end(); iter++)
if ((*iter).wsName == wsName && st2timet(stLocal) - st2timet((*iter).stCreate) >= 60)
{
Log(L"timing shutdown item exist, " + wsName + L", try shutdown process");
if (!ShutdownProcess((*iter).dwPid))
Log(L"process shutdown fail, something was wrong");
}
}
}
}
inline void IntervalShutdown(Yaml::Node& conf, list<PROCINFO>& procInfoList)
{
if (conf.IsSequence() && procInfoList.size())
{
SYSTEMTIME stLocal;
GetLocalTime(&stLocal);
for (int i = 0; i < conf.Size(); i++)
{
wstring wsName = ansi2unicode(conf[i]["name"].As<string>());
wstring2lower(wsName);
for (list<PROCINFO>::iterator iter = procInfoList.begin(); iter != procInfoList.end(); iter++)
if ((*iter).wsName == wsName && st2timet(stLocal) - st2timet((*iter).stCreate) >= conf[i]["interval"].As<int>())
{
Log(L"interval shutdown item exist, " + wsName + L", try shutdown process");
if (!ShutdownProcess((*iter).dwPid))
Log(L"process shutdown fail, something was wrong");
}
}
}
}
inline void WindowMessage(Yaml::Node& conf)
{
if (conf.IsSequence())
for (int i = 0; i < conf.Size(); i++)
{
wstring wsTitle = ansi2unicode(conf[i]["title"].As<string>());
wstring wsClass = ansi2unicode(conf[i]["class"].As<string>());
wstring wsMessage = ansi2unicode(conf[i]["message"].As<string>());
wstringReplace(wsTitle, L"//", L"#");
wstringReplace(wsClass, L"//", L"#");
wstring2lower(wsMessage);
if (wsMessage == L"close")
PostSendMessage(wsClass, wsTitle, WM_CLOSE);
else if (wsMessage == L"destroy")
PostSendMessage(wsClass, wsTitle, WM_DESTROY);
else if (wsMessage == L"keyentry")
PostSendMessage(wsClass, wsTitle, WM_KEYDOWN, VK_RETURN);
else if (wsMessage == L"keyesc")
PostSendMessage(wsClass, wsTitle, WM_KEYDOWN, VK_ESCAPE);
}
}
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/gwsbhqt/daemon.git
git@gitee.com:gwsbhqt/daemon.git
gwsbhqt
daemon
daemon
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385