101 lines
2.7 KiB
JavaScript
101 lines
2.7 KiB
JavaScript
import { Client } from 'ssh2';
|
|
|
|
const conn = new Client();
|
|
|
|
conn.on('ready', () => {
|
|
console.log('=== 创建站点配置 ===\n');
|
|
|
|
const siteConfig = `server {
|
|
listen 80;
|
|
server_name hs.yifenbao.cn;
|
|
|
|
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 http://hs.yifenbao.cn/api/prices | head -100', (err, stream4) => {
|
|
if (err) {
|
|
console.log('测试失败:', err.message);
|
|
conn.end();
|
|
return;
|
|
}
|
|
|
|
stream4.on('data', (data) => {
|
|
console.log('\nAPI测试:', 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'
|
|
}); |