#!/usr/bin/env python3 """Speed up MiroTalk join: skip geo/QR/sounds/RNNoise, hide loading ASAP.""" from __future__ import annotations from pathlib import Path import paramiko HOST = "82.157.112.245" USER = "ubuntu" PASS = "Xiao4669805" BASE = "/opt/nomadro-services/mirotalk" SPEED_JS = """'use strict'; /* nomadro: instant join UX */ (function () { function hideLoading() { var el = document.getElementById('loadingDiv'); if (el) { el.style.display = 'none'; el.style.visibility = 'hidden'; el.style.opacity = '0'; el.setAttribute('hidden', ''); } } // Hide as soon as HTML parses if name is already in URL (portal always sends name) try { var q = new URLSearchParams(location.search); if (q.get('name')) hideLoading(); } catch (e) {} document.addEventListener('DOMContentLoaded', function () { try { if (new URLSearchParams(location.search).get('name')) hideLoading(); } catch (e) {} }); // Also hide once media permission already granted (prior visit) if (navigator.permissions && navigator.permissions.query) { Promise.all([ navigator.permissions.query({ name: 'camera' }).catch(function () { return null; }), navigator.permissions.query({ name: 'microphone' }).catch(function () { return null; }), ]).then(function (states) { var ok = states.some(function (s) { return s && s.state === 'granted'; }); if (ok) hideLoading(); }); } })(); """ def ssh(): 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, cmd, t=120): _i, o, e = c.exec_command(cmd, timeout=t) out = o.read().decode("utf-8", "ignore") err = e.read().decode("utf-8", "ignore") return out + (("\n" + err) if err.strip() else ""), o.channel.recv_exit_status() def write(c, remote, data: str): tmp = f"/tmp/{Path(remote).name}" sftp = c.open_sftp() with sftp.file(tmp, "w") as f: 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(out) def patch_env(text: str) -> str: import re def set_kv(src, key, value): 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, "CUSTOM_NOISE_SUPPRESSION_ENABLED", "false") text = set_kv(text, "IP_LOOKUP_ENABLED", "false") text = set_kv(text, "STATS_ENABLED", "false") text = set_kv(text, "SURVEY_ENABLED", "false") return text def patch_client_js(text: str) -> str: # Skip geo if "nomadro: skip geo" not in text: text = text.replace( "function loadGeo() {", "function loadGeo() {\n" " // nomadro: skip geo\n" " return;", 1, ) # Skip QR popup generation cost if "async function makeRoomPopupQR" in text and "nomadro: skip QR" not in text: text = text.replace( "async function makeRoomPopupQR", "async function makeRoomPopupQR_disabled_nomadro", 1, ) # add fast stub before old (renamed) - find call sites use makeRoomPopupQR() # Better: replace function body start # Revert rename approach - patch call instead text = text.replace( "makeRoomPopupQR();", "/* nomadro: skip QR */ void 0;", ) # If rename left a broken fn, fix by stubbing renamed back if "async function makeRoomPopupQR_disabled_nomadro" in text and "async function makeRoomPopupQR(" not in text: text = text.replace( "async function makeRoomPopupQR_disabled_nomadro", "async function makeRoomPopupQR", 1, ) if "nomadro: skip QR body" not in text: text = text.replace( "async function makeRoomPopupQR", "async function makeRoomPopupQR(){ /* nomadro: skip QR body */ return; }\n" "async function makeRoomPopupQR_orig_unused", 1, ) # Hide loading at start of handleConnect if "nomadro: hide loading ASAP" not in text: text = text.replace( "async function handleConnect() {\n console.log('03. Connected to signaling server');", "async function handleConnect() {\n" " // nomadro: hide loading ASAP\n" " try { if (typeof loadingDiv !== 'undefined' && loadingDiv) { " "loadingDiv.style.display='none'; loadingDiv.hidden=true; } } catch(e) {}\n" " console.log('03. Connected to signaling server');", 1, ) # Skip RNNoise init await cost text = text.replace( "await initRNNoiseSuppression();", "/* nomadro: skip RNNoise */ void 0;", ) # Mute join sounds text = text.replace( " playSound('addPeer');\n return;\n }\n\n playSound('newMessage');", " /* nomadro: no join sound */ return;\n }\n\n /* nomadro: no join sound */", ) # whoAreYou named path: hide loading first line already has elemDisplay - make sync hide stronger text = text.replace( " if (myPeerName) {\n elemDisplay(loadingDiv, false);", " if (myPeerName) {\n" " try { loadingDiv.style.display='none'; loadingDiv.hidden=true; } catch(e) {}\n" " elemDisplay(loadingDiv, false);", ) return text def patch_client_html(text: str) -> str: if "nomadro-speed.js" not in text: text = text.replace( '', '\n' ' ', 1, ) if "nomadro-speed.js" not in text: text = text.replace( "", ' \n ', 1, ) return text def main(): c = ssh() print("connected") write(c, f"{BASE}/public/js/nomadro-speed.js", SPEED_JS) print("speed js uploaded") for remote, fn in [ (f"{BASE}/.env", patch_env), (f"{BASE}/public/js/client.js", patch_client_js), (f"{BASE}/public/views/client.html", patch_client_html), ]: out, code = run(c, f"sudo cat {remote}") if code != 0: raise SystemExit(f"read fail {remote}") new = fn(out) if new != out: write(c, remote, new) print("patched", remote) else: print("unchanged", remote) out, code = run(c, "sudo systemctl restart mirotalk-p2p && sleep 2 && systemctl is-active mirotalk-p2p") print(out.strip()) smoke, _ = run( c, "curl -s 'http://127.0.0.1:9300/join?room=t&name=speed¬ify=0' | " "grep -oE 'nomadro-speed.js|CUSTOM_NOISE' | sort -u; " "grep CUSTOM_NOISE /opt/nomadro-services/mirotalk/.env", ) print(smoke) c.close() if __name__ == "__main__": main()