sslvpn.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // sslvpn
  2. 'use strict';
  3. const Controller = require('egg').Controller;
  4. const filePath = require('../../config/filespath');
  5. class SslvpnController extends Controller {
  6. // sslvpn客户端
  7. async sslvpnclient() {
  8. const { ctx } = this;
  9. const form = ctx.request.body;
  10. // 引入数据文件
  11. const person = require(filePath.configJson);
  12. person.sslvpn = form;
  13. const jsonstr = JSON.stringify(person);
  14. try {
  15. // 写入数据文件
  16. await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
  17. form.capath = `${filePath.CAcert}${form.ca}.cer`;
  18. form.certpath = `${filePath.cert}${form.cert}.cer`;
  19. form.keys = `${filePath.keys}${form.cert}.key`;
  20. // 写入字符串模板
  21. const sslvpnStr = await ctx.renderView('sslvpn.nj', form);
  22. if (sslvpnStr) {
  23. // 写入sslvpn配置文件
  24. await this.service.fileshandler.write({ filePath: filePath.sslConf, str: sslvpnStr });
  25. }
  26. this.ctx.body = { errcode: 0, errmsg: '' };
  27. } catch (error) {
  28. const body = { errcode: -1002, errmsg: '设置失败', error };
  29. throw new Error(JSON.stringify(body));
  30. }
  31. }
  32. // ssl服务端
  33. async sslvpnservice() {
  34. const { ctx } = this;
  35. const form = ctx.request.body;
  36. const person = require(filePath.configJson);
  37. person.sslvpnservice = form;
  38. const jsonstr = JSON.stringify(person);
  39. try {
  40. // 写入数据文件
  41. await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
  42. form.capath = `${filePath.CAcert}${form.ca}.cer`;
  43. form.certpath = `${filePath.cert}${form.cert}.cer`;
  44. form.keys = `${filePath.key}${form.cert}.key`;
  45. // 写入字符串模板
  46. const sslvpnserviceStr = await ctx.renderView('sslvpnservice.nj', form);
  47. if (sslvpnserviceStr) {
  48. // 写入sslvpn配置文件
  49. await this.service.fileshandler.write({ filePath: filePath.sslConf, str: sslvpnserviceStr });
  50. }
  51. this.ctx.body = { errcode: 0, errmsg: '' };
  52. } catch (error) {
  53. const body = { errcode: -1002, errmsg: '设置失败', error };
  54. throw new Error(JSON.stringify(body));
  55. }
  56. }
  57. // 客户端查询
  58. async sslquery() {
  59. try {
  60. const { ctx } = this;
  61. const person = require(filePath.configJson);
  62. const data = person.sslvpn;
  63. ctx.body = { errcode: 0, errmsg: '', data };
  64. } catch (error) {
  65. const body = { errcode: -1001, errmsg: '查询失败', error };
  66. throw new Error(JSON.stringify(body));
  67. }
  68. }
  69. // 服务端查询
  70. async sslserivcequery() {
  71. try {
  72. const { ctx } = this;
  73. const person = require(filePath.configJson);
  74. const data = person.sslvpnservice;
  75. ctx.body = { errcode: 0, errmsg: '', data };
  76. } catch (error) {
  77. const body = { errcode: -1001, errmsg: '查询失败', error };
  78. throw new Error(JSON.stringify(body));
  79. }
  80. }
  81. }
  82. module.exports = SslvpnController;