Google
Edit File: Autom8Redis.live.py
import html import os import socket import stat def close_cpanel_liveapi_socket(): """Close the cPanel LiveAPI socket; this page does not use the API.""" sock_path = os.environ.get("CPANEL_CONNECT_SOCKET") if not sock_path: return try: sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(sock_path) sock.sendall(b'<cpanelxml shutdown="1" />') sock.close() except OSError: pass def redis_enabled(path): try: return stat.S_ISSOCK(os.lstat(path).st_mode) except OSError: return False close_cpanel_liveapi_socket() print("Content-Type: text/html") print("") sock_path = os.path.join(os.environ["HOME"], "tmp", "redis.sock") enabled = redis_enabled(sock_path) print("""<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Redis</title> <style> body { font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; background: #f4f5f7; color: #1f2933; margin: 0; padding: 24px 16px; } .card { max-width: 640px; margin: 0 auto; background: #ffffff; border: 1px solid #d9dde3; border-radius: 8px; padding: 24px; } h1 { margin: 0 0 4px; font-size: 22px; } h1 span { color: #d82c20; } p.lead { margin: 0 0 20px; color: #52606d; font-size: 14px; } .status { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 13px; font-weight: 600; } .status.on { background: #e3f6e8; color: #19703a; } .status.off { background: #f1f2f4; color: #52606d; } fieldset { border: 1px solid #d9dde3; border-radius: 6px; margin: 16px 0; padding: 12px 16px; } legend { font-size: 13px; color: #52606d; padding: 0 6px; } label { margin-right: 24px; font-size: 15px; } button { background: #d82c20; color: #fff; border: 0; border-radius: 6px; padding: 8px 20px; font-size: 14px; cursor: pointer; } button:disabled { opacity: 0.6; cursor: wait; } .sockpath { background: #f1f2f4; border-radius: 4px; padding: 8px 10px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; word-break: break-all; } .hint { font-size: 13px; color: #52606d; margin-top: 16px; } #result { margin-top: 12px; font-size: 14px; color: #b3261e; min-height: 1.2em; } </style> </head> <body> <div class="card"> <h1><span>Redis</span> for your account</h1> <p class="lead">A private Redis cache for your account. Data is kept in memory only and never saved to disk, so it can be lost at any time — store only data your application can recreate, not permanent records.</p>""") if enabled: print(' <div>Status: <span class="status on">ON</span></div>') else: print(' <div>Status: <span class="status off">OFF</span></div>') print(""" <form id="myform"> <fieldset> <legend>Redis instance</legend> <label><input type="radio" name="Option" value="ON"%s> ON</label> <label><input type="radio" name="Option" value="OFF"%s> OFF</label> <button type="submit" id="btnId">Apply</button> </fieldset> </form> <div id="result"></div>""" % (" checked" if enabled else "", "" if enabled else " checked")) if enabled: print(""" <div class="hint">Connect your application through this unix socket (no TCP port is exposed):</div> <div class="sockpath">%s</div>""" % html.escape(sock_path)) else: print(' <div class="hint">Turning Redis ON creates a private instance reachable only through a unix socket in your home directory.</div>') print("""</div> <script> document.getElementById("myform").addEventListener("submit", function (e) { e.preventDefault(); var option = document.querySelector('input[name="Option"]:checked').value; var button = document.getElementById("btnId"); var result = document.getElementById("result"); button.disabled = true; result.textContent = "Applying\\u2026 this can take a few seconds."; fetch("save_Autom8Redis.live.py?Option=" + encodeURIComponent(option)) .then(function (r) { return r.text(); }) .then(function (text) { text = text.trim(); if (text.indexOf("ERROR") === 0) { result.textContent = text; button.disabled = false; } else { location.reload(); } }) .catch(function () { result.textContent = "ERROR: request failed"; button.disabled = false; }); }); </script> </body> </html>""")