import paramiko

host = '8.140.221.49'
username = 'root'
private_key_path = r'C:\Users\FZX\.ssh\FzxBdsh-ECS.pem'

def run_command(ssh, cmd):
    stdin, stdout, stderr = ssh.exec_command(cmd)
    return stdout.read().decode().strip(), stderr.read().decode().strip()

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=username, key_filename=private_key_path, timeout=30)

print('='*80)
print('配置正确的域名解析（根据DNS截图）')
print('='*80)

nginx_conf = '''user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

    server {
        listen 80;
        server_name api.fengzhongxing.com;

        location /health {
            return 200 "OK";
        }

        location / {
            proxy_pass http://api-gateway:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

    server {
        listen 80;
        server_name user.fengzhongxing.com;

        location / {
            alias /usr/share/nginx/html/user/;
            index index.html;
            try_files $uri $uri/ /index.html =404;
        }
    }

    server {
        listen 80;
        server_name merchant.fengzhongxing.com;

        location / {
            alias /usr/share/nginx/html/merchant/;
            index index.html;
            try_files $uri $uri/ /index.html =404;
        }
    }

    server {
        listen 80;
        server_name rider.fengzhongxing.com;

        location / {
            alias /usr/share/nginx/html/rider/;
            index index.html;
            try_files $uri $uri/ /index.html =404;
        }
    }

    server {
        listen 80;
        server_name platform.fengzhongxing.com;

        location / {
            alias /usr/share/nginx/html/platform/;
            index index.html;
            try_files $uri $uri/ /index.html =404;
        }
    }

    server {
        listen 80;
        server_name official.fengzhongxing.com;

        location / {
            alias /usr/share/nginx/html/official/;
            index index.html;
            try_files $uri $uri/ /index.html =404;
        }
    }

    server {
        listen 80;
        server_name www.fengzhongxing.com;

        location / {
            alias /usr/share/nginx/html/official/;
            index index.html;
            try_files $uri $uri/ /index.html =404;
        }
    }

    server {
        listen 80;
        server_name fengzhongxing.com;

        location / {
            alias /usr/share/nginx/html/official/;
            index index.html;
            try_files $uri $uri/ /index.html =404;
        }
    }
}
'''

print('1. 更新Nginx配置文件')
sftp = ssh.open_sftp()
with sftp.file('/opt/Fzxpodman/nginx/conf/nginx.conf', 'w') as f:
    f.write(nginx_conf)
sftp.close()
print('✅ Nginx配置已更新')

index_html = '''<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>风七六平台 - {title}</title>
    <style>
        body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; text-align: center; padding: 50px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }}
        .container {{ background: white; padding: 40px; border-radius: 12px; box-shadow: 0 10px 40px rgba(0,0,0,0.1); max-width: 700px; margin: 0 auto; }}
        h1 {{ color: #067bff; margin-bottom: 20px; }}
        p {{ color: #666; line-height: 1.6; }}
        .status {{ display: inline-block; padding: 8px 16px; border-radius: 20px; font-weight: bold; margin-top: 20px; }}
        .status.ok {{ background: #d4edda; color: #155724; }}
        .env {{ margin-top: 20px; padding: 10px; background: #f8f9fa; border-radius: 6px; font-family: monospace; }}
        .links {{ margin-top: 20px; }}
        .links a {{ display: inline-block; margin: 5px 10px; padding: 8px 16px; background: #067bff; color: white; text-decoration: none; border-radius: 6px; }}
        .links a:hover {{ background: #0568d8; }}
        .domain-info {{ margin-top: 20px; padding: 15px; background: #e7f3ff; border-radius: 8px; text-align: left; }}
        .domain-info h3 {{ color: #0c63e4; margin-bottom: 10px; }}
        .domain-info ul {{ margin: 0; padding-left: 20px; }}
        .domain-info li {{ margin: 5px 0; color: #333; }}
    </style>
</head>
<body>
    <div class="container">
        <h1>🚀 风七六{title}</h1>
        <p>服务已成功部署到阿里云开发环境</p>
        <div class="status ok">✅ 服务运行正常</div>
        <div class="env">环境: aliyun-dev | 时间: {time}</div>
        <div class="links">
            <a href="http://user.fengzhongxing.com">用户端</a>
            <a href="http://merchant.fengzhongxing.com">商家端</a>
            <a href="http://rider.fengzhongxing.com">骑手端</a>
            <a href="http://platform.fengzhongxing.com">平台端</a>
            <a href="http://www.fengzhongxing.com">官网</a>
        </div>
        <div class="domain-info">
            <h3>🌐 域名配置</h3>
            <ul>
                <li>用户端: <strong>user.fengzhongxing.com</strong></li>
                <li>商家端: <strong>merchant.fengzhongxing.com</strong></li>
                <li>骑手端: <strong>rider.fengzhongxing.com</strong></li>
                <li>平台端: <strong>platform.fengzhongxing.com</strong></li>
                <li>官网: <strong>www.fengzhongxing.com</strong></li>
                <li>API网关: <strong>api.fengzhongxing.com</strong></li>
            </ul>
        </div>
    </div>
</body>
</html>
'''

import datetime
current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

sftp = ssh.open_sftp()
with sftp.file('/opt/Fzxpodman/nginx/html/user/index.html', 'w') as f:
    f.write(index_html.format(title='用户端', time=current_time))
with sftp.file('/opt/Fzxpodman/nginx/html/merchant/index.html', 'w') as f:
    f.write(index_html.format(title='商家端', time=current_time))
with sftp.file('/opt/Fzxpodman/nginx/html/rider/index.html', 'w') as f:
    f.write(index_html.format(title='骑手端', time=current_time))
with sftp.file('/opt/Fzxpodman/nginx/html/platform/index.html', 'w') as f:
    f.write(index_html.format(title='平台管理端', time=current_time))
with sftp.file('/opt/Fzxpodman/nginx/html/official/index.html', 'w') as f:
    f.write(index_html.format(title='官方网站', time=current_time))
sftp.close()
print('✅ 前端页面已更新')

print('\n2. 重启Nginx容器')
run_command(ssh, 'podman rm -f fzx-aliyun-nginx')
output, error = run_command(ssh, '''
podman run -d --name fzx-aliyun-nginx \
  --network fzx-aliyun-dev-network --network-alias nginx \
  -p 0.0.0.0:80:80 -p 0.0.0.0:443:443 \
  -v /opt/Fzxpodman/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
  -v /opt/Fzxpodman/nginx/html:/usr/share/nginx/html:ro \
  docker.m.daocloud.io/library/nginx:1.25.5
''')
print('✅ Nginx已重启')

print('\n3. 测试域名配置')
import time
time.sleep(3)

domains = [
    ('api.fengzhongxing.com', '/health'),
    ('user.fengzhongxing.com', '/'),
    ('merchant.fengzhongxing.com', '/'),
    ('rider.fengzhongxing.com', '/'),
    ('platform.fengzhongxing.com', '/'),
    ('official.fengzhongxing.com', '/'),
    ('www.fengzhongxing.com', '/'),
    ('fengzhongxing.com', '/')
]

print('\n域名测试结果:')
print('-' * 50)
for domain, path in domains:
    output, error = run_command(ssh, f'curl -s -o /dev/null -w "%{{http_code}}" -H "Host: {domain}" http://localhost{path}')
    status = '✅' if output == '200' else '❌'
    print(f'{status} {domain}: {output}')

ssh.close()

print('\n' + '='*80)
print('域名配置完成!')
print('='*80)