import paramiko import sys import datetime import json sys.stdout.reconfigure(encoding='utf-8') key_path = r'C:\Users\Snarf\.ssh\id_ed25519_snarfnet' c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("140.44.4.71", username="root", key_filename=key_path, timeout=10) def main(): remote_script = """import sqlite3, datetime, json db_path = '/var/lib/docker/volumes/nginx-proxy-manager_nginx-data/_data/database.sqlite' conn = sqlite3.connect(db_path) cur = conn.cursor() # Check existing games host cur.execute("SELECT id, domain_names, forward_host, forward_port FROM proxy_host WHERE domain_names LIKE '%games.snarfnet.net%'") existing = cur.fetchone() now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") if existing: pid = existing[0] print(f"Updating existing proxy_host ID {pid}...") cur.execute('''UPDATE proxy_host SET forward_host = '140.44.44.80', forward_port = 8080, forward_scheme = 'http', certificate_id = 1, ssl_forced = 1, hsts_enabled = 1, http2_support = 1, block_exploits = 1, allow_websocket_upgrade = 1, enabled = 1, modified_on = ? WHERE id = ?''', (now, pid)) else: cur.execute("SELECT MAX(id) FROM proxy_host") max_id = cur.fetchone()[0] or 0 pid = max_id + 1 print(f"Creating new proxy_host with ID {pid}...") cur.execute('''INSERT INTO proxy_host ( id, created_on, modified_on, owner_user_id, domain_names, forward_host, forward_port, forward_scheme, certificate_id, ssl_forced, hsts_enabled, hsts_subdomains, http2_support, block_exploits, caching_enabled, allow_websocket_upgrade, access_list_id, advanced_config, enabled, meta ) VALUES ( ?, ?, ?, 1, '["games.snarfnet.net"]', '140.44.44.80', 8080, 'http', 1, 1, 1, 0, 1, 1, 0, 1, 0, '', 1, '{}' )''', (pid, now, now)) conn.commit() conn.close() print(f"SUCCESS: pid={pid}") """ sftp = c.open_sftp() with sftp.open("/tmp/add_games_proxy.py", "w") as f: f.write(remote_script) sftp.close() stdin, stdout, stderr = c.exec_command("python3 /tmp/add_games_proxy.py") out = stdout.read().decode().strip() err = stderr.read().decode().strip() print("DB Result:\n", out) if err: print("DB Err:\n", err) # Extract pid pid = None for line in out.splitlines(): if "pid=" in line: pid = line.split("pid=")[1].strip() if not pid: print("Failed to get pid!") return # Write nginx config nginx_conf = f"""# ------------------------------------------------------------ # games.snarfnet.net # ------------------------------------------------------------ map $scheme $hsts_header {{ https "max-age=63072000; preload"; }} server {{ set $forward_scheme http; set $server "140.44.44.80"; set $port 8080; listen 80; listen [::]:80; listen 443 ssl; listen [::]:443 ssl; server_name games.snarfnet.net; http2 on; # Let's Encrypt SSL (*.snarfnet.net is cert 1) include conf.d/include/letsencrypt-acme-challenge.conf; include conf.d/include/ssl-cache.conf; include conf.d/include/ssl-ciphers.conf; ssl_certificate /etc/letsencrypt/live/npm-1/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/npm-1/privkey.pem; # Asset Caching include conf.d/include/assets.conf; # Block Exploits include conf.d/include/block-exploits.conf; # Force SSL set $trust_forwarded_proto "F"; include conf.d/include/force-ssl.conf; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $http_connection; proxy_http_version 1.1; access_log /data/logs/proxy-host-{pid}_access.log proxy; error_log /data/logs/proxy-host-{pid}_error.log warn; location / {{ proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $http_connection; proxy_http_version 1.1; # Proxy! include conf.d/include/proxy.conf; }} # Custom include /data/nginx/custom/server_proxy[.]conf; }} """ conf_path = f"/var/lib/docker/volumes/nginx-proxy-manager_nginx-data/_data/nginx/proxy_host/{pid}.conf" sftp = c.open_sftp() with sftp.open(conf_path, "w") as f: f.write(nginx_conf) sftp.close() print(f"Wrote {conf_path}") # Test Nginx stdin, stdout, stderr = c.exec_command("docker exec $(docker ps -q -f name=nginx-proxy-manager_app | head -n1) nginx -t") t_out = stdout.read().decode() t_err = stderr.read().decode() print("nginx -t stdout:\n", t_out) print("nginx -t stderr:\n", t_err) if "successful" in t_out or "successful" in t_err: print("Reloading Nginx in container...") c.exec_command("docker exec $(docker ps -q -f name=nginx-proxy-manager_app | head -n1) nginx -s reload") # Also check other swarm node if running dual c2 = paramiko.SSHClient() c2.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: c2.connect("140.44.4.72", username="root", key_filename=key_path, timeout=5) c2.exec_command("docker exec $(docker ps -q -f name=nginx-proxy-manager_app | head -n1) nginx -s reload") c2.close() print("Reloaded Swarm 02 NPM as well!") except Exception as e: print("Swarm 02 reload skipped/note:", e) print("Proxy setup complete!") else: print("ERROR: nginx -t failed!") c.close() if __name__ == "__main__": main()