Procházet zdrojové kódy

恢复默认配置 & 调整错误码

dygapp před 4 roky
rodič
revize
b3ac9ccd65

+ 1 - 1
app/controller/admin.js

@@ -50,7 +50,7 @@ class AdminController extends Controller {
       }
       ctx.body = msg;
     } catch (error) {
-      const body = { errcode: -1003, errmsg: '密码修改失败', error };
+      const body = { errcode: -1001, errmsg: '密码修改失败', error };
       throw new Error(JSON.stringify(body));
     }
   }

+ 3 - 3
app/controller/cacert.js

@@ -35,7 +35,7 @@ class CAcertController extends Controller {
         }
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '证书上传失败', error };
+      const body = { errcode: -1011, errmsg: '证书上传失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -58,7 +58,7 @@ class CAcertController extends Controller {
         throw new Error(res);
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '证书删除失败', error };
+      const body = { errcode: -1012, errmsg: '证书删除失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -77,7 +77,7 @@ class CAcertController extends Controller {
       const res = await this.service.fileshandler.download({ filePath: cafilePath });
       this.ctx.body = res;
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '证书下载失败', error };
+      const body = { errcode: -1013, errmsg: '证书下载失败', error };
       throw new Error(JSON.stringify(body));
     }
   }

+ 1 - 1
app/controller/enccert.js

@@ -67,7 +67,7 @@ class EnccertController extends Controller {
         }
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '证书上传失败', error };
+      const body = { errcode: -1014, errmsg: '证书上传失败', error };
       throw new Error(JSON.stringify(body));
     }
   }

+ 2 - 2
app/controller/ipsecvpn.js

@@ -28,7 +28,7 @@ class IpsecvpnController extends Controller {
       }
       this.ctx.body = { errcode: 0, errmsg: '' };
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '设置失败', error };
+      const body = { errcode: -1004, errmsg: '设置失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -54,7 +54,7 @@ class IpsecvpnController extends Controller {
       }
       this.ctx.body = { errcode: 0, errmsg: '' };
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '设置失败', error };
+      const body = { errcode: -1004, errmsg: '设置失败', error };
       throw new Error(JSON.stringify(body));
     }
   }

+ 1 - 1
app/controller/lan.js

@@ -38,7 +38,7 @@ class LanController extends Controller {
       }
       this.ctx.body = { errcode: 0, errmsg: '' };
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '设置失败', error };
+      const body = { errcode: -1004, errmsg: '设置失败', error };
       throw new Error(JSON.stringify(body));
     }
   }

+ 1 - 1
app/controller/log.js

@@ -36,7 +36,7 @@ class LogController extends Controller {
         this.ctx.body = { errcode: -0, errmsg: '下载日志失败' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '下载失败', error };
+      const body = { errcode: -1003, errmsg: '下载失败', error };
       throw new Error(JSON.stringify(body));
     }
   }

+ 5 - 5
app/controller/sigcert.js

@@ -28,7 +28,7 @@ class SigcertController extends Controller {
         }
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '创建申请书失败', error };
+      const body = { errcode: -1015, errmsg: '创建申请书失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -68,7 +68,7 @@ class SigcertController extends Controller {
         this.ctx.body = { errcode: -1, errmsg: '证书删除失败' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '证书删除失败', error };
+      const body = { errcode: -1016, errmsg: '证书删除失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -94,7 +94,7 @@ class SigcertController extends Controller {
         ctx.body = { errcode: 0, errmsg: '' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '证书上传失败', error };
+      const body = { errcode: -1017, errmsg: '证书上传失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -106,7 +106,7 @@ class SigcertController extends Controller {
       const res = await this.service.fileshandler.download({ filePath: reqfilePath });
       this.ctx.body = res;
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '申请书下载失败', error };
+      const body = { errcode: -1018, errmsg: '申请书下载失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -118,7 +118,7 @@ class SigcertController extends Controller {
       const res = await this.service.fileshandler.download({ filePath: sigfilePath });
       this.ctx.body = res;
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '证书下载失败', error };
+      const body = { errcode: -1019, errmsg: '证书下载失败', error };
       throw new Error(JSON.stringify(body));
     }
   }

+ 4 - 4
app/controller/sslvpn.js

