123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- 'use strict';
- const Controller = require('egg').Controller;
- class CertController extends Controller {
- // wan添加
- async wanadd() {
- const login = await this.service.files.login();
- if (login.errcode !== 0) {
- this.ctx.body = login;
- return false;
- }
- try {
- const { ctx } = this;
- const { address, netmask, gateway, type } = ctx.request.body;
- const form = { type, address: address || '192.168.0.10', netmask: netmask || '255.255.255.0', gateway: gateway || '192.168.0.1' };
- const filePath = this.app.config.filePath.configJson;
- const person = require(this.app.config.filePath.configJson);
- person.wan = form;
- const jsonstr = JSON.stringify(person);
- const paths = this.app.config.filePath.wan;
- // 写入自己管理的文件
- await this.service.files.write({ filePath, str: jsonstr });
- const wanstr = await ctx.renderView('wan.nj', form);
- console.log(wanstr);
- // 写入配置文件
- await this.service.files.write({ filePath: paths, str: wanstr });
- ctx.body = { errcode: 0, errmsg: '' };
- } catch (error) {
- // this.ctx.body = { errcode: -2, errmsg: error };
- throw error;
- }
- }
- // lan添加
- async lanadd() {
- const login = await this.service.files.login();
- if (login.errcode !== 0) {
- this.ctx.body = login;
- return false;
- }
- const { ctx } = this;
- try {
- const form = { ...ctx.request.body };
- const filePath = this.app.config.filePath.configJson;
- const person = require(this.app.config.filePath.configJson);
- person.lan = { address: form.address || '192.168.3.1', type: form.type };
- person.dhcp = { start: form.start || '120', end: form.end || '200', address: form.address || '192.168.3.1' };
- const jsonstr = JSON.stringify(person);
- await this.service.files.write({ filePath, str: jsonstr });
- const items = [];
- if (!form.address) {
- form.address = '192.168.3.1';
- }
- for (let index = 0; index < form.address.length; index++) {
- if (form.address[index] === '.') {
- items.push(index);
- }
- }
- form.addressTow = form.address.slice(0, items[2]);
- const lanstr = await ctx.renderView('lan.nj', form);
- const paths = this.app.config.filePath.lan;
- await this.service.files.write({ filePath: paths, str: lanstr });
- if (form.type === 0) {
- form.start = form.start || '120';
- form.end = form.end || '200';
- const dhcpstr = await ctx.renderView('dhcp.nj', form);
- // 写入dhcp
- const dhcppath = this.app.config.filePath.dhcpd;
- const dhcpres = await this.service.files.write({ filePath: dhcppath, str: dhcpstr });
- // 启动dhcp
- if (dhcpres.errcode === 0) {
- await this.service.files.dhcp({ type: 'enable' });
- }
- } else {
- // 停止dhcp服务
- await this.service.files.dhcp({ type: 'disable' });
- }
- ctx.body = { errcode: 0, errmsg: '' };
- } catch (error) {
- // console.log(error);
- // this.ctx.body = { errcode: -2, errmsg: error };
- throw error;
- }
- }
- // wan查询
- async wanquery() {
- try {
- const { ctx } = this;
- const person = require(this.app.config.filePath.configJson);
- const data = person.wan;
- ctx.body = { errcode: 0, errmsg: '', data };
- } catch (error) {
- // this.ctx.body = { errcode: -2, errmsg: error };
- throw error;
- }
- }
- // lan查询
- async lanquery() {
- try {
- const { ctx } = this;
- const person = require(`${this.app.config.filePath.configJson}`);
- const data = person.lan;
- ctx.body = { errcode: 0, errmsg: '', data };
- } catch (error) {
- // this.ctx.body = { errcode: -2, errmsg: error };
- throw error;
- }
- }
- // dhcp查询
- async dhcpquery() {
- try {
- const { ctx } = this;
- const person = require(this.app.config.filePath.configJson);
- const data = person.dhcp;
- ctx.body = { errcode: 0, errmsg: '', data };
- } catch (error) {
- // this.ctx.body = { errcode: -2, errmsg: error };
- throw error;
- }
- }
- }
- module.exports = CertController;
|