Created
May 30, 2026 00:48
-
-
Save me-suzy/6cb89af046e69330e9b429af932b9191 to your computer and use it in GitHub Desktop.
afsddgsdf.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """Always-on listener: waits for a remote 'start' command via ntfy.sh and | |
| launches start.bat (which starts the Flask + ngrok server). | |
| Outbound-only (long-poll). No port forwarding needed. | |
| Runs at logon via Task Scheduler; keeps running while you are logged in. | |
| """ | |
| import json | |
| import socket | |
| import subprocess | |
| import sys | |
| import time | |
| from datetime import datetime | |
| from pathlib import Path | |
| from urllib.request import urlopen | |
| BASE_DIR = Path(__file__).resolve().parent | |
| # The server (start.bat) lives in a sibling folder; the watcher only launches it. | |
| START_BAT = BASE_DIR.parent / "server-arcanum-files" / "start.bat" | |
| LOG_FILE = BASE_DIR / "watcher.log" | |
| # --- secret command channel (do NOT share / do NOT commit) --- | |
| NTFY_TOPIC = "laptop-89d9cbd077bfb2b0f" | |
| TRIGGER_SECRET = " " | |
| # ------------------------------------------------------------- | |
| SERVER_PORT = 5000 # if this port is already open, the server is running | |
| def log(msg: str) -> None: | |
| line = f"{datetime.now():%Y-%m-%d %H:%M:%S} {msg}" | |
| print(line, flush=True) | |
| try: | |
| with LOG_FILE.open("a", encoding="utf-8") as f: | |
| f.write(line + "\n") | |
| except OSError: | |
| pass | |
| def server_running() -> bool: | |
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
| s.settimeout(0.7) | |
| return s.connect_ex(("127.0.0.1", SERVER_PORT)) == 0 | |
| def launch_server() -> None: | |
| if server_running(): | |
| log("Trigger received, but server is already running. Skipping.") | |
| return | |
| log("Trigger valid. Launching start.bat ...") | |
| subprocess.Popen( | |
| ["cmd.exe", "/c", "start", "", str(START_BAT)], | |
| cwd=str(BASE_DIR), | |
| shell=False, | |
| creationflags=subprocess.CREATE_NEW_CONSOLE, | |
| ) | |
| # mutable holder so reconnects resume after the last seen message | |
| _state = {"since": str(int(time.time()))} | |
| def main() -> None: | |
| if not START_BAT.exists(): | |
| log(f"FATAL: {START_BAT} not found.") | |
| sys.exit(1) | |
| log("Watcher started.") | |
| while True: | |
| try: | |
| url = f"https://ntfy.sh/{NTFY_TOPIC}/json?since={_state['since']}" | |
| with urlopen(url, timeout=320) as resp: | |
| log(f"Listening for trigger... (server up: {server_running()})") | |
| for raw in resp: | |
| line = raw.decode("utf-8", "replace").strip() | |
| if not line: | |
| continue | |
| try: | |
| data = json.loads(line) | |
| except json.JSONDecodeError: | |
| continue | |
| ev = data.get("event") | |
| if ev in ("keepalive", "open"): | |
| continue | |
| if ev != "message": | |
| continue | |
| if data.get("time"): | |
| _state["since"] = str(int(data["time"]) + 1) | |
| body = (data.get("message") or "").strip() | |
| if body == TRIGGER_SECRET: | |
| launch_server() | |
| else: | |
| log("Message with wrong secret. Ignored.") | |
| except Exception as e: # noqa: BLE001 | |
| log(f"Connection dropped ({e!r}). Reconnecting in 5s...") | |
| time.sleep(5) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment