Add browser download confirmation and parameterized client startup
This commit is contained in:
1 parent
028fec64be
commit
3cf197a98c
25 files changed
+568
-46
No files matched your search
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include "../shared/protocol.h"
|
||||
#include <functional>
|
||||
namespace browser_bridge {
|
||||
// Launch only for an explicit add. Queries and connection checks must never start the app.
|
||||
inline std::string ForwardToClient(const Message& request,const std::string& json,
|
||||
const std::function<std::string(const std::string&)>& exchange,
|
||||
const std::function<std::string()>& launch,
|
||||
const std::function<std::string(const std::string&)>& wait_ready) {
|
||||
auto response=exchange(json);
|
||||
Message reply; std::string error;
|
||||
if (Get(request,"action")!="add" || !Parse(response,reply,error)) return response;
|
||||
const auto status=Get(reply,"status");
|
||||
if (status!="client_not_running" && status!="client_not_ready") return response;
|
||||
if (status=="client_not_running") {
|
||||
auto failure=launch(); if (!failure.empty()) return failure;
|
||||
}
|
||||
auto probe=wait_ready(Serialize({{"action","ping"},{"request_id",Get(request,"request_id")}}));
|
||||
if (Parse(probe,reply,error) && Get(reply,"status")=="ready") return exchange(json);
|
||||
return probe;
|
||||
}
|
||||
}
|
||||
+42
-1
@@ -1,6 +1,8 @@
|
||||
#include "../shared/protocol.h"
|
||||
#include "../shared/pipe.h"
|
||||
#include "../shared/identity.h"
|
||||
#include "client_connection.h"
|
||||
#include "../shared/command_line.h"
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
#include <iostream>
|
||||
@@ -10,6 +12,45 @@ std::string Utf8(const std::wstring& value) {
|
||||
int size=WideCharToMultiByte(CP_UTF8,0,value.data(),static_cast<int>(value.size()),nullptr,0,nullptr,nullptr);
|
||||
std::string text(size,0); WideCharToMultiByte(CP_UTF8,0,value.data(),static_cast<int>(value.size()),text.data(),size,nullptr,nullptr); return text;
|
||||
}
|
||||
std::string LaunchClient(const browser_bridge::Message& request) {
|
||||
using namespace browser_bridge;
|
||||
// Launch a fixed sibling. Validated browser data is one argv value, never shell code.
|
||||
std::vector<wchar_t> module(32768);
|
||||
DWORD size=GetModuleFileNameW(nullptr,module.data(),static_cast<DWORD>(module.size()));
|
||||
if (!size || size>=module.size()) return Serialize(Reply("launch_failed","无法确定客户端安装目录"));
|
||||
std::wstring path(module.data()); auto slash=path.find_last_of(L"\\/");
|
||||
const auto directory=path.substr(0,slash);
|
||||
const bool debug=path.substr(slash+1)==L"ttdownloader_native_host_d.exe";
|
||||
path=directory+(debug?L"\\ttdownloader_d.exe":L"\\ttdownloader.exe");
|
||||
const auto json=Serialize(request);
|
||||
int length=MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,json.data(),static_cast<int>(json.size()),nullptr,0);
|
||||
if (!length) return Serialize(Reply("launch_failed","启动参数不是有效 UTF-8"));
|
||||
std::wstring payload(length,L'\0');
|
||||
MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,json.data(),static_cast<int>(json.size()),payload.data(),length);
|
||||
std::wstring command=QuoteArgument(path)+L" --browser-request "+QuoteArgument(payload);
|
||||
if (command.size()>=32767) return Serialize(Reply("launch_failed","下载地址和上下文过长,请先手动启动客户端"));
|
||||
STARTUPINFOW startup{sizeof(startup)}; PROCESS_INFORMATION process{};
|
||||
startup.dwFlags=STARTF_USESHOWWINDOW; startup.wShowWindow=SW_SHOWNORMAL;
|
||||
if (!CreateProcessW(path.c_str(),command.data(),nullptr,nullptr,FALSE,0,nullptr,directory.c_str(),&startup,&process))
|
||||
return Serialize(Reply("launch_failed","无法启动 TTDownloader(Windows 错误 "+std::to_string(GetLastError())+"),请检查安装目录"));
|
||||
CloseHandle(process.hThread); CloseHandle(process.hProcess);
|
||||
return {};
|
||||
}
|
||||
std::string WaitForClient(const std::string& ping) {
|
||||
using namespace browser_bridge;
|
||||
Message reply; std::string error;
|
||||
const auto deadline=GetTickCount64()+10000;
|
||||
do {
|
||||
Sleep(100); auto probe=Exchange(ping);
|
||||
if (Parse(probe,reply,error) && Get(reply,"status")=="ready") return probe;
|
||||
if (Get(reply,"status")!="client_not_running" && Get(reply,"status")!="client_not_ready") return probe;
|
||||
} while (GetTickCount64()<deadline);
|
||||
return Serialize(Reply("client_not_ready","客户端未能就绪,请完全退出旧版后启动新版,再发送下载地址"));
|
||||
}
|
||||
std::string SendToClient(const browser_bridge::Message& request,const std::string& json) {
|
||||
return browser_bridge::ForwardToClient(request,json,
|
||||
[](const std::string& value) { return browser_bridge::Exchange(value); },[&] { return LaunchClient(request); },WaitForClient);
|
||||
}
|
||||
int RegisterHost(const std::string& browser,bool remove) {
|
||||
if (browser!="chrome" && browser!="edge" && browser!="both") { std::cerr<<"Expected chrome, edge or both\n"; return 1; }
|
||||
std::vector<wchar_t> executable(32768),local(32768);
|
||||
@@ -57,7 +98,7 @@ int main(int argc,char** argv) {
|
||||
if (!browser_bridge::Parse(json,fields,error) || !browser_bridge::Validate(fields,error))
|
||||
response=browser_bridge::Serialize(browser_bridge::Reply("invalid_request",error,browser_bridge::Get(fields,"request_id")));
|
||||
else {
|
||||
response=browser_bridge::Exchange(json);
|
||||
response=SendToClient(fields,json);
|
||||
browser_bridge::Message reply;
|
||||
const auto expected=browser_bridge::Get(fields,"request_id");
|
||||
if (!browser_bridge::Parse(response,reply,error) || browser_bridge::Get(reply,"status").empty()
|
||||
|
||||
@@ -19,6 +19,6 @@
|
||||
<Link><SubSystem>Console</SubSystem><MinimumRequiredVersion>6.01</MinimumRequiredVersion></Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup><ClCompile Include="main.cpp" /><ClCompile Include="..\shared\protocol.cpp" /><ClCompile Include="..\shared\pipe.cpp" /></ItemGroup>
|
||||
<ItemGroup><ClInclude Include="..\shared\protocol.h" /><ClInclude Include="..\shared\pipe.h" /><ClInclude Include="..\shared\identity.h" /></ItemGroup>
|
||||
<ItemGroup><ClInclude Include="client_connection.h" /><ClInclude Include="..\shared\command_line.h" /><ClInclude Include="..\shared\protocol.h" /><ClInclude Include="..\shared\pipe.h" /><ClInclude Include="..\shared\identity.h" /></ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
</Project>
|
||||
Reference in new issue
Block a user