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

+13
View File
@@ -0,0 +1,13 @@
#pragma once
#include <string>
namespace browser_bridge {
// Quote one argv value for CommandLineToArgvW/CreateProcessW; no shell is involved.
inline std::wstring QuoteArgument(const std::wstring& value) {
std::wstring result=L"\""; size_t slashes=0;
for (wchar_t c:value) {
if (c==L'\\') { ++slashes; continue; }
result.append(c==L'"'?slashes*2+1:slashes,L'\\'); slashes=0; result+=c;
}
result.append(slashes*2,L'\\'); return result+L'"';
}
}
+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;
+4 -3
View File
@@ -89,7 +89,7 @@ std::string Serialize(const Message& fields) {
}
std::string Get(const Message& fields,const std::string& key) { auto it=fields.find(key); return it==fields.end()?"":it->second; }
bool Validate(const Message& fields,std::string& error) {
static const std::set<std::string> allowed={"action","request_id","url","referrer","user_agent","filename","method"};
static const std::set<std::string> allowed={"action","request_id","url","referrer","user_agent","filename","method","confirmation"};
for (const auto& [key,value]:fields) {
if (!allowed.count(key) || value.find('\0')!=std::string::npos) { error="不支持的字段"; return false; }
for (unsigned char c:value) if (c<32 || c==127) { error="字段不能含控制字符"; return false; }
@@ -97,8 +97,9 @@ bool Validate(const Message& fields,std::string& error) {
const auto action=Get(fields,"action"),id=Get(fields,"request_id");
if (id.empty() || id.size()>80) { error="请求编号无效"; return false; }
for (unsigned char c:id) if (!std::isalnum(c) && c!='-') { error="请求编号无效"; return false; }
if (action=="ping" && fields.size()<=2) return true;
if (action!="add" || Get(fields,"method")!="GET" || !HttpUrl(Get(fields,"url"))
if ((action=="ping" || action=="query" || action=="cancel") && fields.size()<=2) return true;
if (action!="add" || (!Get(fields,"confirmation").empty() && Get(fields,"confirmation")!="required")
|| Get(fields,"method")!="GET" || !HttpUrl(Get(fields,"url"))
|| (!Get(fields,"referrer").empty() && !HttpUrl(Get(fields,"referrer")))
|| Get(fields,"user_agent").size()>2048 || Get(fields,"filename").size()>1024) {
error="仅支持 HTTP/HTTPS GET 下载地址及合法上下文"; return false;