feat: 接入授权中心 hub 形态 v1.3.12(安装统计/在线/公告/验新,右上角铃铛+版本号点击验新,失败开放)
This commit is contained in:
1 parent
214a62d872
commit
02cbda07e1
2 files changed
+138
-1
No files matched your search
@@ -13,6 +13,114 @@ if os.path.isdir(PROCFS):
|
|||||||
app = Flask(__name__, static_folder="public", static_url_path="")
|
app = Flask(__name__, static_folder="public", static_url_path="")
|
||||||
PORT = int(os.environ.get("PORT", "7031"))
|
PORT = int(os.environ.get("PORT", "7031"))
|
||||||
|
|
||||||
|
# ---------- CardAuth hub (免费形态: 安装统计/在线/公告/版本推送,失败开放) ----------
|
||||||
|
HUB_URL = os.environ.get("HUB_URL", "https://auth.88531.cn/api").rstrip("/")
|
||||||
|
HUB_PROJECT_ID = os.environ.get("HUB_PROJECT_ID", "12")
|
||||||
|
HUB_VERSION = os.environ.get("SITE_VERSION", "1.3.12")
|
||||||
|
HUB = {"ok": False, "install_id": None, "latest": "", "update_available": False,
|
||||||
|
"update_required": False, "download_url": "", "changelog": "",
|
||||||
|
"announcements": [], "popup": [], "ts": 0}
|
||||||
|
|
||||||
|
def _hub_key():
|
||||||
|
try:
|
||||||
|
kf = os.environ.get("HUB_KEY_FILE", "")
|
||||||
|
if kf and os.path.exists(kf):
|
||||||
|
with open(kf) as f:
|
||||||
|
k = f.read().strip()
|
||||||
|
if k:
|
||||||
|
return k
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return os.environ.get("HUB_API_KEY", "").strip()
|
||||||
|
|
||||||
|
def _machine_id():
|
||||||
|
raw = ""
|
||||||
|
for p in ("/host/etc/machine-id", "/etc/machine-id"):
|
||||||
|
try:
|
||||||
|
with open(p) as f:
|
||||||
|
raw = f.read().strip()
|
||||||
|
if raw:
|
||||||
|
break
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
if not raw:
|
||||||
|
try:
|
||||||
|
raw = open("/proc/sys/kernel/random/uuid").read().strip()
|
||||||
|
except Exception:
|
||||||
|
raw = str(time.time())
|
||||||
|
h = hashlib.sha256(("fnos-resmon-12-" + raw).encode()).hexdigest()
|
||||||
|
return str(int(h[:15], 16) % 10**16).zfill(16)[:16]
|
||||||
|
|
||||||
|
def _hub_post(path, body):
|
||||||
|
key = _hub_key()
|
||||||
|
if not key:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
req = urllib.request.Request(
|
||||||
|
HUB_URL + path, data=json.dumps(body).encode(),
|
||||||
|
headers={"Content-Type": "application/json", "X-Api-Key": key,
|
||||||
|
"X-Project-Id": HUB_PROJECT_ID}, method="POST")
|
||||||
|
with urllib.request.urlopen(req, timeout=8) as r:
|
||||||
|
j = json.loads(r.read().decode())
|
||||||
|
return j if j.get("code") == 200 else None
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _hub_get_ann():
|
||||||
|
try:
|
||||||
|
u = (HUB_URL + "/public/announcements?target=client&project_id="
|
||||||
|
+ urllib.parse.quote(HUB_PROJECT_ID) + "&limit=20")
|
||||||
|
with urllib.request.urlopen(urllib.request.Request(u), timeout=8) as r:
|
||||||
|
j = json.loads(r.read().decode())
|
||||||
|
if j.get("code") != 200:
|
||||||
|
return None
|
||||||
|
d = j.get("data") or {}
|
||||||
|
items = d.get("list", []) or []
|
||||||
|
out = [a for a in items
|
||||||
|
if a.get("project_id") is None or str(a.get("project_id")) == str(HUB_PROJECT_ID)]
|
||||||
|
return {"all": out, "popup": d.get("popup", []) or []}
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _hub_loop():
|
||||||
|
mid = _machine_id()
|
||||||
|
last_hb, last_cu = 0, 0
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
now = time.time()
|
||||||
|
if now - last_cu >= 6 * 3600:
|
||||||
|
r = _hub_post("/public/installations/check-update",
|
||||||
|
{"machine_id": mid, "client_version": HUB_VERSION,
|
||||||
|
"device_info": "fnos-resmon/docker"})
|
||||||
|
if r:
|
||||||
|
d = r.get("data") or {}
|
||||||
|
HUB.update(ok=True, install_id=d.get("install_id"),
|
||||||
|
latest=d.get("latest_version") or "",
|
||||||
|
update_available=bool(d.get("update_available")),
|
||||||
|
update_required=bool(d.get("update_required")),
|
||||||
|
download_url=d.get("download_url") or "",
|
||||||
|
changelog=d.get("changelog") or "")
|
||||||
|
last_cu = now
|
||||||
|
if now - last_hb >= 30 * 60:
|
||||||
|
r = _hub_post("/public/installations/heartbeat", {"machine_id": mid})
|
||||||
|
if r:
|
||||||
|
HUB["ok"] = True
|
||||||
|
iid = (r.get("data") or {}).get("install_id")
|
||||||
|
if iid:
|
||||||
|
HUB["install_id"] = iid
|
||||||
|
last_hb = now
|
||||||
|
a = _hub_get_ann()
|
||||||
|
if a is not None:
|
||||||
|
HUB["announcements"] = a["all"]
|
||||||
|
HUB["popup"] = a["popup"]
|
||||||
|
HUB["ok"] = True
|
||||||
|
HUB["ts"] = now
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
time.sleep(60)
|
||||||
|
|
||||||
|
threading.Thread(target=_hub_loop, daemon=True).start()
|
||||||
|
|
||||||
# ---------- host netns helpers (桥接模式用) ----------
|
# ---------- host netns helpers (桥接模式用) ----------
|
||||||
# 容器切桥接后,直读 /proc/net 只能看到容器自身 netns。
|
# 容器切桥接后,直读 /proc/net 只能看到容器自身 netns。
|
||||||
# 网络类数据统一经 nsenter 进入宿主(PID 1) netns 读取,保证与 host 模式一致。
|
# 网络类数据统一经 nsenter 进入宿主(PID 1) netns 读取,保证与 host 模式一致。
|
||||||
@@ -1152,5 +1260,20 @@ def api_kill():
|
|||||||
def health():
|
def health():
|
||||||
return jsonify({"ok": True})
|
return jsonify({"ok": True})
|
||||||
|
|
||||||
|
@app.route("/api/license-config")
|
||||||
|
def license_config():
|
||||||
|
return jsonify({
|
||||||
|
"version": HUB_VERSION,
|
||||||
|
"latest": HUB.get("latest") or "",
|
||||||
|
"update_available": bool(HUB.get("update_available")),
|
||||||
|
"update_required": bool(HUB.get("update_required")),
|
||||||
|
"download_url": HUB.get("download_url") or "",
|
||||||
|
"changelog": HUB.get("changelog") or "",
|
||||||
|
"announcement": (HUB.get("announcements") or [None])[0],
|
||||||
|
"announcements": HUB.get("announcements") or [],
|
||||||
|
"popup": HUB.get("popup") or [],
|
||||||
|
"hub_ok": bool(HUB.get("ok")),
|
||||||
|
})
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", port=PORT, threaded=True)
|
app.run(host="0.0.0.0", port=PORT, threaded=True)
|
||||||
+15
-1
@@ -56,7 +56,8 @@ button.refresh{background:#20a53a;color:#fff;border:none;border-radius:3px;paddi
|
|||||||
<div class="tab" data-t="disk">磁盘</div>
|
<div class="tab" data-t="disk">磁盘</div>
|
||||||
<div class="tab" data-t="net">网络</div>
|
<div class="tab" data-t="net">网络</div>
|
||||||
<div style="margin-left:auto;display:flex;align-items:center;gap:12px;padding-right:16px;font-size:12px">
|
<div style="margin-left:auto;display:flex;align-items:center;gap:12px;padding-right:16px;font-size:12px">
|
||||||
<span style="color:#999">v1.3.11</span>
|
<span id="licBell" onclick="toggleAnn()" title="公告" style="cursor:pointer;position:relative;font-size:15px">🔔<i id="licDot" style="display:none;position:absolute;top:-2px;right:-4px;width:8px;height:8px;border-radius:50%;background:#f00"></i></span>
|
||||||
|
<span id="licVer" onclick="checkVer(true)" title="点击检查新版本" style="color:#999;cursor:pointer">v1.3.12</span>
|
||||||
<label style="color:#999">刷新 <select id="ival" onchange="setIval(this.value)" style="border:1px solid #ddd;border-radius:3px;padding:2px 4px;color:#333"><option value="3">3s</option><option value="5">5s</option><option value="10">10s</option><option value="30">30s</option><option value="60">60s</option></select></label>
|
<label style="color:#999">刷新 <select id="ival" onchange="setIval(this.value)" style="border:1px solid #ddd;border-radius:3px;padding:2px 4px;color:#333"><option value="3">3s</option><option value="5">5s</option><option value="10">10s</option><option value="30">30s</option><option value="60">60s</option></select></label>
|
||||||
<button id="pauseBtn" onclick="togglePause()" style="background:#ff9900;color:#fff;border:none;border-radius:3px;padding:3px 10px;cursor:pointer">⏸ 停止刷新</button>
|
<button id="pauseBtn" onclick="togglePause()" style="background:#ff9900;color:#fff;border:none;border-radius:3px;padding:3px 10px;cursor:pointer">⏸ 停止刷新</button>
|
||||||
<a href="https://nav.88531.cn" target="_blank" style="color:#20a53a;text-decoration:none">资享导航站</a>
|
<a href="https://nav.88531.cn" target="_blank" style="color:#20a53a;text-decoration:none">资享导航站</a>
|
||||||
@@ -76,7 +77,10 @@ button.refresh{background:#20a53a;color:#fff;border:none;border-radius:3px;paddi
|
|||||||
<tbody id="tbody"></tbody>
|
<tbody id="tbody"></tbody>
|
||||||
</table>
|
</table>
|
||||||
<div id="disks"></div><div id="netextra"></div></div>
|
<div id="disks"></div><div id="netextra"></div></div>
|
||||||
|
<div id="annPanel" style="display:none;position:fixed;top:46px;right:12px;width:340px;max-height:60vh;overflow:auto;background:#fff;border:1px solid #ddd;border-radius:8px;box-shadow:0 6px 24px rgba(0,0,0,.15);z-index:1000;padding:12px 14px;font-size:13px;line-height:1.8"></div>
|
||||||
|
<div id="annPop" style="display:none;position:fixed;right:12px;bottom:40px;width:340px;background:#fffbe6;border:1px solid #f0c36d;border-radius:8px;box-shadow:0 6px 24px rgba(0,0,0,.15);z-index:1000;padding:12px 14px;font-size:13px;line-height:1.8"></div>
|
||||||
<script>
|
<script>
|
||||||
|
let LIC={announcements:[],popup:[]};
|
||||||
let cur='cpu';
|
let cur='cpu';
|
||||||
let sortState={key:null,dir:-1};
|
let sortState={key:null,dir:-1};
|
||||||
function loadSort(){try{const v=JSON.parse(localStorage.getItem('fnos_sort_'+cur)||'null');if(v&&v.key){sortState=v;return}}catch(e){}sortState={key:null,dir:-1};}
|
function loadSort(){try{const v=JSON.parse(localStorage.getItem('fnos_sort_'+cur)||'null');if(v&&v.key){sortState=v;return}}catch(e){}sortState={key:null,dir:-1};}
|
||||||
@@ -209,6 +213,16 @@ function startTimer(){if(timer)clearInterval(timer);if(!paused)timer=setInterval
|
|||||||
function setIval(v){ivalSec=+v;startTimer()}
|
function setIval(v){ivalSec=+v;startTimer()}
|
||||||
function togglePause(){paused=!paused;document.getElementById('pauseBtn').textContent=paused?'▶ 恢复刷新':'⏸ 停止刷新';startTimer()}
|
function togglePause(){paused=!paused;document.getElementById('pauseBtn').textContent=paused?'▶ 恢复刷新':'⏸ 停止刷新';startTimer()}
|
||||||
startTimer();
|
startTimer();
|
||||||
|
async function licPoll(){try{const r=await fetch('/api/license-config',{cache:'no-store'});const j=await r.json();LIC=j;
|
||||||
|
const dot=document.getElementById('licDot');if(dot)dot.style.display=(j.announcements&&j.announcements.length)?'':'none';
|
||||||
|
const v=document.getElementById('licVer');if(v)v.textContent='v'+(j.version||'1.3.12')+(j.update_available?' ●':'');
|
||||||
|
if(j.popup&&j.popup.length){try{const seen=JSON.parse(localStorage.getItem('fnos_ann_seen')||'[]');const fresh=j.popup.filter(a=>seen.indexOf(a.id)<0);if(fresh.length){const a=fresh[0];const el=document.getElementById('annPop');el.innerHTML=`<div style="display:flex;justify-content:space-between;align-items:center"><b>📢 ${esc(a.title||'公告')}</b><span onclick="dismissPop(${a.id})" style="cursor:pointer;color:#999">✕</span></div><div style="margin-top:6px">${a.content||''}</div>`;el.style.display='';}}catch(e){}}
|
||||||
|
if(document.getElementById('annPanel').style.display!=='none')renderAnn();}catch(e){}}
|
||||||
|
function renderAnn(){const el=document.getElementById('annPanel');const list=LIC.announcements||[];el.innerHTML=list.length?(`<b>📢 公告(${list.length})</b>`+list.map(a=>`<div style="margin-top:8px;padding-top:8px;border-top:1px dashed #eee"><div><b>${esc(a.title||'')}</b></div><div style="color:#666">${a.content||''}</div></div>`).join('')):'<span style="color:#999">暂无公告</span>';}
|
||||||
|
function toggleAnn(){const el=document.getElementById('annPanel');if(el.style.display==='none'){renderAnn();el.style.display=''}else el.style.display='none'}
|
||||||
|
function dismissPop(id){try{const seen=JSON.parse(localStorage.getItem('fnos_ann_seen')||'[]');seen.push(id);localStorage.setItem('fnos_ann_seen',JSON.stringify(seen))}catch(e){}document.getElementById('annPop').style.display='none'}
|
||||||
|
async function checkVer(manual){try{const r=await fetch('/api/license-config',{cache:'no-store'});const j=await r.json();LIC=j;if(j.update_available){alert('发现新版本 v'+(j.latest||'')+'\n'+(j.changelog||'')+(j.download_url?('\n下载:'+j.download_url):''))}else if(manual){alert('当前已是最新版本 v'+(j.version||''))}}catch(e){if(manual)alert('版本检查失败,稍后重试')}}
|
||||||
|
licPoll();setInterval(licPoll,10*60*1000);
|
||||||
</script>
|
</script>
|
||||||
<footer style="position:fixed;bottom:0;left:0;right:0;text-align:center;padding:6px;font-size:12px;color:#999;background:#fff;border-top:1px solid #eee;z-index:999">
|
<footer style="position:fixed;bottom:0;left:0;right:0;text-align:center;padding:6px;font-size:12px;color:#999;background:#fff;border-top:1px solid #eee;z-index:999">
|
||||||
<a href="https://nav.88531.cn" target="_blank" rel="noopener">资享导航站</a> ·
|
<a href="https://nav.88531.cn" target="_blank" rel="noopener">资享导航站</a> ·
|
||||||
|
|||||||
Reference in new issue
Block a user