mirror of
https://gitee.com/xmz7891/ykxmz.git
synced 2026-10-18 14:50:45 +00:00
106 lines
3.5 KiB
Python
106 lines
3.5 KiB
Python
# -*- coding: utf-8 -*-
|
||
r"""给中转管理页加 favicon(用项目的 app.ico)。
|
||
|
||
用户提过:"那个中转管理端那个,网页图标没有啊,图标就是我之前给你那个图标"
|
||
|
||
做法:
|
||
1. 把 windows/app.ico 拷一份到 relay/favicon.ico(跟 relay.py 同目录)
|
||
2. relay.py 加一条 GET /favicon.ico 路由直接给这个文件
|
||
3. 管理页 <head> 里加 <link rel="icon" ...>
|
||
|
||
为什么不做成 base64 内嵌:
|
||
ico 有 147KB,内嵌到 relay.py 里会让源码从 78KB 涨到 270KB,
|
||
而且每次改图标都要动代码。放成独立文件干净得多。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import ast
|
||
import shutil
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote")
|
||
RELAY = ROOT / "relay" / "relay.py"
|
||
ICON_SRC = ROOT / "windows" / "app.ico"
|
||
ICON_DST = ROOT / "relay" / "favicon.ico"
|
||
|
||
# ---------- 1. 路由 ----------
|
||
|
||
ANCHOR_ROUTE = ''' # ---- 成品下载(挂在管理页上给人下 APK / 整包)----'''
|
||
|
||
NEW_ROUTE = ''' # ---- 网站图标 ----
|
||
if path in ("/favicon.ico", "/apple-touch-icon.png"):
|
||
icon = Path(__file__).resolve().parent / "favicon.ico"
|
||
if icon.is_file():
|
||
try:
|
||
blob = icon.read_bytes()
|
||
except OSError:
|
||
blob = b""
|
||
if blob:
|
||
self.send_response(200)
|
||
self.send_header("Content-Type", "image/x-icon")
|
||
self.send_header("Content-Length", str(len(blob)))
|
||
# 图标不常变,让浏览器缓存一天
|
||
self.send_header("Cache-Control", "public, max-age=86400")
|
||
self.end_headers()
|
||
self.wfile.write(blob)
|
||
return
|
||
# 没有图标文件就 404,别让请求打到后面的逻辑
|
||
self.send_response(404)
|
||
self.send_header("Content-Length", "0")
|
||
self.end_headers()
|
||
return
|
||
|
||
''' + ANCHOR_ROUTE
|
||
|
||
# ---------- 2. 页面里声明 ----------
|
||
|
||
ANCHOR_HEAD = '''<meta name=viewport content="width=device-width,initial-scale=1">'''
|
||
|
||
NEW_HEAD = ('''<meta name=viewport content="width=device-width,initial-scale=1">
|
||
<link rel="icon" href="/favicon.ico" sizes="any">
|
||
<link rel="apple-touch-icon" href="/favicon.ico">''')
|
||
|
||
|
||
def main() -> int:
|
||
ok = True
|
||
|
||
# ---- 图标文件 ----
|
||
if not ICON_SRC.is_file():
|
||
print(f" [失败] 找不到 {ICON_SRC}")
|
||
return 1
|
||
shutil.copy2(ICON_SRC, ICON_DST)
|
||
print(f" 拷贝图标 -> relay/favicon.ico({ICON_DST.stat().st_size} 字节)")
|
||
|
||
text = RELAY.read_text(encoding="utf-8")
|
||
|
||
if "favicon.ico" in text:
|
||
print(" relay.py 里已经有 favicon 路由")
|
||
else:
|
||
for old, new, label in ((ANCHOR_ROUTE, NEW_ROUTE, "favicon 路由"),
|
||
(ANCHOR_HEAD, NEW_HEAD, "页面里的 link 声明")):
|
||
if old not in text:
|
||
print(f" [失败] {label} —— 锚点没找到")
|
||
ok = False
|
||
continue
|
||
text = text.replace(old, new, 1)
|
||
print(f" {label}")
|
||
|
||
if ok:
|
||
RELAY.write_text(text, encoding="utf-8", newline="\n")
|
||
|
||
if not ok:
|
||
return 1
|
||
|
||
try:
|
||
ast.parse(RELAY.read_text(encoding="utf-8"))
|
||
print(" relay.py 语法 OK")
|
||
except SyntaxError as exc:
|
||
print(f" 语法错误: 行 {exc.lineno}: {exc.msg}")
|
||
return 1
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|