新增 Chrome 下载接管扩展与 C++ 本地通信组件
实现链接右键发送、手动地址输入和默认关闭的可选 GET 自动接管,支持站点与文件类型过滤。通过 Native Messaging 和当前用户命名管道在任务入库后确认接管,失败恢复浏览器下载并持久化交接状态。保存请求编号、Referer 与 User-Agent,补充本地注册入口、集成包说明及扩展/stdio/IPC/HTTP 回归测试。
This commit is contained in:
1 parent
6c810c3b4f
commit
028fec64be
41 files changed
+1081
-18
No files matched your search
@@ -0,0 +1,48 @@
|
||||
#include "../shared/protocol.h"
|
||||
#include "../shared/pipe.h"
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <chrono>
|
||||
using namespace browser_bridge;
|
||||
static void Check(bool ok,const char* text) { if (!ok) throw std::runtime_error(text); }
|
||||
int main() {
|
||||
try {
|
||||
Message fields; std::string error;
|
||||
Check(Parse(R"({"action":"add","request_id":"abc-1","method":"GET","url":"https://example.test/中文.zip","filename":"\ud83d\ude80.zip"})",fields,error) && Validate(fields,error),"parse UTF-8 and surrogate pair");
|
||||
Message restored; Check(Parse(Serialize(fields),restored,error) && fields==restored,"JSON roundtrip");
|
||||
for (const std::string& invalid : {R"({"x":"1","x":"2"})",R"({"x":1})",R"({"x":"\ud800"})",R"({"x":{}})",R"({"x":"a"} trailing)"}) Check(!Parse(invalid,restored,error),"reject malformed flat protocol");
|
||||
auto invalid=fields; invalid["user_agent"]="test\r\nInjected: yes"; Check(!Validate(invalid,error),"reject header injection");
|
||||
invalid=fields; invalid["method"]="POST"; Check(!Validate(invalid,error),"reject POST");
|
||||
std::stringstream framed; Check(WriteFrame(framed,Serialize(fields)),"write native frame");
|
||||
std::string json; Check(ReadFrame(framed,json) && json==Serialize(fields),"read native frame");
|
||||
std::stringstream truncated(std::string("\xff\xff\xff\x7f",4)); Check(!ReadFrame(truncated,json),"bound frame allocation");
|
||||
std::stringstream partial(std::string("\x05\0\0\0ab",6)); Check(!ReadFrame(partial,json),"reject truncated frame");
|
||||
PipeServer server; auto name=PipeName()+L".Test."+std::to_wstring(GetCurrentProcessId());
|
||||
server.Start([](const std::string& json,HANDLE) {
|
||||
Message message; std::string error;
|
||||
if (!Parse(json,message,error) || !Validate(message,error)) return Serialize(Reply("invalid_request",error));
|
||||
return Serialize(Reply(Get(message,"action")=="ping"?"ready":"accepted","已接收",Get(message,"request_id"),"42"));
|
||||
},name);
|
||||
Message ping{{"action","ping"},{"request_id","ping-1"}};
|
||||
bool ready=false;
|
||||
for (int i=0;i<50;++i) {
|
||||
auto response=Exchange(Serialize(ping),name);
|
||||
Check(Parse(response,restored,error),"pipe reply JSON");
|
||||
if (Get(restored,"status")=="ready") { ready=true; break; } Sleep(20);
|
||||
}
|
||||
Check(ready,"private per-user named pipe handshake");
|
||||
auto response=Exchange(Serialize(fields),name); Check(Parse(response,restored,error) && Get(restored,"status")=="accepted" && Get(restored,"task_id")=="42","native IPC request/ack including Unicode");
|
||||
// A stalled peer must not keep shutdown blocked in a read operation.
|
||||
HANDLE peer=INVALID_HANDLE_VALUE;
|
||||
for (int i=0;i<50 && peer==INVALID_HANDLE_VALUE;++i) {
|
||||
peer=CreateFileW(name.c_str(),GENERIC_READ|GENERIC_WRITE,0,nullptr,OPEN_EXISTING,0,nullptr);
|
||||
if (peer==INVALID_HANDLE_VALUE) Sleep(20);
|
||||
}
|
||||
Check(peer!=INVALID_HANDLE_VALUE,"connect stalled peer");
|
||||
auto start=std::chrono::steady_clock::now(); server.Stop(); CloseHandle(peer);
|
||||
Check(std::chrono::steady_clock::now()-start<std::chrono::seconds(2),"stop cancels pending pipe I/O");
|
||||
std::cout<<"PASS: native framing, UTF-8, protocol bounds, request validation, named-pipe IPC and shutdown\n";
|
||||
return 0;
|
||||
} catch (const std::exception& error) { std::cerr<<"FAIL: "<<error.what()<<"\n"; return 1; }
|
||||
}
|
||||
Reference in new issue
Block a user