Remove the older pre-Qt code.
This commit is contained in:
parent
6021840a1f
commit
7898332d9e
9
.gitignore
vendored
9
.gitignore
vendored
@ -1,12 +1,3 @@
|
||||
CMakeCache.txt
|
||||
CMakeLists.txt.user
|
||||
CMakeLists.txt.user.2.3pre1
|
||||
CMakeFiles
|
||||
Makefile
|
||||
*.exe
|
||||
build.sh
|
||||
Scrape.cbp
|
||||
cmake_install.cmake
|
||||
*.pro.user
|
||||
*.pro.user.2.3pre1
|
||||
*-build-desktop
|
||||
|
113
Agent.cc
113
Agent.cc
@ -1,113 +0,0 @@
|
||||
#include "AgentIo.h"
|
||||
#include "DebugClient.h"
|
||||
#include <windows.h>
|
||||
#include <assert.h>
|
||||
#include <QCoreApplication>
|
||||
|
||||
#define DIM(x) (sizeof(x) / sizeof((x)[0]))
|
||||
|
||||
static HANDLE coninHandle;
|
||||
static HANDLE conoutHandle;
|
||||
|
||||
static void DoRequest(AgentSharedMemory *memory);
|
||||
|
||||
|
||||
// The agent is started with a hidden console (CONSOLE_NO_WINDOW).
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
|
||||
|
||||
|
||||
return app.exec();
|
||||
#if 0
|
||||
assert(argc == 2);
|
||||
HANDLE mapping = OpenFileMapping(FILE_MAP_WRITE, FALSE, argv[1]);
|
||||
assert(mapping != NULL);
|
||||
AgentSharedMemory *sharedMemory = (AgentSharedMemory*)MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
|
||||
assert(sharedMemory != NULL);
|
||||
|
||||
coninHandle = CreateFile(
|
||||
"CONIN$",
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
NULL, OPEN_EXISTING, 0, NULL);
|
||||
conoutHandle = CreateFile(
|
||||
"CONOUT$",
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
NULL, OPEN_EXISTING, 0, NULL);
|
||||
assert(coninHandle != NULL);
|
||||
assert(conoutHandle != NULL);
|
||||
|
||||
HANDLE handles[2] = {
|
||||
sharedMemory->clientProcess,
|
||||
sharedMemory->requestEvent
|
||||
};
|
||||
|
||||
sharedMemory->hwnd = GetConsoleWindow();
|
||||
|
||||
while (true) {
|
||||
Trace("waiting");
|
||||
DWORD ret = WaitForMultipleObjects(DIM(handles), handles, FALSE, INFINITE);
|
||||
HANDLE signaled = NULL;
|
||||
if (ret >= WAIT_OBJECT_0 && (ret - WAIT_OBJECT_0) < DIM(handles))
|
||||
signaled = handles[ret - WAIT_OBJECT_0];
|
||||
if (signaled == sharedMemory->clientProcess) {
|
||||
// The client has exited. The agent should exit. What about the
|
||||
// console? The client could have crashed, so I think we need to
|
||||
// kill the console itself somehow.
|
||||
// TODO: cleanup...
|
||||
break;
|
||||
} else if (signaled == sharedMemory->requestEvent) {
|
||||
DoRequest(sharedMemory);
|
||||
SetEvent(sharedMemory->replyEvent);
|
||||
} else {
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
Trace("agent shutting down");
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void DoRequest(AgentSharedMemory *memory)
|
||||
{
|
||||
switch (memory->requestKind) {
|
||||
case rkInit:
|
||||
// Do nothing.
|
||||
break;
|
||||
case rkWriteConsoleInput: {
|
||||
DWORD actual = 0;
|
||||
memory->u.input.result = WriteConsoleInput(coninHandle,
|
||||
memory->u.input.records,
|
||||
memory->u.input.count,
|
||||
&memory->u.input.resultWritten);
|
||||
break;
|
||||
}
|
||||
case rkGetConsoleTitle:
|
||||
GetConsoleTitle(memory->u.title, sizeof(memory->u.title));
|
||||
break;
|
||||
case rkSetConsoleTitle:
|
||||
SetConsoleTitle(memory->u.title);
|
||||
break;
|
||||
case rkRead: {
|
||||
CONSOLE_SCREEN_BUFFER_INFO &info = memory->read.info;
|
||||
GetConsoleScreenBufferInfo(conoutHandle, &info);
|
||||
COORD bufferSize, bufferCoord;
|
||||
SMALL_RECT readRegion;
|
||||
readRegion = info.srWindow;
|
||||
bufferSize.X = readRegion.Right - readRegion.Left + 1;
|
||||
bufferSize.Y = readRegion.Bottom - readRegion.Top + 1;
|
||||
bufferCoord.X = bufferCoord.Y = 0;
|
||||
ReadConsoleOutput(conoutHandle,
|
||||
memory->read.windowContent,
|
||||
bufferSize,
|
||||
bufferCoord,
|
||||
&readRegion);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
26
AgentIo.cc
26
AgentIo.cc
@ -1,26 +0,0 @@
|
||||
#include "AgentIo.h"
|
||||
#include <assert.h>
|
||||
|
||||
// The shared memory is initialized on the client.
|
||||
void AgentSharedMemory::Init()
|
||||
{
|
||||
BOOL success;
|
||||
|
||||
SECURITY_ATTRIBUTES sa;
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.nLength = sizeof(sa);
|
||||
sa.bInheritHandle = TRUE;
|
||||
|
||||
requestEvent = CreateEvent(&sa, /*bManualReset=*/FALSE, /*bInitialState=*/FALSE, NULL);
|
||||
replyEvent = CreateEvent(&sa, /*bManualReset=*/FALSE, /*bInitialState=*/FALSE, NULL);
|
||||
assert(requestEvent != NULL && replyEvent != NULL);
|
||||
|
||||
clientProcess = NULL;
|
||||
success = DuplicateHandle(
|
||||
GetCurrentProcess(),
|
||||
GetCurrentProcess(),
|
||||
GetCurrentProcess(),
|
||||
&clientProcess,
|
||||
0, TRUE, DUPLICATE_SAME_ACCESS);
|
||||
assert(success);
|
||||
}
|
43
AgentIo.h
43
AgentIo.h
@ -1,43 +0,0 @@
|
||||
#ifndef AGENTIO_H
|
||||
#define AGENTIO_H
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
enum RequestKind {
|
||||
rkInit,
|
||||
rkWriteConsoleInput,
|
||||
rkGetConsoleTitle,
|
||||
rkSetConsoleTitle,
|
||||
rkRead,
|
||||
};
|
||||
|
||||
struct ConsoleData {
|
||||
CONSOLE_SCREEN_BUFFER_INFO info;
|
||||
CHAR_INFO windowContent[65536 / sizeof(CHAR_INFO)];
|
||||
};
|
||||
|
||||
struct AgentSharedMemory
|
||||
{
|
||||
HANDLE clientProcess;
|
||||
HANDLE requestEvent;
|
||||
HANDLE replyEvent;
|
||||
|
||||
HWND hwnd;
|
||||
|
||||
RequestKind requestKind;
|
||||
ConsoleData read;
|
||||
|
||||
union {
|
||||
struct {
|
||||
int count;
|
||||
INPUT_RECORD records[65536 / sizeof(INPUT_RECORD)];
|
||||
BOOL result;
|
||||
DWORD resultWritten;
|
||||
} input;
|
||||
char title[65536];
|
||||
} u;
|
||||
|
||||
void Init();
|
||||
};
|
||||
|
||||
#endif // AGENTIO_H
|
@ -1,34 +0,0 @@
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
project(Scrape)
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
|
||||
find_package(Qt4 REQUIRED)
|
||||
include(${QT_USE_FILE})
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
set_directory_properties(PROPERTIES COMPILE_DEFINITIONS "WINVER=0x0501")
|
||||
|
||||
add_executable(
|
||||
Test
|
||||
Test.cc
|
||||
Client.cc
|
||||
AgentIo.cc
|
||||
DebugClient.cc
|
||||
)
|
||||
|
||||
add_executable(
|
||||
Agent
|
||||
Agent.cc
|
||||
AgentIo.cc
|
||||
DebugClient.cc
|
||||
)
|
||||
|
||||
target_link_libraries(Test ${QT_LIBRARIES})
|
||||
target_link_libraries(Agent ${QT_LIBRARIES})
|
||||
|
||||
set_target_properties(
|
||||
Test
|
||||
Agent
|
||||
PROPERTIES
|
||||
LINK_FLAGS "-static-libgcc"
|
||||
)
|
119
Client.cc
119
Client.cc
@ -1,119 +0,0 @@
|
||||
#include "Client.h"
|
||||
#include "DebugClient.h"
|
||||
#include "AgentIo.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DIM(x) (sizeof(x) / sizeof((x)[0]))
|
||||
|
||||
Client::Client() :
|
||||
m_sharedMemoryMapping(NULL),
|
||||
m_sharedMemory(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
void Client::start()
|
||||
{
|
||||
BOOL success;
|
||||
|
||||
SECURITY_ATTRIBUTES sa;
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.nLength = sizeof(sa);
|
||||
sa.bInheritHandle = TRUE;
|
||||
|
||||
// TODO: note that a static counter makes this code less thread-safe.
|
||||
char mappingName[80];
|
||||
static int consoleCounter = 0;
|
||||
sprintf(mappingName, "AgentSharedMemory-%d-%d",
|
||||
(int)GetCurrentProcessId(),
|
||||
++consoleCounter);
|
||||
m_sharedMemoryMapping = CreateFileMapping(
|
||||
INVALID_HANDLE_VALUE,
|
||||
&sa,
|
||||
PAGE_READWRITE,
|
||||
/*dwMaximumSizeHigh=*/0,
|
||||
/*dwMaximumSizeLow=*/sizeof(AgentSharedMemory),
|
||||
mappingName);
|
||||
assert(m_sharedMemoryMapping != NULL);
|
||||
m_sharedMemory = (AgentSharedMemory*)MapViewOfFile(m_sharedMemoryMapping, FILE_MAP_WRITE, 0, 0, 0);
|
||||
assert(m_sharedMemory != NULL);
|
||||
m_sharedMemory->Init();
|
||||
|
||||
STARTUPINFO sui;
|
||||
memset(&sui, 0, sizeof(sui));
|
||||
sui.cb = sizeof(sui);
|
||||
sui.dwFlags = STARTF_USESHOWWINDOW;
|
||||
sui.wShowWindow = SW_HIDE;
|
||||
PROCESS_INFORMATION pi;
|
||||
memset(&pi, 0, sizeof(pi));
|
||||
// TODO: Don't use the CWD to find the agent.
|
||||
const char program[80] = ".\\Agent.exe";
|
||||
char cmdline[80];
|
||||
sprintf(cmdline, "%s %s", program, mappingName);
|
||||
success = CreateProcess(
|
||||
program,
|
||||
cmdline,
|
||||
NULL, NULL,
|
||||
/*bInheritHandles=*/TRUE,
|
||||
/*dwCreationFlags=*/CREATE_NEW_CONSOLE,
|
||||
NULL, NULL,
|
||||
&sui, &m_agentProcess);
|
||||
if (!success) {
|
||||
Trace("Could not start agent subprocess.");
|
||||
exit(1);
|
||||
}
|
||||
Trace("New child process: PID %d", (int)m_agentProcess.dwProcessId);
|
||||
|
||||
// Make sure the agent has initialized itself before returning.
|
||||
sendRequest(rkInit);
|
||||
}
|
||||
|
||||
DWORD Client::processId()
|
||||
{
|
||||
return m_agentProcess.dwProcessId;
|
||||
}
|
||||
|
||||
HWND Client::hwnd()
|
||||
{
|
||||
return m_sharedMemory->hwnd;
|
||||
}
|
||||
|
||||
void Client::updateConsoleData()
|
||||
{
|
||||
sendRequest(rkRead);
|
||||
}
|
||||
|
||||
ConsoleData &Client::consoleData()
|
||||
{
|
||||
return m_sharedMemory->read;
|
||||
}
|
||||
|
||||
void Client::writeConsoleInput(INPUT_RECORD *records, int count)
|
||||
{
|
||||
assert(count <= DIM(m_sharedMemory->u.input.records));
|
||||
m_sharedMemory->u.input.count = count;
|
||||
memcpy(m_sharedMemory->u.input.records, records, sizeof(INPUT_RECORD) * count);
|
||||
sendRequest(rkWriteConsoleInput);
|
||||
}
|
||||
|
||||
void Client::setTitle(const char *title)
|
||||
{
|
||||
// TODO: buffer overflow, error handling
|
||||
strcpy(m_sharedMemory->u.title, title);
|
||||
sendRequest(rkSetConsoleTitle);
|
||||
}
|
||||
|
||||
const char *Client::getTitle()
|
||||
{
|
||||
// TODO: buffer overflow, error handling
|
||||
sendRequest(rkGetConsoleTitle);
|
||||
return m_sharedMemory->u.title;
|
||||
}
|
||||
|
||||
void Client::sendRequest(RequestKind kind)
|
||||
{
|
||||
m_sharedMemory->requestKind = kind;
|
||||
SetEvent(m_sharedMemory->requestEvent);
|
||||
WaitForSingleObject(m_sharedMemory->replyEvent, INFINITE);
|
||||
}
|
32
Client.h
32
Client.h
@ -1,32 +0,0 @@
|
||||
#ifndef CLIENT_H
|
||||
#define CLIENT_H
|
||||
|
||||
#include <windows.h>
|
||||
#include "AgentIo.h"
|
||||
|
||||
class Client
|
||||
{
|
||||
public:
|
||||
Client();
|
||||
void start();
|
||||
DWORD processId();
|
||||
HWND hwnd();
|
||||
|
||||
void updateConsoleData();
|
||||
ConsoleData &consoleData();
|
||||
|
||||
void writeConsoleInput(INPUT_RECORD *records, int count);
|
||||
|
||||
void setTitle(const char *title);
|
||||
const char *getTitle();
|
||||
|
||||
private:
|
||||
void sendRequest(RequestKind kind);
|
||||
|
||||
private:
|
||||
HANDLE m_sharedMemoryMapping;
|
||||
AgentSharedMemory *m_sharedMemory;
|
||||
PROCESS_INFORMATION m_agentProcess;
|
||||
};
|
||||
|
||||
#endif // CLIENT_H
|
@ -1,58 +0,0 @@
|
||||
#include "DebugClient.h"
|
||||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static void SendToDebugServer(const char *message)
|
||||
{
|
||||
char response[16];
|
||||
DWORD responseSize;
|
||||
CallNamedPipe(
|
||||
"\\\\.\\pipe\\DebugServer",
|
||||
(void*)message, strlen(message),
|
||||
response, sizeof(response), &responseSize,
|
||||
NMPWAIT_WAIT_FOREVER);
|
||||
}
|
||||
|
||||
void TraceRaw(const char *format, ...)
|
||||
{
|
||||
char message[1024];
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
vsnprintf(message, sizeof(message), format, ap);
|
||||
message[sizeof(message) - 1] = '\0';
|
||||
va_end(ap);
|
||||
|
||||
SendToDebugServer(message);
|
||||
}
|
||||
|
||||
void Trace(const char *format, ...)
|
||||
{
|
||||
char message[1024];
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
vsnprintf(message, sizeof(message), format, ap);
|
||||
message[sizeof(message) - 1] = '\0';
|
||||
va_end(ap);
|
||||
|
||||
SYSTEMTIME systemTime;
|
||||
GetSystemTime(&systemTime);
|
||||
int timeInSeconds = systemTime.wHour * 3600 + systemTime.wMinute * 60 + systemTime.wSecond;
|
||||
|
||||
char moduleName[1024];
|
||||
moduleName[0] = '\0';
|
||||
GetModuleFileName(NULL, moduleName, sizeof(moduleName));
|
||||
const char *baseName = strrchr(moduleName, '\\');
|
||||
baseName = (baseName != NULL) ? baseName + 1 : moduleName;
|
||||
|
||||
char fullMessage[1024];
|
||||
snprintf(fullMessage, sizeof(fullMessage),
|
||||
"[%05d.%03d %s,p%04d,t%04d]: %s",
|
||||
timeInSeconds, systemTime.wMilliseconds,
|
||||
baseName, (int)GetCurrentProcessId(), (int)GetCurrentThreadId(),
|
||||
message);
|
||||
|
||||
SendToDebugServer(fullMessage);
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
#ifndef DEBUGCLIENT_H
|
||||
#define DEBUGCLIENT_H
|
||||
|
||||
void TraceRaw(const char *format, ...);
|
||||
void Trace(const char *format, ...);
|
||||
|
||||
#endif // DEBUGCLIENT_H
|
Loading…
Reference in New Issue
Block a user