Spaces:
Sleeping
Sleeping
Igor commited on
Fix: Restore missing Python bridge logic in Web Terminal (fix hang)
Browse files- web/index.html +91 -0
web/index.html
CHANGED
|
@@ -150,6 +150,97 @@
|
|
| 150 |
console.log("Initializing Python bridge...");
|
| 151 |
statusEl.innerText = "Configuring secure bridge...";
|
| 152 |
await pyodide.runPythonAsync(`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
async function doAction(action) {
|
| 155 |
const input = document.getElementById('mainInput').value.trim();
|
|
|
|
| 150 |
console.log("Initializing Python bridge...");
|
| 151 |
statusEl.innerText = "Configuring secure bridge...";
|
| 152 |
await pyodide.runPythonAsync(`
|
| 153 |
+
import json
|
| 154 |
+
import base64
|
| 155 |
+
from crypto import ShitposterCrypto
|
| 156 |
+
from steg import encode_bytes, decode_string
|
| 157 |
+
from stealth import generate_stealth_text
|
| 158 |
+
from fingerprint import get_fingerprint
|
| 159 |
+
|
| 160 |
+
CRYPTO = ShitposterCrypto()
|
| 161 |
+
|
| 162 |
+
def bridge_keygen():
|
| 163 |
+
priv, pub_bytes = CRYPTO.generate_keypair()
|
| 164 |
+
priv_bytes = CRYPTO.get_private_bytes(priv)
|
| 165 |
+
return json.dumps({
|
| 166 |
+
"private_key_b64": base64.b64encode(priv_bytes).decode('utf-8'),
|
| 167 |
+
"public_signal": encode_bytes(pub_bytes)
|
| 168 |
+
})
|
| 169 |
+
|
| 170 |
+
def bridge_derive(peer_signal, my_priv_b64):
|
| 171 |
+
priv_bytes = base64.b64decode(my_priv_b64)
|
| 172 |
+
priv_key = CRYPTO.load_private_key(priv_bytes)
|
| 173 |
+
|
| 174 |
+
# Try Base64 input first
|
| 175 |
+
try:
|
| 176 |
+
if " " not in peer_signal.strip():
|
| 177 |
+
# Add padding just in case
|
| 178 |
+
b64_str = peer_signal.strip()
|
| 179 |
+
missing_padding = len(b64_str) % 4
|
| 180 |
+
if missing_padding:
|
| 181 |
+
b64_str += '=' * (4 - missing_padding)
|
| 182 |
+
peer_pub = base64.b64decode(b64_str)
|
| 183 |
+
|
| 184 |
+
# Handle 33-byte keys (leading null byte)
|
| 185 |
+
if len(peer_pub) == 33 and peer_pub[0] == 0:
|
| 186 |
+
peer_pub = peer_pub[1:]
|
| 187 |
+
else:
|
| 188 |
+
raise ValueError("Not Base64")
|
| 189 |
+
except Exception as e:
|
| 190 |
+
# Fallback to shitpost decoding if not base64
|
| 191 |
+
try:
|
| 192 |
+
peer_pub = decode_string(peer_signal)
|
| 193 |
+
except:
|
| 194 |
+
raise ValueError(f"Failed to decode key: {e}")
|
| 195 |
+
|
| 196 |
+
if len(peer_pub) != 32:
|
| 197 |
+
# Convert to hex for debug
|
| 198 |
+
hex_val = "".join("{:02x}".format(x) for x in peer_pub)
|
| 199 |
+
raise ValueError(f"Invalid Key Length: Got {len(peer_pub)} bytes. Expected 32. Hex: {hex_val}")
|
| 200 |
+
|
| 201 |
+
keys = CRYPTO.derive_shared_secret(priv_key, peer_pub)
|
| 202 |
+
|
| 203 |
+
fp = get_fingerprint(keys)
|
| 204 |
+
|
| 205 |
+
return json.dumps({
|
| 206 |
+
"tx": base64.b64encode(keys['tx']).decode('utf-8'),
|
| 207 |
+
"rx": base64.b64encode(keys['rx']).decode('utf-8'),
|
| 208 |
+
"fingerprint": fp
|
| 209 |
+
})
|
| 210 |
+
|
| 211 |
+
def bridge_encrypt(msg, key_json_or_words):
|
| 212 |
+
try:
|
| 213 |
+
data = json.loads(key_json_or_words)
|
| 214 |
+
key = base64.b64decode(data['tx'])
|
| 215 |
+
except:
|
| 216 |
+
key = decode_string(key_json_or_words)
|
| 217 |
+
|
| 218 |
+
payload = CRYPTO.encrypt_message(key, msg)
|
| 219 |
+
from steg import WORDLIST
|
| 220 |
+
words = [WORDLIST[b] for b in payload]
|
| 221 |
+
return generate_stealth_text(words)
|
| 222 |
+
|
| 223 |
+
def bridge_decrypt(shitpost, key_json_or_words):
|
| 224 |
+
try:
|
| 225 |
+
data = json.loads(key_json_or_words)
|
| 226 |
+
key = base64.b64decode(data['rx'])
|
| 227 |
+
except:
|
| 228 |
+
key = decode_string(key_json_or_words)
|
| 229 |
+
|
| 230 |
+
payload = decode_string(shitpost)
|
| 231 |
+
return CRYPTO.decrypt_message(key, payload)
|
| 232 |
+
`);
|
| 233 |
+
|
| 234 |
+
statusEl.innerText = "WASM Secure Terminal Online";
|
| 235 |
+
statusEl.style.color = "#4ec9b0";
|
| 236 |
+
document.querySelectorAll('button').forEach(b => b.disabled = false);
|
| 237 |
+
|
| 238 |
+
} catch (err) {
|
| 239 |
+
console.error(err);
|
| 240 |
+
statusEl.innerText = "Initialization Failed: " + err.message;
|
| 241 |
+
statusEl.style.color = "#f44747";
|
| 242 |
+
}
|
| 243 |
+
}
|
| 244 |
|
| 245 |
async function doAction(action) {
|
| 246 |
const input = document.getElementById('mainInput').value.trim();
|