import os, threading, subprocess, time, json from flask import Flask, jsonify, Response, request app = Flask(__name__) KING_TOKEN = os.environ.get("NGROK_TOKEN", "3BP3Zy2jMT9bIxdWE31zT21lcjo_2V3MreFKRypdAGku5Y7md") KING_PASS = os.environ.get("VPS_PASS", "godmode123") KING_DOMAIN = "bibliopegistic-casey-symptomatically.ngrok-free.dev" def system_ignition(): os.system("tmux kill-server 2>/dev/null") os.system("tmux new-session -d -s pekka 2>/dev/null") os.system('echo "set -g mouse on" > ~/.tmux.conf') os.system('tmux source-file ~/.tmux.conf 2>/dev/null') os.system("mkdir -p /tmp/client_body /tmp/fastcgi_temp /tmp/proxy_temp /tmp/scgi_temp /tmp/uwsgi_temp") nginx_conf = """ worker_processes auto; daemon off; error_log /tmp/error.log; pid /tmp/nginx.pid; events { worker_connections 1024; } http { client_body_temp_path /tmp/client_body; fastcgi_temp_path /tmp/fastcgi_temp; proxy_temp_path /tmp/proxy_temp; scgi_temp_path /tmp/scgi_temp; uwsgi_temp_path /tmp/uwsgi_temp; access_log off; server { listen 7860; location / { proxy_pass http://127.0.0.1:5000/; } location /terminal/ { proxy_pass http://127.0.0.1:4445; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; } } } """ with open("/tmp/nginx.conf", "w") as f: f.write(nginx_conf) subprocess.Popen(["nginx", "-c", "/tmp/nginx.conf"]) theme = '{"background":"#000000","foreground":"#c9d1d9","cursor":"#58a6ff"}' subprocess.Popen([ "ttyd", "-p", "4445", "-b", "/terminal", "-c", f"pekka:{KING_PASS}", "-t", "fontSize=14", "-t", "enableButtons=false", "-t", f"theme={theme}", "tmux", "attach", "-t", "pekka" ]) os.system(f"ngrok config add-authtoken {KING_TOKEN}") while True: process = subprocess.Popen(f"ngrok http 7860 --domain={KING_DOMAIN} --basic-auth=\"pekka:{KING_PASS}\" --log=stdout", shell=True) process.wait() time.sleep(3) threading.Thread(target=system_ignition, daemon=True).start() # ========================================== # TITANIUM BACKEND # ========================================== @app.route('/cmd', methods=['POST']) def run_cmd(): key = request.form.get('key') text = request.form.get('text') sys_cmd = request.form.get('sys_cmd') try: if sys_cmd: subprocess.Popen(["tmux", sys_cmd, "-t", "pekka"]) elif text: subprocess.Popen(["tmux", "send-keys", "-t", "pekka", "-l", text]) elif key: subprocess.Popen(["tmux", "send-keys", "-t", "pekka", key]) except Exception as e: print(f"Backend Error: {e}") return "ok" @app.route('/capture', methods=['GET']) def capture_term(): try: out = subprocess.check_output(["tmux", "capture-pane", "-p", "-S", "-500", "-t", "pekka"]) return Response(out.decode('utf-8', 'replace'), mimetype='text/plain') except Exception as e: return str(e) # ========================================== # PWA ASSETS # ========================================== @app.route('/manifest.json') def manifest(): manifest_data = { "name": "Pekka Terminal", "short_name": "Pekka", "start_url": "/", "display": "standalone", "background_color": "#000000", "theme_color": "#58a6ff", "icons":[{"src": "https://cdn-icons-png.flaticon.com/512/3208/3208082.png", "sizes": "512x512", "type": "image/png"}] } return Response(json.dumps(manifest_data), mimetype='application/manifest+json') @app.route('/sw.js') def sw(): js = """ const CACHE_NAME = 'pekka-v60'; self.addEventListener('install', e => { e.waitUntil(caches.open(CACHE_NAME).then(c => c.addAll(['/', '/manifest.json']))); self.skipWaiting(); }); self.addEventListener('activate', e => { self.clients.claim(); }); self.addEventListener('fetch', e => { if(e.request.url.includes('/terminal/')) { e.respondWith(fetch(e.request).catch(() => new Response('Offline'))); } else { e.respondWith(caches.match(e.request).then(res => res || fetch(e.request))); } }); """ return Response(js, mimetype='application/javascript') # ========================================== # FRONTEND DASHBOARD (V60 GODMODE) # ========================================== @app.route('/') def dashboard(): return f"""