12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- // ipsecvpn
- 'use strict';
- const Controller = require('egg').Controller;
- const filePath = require('../../config/filespath');
- class IpsecvpnController extends Controller {
- // 客户端
- async secclient() {
- const { ctx } = this;
- const { address, addressTow, cert, digit, loglevel, pwa } = ctx.request.body;
- const form = { address, addressTow, cert, digit, loglevel, pwa };
- const person = require(filePath.configJson);
- person.ipsecvpn = form;
- const jsonstr = JSON.stringify(person);
- try {
- // 写入数据文件
- await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
- form.certpath = `${filePath.cert}${form.cert}.cer`;
- // 写入配置字符串模板
- const ipsecavpnStr = await ctx.renderView('ipsecavpn.nj', form);
- // 写入log字符串模板
- const ipsecalog = await ctx.renderView('ipsecalog.nj', form);
- if (ipsecavpnStr && ipsecalog) {
- // 写入配置文件
- await this.service.fileshandler.write({ filePath: filePath.ipsecClient, str: ipsecavpnStr });
- await this.service.fileshandler.write({ filePath: filePath.swanlog, str: ipsecalog });
- }
- this.ctx.body = { errcode: 0, errmsg: '' };
- } catch (error) {
- const body = { errcode: -1002, errmsg: '设置失败', error };
- throw new Error(JSON.stringify(body));
- }
- }
- // 服务端
- async secservice() {
- const { ctx } = this;
- const form = ctx.request.body;
- const person = require(filePath.configJson);
- person.ipsecservice = form;
- const jsonstr = JSON.stringify(person);
- try {
- // 存储数据
- await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
- form.certpath = `${filePath.cert}${form.cert}.cer`;
- // 写入配置字符串模板
- const ipsecavpnStr = await ctx.renderView('ipsecavpnservice.nj', form);
- // 写入log字符串模板
- const ipsecalog = await ctx.renderView('ipsecalog.nj', form);
- if (ipsecavpnStr && ipsecalog) {
- // 写入配置文件
- await this.service.fileshandler.write({ filePath: filePath.ipsecServer, str: ipsecavpnStr });
- await this.service.fileshandler.write({ filePath: filePath.swanlog, str: ipsecalog });
- }
- this.ctx.body = { errcode: 0, errmsg: '' };
- } catch (error) {
- const body = { errcode: -1002, errmsg: '设置失败', error };
- throw new Error(JSON.stringify(body));
- }
- }
- // 客户端查询
- async secclientquery() {
- try {
- const { ctx } = this;
- const person = require(filePath.configJson);
- const data = person.ipsecvpn;
- ctx.body = { errcode: 0, errmsg: '', data };
- } catch (error) {
- throw error;
- }
- }
- // 服务端查询
- async ipsecservicequery() {
- try {
- const { ctx } = this;
- const person = require(filePath.configJson);
- const data = person.ipsecservice;
- ctx.body = { errcode: 0, errmsg: '', data };
- } catch (error) {
- throw error;
- }
- }
- }
- module.exports = IpsecvpnController;
|