From 447ec6e5ce1152ce09e28ce7819bd4d356c438e9 Mon Sep 17 00:00:00 2001 From: leiyun Date: Mon, 19 May 2025 15:06:18 +0800 Subject: [PATCH] =?UTF-8?q?save=20=E8=BF=9C=E7=A8=8Bcopy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/common/config/env/production.js | 16 ++++++++- src/home/controller/index.js | 50 +++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index b601850..cda4f82 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "greenlock": "^2.8.9", "greenlock-store-fs": "^3.2.2", "le-challenge-fs": "^2.0.9", + "scp2": "^0.5.0", "source-map-support": "0.4.0", "thinkjs": "v2" }, diff --git a/src/common/config/env/production.js b/src/common/config/env/production.js index 888b42e..869d090 100644 --- a/src/common/config/env/production.js +++ b/src/common/config/env/production.js @@ -14,5 +14,19 @@ export default { "xxzz.qbjjyyun.net", // "xxzz.h5.qbjjyyun.net", ], - CERT_DIR:"/etc/nginx/cert" + CERT_DIR:"/etc/nginx/cert", + SCP:[ + { + host:"192.168.12.182", + user:"root", + password:"qbj@123", + port:22, + }, + { + host:"192.168.12.189", + user:"root", + password:"qbj@123", + port:22, + }, + ] }; \ No newline at end of file diff --git a/src/home/controller/index.js b/src/home/controller/index.js index 392af84..07ec7d8 100644 --- a/src/home/controller/index.js +++ b/src/home/controller/index.js @@ -10,7 +10,9 @@ const fs = require('fs-extra'); const { execSync } = require('child_process'); const https = require('https'); const dayjs = require('dayjs'); // 需安装dayjs库 - +const scp = require('scp2'); +const { Client } = require('ssh2'); +const conn = new Client(); // SSL根目录 const ROOT_PATH = path.join(think.ROOT_PATH, 'ssl'); @@ -234,11 +236,55 @@ export default class extends Base { } } + //执行ssh copy + let SCP = this.config().SCP; + if (SCP && SCP.length > 0) { + for (let item of SCP) { + scp.scp( + `${dir}/${domain}.crt`, + { host: item.host, username: item.user, password: item.password, path: `${dir2}/${domain}.crt` }, + (err) => { + console.log('远程复制失败1:', err); + } + ); + scp.scp( + `${dir}/${domain}.key`, + { host: item.host, username: item.user, password: item.password, path: `${dir2}/${domain}.key` }, + (err) => { + console.log('远程复制失败2:', err); + } + ); + } + + } + // 2. 检查配置 execSync('nginx -t'); // 3. 重载服务 execSync('nginx -s reload'); + + if (SCP && SCP.length > 0) { + for (let item of SCP) { + conn.on('ready', () => { + // 执行命令 + conn.exec('nginx -s reload', (err, stream) => { + if (err) throw err; + stream.on('data', data => console.log('输出:', data.toString())) + .stderr.on('data', data => console.error('错误:', data.toString())) + .on('close', code => { + console.log(`退出码: ${code}`); + conn.end(); // 关闭连接 + }); + }); + }).connect({ + host: item.host, + port: 22, + username: item.user, + password: item.password // 建议改用密钥认证 + }); + } + } console.log('证书部署成功!'); } catch (error) { console.error('部署失败:', error.message); @@ -250,7 +296,7 @@ export default class extends Base { */ async lechallengeAction () { let params = this.param(); - let txt = fs.readFileSync(path.join(LECHALLENGE_PATH,params.file)) + let txt = fs.readFileSync(path.join(LECHALLENGE_PATH, params.file)) this.json(txt); } } \ No newline at end of file