@@ -26,7 +26,7 @@ class SslvpnController extends Controller {
       }
       this.ctx.body = { errcode: 0, errmsg: '' };
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '设置失败', error };
+      const body = { errcode: -1004, errmsg: '设置失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -51,7 +51,7 @@ class SslvpnController extends Controller {
       }
       this.ctx.body = { errcode: 0, errmsg: '' };
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '设置失败', error };
+      const body = { errcode: -1004, errmsg: '设置失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -63,7 +63,7 @@ class SslvpnController extends Controller {
       const data = person.sslvpn;
       ctx.body = { errcode: 0, errmsg: '', data };
     } catch (error) {
-      const body = { errcode: -1001, errmsg: '查询失败', error };
+      const body = { errcode: -1005, errmsg: '查询失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -75,7 +75,7 @@ class SslvpnController extends Controller {
       const data = person.sslvpnservice;
       ctx.body = { errcode: 0, errmsg: '', data };
     } catch (error) {
-      const body = { errcode: -1001, errmsg: '查询失败', error };
+      const body = { errcode: -1005, errmsg: '查询失败', error };
       throw new Error(JSON.stringify(body));
     }
   }

+ 37 - 26
app/controller/systemctl.js

@@ -10,7 +10,7 @@ class SystemctlController extends Controller {
     try {
       await this.service.shell.shell(shells.reboot);
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '重启机器失败', error };
+      const body = { errcode: -1021, errmsg: '重启机器失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -19,12 +19,12 @@ class SystemctlController extends Controller {
     try {
       const res = await this.service.shell.shell(shells.wanDown);
       if (res.errcode === 0) {
-        this.ctx.body = { errcode: 0, errmsg: '' };
+        this.ctx.body = { errcode: 0, errmsg: 'ok' };
       } else {
         this.ctx.body = { errcode: -1, errmsg: '禁用wan网卡失败' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '禁用wan网卡失败', error };
+      const body = { errcode: -1022, errmsg: '禁用wan网卡失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -33,12 +33,12 @@ class SystemctlController extends Controller {
     try {
       const res = await this.service.shell.shell(shells.wanUp);
       if (res.errcode === 0) {
-        this.ctx.body = { errcode: 0, errmsg: '' };
+        this.ctx.body = { errcode: 0, errmsg: 'ok' };
       } else {
         this.ctx.body = { errcode: -1, errmsg: '启用wan网卡失败' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '启用wan网卡失败', error };
+      const body = { errcode: -1023, errmsg: '启用wan网卡失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -47,12 +47,12 @@ class SystemctlController extends Controller {
     try {
       const res = await this.service.shell.shell(shells.lanUp);
       if (res.errcode === 0) {
-        this.ctx.body = { errcode: 0, errmsg: '' };
+        this.ctx.body = { errcode: 0, errmsg: 'ok' };
       } else {
         this.ctx.body = { errcode: -1, errmsg: '启用lan网卡失败' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '启用lan网卡失败', error };
+      const body = { errcode: -1024, errmsg: '启用lan网卡失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -61,12 +61,12 @@ class SystemctlController extends Controller {
     try {
       const res = await this.service.shell.shell(shells.lanDown);
       if (res.errcode === 0) {
-        this.ctx.body = { errcode: 0, errmsg: '' };
+        this.ctx.body = { errcode: 0, errmsg: 'ok' };
       } else {
         this.ctx.body = { errcode: -1, errmsg: '禁用lan网卡失败' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '禁用lan网卡失败', error };
+      const body = { errcode: -1025, errmsg: '禁用lan网卡失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -77,12 +77,12 @@ class SystemctlController extends Controller {
       const res = await this.service.shell.shell(`ping ${address} -c 3`);
       console.log(res, 'res');
       if (res.errcode === 0 && res.data !== '') {
-        this.ctx.body = { errcode: 0, errmsg: '' };
+        this.ctx.body = { errcode: 0, errmsg: 'ok' };
       } else {
         this.ctx.body = { errcode: -1, errmsg: '地址连接失败' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '地址连接异常', error };
+      const body = { errcode: -1026, errmsg: '地址连接异常', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -91,12 +91,12 @@ class SystemctlController extends Controller {
     try {
       const res = await this.service.shell.shell('date "+%Y-%m-%d %H:%M:%S"');
       if (res.errcode === 0) {
-        this.ctx.body = { errcode: 0, errmsg: '' };
+        this.ctx.body = { errcode: 0, errmsg: 'ok' };
       } else {
         this.ctx.body = { errcode: -1, errmsg: '获取时间失败' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '获取时间失败', error };
+      const body = { errcode: -1027, errmsg: '获取时间失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -107,12 +107,12 @@ class SystemctlController extends Controller {
       const res = await this.service.shell.shell(`date -s "${date}"`);
       console.log(res);
       if (res.errcode === 0) {
-        this.ctx.body = { errcode: 0, errmsg: '' };
+        this.ctx.body = { errcode: 0, errmsg: 'ok' };
       } else {
         this.ctx.body = { errcode: -0, errmsg: '设置系统时间失败' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '设置系统时间失败', error };
+      const body = { errcode: -1028, errmsg: '设置系统时间失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -121,12 +121,12 @@ class SystemctlController extends Controller {
     try {
       const res = await this.service.shell.shell('swanctl -l | grep established');
       if (res.errcode === 0 && res.data.length > 0) {
-        this.ctx.body = { errcode: 0, errmsg: '', data: '已连接' };
+        this.ctx.body = { errcode: 0, errmsg: 'ok', data: '已连接' };
       } else {
-        this.ctx.body = { errcode: 0, errmsg: '', data: '未连接' };
+        this.ctx.body = { errcode: 0, errmsg: 'ok', data: '未连接' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '获取VPN连接状态失败', error };
+      const body = { errcode: -1029, errmsg: '获取VPN连接状态失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -135,12 +135,12 @@ class SystemctlController extends Controller {
     try {
       const res = await this.service.shell.shell('vmstat | tail -n 1 | awk \'{print $13}\'');
       if (res.errcode === 0 && res.data) {
-        this.ctx.body = { errcode: 0, errmsg: '', data: { cpu: res.data } };
+        this.ctx.body = { errcode: 0, errmsg: 'ok', data: { cpu: res.data } };
       } else {
         this.ctx.body = { errcode: -1, errmsg: '获取CPU使用率失败' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '获取CPU使用率失败', error };
+      const body = { errcode: -1030, errmsg: '获取CPU使用率失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -154,7 +154,7 @@ class SystemctlController extends Controller {
         this.ctx.body = { errcode: -1, errmsg: '获取内存使用率失败' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '获取内存使用率失败', error };
+      const body = { errcode: -1031, errmsg: '获取内存使用率失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -166,7 +166,7 @@ class SystemctlController extends Controller {
       const version = configJson.version;
       this.ctx.body = { errcode: 0, errmsg: '', data: { model, version } };
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '获取设备信息失败', error };
+      const body = { errcode: -1032, errmsg: '获取设备信息失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -186,12 +186,12 @@ class SystemctlController extends Controller {
     try {
       const res = await this.service.shell.shell(data);
       if (res.errcode === 0) {
-        this.ctx.body = { errcode: 0, errmsg: '' };
+        this.ctx.body = { errcode: 0, errmsg: 'ok' };
       } else {
         this.ctx.body = { errcode: -1, errmsg: '操作失败' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '操作失败', error };
+      const body = { errcode: -1033, errmsg: '操作失败', error };
       throw new Error(JSON.stringify(body));
     }
   }
@@ -211,12 +211,23 @@ class SystemctlController extends Controller {
     try {
       const res = await this.service.shell.shell(data);
       if (res.errcode === 0) {
-        this.ctx.body = { errcode: 0, errmsg: '' };
+        this.ctx.body = { errcode: 0, errmsg: 'ok' };
       } else {
         this.ctx.body = { errcode: -1, errmsg: '操作失败' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '操作失败', error };
+      const body = { errcode: -1034, errmsg: '操作失败', error };
+      throw new Error(JSON.stringify(body));
+    }
+  }
+
+  // 恢复默认配置
+  async configDefault() {
+    try {
+      await this.service.shell.shell(shells.configDefault);
+      this.ctx.body = { errcode: 0, errmsg: 'ok' };
+    } catch (error) {
+      const body = { errcode: -1035, errmsg: '恢复默认设置失败', error };
       throw new Error(JSON.stringify(body));
     }
   }

+ 1 - 1
app/controller/wan.js

@@ -37,7 +37,7 @@ class WanController extends Controller {
         this.ctx.body = { errcode: 0, errmsg: '' };
       }
     } catch (error) {
-      const body = { errcode: -1002, errmsg: '设置失败', error };
+      const body = { errcode: -1004, errmsg: '设置失败', error };
       throw new Error(JSON.stringify(body));
     }
   }

+ 2 - 0
app/router.js

@@ -47,6 +47,7 @@ module.exports = app => {
   router.get('/api/devinfo', jwt, controller.systemctl.devinfo);
   router.get('/api/ipsecvpnstate', jwt, controller.systemctl.ipsecvpnstate);
   router.get('/api/sslvpnstate', jwt, controller.systemctl.sslvpnstate);
+  router.get('/api/config_default', jwt, controller.systemctl.configDefault);
   // ipsecvpn
   router.post('/api/secclient', jwt, controller.ipsecvpn.secclient);
   router.post('/api/secservice', jwt, controller.ipsecvpn.secservice);
@@ -60,4 +61,5 @@ module.exports = app => {
   // log
   router.get('/api/logquery', jwt, controller.log.logquery);
   router.get('/api/logdownload', controller.log.logdownload);
+  
 };

+ 2 - 0
config/shells.js

@@ -97,5 +97,7 @@ module.exports = {
   // 连接测试
   // ping 192.168.88.10
 
+  // 恢复默认配置
+  configDefault: 'echo ok',
 };