systemctl.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // 系统管理
  2. 'use strict';
  3. const Controller = require('egg').Controller;
  4. const shells = require('../../config/shells');
  5. const filePath = require('../../config/filespath');
  6. class SystemctlController extends Controller {
  7. // 重启机器
  8. async reboot() {
  9. try {
  10. await this.service.shell.shell(shells.reboot);
  11. } catch (error) {
  12. const body = { errcode: -1021, errmsg: '重启机器失败', error };
  13. throw new Error(JSON.stringify(body));
  14. }
  15. }
  16. // 禁用wan网卡
  17. async wanDown() {
  18. try {
  19. const res = await this.service.shell.shell(shells.wanDown);
  20. if (res.errcode === 0) {
  21. this.ctx.body = { errcode: 0, errmsg: 'ok' };
  22. } else {
  23. this.ctx.body = { errcode: -1, errmsg: '禁用wan网卡失败' };
  24. }
  25. } catch (error) {
  26. const body = { errcode: -1022, errmsg: '禁用wan网卡失败', error };
  27. throw new Error(JSON.stringify(body));
  28. }
  29. }
  30. // 启用wan网卡
  31. async wanup() {
  32. try {
  33. const res = await this.service.shell.shell(shells.wanUp);
  34. if (res.errcode === 0) {
  35. this.ctx.body = { errcode: 0, errmsg: 'ok' };
  36. } else {
  37. this.ctx.body = { errcode: -1, errmsg: '启用wan网卡失败' };
  38. }
  39. } catch (error) {
  40. const body = { errcode: -1023, errmsg: '启用wan网卡失败', error };
  41. throw new Error(JSON.stringify(body));
  42. }
  43. }
  44. // 启用lan网卡
  45. async lanup() {
  46. try {
  47. const res = await this.service.shell.shell(shells.lanUp);
  48. if (res.errcode === 0) {
  49. this.ctx.body = { errcode: 0, errmsg: 'ok' };
  50. } else {
  51. this.ctx.body = { errcode: -1, errmsg: '启用lan网卡失败' };
  52. }
  53. } catch (error) {
  54. const body = { errcode: -1024, errmsg: '启用lan网卡失败', error };
  55. throw new Error(JSON.stringify(body));
  56. }
  57. }
  58. // 禁用lan网卡
  59. async lanDown() {
  60. try {
  61. const res = await this.service.shell.shell(shells.lanDown);
  62. if (res.errcode === 0) {
  63. this.ctx.body = { errcode: 0, errmsg: 'ok' };
  64. } else {
  65. this.ctx.body = { errcode: -1, errmsg: '禁用lan网卡失败' };
  66. }
  67. } catch (error) {
  68. const body = { errcode: -1025, errmsg: '禁用lan网卡失败', error };
  69. throw new Error(JSON.stringify(body));
  70. }
  71. }
  72. // ping
  73. async ping() {
  74. const { address } = this.ctx.request.body;
  75. try {
  76. const res = await this.service.shell.shell(`ping ${address} -c 3`);
  77. console.log(res, 'res');
  78. if (res.errcode === 0 && res.data !== '') {
  79. this.ctx.body = { errcode: 0, errmsg: 'ok' };
  80. } else {
  81. this.ctx.body = { errcode: -1, errmsg: '地址连接失败' };
  82. }
  83. } catch (error) {
  84. const body = { errcode: -1026, errmsg: '地址连接异常', error };
  85. throw new Error(JSON.stringify(body));
  86. }
  87. }
  88. // 获取时间
  89. async getdate() {
  90. try {
  91. const res = await this.service.shell.shell('date "+%Y-%m-%d %H:%M:%S"');
  92. if (res.errcode === 0) {
  93. this.ctx.body = { errcode: 0, errmsg: 'ok' };
  94. } else {
  95. this.ctx.body = { errcode: -1, errmsg: '获取时间失败' };
  96. }
  97. } catch (error) {
  98. const body = { errcode: -1027, errmsg: '获取时间失败', error };
  99. throw new Error(JSON.stringify(body));
  100. }
  101. }
  102. // 设置系统时间
  103. async setdate() {
  104. const { date } = this.ctx.request.body;
  105. try {
  106. const res = await this.service.shell.shell(`date -s "${date}"`);
  107. console.log(res);
  108. if (res.errcode === 0) {
  109. this.ctx.body = { errcode: 0, errmsg: 'ok' };
  110. } else {
  111. this.ctx.body = { errcode: -1028, errmsg: '设置系统时间失败' };
  112. }
  113. } catch (error) {
  114. const body = { errcode: -1028, errmsg: '设置系统时间失败', error };
  115. throw new Error(JSON.stringify(body));
  116. }
  117. }
  118. // 获取vpn连接状态
  119. async vpnstate() {
  120. try {
  121. const res = await this.service.shell.shell('swanctl -l | grep established');
  122. if (res.errcode === 0 && res.data.length > 0) {
  123. this.ctx.body = { errcode: 0, errmsg: 'ok', data: '已连接' };
  124. } else {
  125. this.ctx.body = { errcode: 0, errmsg: 'ok', data: '未连接' };
  126. }
  127. } catch (error) {
  128. const body = { errcode: -1029, errmsg: '获取VPN连接状态失败', error };
  129. throw new Error(JSON.stringify(body));
  130. }
  131. }
  132. // 获取cpu使用率
  133. async cpu() {
  134. try {
  135. const res = await this.service.shell.shell('vmstat | tail -n 1 | awk \'{print $13}\'');
  136. if (res.errcode === 0 && res.data) {
  137. this.ctx.body = { errcode: 0, errmsg: 'ok', data: { cpu: res.data } };
  138. } else {
  139. this.ctx.body = { errcode: -1, errmsg: '获取CPU使用率失败' };
  140. }
  141. } catch (error) {
  142. const body = { errcode: -1030, errmsg: '获取CPU使用率失败', error };
  143. throw new Error(JSON.stringify(body));
  144. }
  145. }
  146. // 获取内存使用率
  147. async memory() {
  148. try {
  149. const res = await this.service.shell.shell('free -t -m |tail -n 1 | awk \'{print $3/$2*100}\'');
  150. if (res.errcode === 0 && res.data) {
  151. this.ctx.body = { errcode: 0, errmsg: '', data: { memory: res.data } };
  152. } else {
  153. this.ctx.body = { errcode: -1, errmsg: '获取内存使用率失败' };
  154. }
  155. } catch (error) {
  156. const body = { errcode: -1031, errmsg: '获取内存使用率失败', error };
  157. throw new Error(JSON.stringify(body));
  158. }
  159. }
  160. // 获取设备信息
  161. async devinfo() {
  162. try {
  163. const configJson = require(filePath.configJson);
  164. const model = configJson.model;
  165. const version = configJson.version;
  166. this.ctx.body = { errcode: 0, errmsg: '', data: { model, version } };
  167. } catch (error) {
  168. const body = { errcode: -1032, errmsg: '获取设备信息失败', error };
  169. throw new Error(JSON.stringify(body));
  170. }
  171. }
  172. // sslvpn 启动 停止 重启
  173. async sslvpnstate() {
  174. const { type } = this.ctx.request.query;
  175. let data;
  176. if (type === 'stop') {
  177. data = shells.opStop;
  178. }
  179. if (type === 'start') {
  180. data = shells.opStart;
  181. }
  182. if (type === 'restart') {
  183. data = shells.opRestart;
  184. }
  185. try {
  186. const res = await this.service.shell.shell(data);
  187. if (res.errcode === 0) {
  188. this.ctx.body = { errcode: 0, errmsg: 'ok' };
  189. } else {
  190. this.ctx.body = { errcode: -1, errmsg: '操作失败' };
  191. }
  192. } catch (error) {
  193. const body = { errcode: -1033, errmsg: '操作失败', error };
  194. throw new Error(JSON.stringify(body));
  195. }
  196. }
  197. // ipsecvpn 启动 停止 重启
  198. async ipsecvpnstate() {
  199. const { type } = this.ctx.request.query;
  200. let data;
  201. if (type === 'stop') {
  202. data = shells.swStop;
  203. }
  204. if (type === 'start') {
  205. data = shells.swStart;
  206. }
  207. if (type === 'restart') {
  208. data = shells.swRestart;
  209. }
  210. try {
  211. const res = await this.service.shell.shell(data);
  212. if (res.errcode === 0) {
  213. this.ctx.body = { errcode: 0, errmsg: 'ok' };
  214. } else {
  215. this.ctx.body = { errcode: -1034, errmsg: '操作失败' };
  216. }
  217. } catch (error) {
  218. const body = { errcode: -1034, errmsg: '操作失败', error };
  219. throw new Error(JSON.stringify(body));
  220. }
  221. }
  222. // 恢复默认配置
  223. async recover() {
  224. try {
  225. await this.service.shell.shell(shells.recovery);
  226. this.ctx.body = { errcode: 0, errmsg: 'ok' };
  227. } catch (error) {
  228. const body = { errcode: -1035, errmsg: '恢复默认设置失败', error };
  229. throw new Error(JSON.stringify(body));
  230. }
  231. }
  232. // 设置时间同步
  233. async setNtp() {
  234. const { status } = this.ctx.request.body;
  235. try {
  236. await this.service.shell.shell(shells.setNtp(status || 'no'));
  237. this.ctx.body = { errcode: 0, errmsg: 'ok' };
  238. } catch (error) {
  239. const body = { errcode: -1036, errmsg: `${status === 'yes' ? '启用' : '停用'}时间同步失败`, error };
  240. throw new Error(JSON.stringify(body));
  241. }
  242. }
  243. // 获得NTP设置
  244. async getNtp() {
  245. try {
  246. const res = await this.service.shell.shell(shells.getNtp);
  247. if (res.errcode === 0) {
  248. this.ctx.body = { errcode: 0, errmsg: 'ok', data: res.data };
  249. } else {
  250. this.ctx.body = { errcode: -1037, errmsg: '获得NTP设置失败' };
  251. }
  252. } catch (error) {
  253. const body = { errcode: -1037, errmsg: '获得NTP设置失败', error };
  254. throw new Error(JSON.stringify(body));
  255. }
  256. }
  257. }
  258. module.exports = SystemctlController;