Merge pull request #96 from yuanyuanxiang/dev
Add logger.h and locker.h
This commit is contained in:
@@ -8,9 +8,6 @@ EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ghost", "client\ghost_vs2015.vcxproj", "{3F756E52-23C2-4EE4-A184-37CF788D50A7}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestRun", "client\TestRun_vs2015.vcxproj", "{B5D7F0E5-E735-4B17-91AE-866CE7E6ABD3}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{BEBAF888-532D-40D3-A8DD-DDAAF69F49AA} = {BEBAF888-532D-40D3-A8DD-DDAAF69F49AA}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ServerDll", "client\ClientDll_vs2015.vcxproj", "{BEBAF888-532D-40D3-A8DD-DDAAF69F49AA}"
|
||||
EndProject
|
||||
|
||||
@@ -24,12 +24,6 @@
|
||||
#pragma once
|
||||
#endif // _MSC_VER > 1000
|
||||
|
||||
#ifdef _DEBUG
|
||||
#define Mprintf(format, ...) printf(format, ##__VA_ARGS__)
|
||||
#else
|
||||
#define Mprintf(format, ...)
|
||||
#endif
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>й©<D0B9><C2A9><EFBFBD>谲װVLD<4C><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>ʹ<EFBFBD><CDB4><EFBFBD>
|
||||
#include "vld.h"
|
||||
|
||||
@@ -49,50 +43,6 @@
|
||||
#include <MMSystem.h>
|
||||
#pragma comment(lib, "winmm.lib")
|
||||
|
||||
// <20>߾<EFBFBD><DFBE>ȵ<EFBFBD>˯<EFBFBD>ߺ<EFBFBD><DFBA><EFBFBD>
|
||||
#define Sleep_m(ms) { timeBeginPeriod(1); Sleep(ms); timeEndPeriod(1); }
|
||||
|
||||
// <20>Բ<EFBFBD><D4B2><EFBFBD>n<EFBFBD><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD>µȴ<C2B5>T<EFBFBD><54>(n<>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1000)
|
||||
#define WAIT_n(C, T, n) {assert(!(1000%(n)));int s=(1000*(T))/(n);do{Sleep(n);}while((C)&&(--s));}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD>ʱ<EFBFBD>ȴ<EFBFBD>T<EFBFBD><54>(<28><><EFBFBD><EFBFBD>10ms)
|
||||
#define WAIT(C, T) { timeBeginPeriod(1); WAIT_n(C, T, 10); timeEndPeriod(1); }
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD>ʱ<EFBFBD>ȴ<EFBFBD>T<EFBFBD><54>(<28><><EFBFBD><EFBFBD>1ms)
|
||||
#define WAIT_1(C, T) { timeBeginPeriod(1); WAIT_n(C, T, 1); timeEndPeriod(1); }
|
||||
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// <20><><EFBFBD>ܼ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㺯<EFBFBD><E3BAAF><EFBFBD>ĺ<EFBFBD>ʱ
|
||||
class auto_tick
|
||||
{
|
||||
private:
|
||||
const char *func;
|
||||
int span;
|
||||
clock_t tick;
|
||||
__inline clock_t now() const { return clock(); }
|
||||
__inline int time() const { return now() - tick; }
|
||||
|
||||
public:
|
||||
auto_tick(const char *func_name, int th = 5) : func(func_name), span(th), tick(now()) { }
|
||||
~auto_tick() { stop(); }
|
||||
|
||||
__inline void stop() {
|
||||
if (span != 0) { int s(this->time()); if (s > span)Mprintf("[%s]ִ<><D6B4>ʱ<EFBFBD><CAB1>: [%d]ms.\n", func, s); span = 0; }
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef _DEBUG
|
||||
// <20><><EFBFBD>ܼ<EFBFBD><DCBC>㵱ǰ<E3B5B1><C7B0><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ӡ
|
||||
#define AUTO_TICK(thresh) auto_tick TICK(__FUNCTION__, thresh)
|
||||
#define STOP_TICK TICK.stop()
|
||||
#else
|
||||
#define AUTO_TICK(thresh)
|
||||
#define STOP_TICK
|
||||
#endif
|
||||
|
||||
#ifndef SAFE_DELETE
|
||||
#define SAFE_DELETE(p) if(NULL !=(p)){ delete (p);(p) = NULL;}
|
||||
#endif
|
||||
@@ -105,42 +55,5 @@ public:
|
||||
#define SAFE_DELETE_AR(p) if(NULL !=(p)){ delete[] (p);(p) = NULL;}
|
||||
#endif
|
||||
|
||||
class CLock
|
||||
{
|
||||
private:
|
||||
CRITICAL_SECTION m_cs;
|
||||
public:
|
||||
CLock()
|
||||
{
|
||||
InitializeCriticalSection(&m_cs);
|
||||
}
|
||||
~CLock()
|
||||
{
|
||||
DeleteCriticalSection(&m_cs);
|
||||
}
|
||||
|
||||
void Unlock()
|
||||
{
|
||||
LeaveCriticalSection(&m_cs);
|
||||
}
|
||||
|
||||
void Lock()
|
||||
{
|
||||
EnterCriticalSection(&m_cs);
|
||||
}
|
||||
};
|
||||
|
||||
class CAutoLock
|
||||
{
|
||||
private:
|
||||
CRITICAL_SECTION &m_cs;
|
||||
public:
|
||||
CAutoLock(CRITICAL_SECTION& cs) : m_cs(cs)
|
||||
{
|
||||
EnterCriticalSection(&m_cs);
|
||||
}
|
||||
~CAutoLock()
|
||||
{
|
||||
LeaveCriticalSection(&m_cs);
|
||||
}
|
||||
};
|
||||
#include "common/logger.h"
|
||||
#include "common/locker.h"
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <corecrt_io.h>
|
||||
#include "common/commands.h"
|
||||
#include "StdAfx.h"
|
||||
#include "MemoryModule.h"
|
||||
#include "ShellcodeInj.h"
|
||||
#include <WS2tcpip.h>
|
||||
#include <common/commands.h>
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
||||
// <20>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ֵ
|
||||
@@ -29,6 +26,8 @@ IsExit bExit = NULL;
|
||||
|
||||
BOOL status = 0;
|
||||
|
||||
HANDLE hEvent = NULL;
|
||||
|
||||
#ifdef _DEBUG
|
||||
CONNECT_ADDRESS g_ConnectAddress = { FLAG_FINDEN, "127.0.0.1", "6543", CLIENT_TYPE_DLL, false, DLL_VERSION, 0, Startup_InjSC };
|
||||
#else
|
||||
@@ -94,6 +93,7 @@ BOOL CALLBACK callback(DWORD CtrlType)
|
||||
if (CtrlType == CTRL_CLOSE_EVENT)
|
||||
{
|
||||
status = 1;
|
||||
if (hEvent) SetEvent(hEvent);
|
||||
if(stop) stop();
|
||||
while(1==status)
|
||||
Sleep(20);
|
||||
@@ -282,20 +282,28 @@ int main(int argc, const char *argv[])
|
||||
// If failed then run memory DLL
|
||||
ShellcodeInj inj;
|
||||
int pid = 0;
|
||||
hEvent = ::CreateEventA(NULL, TRUE, FALSE, NULL);
|
||||
do {
|
||||
if (sizeof(void*) == 4) // Shell code is 64bit
|
||||
break;
|
||||
if (!(pid = inj.InjectProcess(nullptr))) {
|
||||
break;
|
||||
}
|
||||
HANDLE hProcess = OpenProcess(SYNCHRONIZE, FALSE, pid);
|
||||
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, FALSE, pid);
|
||||
if (hProcess == NULL) {
|
||||
break;
|
||||
}
|
||||
Mprintf("Inject process [%d] succeed.\n", pid);
|
||||
DWORD waitResult = WaitForSingleObject(hProcess, INFINITE);
|
||||
HANDLE handles[2] = { hProcess, hEvent };
|
||||
DWORD waitResult = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
|
||||
if (status == 1) {
|
||||
TerminateProcess(hProcess, -1);
|
||||
CloseHandle(hEvent);
|
||||
}
|
||||
CloseHandle(hProcess);
|
||||
Mprintf("Process [%d] is finished.\n", pid);
|
||||
if (status == 1)
|
||||
return -1;
|
||||
} while (pid);
|
||||
}
|
||||
|
||||
|
||||
141
common/locker.h
Normal file
141
common/locker.h
Normal file
@@ -0,0 +1,141 @@
|
||||
#pragma once
|
||||
|
||||
#pragma warning(disable: 4996)
|
||||
#pragma warning(disable: 4819)
|
||||
|
||||
// 互斥锁、睡眠函数、自动锁、自动计时、自动日志等
|
||||
|
||||
#include "logger.h"
|
||||
|
||||
// 自动日志
|
||||
class CAutoLog
|
||||
{
|
||||
private:
|
||||
CRITICAL_SECTION *m_cs;
|
||||
const char* name;
|
||||
public:
|
||||
CAutoLog(const char* _name, CRITICAL_SECTION *cs=NULL) : name(_name), m_cs(cs)
|
||||
{
|
||||
Mprintf(">>> Enter thread %s: [%d]\n", name ? name : "", GetCurrentThreadId());
|
||||
if (m_cs)EnterCriticalSection(m_cs);
|
||||
}
|
||||
|
||||
~CAutoLog()
|
||||
{
|
||||
if (m_cs)LeaveCriticalSection(m_cs);
|
||||
Mprintf(">>> Leave thread %s: [%d]\n", name ? name : "", GetCurrentThreadId());
|
||||
}
|
||||
};
|
||||
|
||||
class CLock {
|
||||
public:
|
||||
CLock(CRITICAL_SECTION& cs) : m_cs(&cs)
|
||||
{
|
||||
Lock();
|
||||
}
|
||||
CLock() : m_cs(nullptr)
|
||||
{
|
||||
InitializeCriticalSection(&i_cs);
|
||||
}
|
||||
~CLock()
|
||||
{
|
||||
m_cs ? Unlock() : DeleteCriticalSection(&i_cs);
|
||||
}
|
||||
|
||||
void Unlock()
|
||||
{
|
||||
LeaveCriticalSection(m_cs ? m_cs : &i_cs);
|
||||
}
|
||||
|
||||
void Lock()
|
||||
{
|
||||
EnterCriticalSection(m_cs ? m_cs : &i_cs);
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
LeaveCriticalSection(m_cs ? m_cs : &i_cs);
|
||||
}
|
||||
|
||||
void lock()
|
||||
{
|
||||
EnterCriticalSection(m_cs ? m_cs : &i_cs);
|
||||
}
|
||||
|
||||
protected:
|
||||
CRITICAL_SECTION* m_cs; // 外部锁
|
||||
CRITICAL_SECTION i_cs; // 内部锁
|
||||
};
|
||||
|
||||
typedef CLock CLocker;
|
||||
|
||||
class CAutoLock
|
||||
{
|
||||
private:
|
||||
CRITICAL_SECTION &m_cs;
|
||||
|
||||
public:
|
||||
CAutoLock(CRITICAL_SECTION& cs) : m_cs(cs)
|
||||
{
|
||||
EnterCriticalSection(&m_cs);
|
||||
}
|
||||
|
||||
~CAutoLock()
|
||||
{
|
||||
LeaveCriticalSection(&m_cs);
|
||||
}
|
||||
};
|
||||
|
||||
// 智能计时器,计算函数的耗时
|
||||
class auto_tick {
|
||||
private:
|
||||
const char* func;
|
||||
int span;
|
||||
clock_t tick;
|
||||
__inline clock_t now() const {
|
||||
return clock();
|
||||
}
|
||||
__inline int time() const {
|
||||
return now() - tick;
|
||||
}
|
||||
|
||||
public:
|
||||
auto_tick(const char* func_name, int th = 5) : func(func_name), span(th), tick(now()) { }
|
||||
~auto_tick() {
|
||||
stop();
|
||||
}
|
||||
|
||||
__inline void stop() {
|
||||
if (span != 0) {
|
||||
int s(this->time());
|
||||
if (s > span)Mprintf("[%s] cost: [%d]ms.\n", func, s);
|
||||
span = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef _DEBUG
|
||||
// 智能计算当前函数的耗时,超时会打印
|
||||
#define AUTO_TICK(thresh) auto_tick TICK(__FUNCTION__, thresh)
|
||||
#define STOP_TICK TICK.stop()
|
||||
#else
|
||||
#define AUTO_TICK(thresh)
|
||||
#define STOP_TICK
|
||||
#endif
|
||||
|
||||
#define AUTO_TICK_C AUTO_TICK
|
||||
|
||||
#include <MMSystem.h>
|
||||
#pragma comment(lib, "winmm.lib")
|
||||
|
||||
// 高精度的睡眠函数
|
||||
#define Sleep_m(ms) { Sleep(ms); }
|
||||
|
||||
// 以步长n毫秒在条件C下等待T秒(n是步长,必须能整除1000)
|
||||
#define WAIT_n(C, T, n) { int s=(1000*(T))/(n); s=max(s,1); do{Sleep(n);}while((C)&&(--s)); }
|
||||
|
||||
// 在条件C成立时等待T秒(步长10ms)
|
||||
#define WAIT(C, T) { WAIT_n(C, T, 10); }
|
||||
|
||||
// 在条件C成立时等待T秒(步长1ms)
|
||||
#define WAIT_1(C, T) { WAIT_n(C, T, 1); }
|
||||
234
common/logger.h
Normal file
234
common/logger.h
Normal file
@@ -0,0 +1,234 @@
|
||||
#pragma once
|
||||
#pragma warning(disable: 4996)
|
||||
#ifdef _WIN32
|
||||
#ifdef _WINDOWS
|
||||
#include <afxwin.h>
|
||||
#else
|
||||
#include <windows.h>
|
||||
#endif
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <condition_variable>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <sstream>
|
||||
#include <cstdarg>
|
||||
#include "skCrypter.h"
|
||||
#include <iomanip>
|
||||
|
||||
|
||||
inline bool stringToBool(const std::string& str) {
|
||||
std::string lower = str;
|
||||
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
|
||||
return (lower == "true" || lower == "1");
|
||||
}
|
||||
|
||||
class Logger {
|
||||
public:
|
||||
enum LogLevel {
|
||||
InfoLevel, WarningLevel, ErrorLevel
|
||||
};
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>ģʽ
|
||||
static Logger& getInstance() {
|
||||
static Logger instance;
|
||||
if (instance.pid.empty()) {
|
||||
char buf[16] = {};
|
||||
sprintf_s(buf, "%d", GetCurrentProcessId());
|
||||
instance.pid = buf;
|
||||
instance.InitLogFile("C:\\Windows\\Temp", instance.pid);
|
||||
#ifdef _WINDOWS
|
||||
instance.enable = true; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־Ĭ<D6BE>ϴ<EFBFBD><CFB4><EFBFBD>
|
||||
#else
|
||||
char var[32] = {};
|
||||
const char* name = skCrypt("ENABLE_LOG");
|
||||
DWORD size = GetEnvironmentVariableA(name, var, sizeof(var));
|
||||
instance.enable = stringToBool(var);
|
||||
instance.log("logger.h", __LINE__, "GetEnvironmentVariable: %s=%s\n", name, var);
|
||||
#endif
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
// <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
||||
Logger(const Logger&) = delete;
|
||||
Logger& operator=(const Logger&) = delete;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
||||
void setLogFile(const std::string& filename) {
|
||||
std::lock_guard<std::mutex> lock(fileMutex);
|
||||
logFileName = filename;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
|
||||
void usingLog(bool b = true) {
|
||||
enable = b;
|
||||
}
|
||||
|
||||
// д<><D0B4>־<EFBFBD><D6BE>֧<EFBFBD><D6A7> printf <20><>ʽ<EFBFBD><CABD>
|
||||
void log(const char* file, int line, const char* format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
|
||||
std::string message = formatString(format, args);
|
||||
|
||||
va_end(args);
|
||||
|
||||
auto timestamp = getCurrentTimestamp();
|
||||
std::string id = pid.empty() ? "" : "[" + pid + "]";
|
||||
|
||||
std::string logEntry = id + "[" + timestamp + "] [" + file + ":" + std::to_string(line) + "] " + message;
|
||||
if (enable)
|
||||
{
|
||||
if (running) {
|
||||
std::lock_guard<std::mutex> lock(queueMutex);
|
||||
logQueue.push(logEntry);
|
||||
} else {
|
||||
writeToFile(logEntry);
|
||||
}
|
||||
}
|
||||
#ifndef _WINDOWS
|
||||
#ifdef _DEBUG
|
||||
printf(logEntry.c_str());
|
||||
#endif
|
||||
#endif
|
||||
cv.notify_one(); // ֪ͨд<D6AA>߳<EFBFBD>
|
||||
}
|
||||
|
||||
// ֹͣ<CDA3><D6B9>־ϵͳ
|
||||
void stop() {
|
||||
if (!running) return;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(queueMutex);
|
||||
running = false; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
|
||||
}
|
||||
cv.notify_one();
|
||||
if (workerThread.joinable()) {
|
||||
workerThread.join();
|
||||
}
|
||||
for (int i = 0; threadRun && i++ < 1000; Sleep(1));
|
||||
}
|
||||
|
||||
private:
|
||||
// <20><>־<EFBFBD><D6BE><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
void InitLogFile(const std::string & dir, const std::string& pid) {
|
||||
time_t currentTime = time(nullptr);
|
||||
tm* localTime = localtime(¤tTime);
|
||||
char timeString[32];
|
||||
strftime(timeString, sizeof(timeString), "%Y-%m", localTime);
|
||||
char fileName[100];
|
||||
#ifdef _WINDOWS
|
||||
sprintf_s(fileName, "\\YAMA_%s_%s.txt", timeString, pid.c_str());
|
||||
#else
|
||||
sprintf_s(fileName, "\\log_%s_%s.txt", timeString, pid.c_str());
|
||||
#endif
|
||||
logFileName = dir + fileName;
|
||||
}
|
||||
std::string logFileName; // <20><>־<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
||||
bool enable; // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
bool threadRun; // <20><>־<EFBFBD>߳<EFBFBD>״̬
|
||||
std::queue<std::string> logQueue; // <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
|
||||
std::mutex queueMutex; // <20><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD>
|
||||
std::condition_variable cv; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
std::atomic<bool> running; // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
std::thread workerThread; // <20><>̨<EFBFBD>߳<EFBFBD>
|
||||
std::mutex fileMutex; // <20>ļ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
|
||||
std::string pid; // <20><><EFBFBD><EFBFBD>ID
|
||||
|
||||
Logger() : enable(false), threadRun(false), running(true), workerThread(&Logger::processLogs, this) {}
|
||||
|
||||
~Logger() {
|
||||
stop();
|
||||
}
|
||||
|
||||
// <20><>̨<EFBFBD>̴߳<DFB3><CCB4><EFBFBD><EFBFBD><EFBFBD>־
|
||||
void processLogs() {
|
||||
threadRun = true;
|
||||
while (running) {
|
||||
std::unique_lock<std::mutex> lock(queueMutex);
|
||||
cv.wait(lock, [this]() {
|
||||
return !running || !logQueue.empty();
|
||||
});
|
||||
|
||||
while (running && !logQueue.empty()) {
|
||||
std::string logEntry = logQueue.front();
|
||||
logQueue.pop();
|
||||
lock.unlock();
|
||||
|
||||
// д<><D0B4><EFBFBD><EFBFBD>־<EFBFBD>ļ<EFBFBD>
|
||||
writeToFile(logEntry);
|
||||
|
||||
lock.lock();
|
||||
}
|
||||
lock.unlock();
|
||||
}
|
||||
threadRun = false;
|
||||
}
|
||||
|
||||
// д<><D0B4><EFBFBD>ļ<EFBFBD>
|
||||
void writeToFile(const std::string& logEntry) {
|
||||
std::lock_guard<std::mutex> lock(fileMutex);
|
||||
std::ofstream logFile(logFileName, std::ios::app);
|
||||
if (logFile.is_open()) {
|
||||
logFile << logEntry << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// <20><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1><EFBFBD><EFBFBD>
|
||||
std::string getCurrentTimestamp() {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto in_time_t = std::chrono::system_clock::to_time_t(now);
|
||||
|
||||
std::tm tm;
|
||||
#ifdef _WIN32
|
||||
localtime_s(&tm, &in_time_t); // Windows <20><>ȫ<EFBFBD>汾
|
||||
#else
|
||||
localtime_r(&in_time_t, &tm); // POSIX <20><>ȫ<EFBFBD>汾
|
||||
#endif
|
||||
|
||||
std::stringstream ss;
|
||||
ss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
||||
std::string logLevelToString(LogLevel level) {
|
||||
switch (level) {
|
||||
case InfoLevel: return "INFO";
|
||||
case WarningLevel: return "WARNING";
|
||||
case ErrorLevel: return "ERROR";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
// <20><>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
||||
std::string formatString(const char* format, va_list args) {
|
||||
char buffer[1024];
|
||||
vsnprintf(buffer, sizeof(buffer), format, args);
|
||||
return std::string(buffer);
|
||||
}
|
||||
};
|
||||
|
||||
inline const char* getFileName(const char* path) {
|
||||
const char* fileName = strrchr(path, '\\');
|
||||
if (!fileName) {
|
||||
fileName = strrchr(path, '/');
|
||||
}
|
||||
return fileName ? fileName + 1 : path;
|
||||
}
|
||||
|
||||
#ifdef _WINDOWS
|
||||
#ifdef _DEBUG
|
||||
#define Mprintf(format, ...) TRACE(format, __VA_ARGS__)
|
||||
#else
|
||||
#define Mprintf(format, ...) Logger::getInstance().log(getFileName(__FILE__), __LINE__, format, __VA_ARGS__)
|
||||
#endif
|
||||
#else
|
||||
#define Mprintf(format, ...) Logger::getInstance().log(getFileName(skCrypt(__FILE__)), __LINE__, skCrypt(format), __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#endif // _WIN32
|
||||
Binary file not shown.
@@ -29,6 +29,7 @@
|
||||
#include <fstream>
|
||||
#include "common/skCrypter.h"
|
||||
#include "common/commands.h"
|
||||
#include <algorithm>
|
||||
|
||||
#ifdef _DEBUG
|
||||
#define new DEBUG_NEW
|
||||
@@ -246,6 +247,8 @@ BEGIN_MESSAGE_MAP(CMy2015RemoteDlg, CDialogEx)
|
||||
ON_COMMAND(ID_TOOL_GEN_MASTER, &CMy2015RemoteDlg::OnToolGenMaster)
|
||||
ON_COMMAND(ID_MAIN_PROXY, &CMy2015RemoteDlg::OnMainProxy)
|
||||
ON_COMMAND(ID_ONLINE_HOSTNOTE, &CMy2015RemoteDlg::OnOnlineHostnote)
|
||||
ON_COMMAND(ID_HELP_IMPORTANT, &CMy2015RemoteDlg::OnHelpImportant)
|
||||
ON_COMMAND(ID_HELP_FEEDBACK, &CMy2015RemoteDlg::OnHelpFeedback)
|
||||
END_MESSAGE_MAP()
|
||||
|
||||
|
||||
@@ -370,14 +373,14 @@ VOID CMy2015RemoteDlg::InitControl()
|
||||
GetWindowRect(&rect);
|
||||
rect.bottom+=20;
|
||||
MoveWindow(rect);
|
||||
|
||||
auto style = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_DOUBLEBUFFER | LVS_EX_HEADERDRAGDROP | LVS_EX_LABELTIP;
|
||||
for (int i = 0;i<g_Column_Count_Online;++i)
|
||||
{
|
||||
m_CList_Online.InsertColumn(i, g_Column_Data_Online[i].szTitle,LVCFMT_CENTER,g_Column_Data_Online[i].nWidth);
|
||||
|
||||
g_Column_Online_Width+=g_Column_Data_Online[i].nWidth;
|
||||
}
|
||||
m_CList_Online.SetExtendedStyle(LVS_EX_FULLROWSELECT);
|
||||
m_CList_Online.SetExtendedStyle(style);
|
||||
|
||||
for (int i = 0; i < g_Column_Count_Message; ++i)
|
||||
{
|
||||
@@ -385,7 +388,7 @@ VOID CMy2015RemoteDlg::InitControl()
|
||||
g_Column_Message_Width+=g_Column_Data_Message[i].nWidth;
|
||||
}
|
||||
|
||||
m_CList_Message.SetExtendedStyle(LVS_EX_FULLROWSELECT);
|
||||
m_CList_Message.SetExtendedStyle(style);
|
||||
}
|
||||
|
||||
|
||||
@@ -1193,6 +1196,72 @@ BOOL CMy2015RemoteDlg::ListenPort()
|
||||
}
|
||||
|
||||
|
||||
std::string exec(const std::string& cmd) {
|
||||
HANDLE hReadPipe, hWritePipe;
|
||||
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
|
||||
|
||||
if (!CreatePipe(&hReadPipe, &hWritePipe, &sa, 0)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
STARTUPINFOA si = {};
|
||||
PROCESS_INFORMATION pi = {};
|
||||
si.cb = sizeof(si);
|
||||
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
||||
si.hStdOutput = hWritePipe;
|
||||
si.hStdError = hWritePipe;
|
||||
si.wShowWindow = SW_HIDE;
|
||||
|
||||
std::string command = "cmd.exe /C " + cmd;
|
||||
|
||||
if (!CreateProcessA(
|
||||
NULL,
|
||||
(char*)command.data(),
|
||||
NULL,
|
||||
NULL,
|
||||
TRUE,
|
||||
CREATE_NO_WINDOW,
|
||||
NULL,
|
||||
NULL,
|
||||
&si,
|
||||
&pi
|
||||
)) {
|
||||
CloseHandle(hReadPipe);
|
||||
CloseHandle(hWritePipe);
|
||||
return "";
|
||||
}
|
||||
|
||||
CloseHandle(hWritePipe);
|
||||
|
||||
char buffer[256];
|
||||
std::string result;
|
||||
DWORD bytesRead;
|
||||
|
||||
while (ReadFile(hReadPipe, buffer, sizeof(buffer) - 1, &bytesRead, NULL) && bytesRead > 0) {
|
||||
buffer[bytesRead] = '\0';
|
||||
result += buffer;
|
||||
}
|
||||
|
||||
CloseHandle(hReadPipe);
|
||||
WaitForSingleObject(pi.hProcess, INFINITE);
|
||||
CloseHandle(pi.hProcess);
|
||||
CloseHandle(pi.hThread);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::string> splitByNewline(const std::string& input) {
|
||||
std::vector<std::string> lines;
|
||||
std::istringstream stream(input);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(stream, line)) {
|
||||
lines.push_back(line);
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
BOOL CMy2015RemoteDlg::Activate(int nPort,int nMaxConnection)
|
||||
{
|
||||
assert(m_iocpServer);
|
||||
@@ -1200,9 +1269,34 @@ BOOL CMy2015RemoteDlg::Activate(int nPort,int nMaxConnection)
|
||||
if ( (ret=m_iocpServer->StartServer(NotifyProc, OfflineProc, nPort)) !=0 )
|
||||
{
|
||||
Mprintf("======> StartServer Failed \n");
|
||||
char code[32];
|
||||
sprintf_s(code, "%d", ret);
|
||||
MessageBox("<EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD>StartServerʧ<EFBFBD><EFBFBD>! <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:"+CString(code));
|
||||
char cmd[200];
|
||||
sprintf_s(cmd, "for /f \"tokens=5\" %%i in ('netstat -ano ^| findstr \":%d \"') do @echo %%i", nPort);
|
||||
std::string output = exec(cmd);
|
||||
output.erase(std::remove(output.begin(), output.end(), '\r'), output.end());
|
||||
if (!output.empty())
|
||||
{
|
||||
std::vector<std::string> lines = splitByNewline(output);
|
||||
std::sort(lines.begin(), lines.end());
|
||||
auto last = std::unique(lines.begin(), lines.end());
|
||||
lines.erase(last, lines.end());
|
||||
|
||||
std::string pids;
|
||||
for (const auto& line : lines) {
|
||||
pids += line + ",";
|
||||
}
|
||||
if (!pids.empty()) {
|
||||
pids.back() = '?';
|
||||
}
|
||||
if (IDYES == MessageBox("<EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD>StartServerʧ<EFBFBD><EFBFBD>! <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:" + CString(std::to_string(ret).c_str()) +
|
||||
"\r\n<EFBFBD>Ƿ<EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: " + pids.c_str(), "<EFBFBD><EFBFBD>ʾ", MB_YESNO)) {
|
||||
for (const auto& line : lines) {
|
||||
auto cmd = std::string("taskkill /f /pid ") + line;
|
||||
exec(cmd.c_str());
|
||||
}
|
||||
return Activate(nPort, nMaxConnection);
|
||||
}
|
||||
}else
|
||||
MessageBox("<EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD>StartServerʧ<EFBFBD><EFBFBD>! <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:" + CString(std::to_string(ret).c_str()));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
@@ -2182,3 +2276,20 @@ void CMy2015RemoteDlg::OnToolGenMaster()
|
||||
}
|
||||
SAFE_DELETE_ARRAY(curEXE);
|
||||
}
|
||||
|
||||
|
||||
void CMy2015RemoteDlg::OnHelpImportant()
|
||||
{
|
||||
const char* msg =
|
||||
"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԡ<EFBFBD><EFBFBD><EFBFBD>״<EFBFBD><EFBFBD><EFBFBD>ṩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κα<EFBFBD>֤<EFBFBD><EFBFBD>ʹ<EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ге<EFBFBD><EFBFBD><EFBFBD>"
|
||||
"<EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķǷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ط<EFBFBD><EFBFBD><EFBFBD>"
|
||||
"<EFBFBD><EFBFBD><EFBFBD>棬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ε<EFBFBD>ʹ<EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ρ<EFBFBD>";
|
||||
MessageBox(msg, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", MB_ICONINFORMATION);
|
||||
}
|
||||
|
||||
|
||||
void CMy2015RemoteDlg::OnHelpFeedback()
|
||||
{
|
||||
CString url = _T("https://github.com/yuanyuanxiang/SimpleRemoter/issues/new");
|
||||
ShellExecute(NULL, _T("open"), url, NULL, NULL, SW_SHOWNORMAL);
|
||||
}
|
||||
|
||||
@@ -220,4 +220,6 @@ public:
|
||||
afx_msg void OnToolGenMaster();
|
||||
afx_msg void OnMainProxy();
|
||||
afx_msg void OnOnlineHostnote();
|
||||
afx_msg void OnHelpImportant();
|
||||
afx_msg void OnHelpFeedback();
|
||||
};
|
||||
|
||||
@@ -211,8 +211,12 @@
|
||||
</ResourceCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\Release\ghost.exe" />
|
||||
<None Include="..\..\Release\ServerDll.dll" />
|
||||
<None Include="..\..\Release\TestRun.exe" />
|
||||
<None Include="..\..\x64\Release\ghost.exe" />
|
||||
<None Include="..\..\x64\Release\ServerDll.dll" />
|
||||
<None Include="..\..\x64\Release\TestRun.exe" />
|
||||
<None Include="res\1.cur" />
|
||||
<None Include="res\2.cur" />
|
||||
<None Include="res\2015Remote.ico" />
|
||||
|
||||
@@ -16,4 +16,7 @@
|
||||
<LocalDebuggerCommand>$(SolutionDir)Bin\$(TargetName)_x64d$(TargetExt)</LocalDebuggerCommand>
|
||||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<RESOURCE_FILE>2015Remote.rc</RESOURCE_FILE>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -373,47 +373,6 @@ public:
|
||||
|
||||
typedef IOCPServer ISocketBase;
|
||||
|
||||
class CLock
|
||||
{
|
||||
public:
|
||||
CLock(CRITICAL_SECTION& cs) : m_cs(&cs)
|
||||
{
|
||||
Lock();
|
||||
}
|
||||
CLock() : m_cs(nullptr)
|
||||
{
|
||||
InitializeCriticalSection(&i_cs);
|
||||
}
|
||||
~CLock()
|
||||
{
|
||||
m_cs ? Unlock() : DeleteCriticalSection(&i_cs);
|
||||
}
|
||||
|
||||
void Unlock()
|
||||
{
|
||||
LeaveCriticalSection(m_cs ? m_cs : &i_cs);
|
||||
}
|
||||
|
||||
void Lock()
|
||||
{
|
||||
EnterCriticalSection(m_cs ? m_cs : &i_cs);
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
LeaveCriticalSection(m_cs ? m_cs : &i_cs);
|
||||
}
|
||||
|
||||
void lock()
|
||||
{
|
||||
EnterCriticalSection(m_cs ? m_cs : &i_cs);
|
||||
}
|
||||
|
||||
protected:
|
||||
CRITICAL_SECTION* m_cs; // <20>ⲿ<EFBFBD><E2B2BF>
|
||||
CRITICAL_SECTION i_cs; // <20>ڲ<EFBFBD><DAB2><EFBFBD>
|
||||
};
|
||||
|
||||
#define TRACK_OVERLAPPEDPLUS 0
|
||||
|
||||
class OVERLAPPEDPLUS
|
||||
|
||||
Binary file not shown.
@@ -20,12 +20,6 @@
|
||||
#define VC_EXTRALEAN // <20><> Windows ͷ<><CDB7><EFBFBD>ų<EFBFBD><C5B3><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD>
|
||||
#endif
|
||||
|
||||
#ifdef _DEBUG
|
||||
#define Mprintf(format, ...) TRACE(format, ##__VA_ARGS__)
|
||||
#else
|
||||
#define Mprintf(format, ...)
|
||||
#endif
|
||||
|
||||
// <20>Ƴ<EFBFBD><C6B3>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD>MFC<46>ؼ<EFBFBD><D8BC><EFBFBD>֧<EFBFBD>֣<EFBFBD><D6A3><EFBFBD>С<EFBFBD><D0A1>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD>С
|
||||
#define _AFX_NO_MFC_CONTROLS_IN_DIALOGS
|
||||
|
||||
@@ -117,47 +111,9 @@ enum
|
||||
#include <MMSystem.h>
|
||||
#pragma comment(lib, "winmm.lib")
|
||||
|
||||
// <20>߾<EFBFBD><DFBE>ȵ<EFBFBD>˯<EFBFBD>ߺ<EFBFBD><DFBA><EFBFBD>
|
||||
#define Sleep_m(ms) { timeBeginPeriod(1); Sleep(ms); timeEndPeriod(1); }
|
||||
|
||||
// <20>Բ<EFBFBD><D4B2><EFBFBD>n<EFBFBD><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD>µȴ<C2B5>T<EFBFBD><54>(n<>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1000)
|
||||
#define WAIT_n(C, T, n) {assert(!(1000%(n)));int s=(1000*(T))/(n);do{Sleep(n);}while((C)&&(--s));}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD>ʱ<EFBFBD>ȴ<EFBFBD>T<EFBFBD><54>(<28><><EFBFBD><EFBFBD>10ms)
|
||||
#define WAIT(C, T) { timeBeginPeriod(1); WAIT_n(C, T, 10); timeEndPeriod(1); }
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD>ʱ<EFBFBD>ȴ<EFBFBD>T<EFBFBD><54>(<28><><EFBFBD><EFBFBD>1ms)
|
||||
#define WAIT_1(C, T) { timeBeginPeriod(1); WAIT_n(C, T, 1); timeEndPeriod(1); }
|
||||
|
||||
// <20><><EFBFBD>ܼ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㺯<EFBFBD><E3BAAF><EFBFBD>ĺ<EFBFBD>ʱ
|
||||
class auto_tick
|
||||
{
|
||||
private:
|
||||
const char *func;
|
||||
int span;
|
||||
clock_t tick;
|
||||
__inline clock_t now() const { return clock(); }
|
||||
__inline int time() const { return now() - tick; }
|
||||
|
||||
public:
|
||||
auto_tick(const char *func_name, int th = 5) : func(func_name), span(th), tick(now()) { }
|
||||
~auto_tick() { stop(); }
|
||||
|
||||
__inline void stop() {
|
||||
if (span != 0) { int s(this->time()); if (s > span)TRACE("[%s]ִ<><D6B4>ʱ<EFBFBD><CAB1>: [%d]ms.\n", func, s); span = 0; }
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef _DEBUG
|
||||
// <20><><EFBFBD>ܼ<EFBFBD><DCBC>㵱ǰ<E3B5B1><C7B0><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ӡ
|
||||
#define AUTO_TICK(thresh) auto_tick TICK(__FUNCTION__, thresh)
|
||||
#define STOP_TICK TICK.stop()
|
||||
#else
|
||||
#define AUTO_TICK(thresh)
|
||||
#define STOP_TICK
|
||||
#endif
|
||||
|
||||
#define SAFE_DELETE(p) if(p){ delete (p); (p) = NULL; }
|
||||
#define SAFE_DELETE_ARRAY(p) if(p){ delete[] (p); (p) = NULL; }
|
||||
|
||||
#include "common/logger.h"
|
||||
#include "common/locker.h"
|
||||
#include "common/commands.h"
|
||||
|
||||
Reference in New Issue
Block a user