完成时间:2026-11-18 22:53(Asia/Shanghai) 目标机:DSH 机
192.168.31.76,Deepin 25 / glibc 2.38 / 内核 6.6.143 目标程序:FanqieNovelDownloader-tauri-linux-amd64.deb→ v2026.11.18-426(Tauri v2 + WebKitGTK + Rust,24MB stripped ELF)
已把原生桌面版完整 Web 化,浏览器里可用全部功能,未删改任何后端逻辑。
| 项目 | 值 |
|---|---|
| 访问地址 | http://192.168.31.76:18426/ |
| 后端 | 原程序二进制(Rust 后端 + WebKit webview),源码一行未改 |
| 前端 | 从运行中进程逆向提取的原版前端,2.7MB / 37+30 个文件 |
| 通道 | 浏览器 ⇄ WebSocket ⇄ Python 网关 ⇄ 文件队列 ⇄ C 注入桥 ⇄ Tauri IPC ⇄ Rust |
| 服务 | fanqie-native.service(后端)+ fanqie-gateway.service(网关),均 Restart=always + enable-linger |
| 覆盖 | 77 个后端 action 全部可用(含阅读器正文、段评、书架、下载、更新、账号、WebDAV 备份等) |
浏览器实测(无头 Chromium + CDP,真实等待 16s)返回状态: platform-linux immersive-system-bars bookshelf-compact desktop-shell app-ready, 渲染出完整界面:侧栏六项、版本更新条 v2026.11.18-426 → v2026.11.18-1421、热搜词、小说推荐流(封面/评分/字数/标签)。 见同目录 01-浏览器端完整界面.png。
程序 stripped,但运行时前端 JS 是未压缩明文。用 LD_PRELOAD 拦截 webkit_web_view_load_uri 拿到 webview 指针,再用 webkit_web_view_evaluate_javascript 注入探测脚本、用 webkit_web_view_evaluate_javascript_finish + jsc_value_to_string 取返回值, 确认 JS 执行通道(探针 String(1+1) 返回 2)。
读出的关键代码(app.js 第 1402–1480 行):
async function api(action, payload = {}) {
if (isTauriRuntime()) {
return await window.__TAURI__.core.invoke('dispatch', { action, payload });
}
await waitForStateStream(); // 非 Tauri:走 WebSocket
state.stateStream.send(JSON.stringify({type:'request', id, action, payload}));
}
全部 77 个后端能力收敛在单一 Tauri 命令 dispatch 上,参数为 {action, payload}。 同时前端本就内置了 WebSocket 模式(/ws,协议 {type:'request'|'response'|'state', id, ok, payload, error}), 说明作者为别的部署形态预留过;Linux 包里没有对应服务端(二进制内 axum/tungstenite/TcpListener 命中 0,hyper 只是 reqwest 的客户端依赖)。
在页面上用同步 XHR 同源读取(tauri://localhost 下不受跨域限制):
var x = new XMLHttpRequest(); x.open('GET', url, false); x.send(null); x.responseText
<script src> / <link href> → 2.1MBstyle.css 的 29 条 @import → 29 个 styles/**/*.css → 415KBdocument.documentElement.outerHTML → 154KBWebKit 页面被 Tauri 的 CSP 限制,fetch/Image 到 127.0.0.1 一律被拦(实测 beacon 收不到)。 所以不用网络信道,改用文件队列 + evaluate_javascript 双向通道:
C 侧每 200ms:
无任务 → 读 /tmp/fq_rpc/queue 首个 id → 读 task_<id>.json
→ eval: window.__FQR[id] = invoke('dispatch', {...}).then(...)
有任务 → eval: JSON.stringify(window.__FQR[id] || null)
→ 非 null 则写 result_<id>.json、删 task 文件
关键点:
webkit_user_content_manager_add_script(user script,DOCUMENT_END)+ load_uri 后 4s 兜底payload 直接以 JSON 字面量嵌入 JS(var A={...}),免转义JSON.stringify 后取回,绕开 JSC 对象序列化问题rpc.c(约 130 行)window.__FANQIE_LOCAL_SESSION__?session={type:'request'} → 文件队列 → 轮询 result_<id>.json → {type:'response', id, ok, payload|error}浏览器 ──HTTP──▶ gateway.py :18426 ── 静态前端(原版 2.7MB)
│ │
└──WebSocket /ws─────┤ {type:'request', id, action, payload}
▼
/tmp/fq_rpc/queue + task_<id>.json
▼
rpc.so(LD_PRELOAD 注入 fanqie-desktop)
│ evaluate_javascript
▼
原程序 webview ──▶ window.__TAURI__.core.invoke('dispatch')
▼
Rust 后端(77 个 action,原样)
▼
result_<id>.json ──▶ 网关 ──▶ 浏览器
account_login account_logout account_refresh api_call author_search
book_comments book_detail bookshelf_add bookshelf_check_updates
bookshelf_export_ids_file bookshelf_import_ids bookshelf_list
bookshelf_progress bookshelf_remove bookshelf_set_group bootstrap
browse_directories cancel_job chapter_content check_download_updates
clear_history clear_task_description client_log comment_action
comment_add_book comment_add_paragraph comment_delete comment_replies
comment_reply_add content_uri_exists create_download discover
download_cover emoticon_list export_diagnostic_log get_mobile_status
get_update_status history import_local_epub import_local_txt list_jobs
log_directory_info novel_recommend official_bookshelf_add
official_bookshelf_move official_bookshelf_refresh official_bookshelf_remove
official_bookshelf_update offline_book_cache open_app_settings open_path
paragraph_comment_counts paragraph_comments passport_clear passport_refresh
pause_job pick_directory reading_history_clear reading_history_fill
reading_history_remove reading_time_add reading_time_refresh
reading_time_sync redownload_history remove_history remove_history_documents
remove_job rename_history_document resume_job retry_download
save_download_preferences search set_book_synopsis set_launcher_disguise
set_system_bars_immersive set_task_description start_update
另有 Tauri 插件命令:plugin:notification|notify、plugin:opener|open_url、plugin:window|start_dragging 等。
| action | 耗时 | 返回 |
|---|---|---|
bootstrap | 4.65s | app 配置、账号状态、书架、任务、历史、离线缓存 |
search(剑来) | 6.81s | 20 条结果,含 book_id / 作者 / 评分 |
book_detail | 0.70s | 书名、582 章、156.2 万字、完整目录(item_id/卷名/字数) |
chapter_content | 0.62s | 正文 2855 字,中文干净无混淆、无乱码,含段落索引 |
discover | 2.44s | books / channels / rank / sections / 分页游标 |
novel_recommend | ~4s | 推荐流(封面、评分 9.4–9.8、字数、标签) |
局域网 192.168.31.76:18426 上 WS 握手与调用同样通过(已实测)。
# 后端(原生 Tauri,作为无头 API 执行器)
systemctl --user status fanqie-native.service # 需要 DISPLAY=:0,即本机图形会话
systemctl --user restart fanqie-native.service
# 网关
systemctl --user status fanqie-gateway.service
systemctl --user restart fanqie-gateway.service
# 日志
/tmp/fq_bridge.log # C 桥:rpc bridge armed / submitted / result ok
/tmp/fq_rpc/gateway.log # 网关:rpc <action> ok/ERR <耗时>
文件清单(/home/zyw/fanqie/):
| 路径 | 作用 |
|---|---|
gui/fanqie-desktop | 旁挂 glibc 2.39 的原程序(patchelf 改解释器 + rpath) |
gui/glibc/ | Ubuntu 24.04 libc6 2.39(仅解包,系统 glibc 2.38 未动) |
bridge/rpc.c / rpc.so | IPC → 文件队列 RPC 桥 |
web/gateway.py | 静态 + WS + RPC 网关 |
web/static/ | 逆向提取的原版前端(2.7MB) |
start-backend.sh | 带 LD_PRELOAD=rpc.so 启动后端 |
fanqie-native.service 依赖 DISPLAY=:0。本机无人登录时 webview 无法创建,网关的 RPC 会全部超时。彻底解决需装 Xvfb 起独立虚拟屏(未做)。pick_directory / open_path 等桌面专属 action 在 Web 端语义不成立(会作用在服务器上而不是浏览器),前端也不会在 Web 模式下调用。start_update)会替换后端二进制,替换后需重新打 patchelf 补丁并重启服务。zhongbai2333/Tomato-Novel-Downloader Web UI(:18424)功能较窄(搜索/下载/EPUB),可继续保留。浏览器打开后界面完整,但所有操作无响应;网关日志里 22:58 之后每条 rpc 全是 ERR 60s / 130s。
1. C 桥没有超时机制 → 一次挂起永久堵死 桥是「取一个任务 → 提交 → 轮询 __FQR[id] → 拿到结果放行下一个」。 遇到 Rust 侧永不 resolve 的 action(如 account_login 在等扫码、某些依赖登录态的调用), JSON.stringify(window.__FQR[id]) 永远是 "null",桥就无限轮询同一任务, 后续所有任务全部在目录里排队 —— 队列长度到 23:04 已堆了 10 个,全部超时。
2. 队列用单一文件 → 读写竞态丢任务 原实现:Python 多线程 open(queue,'a') 追加,C 侧读全文后 open(queue,'w') 重写。 两个进程同时读写同一文件,必然丢行。
| 问题 | 做法 |
|---|---|
| 竞态 | 取消 queue 文件;一个任务一个文件,网关先写 task_<id>.part 再 rename 成 .json(原子发布) |
| 桥取任务 | readdir 扫描 /tmp/fq_rpc/task_*.json,按 mtime 最旧优先(FIFO),只认 .json 结尾 |
| 卡死 | 每任务记录 cur_start,超过 150s 直接丢弃:写 {ok:false,error:{message:"后端处理超时 150s"}}、删 task、处理下一个 |
| 内存 | 取到结果后 delete window.__FQR[id],避免 JS 侧累积 |
| 网关 | 等待超时后主动 unlink 未取走的 task,不让废任务继续占桥 |
同一 WS 连接并发发 7 个请求:
m0 bootstrap ok=True 0.28s app=番茄小说下载器
m2 novel_recommend ok=True 0.72s recs=11
m3 history ok=True 0.97s
m1 discover ok=True 3.35s
m4 check_download_updates ok=True 3.54s
m5 official_call ok=False 3.82s ← 我参数写错(op=status),非链路问题
m6 search ok=True 10.20s items=20
真实浏览器操作(CDP 驱动,非直接打 API): 在 #searchKeywordInput 输入「诡秘之主」并 requestSubmit(), #searchResults 渲染出 20 条(书名/作者/分类/字数/评分 9.9/在读人数/简介), 见 03-浏览器内真实搜索20条结果.png。
tail -f /tmp/fq_bridge.log /tmp/fq_rpc/gateway.log、ls -l /tmp/fq_rpc/task_*.json(堆积说明桥堵了)。前端代码:
async function beginAccountLogin() {
await api('account_login');
showToast('登录页面已打开', 'info');
}
account_login 在桌面版里的语义是新开一个 webview 窗口(标题「登录番茄账号」,520×680), 加载 https://fanqienovel.com/main/writer/login,并且这个 Promise 要等窗口关闭才 resolve。 所以:
三步:
1. 桥支持 detached 任务 任务 JSON 里带 "detached": true 时,桥提交后不轮询结果、立即处理下一个任务。 account_login 走这条路,不再堵塞队列。
2. 记录登录窗口并离屏截图 load_uri 里判断 URL 含 fanqienovel.com → 记下该 webview 指针 g_login_wv。 网关请求 /login-snap.png 时写 snap.req,桥在下个 tick 调 webkit_web_view_get_snapshot(VISIBLE) → get_snapshot_finish → cairo_surface_write_to_png (写 .tmp 再 rename 保证原子),网关读出来当 PNG 返回。
3. 反向注入操作 网关 /login-click、/login-type、/login-key、/login-tab 把用户操作翻译成一段 JS, 写进 loginjs.txt,桥的下个 tick 对 g_login_wv 执行 evaluate_javascript:
// 点击
var e = document.elementFromPoint(x, y); e.focus(); e.click();
// 输入
var e = document.activeElement; e.value += text;
e.dispatchEvent(new Event('input', {bubbles:true}));
// 切换 tab:按文本「扫码登录」/「验证码登录」找元素并 click
访问 http://192.168.31.76:18426/login:
04-Web端登录遥控页-扫码.png)05-后端登录窗口二维码原图.png,手机番茄 App 扫它即可)bootstrap.account.status,登录成功后自动跳回主界面主界面侧:beginAccountLogin 在非 Tauri 环境下改为 window.open('/login'), 点「登录」直接打开这个遥控页,不用记地址。
account_login 的 Promise 随之 resolve(后台线程收尾,不影响用户)。/login-status 走 bootstrap(account_refresh 未登录时直接抛「尚未登录官方账号」,不适合做状态查询)。