Feature: File copy/paste support in remote control

This commit is contained in:
yuanyuanxiang
2025-10-25 16:13:18 +08:00
parent 2855af1932
commit ea4472445d
23 changed files with 339 additions and 24 deletions

View File

@@ -17,9 +17,34 @@
#include "ScreenCapturerDXGI.h"
#include <Shlwapi.h>
#include <shlobj_core.h>
#include "common/file_upload.h"
#include <thread>
#pragma comment(lib, "Shlwapi.lib")
#ifndef PLUGIN
#ifdef _WIN64
#ifdef _DEBUG
#pragma comment(lib, "FileUpload_Libx64d.lib")
#else
#pragma comment(lib, "FileUpload_Libx64.lib")
#endif
#else
int InitFileUpload(const std::string hmac, int chunkSizeKb, int sendDurationMs) { return 0; }
int UninitFileUpload() { return 0; }
std::vector<std::string> GetClipboardFiles() { return{}; }
bool GetCurrentFolderPath(std::string& outDir) { return false; }
int FileBatchTransferWorker(const std::vector<std::string>& files, const std::string& targetDir,
void* user, OnTransform f, OnFinish finish, const std::string& hash, const std::string& hmac) {
finish(user);
return 0;
}
int RecvFileChunk(char* buf, size_t len, void* user, OnFinish f, const std::string& hash, const std::string& hmac) {
return 0;
}
#endif
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
@@ -46,6 +71,11 @@ bool IsWindows8orHigher()
CScreenManager::CScreenManager(IOCPClient* ClientObject, int n, void* user):CManager(ClientObject)
{
#ifndef PLUGIN
extern CONNECT_ADDRESS g_SETTINGS;
m_conn = &g_SETTINGS;
InitFileUpload("");
#endif
m_bIsWorking = TRUE;
m_bIsBlockInput = FALSE;
g_hDesk = nullptr;
@@ -269,7 +299,7 @@ VOID CScreenManager::SendBitMapInfo()
CScreenManager::~CScreenManager()
{
Mprintf("ScreenManager <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
UninitFileUpload();
m_bIsWorking = FALSE;
WaitForSingleObject(m_hWorkThread, INFINITE);
@@ -281,6 +311,45 @@ CScreenManager::~CScreenManager()
m_ScreenSpyObject = NULL;
}
void RunFileReceiver(CScreenManager *mgr, const std::string &folder) {
auto start = time(0);
Mprintf("Enter thread RunFileReceiver: %d\n", GetCurrentThreadId());
IOCPClient* pClient = new IOCPClient(mgr->g_bExit, true, MaskTypeNone, mgr->m_conn->GetHeaderEncType());
if (pClient->ConnectServer(mgr->m_ClientObject->ServerIP().c_str(), mgr->m_ClientObject->ServerPort())) {
pClient->setManagerCallBack(mgr, CManager::DataProcess);
// <20><><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
char cmd[300] = { COMMAND_GET_FILE };
memcpy(cmd + 1, folder.c_str(), folder.length());
pClient->Send2Server(cmd, sizeof(cmd));
pClient->RunEventLoop(TRUE);
}
delete pClient;
Mprintf("Leave thread RunFileReceiver: %d. Cost: %d s\n", GetCurrentThreadId(), time(0)-start);
}
bool SendData(void* user, FileChunkPacket* chunk, BYTE* data, int size) {
IOCPClient* pClient = (IOCPClient*)user;
if (!pClient->IsConnected() || !pClient->Send2Server((char*)data, size)) {
return false;
}
return true;
}
void RecvData(void* ptr) {
FileChunkPacket* pkt = (FileChunkPacket*)ptr;
}
void delay_destroy(IOCPClient* pClient, int sec) {
if (!pClient) return;
Sleep(sec * 1000);
delete pClient;
}
void FinishSend(void* user) {
IOCPClient* pClient = (IOCPClient*)user;
std::thread(delay_destroy, pClient, 15).detach();
}
VOID CScreenManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
{
switch(szBuffer[0]) {
@@ -303,6 +372,17 @@ VOID CScreenManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
break;
}
case COMMAND_SCREEN_GET_CLIPBOARD: {
auto files = GetClipboardFiles();
if (!files.empty())
{
char h[100] = {};
memcpy(h, szBuffer + 1, ulLength - 1);
m_hash = std::string(h, h + 64);
m_hmac = std::string(h + 64, h + 80);
BYTE szBuffer[1] = { COMMAND_GET_FOLDER };
SendData(szBuffer, sizeof(szBuffer));
break;
}
SendClientClipboard();
break;
}
@@ -310,6 +390,47 @@ VOID CScreenManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
UpdateClientClipboard((char*)szBuffer + 1, ulLength - 1);
break;
}
case COMMAND_GET_FOLDER: {
std::string folder;
if (GetCurrentFolderPath(folder)) {
char h[100] = {};
memcpy(h, szBuffer + 1, ulLength - 1);
m_hash = std::string(h, h + 64);
m_hmac = std::string(h + 64, h + 80);
if (OpenClipboard(nullptr))
{
EmptyClipboard();
CloseClipboard();
}
std::thread(RunFileReceiver, this, folder).detach();
}
break;
}
case COMMAND_GET_FILE: {
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
auto files = GetClipboardFiles();
std::string dir = (char*)(szBuffer + 1);
if (!files.empty() && !dir.empty()) {
IOCPClient* pClient = new IOCPClient(g_bExit, true, MaskTypeNone, m_conn->GetHeaderEncType());
if (pClient->ConnectServer(m_ClientObject->ServerIP().c_str(), m_ClientObject->ServerPort())) {
std::thread(FileBatchTransferWorker, files, dir, pClient, ::SendData, ::FinishSend,
m_hash, m_hmac).detach();
}
else {
delete pClient;
}
}
break;
}
case COMMAND_SEND_FILE: {
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
int n = RecvFileChunk((char*)szBuffer, ulLength, m_conn, RecvData, m_hash, m_hmac);
if (n) {
Mprintf("RecvFileChunk failed: %d. hash: %s, hmac: %s\n", n, m_hash.c_str(), m_hmac.c_str());
}
break;
}
}
}