127 lines
3.7 KiB
JavaScript
127 lines
3.7 KiB
JavaScript
import { Client } from 'ssh2';
|
||
|
||
const conn = new Client();
|
||
|
||
conn.on('ready', () => {
|
||
console.log('=== 更新站点配置(添加HTTPS) ===\n');
|
||
|
||
const siteConfig = `server {
|
||
listen 80;
|
||
server_name hs.yifenbao.cn;
|
||
return 301 https://$server_name$request_uri;
|
||
}
|
||
|
||
server {
|
||
listen 443 ssl;
|
||
server_name hs.yifenbao.cn;
|
||
|
||
ssl_certificate /www/server/panel/vhost/cert/hs.yifenbao.cn/fullchain.pem;
|
||
ssl_certificate_key /www/server/panel/vhost/cert/hs.yifenbao.cn/privkey.pem;
|
||
ssl_protocols TLSv1.2 TLSv1.3;
|
||
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
|
||
ssl_prefer_server_ciphers on;
|
||
|
||
location / {
|
||
proxy_pass http://127.0.0.1:4000;
|
||
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 /api {
|
||
proxy_pass http://127.0.0.1:4000;
|
||
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 /icons {
|
||
proxy_pass http://127.0.0.1:4000;
|
||
proxy_set_header Host $host;
|
||
}
|
||
}
|
||
`;
|
||
|
||
conn.exec(`echo "${siteConfig.replace(/"/g, '\\"')}" > /www/server/panel/vhost/nginx/hs.yifenbao.cn.conf`, (err, stream) => {
|
||
if (err) {
|
||
console.log('更新配置失败:', err.message);
|
||
conn.end();
|
||
return;
|
||
}
|
||
|
||
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 -I 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.exec('curl -s -k https://hs.yifenbao.cn/api/prices | head -20', (err, stream5) => {
|
||
if (err) {
|
||
console.log('API测试失败:', err.message);
|
||
conn.end();
|
||
return;
|
||
}
|
||
|
||
stream5.on('data', (data) => {
|
||
console.log('\nAPI响应:', data.toString());
|
||
});
|
||
|
||
stream5.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'
|
||
}); |