cacert.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // 证书链
  2. 'use strict';
  3. const Controller = require('egg').Controller;
  4. const UUID = require('uuid');
  5. const filePath = require('../../config/filespath');
  6. class CAcertController extends Controller {
  7. async caupload() {
  8. const { ctx } = this;
  9. const configJson = require(filePath.configJson);
  10. const uuid = UUID.v1();
  11. // 获取数据流
  12. const stream = await ctx.getFileStream();
  13. // 路径 + 文件名
  14. const name = `${filePath.CAcert}${uuid}.cer`;
  15. try {
  16. // 文件存储
  17. const files = await this.service.fileshandler.upload({ name, stream });
  18. if (files.errcode === 0) {
  19. // 解析证书
  20. const dns = await this.service.shell.read({ filePath: name });
  21. let dn;
  22. if (dns.errcode === 0 && dns.data) {
  23. dns.data.trim().split('\n').forEach(function(v) {
  24. if (v.includes('Subject:')) {
  25. dn = v.replace('Subject:', '');
  26. }
  27. });
  28. const form = { uuid, dn };
  29. configJson.ca.push(form);
  30. const jsonstr = JSON.stringify(configJson);
  31. // 存储数据
  32. await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
  33. this.ctx.body = { errcode: 0, errmsg: '' };
  34. }
  35. }
  36. } catch (error) {
  37. const body = { errcode: -1002, errmsg: '证书上传失败', error };
  38. throw new Error(JSON.stringify(body));
  39. }
  40. }
  41. // 删除ca证书
  42. async cadelete() {
  43. const uuid = this.ctx.query.uuid;
  44. const configJson = require(filePath.configJson);
  45. // 过滤掉当前uuid数据
  46. const data = configJson.ca.filter(p => p.uuid !== uuid);
  47. configJson.ca = data;
  48. const jsonstr = JSON.stringify(configJson);
  49. try {
  50. // 存储到数据文件
  51. await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
  52. // 删除证书文件
  53. const res = await this.service.fileshandler.filesDel(`${filePath.CAcert}${uuid}.cer`);
  54. if (res.errcode === 0) {
  55. this.ctx.body = { errcode: 0, errmsg: '' };
  56. } else {
  57. throw new Error(res);
  58. }
  59. } catch (error) {
  60. const body = { errcode: -1002, errmsg: '证书删除失败', error };
  61. throw new Error(JSON.stringify(body));
  62. }
  63. }
  64. // 查询ca信息
  65. async caquery() {
  66. const { ctx } = this;
  67. const configJson = require(filePath.configJson);
  68. const data = configJson.ca;
  69. ctx.body = { errcode: 0, errmsg: '', data };
  70. }
  71. // ca证书下载
  72. async cacertdownload() {
  73. try {
  74. const uuid = this.ctx.query.uuid;
  75. const cafilePath = `${filePath.CAcert}${uuid}.cer`;
  76. const res = await this.service.fileshandler.download({ filePath: cafilePath });
  77. this.ctx.body = res;
  78. } catch (error) {
  79. const body = { errcode: -1002, errmsg: '证书下载失败', error };
  80. throw new Error(JSON.stringify(body));
  81. }
  82. }
  83. }
  84. module.exports = CAcertController;