leiyun 10 месяцев назад
Родитель
Сommit
447ec6e5ce
3 измененных файлов: 64 добавлений и 3 удалений
  1. +1
    -0
      package.json
  2. +15
    -1
      src/common/config/env/production.js
  3. +48
    -2
      src/home/controller/index.js

+ 1
- 0
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"
},


+ 15
- 1
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,
},
]
};

+ 48
- 2
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);
}
}

Загрузка…
Отмена
Сохранить