1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- // 证书链
- 'use strict';
- const Controller = require('egg').Controller;
- const UUID = require('uuid');
- const filePath = require('../../config/filespath');
- class CAcertController extends Controller {
- async caupload() {
- const { ctx } = this;
- const configJson = require(filePath.configJson);
- const uuid = UUID.v1();
- // 获取数据流
- const stream = await ctx.getFileStream();
- // 路径 + 文件名
- const name = `${filePath.CAcert}${uuid}.cer`;
- try {
- // 文件存储
- const files = await this.service.fileshandler.upload({ name, stream });
- if (files.errcode === 0) {
- // 解析证书
- const dns = await this.service.shell.read({ filePath: name });
- let dn;
- if (dns.errcode === 0 && dns.data) {
- dns.data.trim().split('\n').forEach(function(v) {
- if (v.includes('Subject:')) {
- dn = v.replace('Subject:', '');
- }
- });
- const form = { uuid, dn };
- configJson.ca.push(form);
- const jsonstr = JSON.stringify(configJson);
- // 存储数据
- await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
- this.ctx.body = { errcode: 0, errmsg: '' };
- }
- }
- } catch (error) {
- const body = { errcode: -1002, errmsg: '证书上传失败', error };
- throw new Error(JSON.stringify(body));
- }
- }
- // 删除ca证书
- async cadelete() {
- const uuid = this.ctx.query.uuid;
- const configJson = require(filePath.configJson);
- // 过滤掉当前uuid数据
- const data = configJson.ca.filter(p => p.uuid !== uuid);
- configJson.ca = data;
- const jsonstr = JSON.stringify(configJson);
- try {
- // 存储到数据文件
- await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
- // 删除证书文件
- const res = await this.service.fileshandler.filesDel(`${filePath.CAcert}${uuid}.cer`);
- if (res.errcode === 0) {
- this.ctx.body = { errcode: 0, errmsg: '' };
- } else {
- throw new Error(res);
- }
- } catch (error) {
- const body = { errcode: -1002, errmsg: '证书删除失败', error };
- throw new Error(JSON.stringify(body));
- }
- }
- // 查询ca信息
- async caquery() {
- const { ctx } = this;
- const configJson = require(filePath.configJson);
- const data = configJson.ca;
- ctx.body = { errcode: 0, errmsg: '', data };
- }
- // ca证书下载
- async cacertdownload() {
- try {
- const uuid = this.ctx.query.uuid;
- const cafilePath = `${filePath.CAcert}${uuid}.cer`;
- const res = await this.service.fileshandler.download({ filePath: cafilePath });
- this.ctx.body = res;
- } catch (error) {
- const body = { errcode: -1002, errmsg: '证书下载失败', error };
- throw new Error(JSON.stringify(body));
- }
- }
- }
- module.exports = CAcertController;
|