// ipsecvpn 'use strict'; const Controller = require('egg').Controller; const filePath = require('../../config/filespath'); class IpsecvpnController extends Controller { // 客户端 async setClient() { const { ctx } = this; const { address, addressTow, cert, digit, loglevel, pwa } = ctx.request.body; const form = { address, addressTow, cert, digit, loglevel, pwa }; const person = await this.app.readConfig(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.swanLogConf, str: ipsecalog }); } this.ctx.body = { errcode: 0, errmsg: '' }; } catch (error) { const body = { errcode: -1004, errmsg: '设置失败', error }; throw new Error(JSON.stringify(body)); } } // 服务端 async setServer() { const { ctx } = this; const form = ctx.request.body; const person = await this.app.readConfig(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.swanLogConf, str: ipsecalog }); } this.ctx.body = { errcode: 0, errmsg: '' }; } catch (error) { const body = { errcode: -1004, errmsg: '设置失败', error }; throw new Error(JSON.stringify(body)); } } // 客户端查询 async getClient() { try { const { ctx } = this; const person = await this.app.readConfig(filePath.configJson); const data = person.ipsecvpn; ctx.body = { errcode: 0, errmsg: '', data }; } catch (error) { throw error; } } // 服务端查询 async getServer() { try { const { ctx } = this; const person = await this.app.readConfig(filePath.configJson); const data = person.ipsecservice; ctx.body = { errcode: 0, errmsg: '', data }; } catch (error) { throw error; } } } module.exports = IpsecvpnController;