mirror of https://github.com/certd/certd
perf: 修复windows下无法执行第二条命令的bug
parent
1480efb43d
commit
d5bfcdb6de
|
@ -32,7 +32,7 @@ exports.directory = {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
exports.crypto = require('./crypto');
|
exports.crypto = require('./crypto');
|
||||||
exports.forge = require('./crypto/forge');
|
// exports.forge = require('./crypto/forge');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Axios
|
* Axios
|
||||||
|
|
|
@ -81,6 +81,7 @@ export class AcmeService {
|
||||||
if (conf.key == null) {
|
if (conf.key == null) {
|
||||||
conf.key = await this.createNewKey();
|
conf.key = await this.createNewKey();
|
||||||
await this.saveAccountConfig(email, conf);
|
await this.saveAccountConfig(email, conf);
|
||||||
|
this.logger.info(`创建新的Accountkey:${email}`);
|
||||||
}
|
}
|
||||||
let directoryUrl = "";
|
let directoryUrl = "";
|
||||||
if (isTest) {
|
if (isTest) {
|
||||||
|
@ -124,7 +125,7 @@ export class AcmeService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async createNewKey() {
|
async createNewKey() {
|
||||||
const key = await acme.forge.createPrivateKey();
|
const key = await acme.crypto.createPrivateKey(2048);
|
||||||
return key.toString();
|
return key.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -64,6 +64,7 @@
|
||||||
"pg": "^8.12.0",
|
"pg": "^8.12.0",
|
||||||
"reflect-metadata": "^0.1.13",
|
"reflect-metadata": "^0.1.13",
|
||||||
"ssh2": "^1.15.0",
|
"ssh2": "^1.15.0",
|
||||||
|
"strip-ansi": "^7.1.0",
|
||||||
"svg-captcha": "^1.4.0",
|
"svg-captcha": "^1.4.0",
|
||||||
"tencentcloud-sdk-nodejs": "^4.0.44",
|
"tencentcloud-sdk-nodejs": "^4.0.44",
|
||||||
"typeorm": "^0.3.20"
|
"typeorm": "^0.3.20"
|
||||||
|
|
|
@ -3,8 +3,8 @@ import ssh2, { ConnectConfig } from 'ssh2';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as _ from 'lodash-es';
|
import * as _ from 'lodash-es';
|
||||||
import { ILogger } from '@certd/pipeline';
|
import { ILogger } from '@certd/pipeline';
|
||||||
import iconv from 'iconv-lite';
|
|
||||||
import { SshAccess } from '../access/index.js';
|
import { SshAccess } from '../access/index.js';
|
||||||
|
import stripAnsi from 'strip-ansi';
|
||||||
export class AsyncSsh2Client {
|
export class AsyncSsh2Client {
|
||||||
conn: ssh2.Client;
|
conn: ssh2.Client;
|
||||||
logger: ILogger;
|
logger: ILogger;
|
||||||
|
@ -18,7 +18,7 @@ export class AsyncSsh2Client {
|
||||||
this.encoding = connConf.encoding;
|
this.encoding = connConf.encoding;
|
||||||
}
|
}
|
||||||
|
|
||||||
convert(buffer: Buffer) {
|
convert(iconv: any, buffer: Buffer) {
|
||||||
if (this.encoding) {
|
if (this.encoding) {
|
||||||
return iconv.decode(buffer, this.encoding);
|
return iconv.decode(buffer, this.encoding);
|
||||||
}
|
}
|
||||||
|
@ -79,6 +79,7 @@ export class AsyncSsh2Client {
|
||||||
this.logger.info('script 为空,取消执行');
|
this.logger.info('script 为空,取消执行');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const iconv = await import('iconv-lite');
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.logger.info(`执行命令:[${this.connConf.host}][exec]: ` + script);
|
this.logger.info(`执行命令:[${this.connConf.host}][exec]: ` + script);
|
||||||
this.conn.exec(script, (err: Error, stream: any) => {
|
this.conn.exec(script, (err: Error, stream: any) => {
|
||||||
|
@ -97,7 +98,7 @@ export class AsyncSsh2Client {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on('data', (ret: Buffer) => {
|
.on('data', (ret: Buffer) => {
|
||||||
const out = this.convert(ret);
|
const out = this.convert(iconv, ret);
|
||||||
data += out;
|
data += out;
|
||||||
this.logger.info(`[${this.connConf.host}][info]: ` + out.trimEnd());
|
this.logger.info(`[${this.connConf.host}][info]: ` + out.trimEnd());
|
||||||
})
|
})
|
||||||
|
@ -106,7 +107,7 @@ export class AsyncSsh2Client {
|
||||||
this.logger.error(err);
|
this.logger.error(err);
|
||||||
})
|
})
|
||||||
.stderr.on('data', (ret: Buffer) => {
|
.stderr.on('data', (ret: Buffer) => {
|
||||||
const err = this.convert(ret);
|
const err = this.convert(iconv, ret);
|
||||||
data += err;
|
data += err;
|
||||||
this.logger.info(`[${this.connConf.host}][error]: ` + err.trimEnd());
|
this.logger.info(`[${this.connConf.host}][error]: ` + err.trimEnd());
|
||||||
});
|
});
|
||||||
|
@ -123,22 +124,33 @@ export class AsyncSsh2Client {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const output: string[] = [];
|
const output: string[] = [];
|
||||||
|
function ansiHandle(data: string) {
|
||||||
|
data = data.replace(/\[[0-9]+;1H/g, '\n');
|
||||||
|
data = stripAnsi(data);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
stream
|
stream
|
||||||
.on('close', () => {
|
.on('close', () => {
|
||||||
this.logger.info('Stream :: close');
|
this.logger.info('Stream :: close');
|
||||||
resolve(output);
|
resolve(output);
|
||||||
})
|
})
|
||||||
.on('data', (ret: Buffer) => {
|
.on('data', (ret: Buffer) => {
|
||||||
const data = this.convert(ret);
|
const data = ansiHandle(ret.toString());
|
||||||
this.logger.info('' + data);
|
this.logger.info(data);
|
||||||
output.push(data);
|
output.push(data);
|
||||||
})
|
})
|
||||||
|
.on('error', (err: any) => {
|
||||||
|
reject(err);
|
||||||
|
this.logger.error(err);
|
||||||
|
})
|
||||||
.stderr.on('data', (ret: Buffer) => {
|
.stderr.on('data', (ret: Buffer) => {
|
||||||
const data = this.convert(ret);
|
const data = ansiHandle(ret.toString());
|
||||||
output.push(data);
|
output.push(data);
|
||||||
this.logger.info(`[${this.connConf.host}][error]: ` + data);
|
this.logger.info(`[${this.connConf.host}][error]: ` + data);
|
||||||
});
|
});
|
||||||
stream.end(script + '\nexit\n');
|
//保证windows下正常退出
|
||||||
|
const exit = '\r\nexit\r\n';
|
||||||
|
stream.end(script + exit);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -189,7 +201,7 @@ export class SshClient {
|
||||||
mkdirCmd = `if not exist "${filePath}" mkdir "${filePath}"`;
|
mkdirCmd = `if not exist "${filePath}" mkdir "${filePath}"`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await conn.exec(mkdirCmd);
|
await conn.shell(mkdirCmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
await conn.fastPut({ sftp, ...transport });
|
await conn.fastPut({ sftp, ...transport });
|
||||||
|
@ -204,9 +216,17 @@ export class SshClient {
|
||||||
const { connectConf } = options;
|
const { connectConf } = options;
|
||||||
if (_.isArray(script)) {
|
if (_.isArray(script)) {
|
||||||
script = script as Array<string>;
|
script = script as Array<string>;
|
||||||
script = script.join('\n');
|
if (connectConf.windows) {
|
||||||
|
script = script.join('\r\n');
|
||||||
|
} else {
|
||||||
|
script = script.join('\n');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (connectConf.windows) {
|
||||||
|
script = script.replaceAll('\n', '\r\n');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.logger.info('执行命令:', script);
|
this.logger.info('命令:', script);
|
||||||
return await this._call({
|
return await this._call({
|
||||||
connectConf,
|
connectConf,
|
||||||
callable: async (conn: AsyncSsh2Client) => {
|
callable: async (conn: AsyncSsh2Client) => {
|
||||||
|
@ -215,8 +235,22 @@ export class SshClient {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async shell(options: { connectConf: SshAccess; script: string }): Promise<string[]> {
|
//废弃
|
||||||
const { connectConf, script } = options;
|
async shell(options: { connectConf: SshAccess; script: string | Array<string> }): Promise<string[]> {
|
||||||
|
let { script } = options;
|
||||||
|
const { connectConf } = options;
|
||||||
|
if (_.isArray(script)) {
|
||||||
|
script = script as Array<string>;
|
||||||
|
if (connectConf.windows) {
|
||||||
|
script = script.join('\r\n');
|
||||||
|
} else {
|
||||||
|
script = script.join('\n');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (connectConf.windows) {
|
||||||
|
script = script.replaceAll('\n', '\r\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
return await this._call({
|
return await this._call({
|
||||||
connectConf,
|
connectConf,
|
||||||
callable: async (conn: AsyncSsh2Client) => {
|
callable: async (conn: AsyncSsh2Client) => {
|
||||||
|
|
|
@ -40,11 +40,11 @@ export class HostShellExecutePlugin extends AbstractTaskPlugin {
|
||||||
const { script, accessId } = this;
|
const { script, accessId } = this;
|
||||||
const connectConf = await this.accessService.getById(accessId);
|
const connectConf = await this.accessService.getById(accessId);
|
||||||
const sshClient = new SshClient(this.logger);
|
const sshClient = new SshClient(this.logger);
|
||||||
const ret = await sshClient.exec({
|
await sshClient.shell({
|
||||||
connectConf,
|
connectConf,
|
||||||
script,
|
script,
|
||||||
});
|
});
|
||||||
this.logger.info('exec res:', ret);
|
// this.logger.info('exec res:', ret);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue