ipsecvpn.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // ipsecvpn
  2. 'use strict';
  3. const Controller = require('egg').Controller;
  4. const filePath = require('../../config/filespath');
  5. class IpsecvpnController extends Controller {
  6. // 客户端
  7. async secclient() {
  8. const { ctx } = this;
  9. const { address, addressTow, cert, digit, loglevel, pwa } = ctx.request.body;
  10. const form = { address, addressTow, cert, digit, loglevel, pwa };
  11. const person = require(filePath.configJson);
  12. person.ipsecvpn = form;
  13. const jsonstr = JSON.stringify(person);
  14. try {
  15. // 写入数据文件
  16. await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
  17. form.certpath = `${filePath.cert}${form.cert}.cer`;
  18. // 写入配置字符串模板
  19. const ipsecavpnStr = await ctx.renderView('ipsecavpn.nj', form);
  20. // 写入log字符串模板
  21. const ipsecalog = await ctx.renderView('ipsecalog.nj', form);
  22. if (ipsecavpnStr && ipsecalog) {
  23. // 写入配置文件
  24. await this.service.fileshandler.write({ filePath: filePath.ipsecClient, str: ipsecavpnStr });
  25. await this.service.fileshandler.write({ filePath: filePath.swanlog, str: ipsecalog });
  26. }
  27. this.ctx.body = { errcode: 0, errmsg: '' };
  28. } catch (error) {
  29. const body = { errcode: -1002, errmsg: '设置失败', error };
  30. throw new Error(JSON.stringify(body));
  31. }
  32. }
  33. // 服务端
  34. async secservice() {
  35. const { ctx } = this;
  36. const form = ctx.request.body;
  37. const person = require(filePath.configJson);
  38. person.ipsecservice = form;
  39. const jsonstr = JSON.stringify(person);
  40. try {
  41. // 存储数据
  42. await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
  43. form.certpath = `${filePath.cert}${form.cert}.cer`;
  44. // 写入配置字符串模板
  45. const ipsecavpnStr = await ctx.renderView('ipsecavpnservice.nj', form);
  46. // 写入log字符串模板
  47. const ipsecalog = await ctx.renderView('ipsecalog.nj', form);
  48. if (ipsecavpnStr && ipsecalog) {
  49. // 写入配置文件
  50. await this.service.fileshandler.write({ filePath: filePath.ipsecServer, str: ipsecavpnStr });
  51. await this.service.fileshandler.write({ filePath: filePath.swanlog, str: ipsecalog });
  52. }
  53. this.ctx.body = { errcode: 0, errmsg: '' };
  54. } catch (error) {
  55. const body = { errcode: -1002, errmsg: '设置失败', error };
  56. throw new Error(JSON.stringify(body));
  57. }
  58. }
  59. // 客户端查询
  60. async secclientquery() {
  61. try {
  62. const { ctx } = this;
  63. const person = require(filePath.configJson);
  64. const data = person.ipsecvpn;
  65. ctx.body = { errcode: 0, errmsg: '', data };
  66. } catch (error) {
  67. throw error;
  68. }
  69. }
  70. // 服务端查询
  71. async ipsecservicequery() {
  72. try {
  73. const { ctx } = this;
  74. const person = require(filePath.configJson);
  75. const data = person.ipsecservice;
  76. ctx.body = { errcode: 0, errmsg: '', data };
  77. } catch (error) {
  78. throw error;
  79. }
  80. }
  81. }
  82. module.exports = IpsecvpnController;