import { Client } from 'ssh2'; import fs from 'fs'; const conn = new Client(); conn.on('ready', () => { console.log('=== 修复配置文件 ===\n'); const config = fs.readFileSync('./admin/nginx.conf', 'utf-8'); conn.exec('cat > /www/server/panel/vhost/nginx/hs.yifenbao.cn.conf << "EOF"\n' + config + '\nEOF', (err, stream) => { if (err) { console.log('写入配置失败:', err.message); conn.end(); return; } stream.on('data', (data) => { console.log(data.toString()); }); stream.on('close', () => { console.log('配置文件已写入'); conn.exec('/www/server/nginx/sbin/nginx -t', (err, stream2) => { if (err) { console.log('配置检查失败:', err.message); conn.end(); return; } stream2.on('data', (data) => { console.log('配置检查:', data.toString()); }); stream2.on('close', () => { conn.exec('/www/server/nginx/sbin/nginx -s reload', (err, stream3) => { if (err) { console.log('重启失败:', err.message); conn.end(); return; } stream3.on('close', () => { console.log('Nginx已重启'); setTimeout(() => { conn.exec('curl -s -k https://hs.yifenbao.cn/', (err, stream4) => { if (err) { console.log('HTTPS测试失败:', err.message); conn.end(); return; } stream4.on('data', (data) => { console.log('\nHTTPS响应:', data.toString()); }); stream4.on('close', () => { conn.end(); }); }); }, 2000); }); }); }); }); }); }); }); conn.on('error', (err) => { console.log('连接错误:', err.message); }); conn.connect({ host: 'hs.yifenbao.cn', port: 22, username: 'root', password: 'Torch1112' });