Feature: add a remote file hash query for the file manager
Add COMMAND_FILE_HASH/TOKEN_FILE_HASH (182/183) and the CLIENT_CAP_FILE_HASH capability so the server can ask a client for a single file's exact size, mtime and SHA-256 without transferring it. This is the P2 half of the double-click preview design; old clients do not advertise the capability and are skipped by the caller. Windows hashes with BCrypt through the existing VerifyV2 helper; Unix and Android stream Sha256 while reading. Both clamp the peer-supplied maxBytes to FILE_PREVIEW_MAX_BYTES, echo the original request path bytes for correlation, and return TOO_LARGE without reading the file. The Unix loop also verifies the bytes hashed match stat() so a file changed mid-read reports an error instead of a mismatched hash. No existing command changes; the new values are explicit and the four DefaultCaps branches advertise the capability. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
b44935270a
commit
e39b9d1ed5
5 files changed
+263
No files matched your search
@@ -9,6 +9,7 @@
|
||||
#include "file_upload.h"
|
||||
#include "IOCPClient.h"
|
||||
#include "KernelManager.h"
|
||||
#include <common/VerifyV2.h>
|
||||
|
||||
#if ENABLE_FILE_MNG
|
||||
|
||||
@@ -152,6 +153,9 @@ VOID CFileManager::OnReceive(PBYTE lpBuffer, ULONG nSize)
|
||||
case CMD_DOWN_FILES_V2: // V2上传文件到服务端
|
||||
UploadToRemoteV2(lpBuffer + 1, nSize - 1);
|
||||
break;
|
||||
case COMMAND_FILE_HASH: // 远程文件信息查询(双击预览)
|
||||
HandleFileHash(lpBuffer + 1, nSize - 1);
|
||||
break;
|
||||
case COMMAND_CONTINUE: // 上传文件
|
||||
SendFileData(lpBuffer + 1);
|
||||
break;
|
||||
@@ -1247,4 +1251,116 @@ void CFileManager::UploadToRemoteV2(LPBYTE lpBuffer, UINT nSize)
|
||||
Mprintf("[V2] 连接服务器失败\n");
|
||||
}
|
||||
}
|
||||
|
||||
static uint64_t FileTimeToUnixMs(const FILETIME& ft)
|
||||
{
|
||||
ULARGE_INTEGER u;
|
||||
u.LowPart = ft.dwLowDateTime;
|
||||
u.HighPart = ft.dwHighDateTime;
|
||||
// 1601-01-01 到 1970-01-01 的 100ns 数
|
||||
const uint64_t kEpochDiff = 116444736000000000ULL;
|
||||
if (u.QuadPart < kEpochDiff) return 0;
|
||||
return (u.QuadPart - kEpochDiff) / 10000ULL;
|
||||
}
|
||||
|
||||
void CFileManager::SendFileHashResponse(uint32_t status, uint32_t err, uint64_t size,
|
||||
uint64_t mtimeMs, const BYTE* sha,
|
||||
const std::string& pathOrig)
|
||||
{
|
||||
std::vector<BYTE> pkt(sizeof(FileHashResponseHeader) + pathOrig.size() + 1, 0);
|
||||
FileHashResponseHeader* h = (FileHashResponseHeader*)pkt.data();
|
||||
h->token = TOKEN_FILE_HASH;
|
||||
h->status = status;
|
||||
h->err = err;
|
||||
h->size = size;
|
||||
h->mtimeMs = mtimeMs;
|
||||
if (sha) memcpy(h->sha256, sha, sizeof(h->sha256));
|
||||
memcpy(pkt.data() + sizeof(FileHashResponseHeader), pathOrig.data(), pathOrig.size());
|
||||
Send(pkt.data(), (UINT)pkt.size());
|
||||
}
|
||||
|
||||
void CFileManager::HandleFileHash(LPBYTE lpBuffer, UINT nSize)
|
||||
{
|
||||
// 请求: [maxBytes:8 LE][path\0](命令字节已由 OnReceive 去掉)
|
||||
if (!lpBuffer || nSize < 9) return;
|
||||
|
||||
uint64_t maxBytes = 0;
|
||||
memcpy(&maxBytes, lpBuffer, sizeof(maxBytes));
|
||||
// 对端不可信:把上限钳到协议允许的最大预览大小,避免超大分配/DWORD 截断。
|
||||
if (maxBytes > FILE_PREVIEW_MAX_BYTES) maxBytes = FILE_PREVIEW_MAX_BYTES;
|
||||
|
||||
const char* pathRaw = (const char*)(lpBuffer + 8);
|
||||
size_t pathLen = strnlen(pathRaw, nSize - 8);
|
||||
std::string pathOrig(pathRaw, pathLen);
|
||||
if (pathOrig.empty()) {
|
||||
SendFileHashResponse(FILE_HASH_ERROR, ERROR_INVALID_PARAMETER, 0, 0, NULL, pathOrig);
|
||||
return;
|
||||
}
|
||||
|
||||
std::string path = ExpandPath(pathOrig.c_str());
|
||||
uint32_t status = FILE_HASH_OK;
|
||||
uint32_t err = 0;
|
||||
uint64_t size = 0;
|
||||
uint64_t mtimeMs = 0;
|
||||
BYTE sha[32] = {};
|
||||
|
||||
DWORD attr = GetFileAttributesA(path.c_str());
|
||||
if (attr == INVALID_FILE_ATTRIBUTES) {
|
||||
status = FILE_HASH_ERROR;
|
||||
err = GetLastError();
|
||||
} else if (attr & FILE_ATTRIBUTE_DIRECTORY) {
|
||||
status = FILE_HASH_ERROR;
|
||||
err = ERROR_DIRECTORY;
|
||||
} else {
|
||||
HANDLE h = CreateFileA(path.c_str(), GENERIC_READ,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (h == INVALID_HANDLE_VALUE) {
|
||||
status = FILE_HASH_ERROR;
|
||||
err = GetLastError();
|
||||
} else {
|
||||
LARGE_INTEGER li = {};
|
||||
if (!GetFileSizeEx(h, &li) || li.QuadPart < 0) {
|
||||
status = FILE_HASH_ERROR;
|
||||
err = GetLastError();
|
||||
} else {
|
||||
size = (uint64_t)li.QuadPart;
|
||||
FILETIME ftWrite = {};
|
||||
if (GetFileTime(h, NULL, NULL, &ftWrite))
|
||||
mtimeMs = FileTimeToUnixMs(ftWrite);
|
||||
|
||||
if (size > maxBytes) {
|
||||
status = FILE_HASH_TOO_LARGE; // 不读取文件内容
|
||||
} else {
|
||||
std::vector<BYTE> data((size_t)size);
|
||||
bool ok = true;
|
||||
if (size > 0) {
|
||||
DWORD got = 0;
|
||||
if (!ReadFile(h, data.data(), (DWORD)size, &got, NULL)) {
|
||||
status = FILE_HASH_ERROR;
|
||||
err = GetLastError();
|
||||
ok = false;
|
||||
} else if (got != (DWORD)size) {
|
||||
// 文件在 GetFileSizeEx 与 ReadFile 之间被截断:GetLastError 通常为 0。
|
||||
status = FILE_HASH_ERROR;
|
||||
err = ERROR_HANDLE_EOF;
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
if (ok) {
|
||||
static const BYTE kEmpty = 0;
|
||||
const BYTE* p = data.empty() ? &kEmpty : data.data();
|
||||
if (!VerifyV2::computeSHA256(p, (size_t)size, sha)) {
|
||||
status = FILE_HASH_ERROR;
|
||||
err = ERROR_GEN_FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
CloseHandle(h);
|
||||
}
|
||||
}
|
||||
|
||||
SendFileHashResponse(status, err, size, mtimeMs, sha, pathOrig);
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user