Add browser download confirmation and parameterized client startup

This commit is contained in:
tongtong committed 2026-11-20 18:41:05 +08:00
1 parent 028fec64be
commit 3cf197a98c
25 files changed
+568 -46

No files matched your search

+26 -3
View File
@@ -1,17 +1,19 @@
#include "pipe.h"
#include "protocol.h"
#include <sddl.h>
#include <tlhelp32.h>
#include <vector>
#include <memory>
#include <sstream>
#include <utility>
#pragma comment(lib,"advapi32.lib")
#pragma comment(lib,"user32.lib")
namespace browser_bridge {
namespace {
using Handle=std::unique_ptr<void,decltype(&CloseHandle)>;
std::wstring UserSid() {
std::wstring UserSid(HANDLE process=GetCurrentProcess()) {
HANDLE raw=nullptr;
if (!OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&raw)) return {};
if (!OpenProcessToken(process,TOKEN_QUERY,&raw)) return {};
Handle token(raw,&CloseHandle); DWORD size=0;
GetTokenInformation(token.get(),TokenUser,nullptr,0,&size);
std::vector<char> buffer(size);
@@ -20,6 +22,23 @@ std::wstring UserSid() {
if (!ConvertSidToStringSidW(reinterpret_cast<TOKEN_USER*>(buffer.data())->User.Sid,&sid)) return {};
std::wstring result(sid); LocalFree(sid); return result;
}
bool ClientRunning() {
if (FindWindowW(L"TTDownloaderMainWindow",nullptr)) return true;
// A minimized/tray client may belong to another desktop. Match both user and session.
const auto sid=UserSid(); DWORD session=0;
if (sid.empty() || !ProcessIdToSessionId(GetCurrentProcessId(),&session)) return false;
Handle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0),&CloseHandle);
if (snapshot.get()==INVALID_HANDLE_VALUE) { snapshot.release(); return false; }
PROCESSENTRY32W entry{}; entry.dwSize=sizeof(entry);
for (BOOL more=Process32FirstW(snapshot.get(),&entry);more;more=Process32NextW(snapshot.get(),&entry)) {
if (_wcsicmp(entry.szExeFile,L"ttdownloader.exe") && _wcsicmp(entry.szExeFile,L"ttdownloader_d.exe")) continue;
DWORD candidateSession=0;
if (!ProcessIdToSessionId(entry.th32ProcessID,&candidateSession) || candidateSession!=session) continue;
Handle process(OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,FALSE,entry.th32ProcessID),&CloseHandle);
if (process && UserSid(process.get())==sid) return true;
}
return false;
}
bool Complete(HANDLE pipe,OVERLAPPED& operation,HANDLE stop,ULONGLONG deadline,DWORD& done) {
HANDLE waits[]={operation.hEvent,stop};
ULONGLONG now=GetTickCount64();
@@ -63,8 +82,12 @@ std::string Exchange(const std::string& input,const std::wstring& name) {
}
if (pipe.get()==INVALID_HANDLE_VALUE) {
DWORD error=GetLastError(); pipe.release();
if (error==ERROR_FILE_NOT_FOUND && ClientRunning())
return Serialize(Reply("client_not_ready",
"客户端已运行,但浏览器接口未就绪。请完全退出旧客户端(包括托盘),再启动新版 TTDownloader。"));
return Serialize(Reply(error==ERROR_FILE_NOT_FOUND?"client_not_running":"connection_failed",
error==ERROR_FILE_NOT_FOUND?"桌面客户端未运行,请先启动 TTDownloader":"桌面客户端繁忙或连接失败"));
error==ERROR_FILE_NOT_FOUND?"未找到当前用户会话的客户端,请启动新版 TTDownloader":
"桌面客户端繁忙或连接失败(Windows 错误 "+std::to_string(error)+")"));
}
ULONGLONG deadline=GetTickCount64()+28000;
auto json=input;