Pay-then-profile join desk with live card, S3 upload fallback, hydration-safe drafts; document on test9 README. Co-authored-by: Cursor <cursoragent@cursor.com>
457 lines
17 KiB
Python
457 lines
17 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Fix Let's Encrypt + TLS nginx on US for nomadro.com."""
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import sys
|
|
import time
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
|
|
|
|
US = ("108.174.50.68", "root", "Xiao4669805")
|
|
|
|
NGINX_PORTAL = open(
|
|
__file__.replace("fix_us_tls.py", "finish_us_nomadro_com.py"), encoding="utf-8"
|
|
).read() # unused; configs inlined below
|
|
|
|
|
|
def connect():
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(*US[:1], 22, US[1], US[2], timeout=60, allow_agent=False, look_for_keys=False, banner_timeout=120)
|
|
# fix: connect signature
|
|
return c
|
|
|
|
|
|
def connect2():
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
host, user, password = US
|
|
c.connect(host, 22, user, password, timeout=60, allow_agent=False, look_for_keys=False, banner_timeout=120)
|
|
return c
|
|
|
|
|
|
def run(c, cmd, timeout=300, check=True):
|
|
print(f"\n$ {cmd[:240]}", flush=True)
|
|
chan = c.get_transport().open_session()
|
|
chan.get_pty(width=200)
|
|
chan.settimeout(timeout)
|
|
chan.exec_command(cmd)
|
|
buf = io.BytesIO()
|
|
deadline = time.time() + timeout
|
|
while True:
|
|
if chan.recv_ready():
|
|
chunk = chan.recv(8192)
|
|
buf.write(chunk)
|
|
sys.stdout.write(chunk.decode("utf-8", "replace"))
|
|
sys.stdout.flush()
|
|
if chan.recv_stderr_ready():
|
|
chunk = chan.recv_stderr(8192)
|
|
buf.write(chunk)
|
|
sys.stderr.write(chunk.decode("utf-8", "replace"))
|
|
sys.stderr.flush()
|
|
if chan.exit_status_ready():
|
|
while chan.recv_ready():
|
|
buf.write(chan.recv(8192))
|
|
while chan.recv_stderr_ready():
|
|
buf.write(chan.recv_stderr(8192))
|
|
break
|
|
if time.time() > deadline:
|
|
raise TimeoutError(cmd[:120])
|
|
time.sleep(0.1)
|
|
code = chan.recv_exit_status()
|
|
text = buf.getvalue().decode("utf-8", "replace")
|
|
if check and code != 0:
|
|
raise RuntimeError(f"fail {code}: {cmd[:120]}\n{text[-1500:]}")
|
|
return code, text
|
|
|
|
|
|
def put(c, path, content, mode=0o644):
|
|
sftp = c.open_sftp()
|
|
with sftp.file(path, "w") as f:
|
|
f.write(content)
|
|
sftp.chmod(path, mode)
|
|
sftp.close()
|
|
|
|
|
|
# Minimal HTTP ACME-first configs (no redirects at all until certs exist)
|
|
ACME_HTTP = r"""
|
|
server {
|
|
listen 80 default_server;
|
|
listen [::]:80 default_server;
|
|
server_name _;
|
|
location ^~ /.well-known/acme-challenge/ {
|
|
root /var/www/acme;
|
|
default_type text/plain;
|
|
allow all;
|
|
}
|
|
location / { return 200 'ok\n'; add_header Content-Type text/plain; }
|
|
}
|
|
server {
|
|
listen 80;
|
|
listen [::]:80;
|
|
server_name nomadro.com www.nomadro.com api.nomadro.com admin.nomadro.com;
|
|
location ^~ /.well-known/acme-challenge/ {
|
|
root /var/www/acme;
|
|
default_type text/plain;
|
|
allow all;
|
|
}
|
|
location / {
|
|
# temporary: keep sites up on HTTP while issuing certs
|
|
return 200 'http-ready\n';
|
|
add_header Content-Type text/plain;
|
|
}
|
|
}
|
|
"""
|
|
|
|
NGINX_PORTAL = r"""
|
|
server {
|
|
listen 80;
|
|
listen [::]:80;
|
|
server_name nomadro.com www.nomadro.com;
|
|
location ^~ /.well-known/acme-challenge/ { root /var/www/acme; default_type text/plain; allow all; }
|
|
location / { return 301 https://nomadro.com$request_uri; }
|
|
}
|
|
server {
|
|
listen 443 ssl;
|
|
listen [::]:443 ssl;
|
|
http2 on;
|
|
server_name nomadro.com www.nomadro.com;
|
|
ssl_certificate /etc/letsencrypt/live/nomadro.com/fullchain.pem;
|
|
ssl_certificate_key /etc/letsencrypt/live/nomadro.com/privkey.pem;
|
|
ssl_protocols TLSv1.2 TLSv1.3;
|
|
client_max_body_size 32m;
|
|
location = /api/config {
|
|
proxy_pass http://127.0.0.1:8010;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host api.nomadro.com;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
add_header Cache-Control "no-store";
|
|
}
|
|
location = /api/config/meta {
|
|
proxy_pass http://127.0.0.1:8010;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host api.nomadro.com;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
add_header Cache-Control "no-store";
|
|
}
|
|
location ^~ /api/social/ {
|
|
proxy_pass http://127.0.0.1:8020;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_set_header X-Social-Guest-Id $http_x_social_guest_id;
|
|
}
|
|
location / {
|
|
proxy_pass http://127.0.0.1:4003;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection "upgrade";
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_read_timeout 300s;
|
|
}
|
|
}
|
|
"""
|
|
|
|
NGINX_API = r"""
|
|
server {
|
|
listen 80;
|
|
listen [::]:80;
|
|
server_name api.nomadro.com;
|
|
location ^~ /.well-known/acme-challenge/ { root /var/www/acme; default_type text/plain; allow all; }
|
|
location / { return 301 https://$host$request_uri; }
|
|
}
|
|
server {
|
|
listen 443 ssl;
|
|
listen [::]:443 ssl;
|
|
http2 on;
|
|
server_name api.nomadro.com;
|
|
client_max_body_size 32m;
|
|
ssl_certificate /etc/letsencrypt/live/api.nomadro.com/fullchain.pem;
|
|
ssl_certificate_key /etc/letsencrypt/live/api.nomadro.com/privkey.pem;
|
|
ssl_protocols TLSv1.2 TLSv1.3;
|
|
location / {
|
|
proxy_pass http://127.0.0.1:8010;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_read_timeout 300s;
|
|
}
|
|
}
|
|
"""
|
|
|
|
NGINX_ADMIN = r"""
|
|
server {
|
|
listen 80;
|
|
listen [::]:80;
|
|
server_name admin.nomadro.com;
|
|
location ^~ /.well-known/acme-challenge/ { root /var/www/acme; default_type text/plain; allow all; }
|
|
location / { return 301 https://$host$request_uri; }
|
|
}
|
|
server {
|
|
listen 443 ssl;
|
|
listen [::]:443 ssl;
|
|
http2 on;
|
|
server_name admin.nomadro.com;
|
|
root /www/wwwroot/admin.nomadro.com;
|
|
index index.html;
|
|
client_max_body_size 20m;
|
|
ssl_certificate /etc/letsencrypt/live/admin.nomadro.com/fullchain.pem;
|
|
ssl_certificate_key /etc/letsencrypt/live/admin.nomadro.com/privkey.pem;
|
|
ssl_protocols TLSv1.2 TLSv1.3;
|
|
location /api/ {
|
|
proxy_pass http://127.0.0.1:8010/api/;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
}
|
|
location /health { proxy_pass http://127.0.0.1:8010/health; proxy_set_header Host $host; }
|
|
location /assets/ { try_files $uri =404; expires 30d; }
|
|
location / { try_files $uri $uri/ /index.html; }
|
|
}
|
|
"""
|
|
|
|
|
|
def main():
|
|
c = connect2()
|
|
|
|
print("=== DNS + ACME probe ===", flush=True)
|
|
run(
|
|
c,
|
|
r"""
|
|
set +e
|
|
getent hosts nomadro.com www.nomadro.com api.nomadro.com admin.nomadro.com
|
|
mkdir -p /var/www/acme/.well-known/acme-challenge
|
|
echo probe-ok > /var/www/acme/.well-known/acme-challenge/ping.txt
|
|
curl -sS -D- -o /tmp/acme_body.txt http://127.0.0.1/.well-known/acme-challenge/ping.txt -H 'Host: nomadro.com' | head -20
|
|
echo BODY:$(cat /tmp/acme_body.txt)
|
|
curl -sS -D- -o /dev/null http://nomadro.com/.well-known/acme-challenge/ping.txt | head -25
|
|
curl -sS -D- -o /dev/null http://www.nomadro.com/.well-known/acme-challenge/ping.txt | head -25
|
|
curl -sS -D- -o /dev/null http://api.nomadro.com/.well-known/acme-challenge/ping.txt | head -25
|
|
# check if something external forces https
|
|
curl -sS -D- -o /dev/null http://nomadro.com/ | head -20
|
|
""",
|
|
check=False,
|
|
)
|
|
|
|
print("=== install ACME-only nginx ===", flush=True)
|
|
put(c, "/etc/nginx/sites-available/nomadro-http.conf", ACME_HTTP)
|
|
run(
|
|
c,
|
|
"ln -sfn /etc/nginx/sites-available/nomadro-http.conf /etc/nginx/sites-enabled/nomadro-http.conf && "
|
|
"rm -f /etc/nginx/sites-enabled/nomadro.com.conf /etc/nginx/sites-enabled/api.nomadro.com.conf /etc/nginx/sites-enabled/admin.nomadro.com.conf && "
|
|
"nginx -t && systemctl reload nginx",
|
|
)
|
|
|
|
# Re-probe after ACME-only
|
|
run(
|
|
c,
|
|
"curl -sS http://nomadro.com/.well-known/acme-challenge/ping.txt; echo; "
|
|
"curl -sS -o /dev/null -w '%{http_code} %{redirect_url}\\n' http://www.nomadro.com/.well-known/acme-challenge/ping.txt",
|
|
check=False,
|
|
)
|
|
|
|
print("=== certbot one domain at a time (no www first if www broken) ===", flush=True)
|
|
run(
|
|
c,
|
|
r"""
|
|
set +e
|
|
# apex only
|
|
certbot certonly --webroot -w /var/www/acme -d nomadro.com \
|
|
--non-interactive --agree-tos --email admin@nomadro.com --keep-until-expiring -v 2>&1 | tail -40
|
|
echo EXIT_APEX:$?
|
|
# try www separately
|
|
certbot certonly --webroot -w /var/www/acme -d www.nomadro.com \
|
|
--non-interactive --agree-tos --email admin@nomadro.com --keep-until-expiring 2>&1 | tail -30
|
|
echo EXIT_WWW:$?
|
|
certbot certonly --webroot -w /var/www/acme -d api.nomadro.com \
|
|
--non-interactive --agree-tos --email admin@nomadro.com --keep-until-expiring 2>&1 | tail -30
|
|
echo EXIT_API:$?
|
|
certbot certonly --webroot -w /var/www/acme -d admin.nomadro.com \
|
|
--non-interactive --agree-tos --email admin@nomadro.com --keep-until-expiring 2>&1 | tail -30
|
|
echo EXIT_ADMIN:$?
|
|
ls -la /etc/letsencrypt/live/ 2>/dev/null
|
|
""",
|
|
timeout=400,
|
|
check=False,
|
|
)
|
|
|
|
_code, out = run(
|
|
c,
|
|
"ls /etc/letsencrypt/live/nomadro.com/fullchain.pem /etc/letsencrypt/live/api.nomadro.com/fullchain.pem /etc/letsencrypt/live/admin.nomadro.com/fullchain.pem 2>&1",
|
|
check=False,
|
|
)
|
|
|
|
have_portal = "No such file" not in out and "nomadro.com/fullchain" in out
|
|
have_api = "api.nomadro.com/fullchain" in out and "No such file" not in out.split("api")[0][-20:] + out
|
|
# simpler checks:
|
|
_c2, checks = run(
|
|
c,
|
|
"test -f /etc/letsencrypt/live/nomadro.com/fullchain.pem && echo P=1 || echo P=0; "
|
|
"test -f /etc/letsencrypt/live/api.nomadro.com/fullchain.pem && echo A=1 || echo A=0; "
|
|
"test -f /etc/letsencrypt/live/admin.nomadro.com/fullchain.pem && echo D=1 || echo D=0; "
|
|
# if www cert separate, expand portal cert later
|
|
"test -f /etc/letsencrypt/live/www.nomadro.com/fullchain.pem && echo W=1 || echo W=0",
|
|
check=False,
|
|
)
|
|
|
|
if "P=1" in checks and "A=1" in checks and "D=1" in checks:
|
|
# Prefer combining www into nomadro.com cert if www succeeded separately — for nginx use apex cert for both names only if SAN includes www
|
|
# Check SAN
|
|
run(
|
|
c,
|
|
"openssl x509 -in /etc/letsencrypt/live/nomadro.com/fullchain.pem -noout -ext subjectAltName 2>/dev/null || "
|
|
"openssl x509 -in /etc/letsencrypt/live/nomadro.com/cert.pem -noout -text | grep -A2 'Subject Alternative'",
|
|
check=False,
|
|
)
|
|
# If www not in SAN, only serve nomadro.com on that cert; www can redirect via HTTP or use www cert
|
|
portal_conf = NGINX_PORTAL
|
|
if "W=1" in checks and "DNS:www.nomadro.com" not in (
|
|
run(c, "openssl x509 -in /etc/letsencrypt/live/nomadro.com/fullchain.pem -noout -text | grep -o 'DNS:[^,]*' | tr '\\n' ' '", check=False)[1]
|
|
):
|
|
# expand: certbot certonly --expand -d nomadro.com -d www.nomadro.com if www works
|
|
run(
|
|
c,
|
|
"certbot certonly --webroot -w /var/www/acme -d nomadro.com -d www.nomadro.com --expand "
|
|
"--non-interactive --agree-tos --email admin@nomadro.com 2>&1 | tail -25",
|
|
check=False,
|
|
)
|
|
|
|
put(c, "/etc/nginx/sites-available/nomadro.com.conf", portal_conf)
|
|
put(c, "/etc/nginx/sites-available/api.nomadro.com.conf", NGINX_API)
|
|
put(c, "/etc/nginx/sites-available/admin.nomadro.com.conf", NGINX_ADMIN)
|
|
run(
|
|
c,
|
|
"rm -f /etc/nginx/sites-enabled/nomadro-http.conf && "
|
|
"ln -sfn /etc/nginx/sites-available/nomadro.com.conf /etc/nginx/sites-enabled/nomadro.com.conf && "
|
|
"ln -sfn /etc/nginx/sites-available/api.nomadro.com.conf /etc/nginx/sites-enabled/api.nomadro.com.conf && "
|
|
"ln -sfn /etc/nginx/sites-available/admin.nomadro.com.conf /etc/nginx/sites-enabled/admin.nomadro.com.conf && "
|
|
"nginx -t && systemctl reload nginx && echo TLS_OK",
|
|
)
|
|
else:
|
|
print("CERTS still incomplete:", checks, flush=True)
|
|
# Fall back: HTTP full app proxy so site works without TLS
|
|
http_full = open(
|
|
__file__.replace("fix_us_tls.py", "finish_us_nomadro_com.py"),
|
|
encoding="utf-8",
|
|
)
|
|
# use previous NGINX_HTTP from finish script via re-put simpler version
|
|
HTTP_APP = r"""
|
|
server {
|
|
listen 80 default_server;
|
|
listen [::]:80 default_server;
|
|
server_name _;
|
|
location ^~ /.well-known/acme-challenge/ { root /var/www/acme; default_type text/plain; allow all; }
|
|
location / { return 200 'default\n'; add_header Content-Type text/plain; }
|
|
}
|
|
server {
|
|
listen 80;
|
|
listen [::]:80;
|
|
server_name nomadro.com www.nomadro.com;
|
|
location ^~ /.well-known/acme-challenge/ { root /var/www/acme; default_type text/plain; allow all; }
|
|
location = /api/config {
|
|
proxy_pass http://127.0.0.1:8010;
|
|
proxy_set_header Host api.nomadro.com;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
}
|
|
location = /api/config/meta {
|
|
proxy_pass http://127.0.0.1:8010;
|
|
proxy_set_header Host api.nomadro.com;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
}
|
|
location ^~ /api/social/ {
|
|
proxy_pass http://127.0.0.1:8020;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_set_header X-Social-Guest-Id $http_x_social_guest_id;
|
|
}
|
|
location / {
|
|
proxy_pass http://127.0.0.1:4003;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection "upgrade";
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
}
|
|
}
|
|
server {
|
|
listen 80;
|
|
listen [::]:80;
|
|
server_name api.nomadro.com;
|
|
location ^~ /.well-known/acme-challenge/ { root /var/www/acme; default_type text/plain; allow all; }
|
|
location / {
|
|
proxy_pass http://127.0.0.1:8010;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
}
|
|
}
|
|
server {
|
|
listen 80;
|
|
listen [::]:80;
|
|
server_name admin.nomadro.com;
|
|
root /www/wwwroot/admin.nomadro.com;
|
|
index index.html;
|
|
location ^~ /.well-known/acme-challenge/ { root /var/www/acme; default_type text/plain; allow all; }
|
|
location /api/ {
|
|
proxy_pass http://127.0.0.1:8010/api/;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
}
|
|
location /health { proxy_pass http://127.0.0.1:8010/health; }
|
|
location / { try_files $uri $uri/ /index.html; }
|
|
}
|
|
"""
|
|
put(c, "/etc/nginx/sites-available/nomadro-http.conf", HTTP_APP)
|
|
run(c, "nginx -t && systemctl reload nginx && echo HTTP_APP_OK")
|
|
|
|
print("=== final smoke ===", flush=True)
|
|
run(
|
|
c,
|
|
r"""
|
|
set +e
|
|
ss -lntp | grep -E ':80|:443'
|
|
curl -sS -o /dev/null -w 'http_portal:%{http_code}\n' http://nomadro.com/
|
|
curl -sS -o /dev/null -w 'http_api:%{http_code}\n' http://api.nomadro.com/health
|
|
curl -sS -o /dev/null -w 'http_admin:%{http_code}\n' http://admin.nomadro.com/
|
|
curl -sS -o /dev/null -w 'https_portal:%{http_code}\n' https://nomadro.com/
|
|
curl -sS -o /dev/null -w 'https_api:%{http_code}\n' https://api.nomadro.com/health
|
|
curl -sS -o /dev/null -w 'https_admin:%{http_code}\n' https://admin.nomadro.com/
|
|
systemctl is-active nomadro nomadmini-api nomadro-social nginx
|
|
""",
|
|
check=False,
|
|
)
|
|
c.close()
|
|
print("=== DONE ===", flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|