nomadro/deploy/social/mirotalk/apply_nomadro_whitelabel.py
Eric 4789785f11 Ship remaining business changes across social API, miniprogram audit/book, VIP track, and deploy.
Includes social realtime entrypoints, weapp audit book surfaces, VIP analytics track routes, MiroTalk/Centrifugo deploy helpers, and related admin/config wiring. Excludes local DB dumps and .tmp probe scripts.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-01 17:10:10 +08:00

355 lines
12 KiB
Python

#!/usr/bin/env python3
"""White-label MiroTalk P2P on 82.157 for nomadro.cn embed (no MiroTalk chrome/popups)."""
from __future__ import annotations
import io
import re
import sys
from pathlib import Path
import paramiko
HOST = "82.157.112.245"
USER = "ubuntu"
PASS = "Xiao4669805"
BASE = "/opt/nomadro-services/mirotalk"
LOCAL_CSS = Path(__file__).with_name("nomadro-embed.css")
REMOTE_CSS = f"{BASE}/public/css/nomadro-embed.css"
MARKER = "/* nomadro-embed */"
def ssh() -> paramiko.SSHClient:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
HOST,
username=USER,
password=PASS,
timeout=30,
allow_agent=False,
look_for_keys=False,
)
return c
def run(c: paramiko.SSHClient, cmd: str, timeout: int = 120) -> tuple[str, int]:
_i, o, e = c.exec_command(cmd, timeout=timeout)
out = o.read().decode("utf-8", "ignore")
err = e.read().decode("utf-8", "ignore")
code = o.channel.recv_exit_status()
text = out + (("\n" + err) if err.strip() else "")
return text, code
def sftp_write(c: paramiko.SSHClient, remote: str, data: str | bytes) -> None:
sftp = c.open_sftp()
# write to /tmp then sudo mv (ubuntu may lack write on /opt)
tmp = f"/tmp/{Path(remote).name}"
with sftp.file(tmp, "w") as f:
if isinstance(data, bytes):
f.write(data)
else:
f.write(data.encode("utf-8"))
sftp.close()
out, code = run(c, f"sudo mv {tmp} {remote} && sudo chmod 644 {remote}")
if code != 0:
raise RuntimeError(f"sftp_write failed: {out}")
def patch_config(text: str) -> str:
# brand.app
text = re.sub(
r"name:\s*'MiroTalk'",
"name: 'nomadro'",
text,
count=1,
)
text = re.sub(
r"title:\s*'<h1>MiroTalk</h1>[^']*'",
"title: '<h1>nomadro</h1>游民社区 · 线上活动厅'",
text,
count=1,
)
text = re.sub(
r"description:\s*\n\s*'Start your next video call[^']*'",
"description: 'nomadro 游民社区视频厅。站内进入,无需下载。'",
text,
count=1,
)
text = re.sub(
r"joinDescription:\s*'Pick a room name\.<br />How about this one\?'",
"joinDescription: '进入活动房间'",
text,
count=1,
)
text = re.sub(
r"joinButtonLabel:\s*'JOIN ROOM'",
"joinButtonLabel: '进入房间'",
text,
count=1,
)
text = re.sub(
r"siteName:\s*'MiroTalk'",
"siteName: 'nomadro'",
text,
count=1,
)
text = re.sub(
r"title:\s*'Click the link to make a call\.'",
"title: 'nomadro 线上活动'",
text,
count=1,
)
text = re.sub(
r"description:\s*\n\s*'MiroTalk calling provides[^']*'",
"description: 'nomadro 游民社区 · 视频活动'",
text,
count=1,
)
# hide landing marketing blocks
for key in (
"topSponsors",
"features",
"browsers",
"teams",
"tryEasier",
"poweredBy",
"sponsors",
"pastSponsors",
"advertisers",
"supportUs",
"footer",
):
text = re.sub(rf"{key}:\s*true", f"{key}: false", text)
# buttons.main — hide share/about/extra chrome; keep A/V/screen
replacements = {
"showShareQr: true": "showShareQr: false",
"showShareRoomBtn: true": "showShareRoomBtn: false",
"showAboutBtn: true": "showAboutBtn: false",
"showExtraBtn: true": "showExtraBtn: false",
"showChatRoomBtn: true": "showChatRoomBtn: false", # portal has side chat
"showCaptionRoomBtn: true": "showCaptionRoomBtn: false",
"showRoomEmojiPickerBtn: true": "showRoomEmojiPickerBtn: false",
"showWhiteboardBtn: true": "showWhiteboardBtn: false",
"showSnapshotRoomBtn: true": "showSnapshotRoomBtn: false",
"showFileShareBtn: true": "showFileShareBtn: false",
"showDocumentPipBtn: true": "showDocumentPipBtn: false",
"showRecordStreamBtn: true": "showRecordStreamBtn: false",
"showMyHandBtn: true": "showMyHandBtn: false",
"showTabEmailInvitation: true": "showTabEmailInvitation: false",
}
for a, b in replacements.items():
text = text.replace(a, b)
# themes.dark — enable nomadro palette
if "dark: {" not in text or "--body-bg': 'radial-gradient(#0a0e17" not in text:
theme_block = """themes: {
dark: {
'--body-bg': 'radial-gradient(#111827, #0a0e17)',
'--msger-bg': 'radial-gradient(#111827, #0a0e17)',
'--msger-private-bg': 'radial-gradient(#111827, #0a0e17)',
'--wb-bg': 'radial-gradient(#111827, #0a0e17)',
'--elem-border-color': '1px solid rgba(255, 255, 255, 0.08)',
'--navbar-bg': 'rgba(10, 14, 23, 0.88)',
'--select-bg': '#111827',
'--tab-btn-active': '#1f2937',
'--box-shadow': '0px 8px 32px 0px rgba(0, 0, 0, 0.35)',
'--left-msg-bg': '#1f2937',
'--right-msg-bg': '#0f766e',
'--private-msg-bg': '#1f2937',
'--btn-bar-bg-color': '#4ecdc4',
'--btn-bar-color': '#0a0e17',
'--btns-bg-color': 'rgba(10, 14, 23, 0.78)',
'--dd-color': '#4ecdc4',
},
},"""
text = re.sub(
r"themes:\s*\{[\s\S]*?\n \},\n /\*\*\n \* Configuration for controlling the visibility of buttons",
theme_block
+ "\n /**\n * Configuration for controlling the visibility of buttons",
text,
count=1,
)
# about box — strip MiroTalk promo
text = re.sub(
r"about:\s*\{[\s\S]*?\n \},",
"""about: {
imageUrl: '../images/logo.svg',
title: 'nomadro 视频厅',
html: '<span>游民社区 · 线上活动</span>',
},""",
text,
count=1,
)
return text
def patch_client_js(text: str) -> str:
# Never auto-open share room popup
text = re.sub(
r"function getNotify\(\)\s*\{[\s\S]*?return notify;\s*\}",
"function getNotify() {\n"
" // nomadro: never auto-popup Share the room\n"
" return false;\n"
"}",
text,
count=1,
)
if "function shareRoomMeetingURL" in text and "nomadro: skip share popup" not in text:
text = text.replace(
"function shareRoomMeetingURL(checkScreen = false) {",
"function shareRoomMeetingURL(checkScreen = false) {\n"
" // nomadro: skip share popup\n"
" return;",
1,
)
if "function shareRoomUrl" in text and "nomadro: skip shareRoomUrl" not in text:
text = text.replace(
"async function shareRoomUrl() {",
"async function shareRoomUrl() {\n"
" // nomadro: skip shareRoomUrl\n"
" return;",
1,
)
# MiroTalk sometimes force-enables share for guests — keep off
text = text.replace(
"buttons.main.showShareRoomBtn = true;",
"buttons.main.showShareRoomBtn = false; // nomadro",
)
text = text.replace(
"buttons.main.showAboutBtn = true;",
"buttons.main.showAboutBtn = false; // nomadro",
)
return text
def patch_buttons_js(text: str) -> str:
for a, b in {
"showShareQr: true": "showShareQr: false",
"showShareRoomBtn: true": "showShareRoomBtn: false",
"showAboutBtn: true": "showAboutBtn: false",
"showExtraBtn: true": "showExtraBtn: false",
"showChatRoomBtn: true": "showChatRoomBtn: false",
"showCaptionRoomBtn: true": "showCaptionRoomBtn: false",
"showRoomEmojiPickerBtn: true": "showRoomEmojiPickerBtn: false",
"showWhiteboardBtn: true": "showWhiteboardBtn: false",
"showSnapshotRoomBtn: true": "showSnapshotRoomBtn: false",
"showFileShareBtn: true": "showFileShareBtn: false",
"showDocumentPipBtn: true": "showDocumentPipBtn: false",
"showRecordStreamBtn: true": "showRecordStreamBtn: false",
"showMyHandBtn: true": "showMyHandBtn: false",
}.items():
text = text.replace(a, b)
return text
def patch_client_html(text: str) -> str:
if "nomadro-embed.css" not in text:
text = text.replace(
'<link rel="stylesheet" href="../css/client.css" />',
'<link rel="stylesheet" href="../css/client.css" />\n'
' <link rel="stylesheet" href="../css/nomadro-embed.css" />',
1,
)
boot = (
"<script>"
"document.documentElement.classList.add('nomadro-embed');"
"document.addEventListener('DOMContentLoaded',function(){"
"document.body&&document.body.classList.add('nomadro-embed');"
"});"
"</script>"
)
if "nomadro-embed" not in text.split("</head>")[0]:
text = text.replace("</head>", f" {boot}\n </head>", 1)
# title
text = re.sub(
r"<title id=\"clientTitle\">[^<]*</title>",
'<title id="clientTitle">nomadro 视频厅</title>',
text,
count=1,
)
return text
def patch_env(text: str) -> str:
def set_kv(src: str, key: str, value: str) -> str:
if re.search(rf"^{re.escape(key)}=", src, re.M):
return re.sub(rf"^{re.escape(key)}=.*$", f"{key}={value}", src, count=1, flags=re.M)
return src.rstrip() + f"\n{key}={value}\n"
text = set_kv(text, "SURVEY_ENABLED", "false")
text = set_kv(text, "STATS_ENABLED", "false")
text = set_kv(text, "REDIRECT_ENABLED", "true")
text = set_kv(text, "REDIRECT_URL", "https://nomadro.cn/social/meetup")
text = set_kv(text, "SHOW_ACTIVE_ROOMS", "false")
# keep ALLOWED_EMBED_ORIGINS if present
return text
def main() -> int:
if not LOCAL_CSS.exists():
print("missing", LOCAL_CSS, file=sys.stderr)
return 1
c = ssh()
print("connected")
# backup once
run(
c,
f"sudo test -f {BASE}/app/src/config.js.bak_nomadro || "
f"sudo cp {BASE}/app/src/config.js {BASE}/app/src/config.js.bak_nomadro; "
f"sudo test -f {BASE}/public/js/client.js.bak_nomadro || "
f"sudo cp {BASE}/public/js/client.js {BASE}/public/js/client.js.bak_nomadro; "
f"sudo test -f {BASE}/public/views/client.html.bak_nomadro || "
f"sudo cp {BASE}/public/views/client.html {BASE}/public/views/client.html.bak_nomadro",
)
# upload CSS
sftp_write(c, REMOTE_CSS, LOCAL_CSS.read_text(encoding="utf-8"))
print("css uploaded")
# patch files
for remote, fn in [
(f"{BASE}/app/src/config.js", patch_config),
(f"{BASE}/public/js/client.js", patch_client_js),
(f"{BASE}/public/js/buttons.js", patch_buttons_js),
(f"{BASE}/public/views/client.html", patch_client_html),
(f"{BASE}/.env", patch_env),
]:
out, code = run(c, f"sudo cat {remote}")
if code != 0:
print("FAIL read", remote, out)
return 1
new = fn(out)
if new == out:
print("unchanged", remote)
else:
sftp_write(c, remote, new)
print("patched", remote)
# restart
out, code = run(c, "sudo systemctl restart mirotalk-p2p && sleep 2 && systemctl is-active mirotalk-p2p")
print(out)
if code != 0 or "active" not in out:
print("restart failed", file=sys.stderr)
return 1
# smoke: local headers + join page contains our css
out, _ = run(
c,
"curl -sI http://127.0.0.1:9300/join?room=t&name=t&notify=0 | head -20; "
"curl -s 'http://127.0.0.1:9300/join?room=t&name=t&notify=0' | "
"grep -o 'nomadro-embed.css\\|nomadro 视频厅\\|getNotify' | head",
)
print("smoke:\n", out)
c.close()
return 0
if __name__ == "__main__":
raise SystemExit(main())