home.controller.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { All, Controller, Get, Inject, Put } from '@midwayjs/core';
  2. import { Context } from '@midwayjs/koa';
  3. import { ProxyService } from '../service/proxy.service';
  4. import { SingleSignOnService } from '../service/singleSignOn.service';
  5. import { PermissionService } from '../service/permission.service';
  6. import { PemService } from '../service/pem.service';
  7. import SensitiveWordTool from 'sensitive-word-tool';
  8. import { EsDictService } from '../service/esDict.service';
  9. @Controller('/cxyy')
  10. export class HomeController {
  11. @Inject()
  12. ctx: Context;
  13. @Inject()
  14. service: ProxyService;
  15. @Inject()
  16. singleSignOnService: SingleSignOnService;
  17. @Inject()
  18. permissionService: PermissionService;
  19. @Inject()
  20. pemService: PemService;
  21. @Inject()
  22. esDict: EsDictService;
  23. @Get('/')
  24. async home(): Promise<string> {
  25. return 'proxy starting....';
  26. }
  27. @Get('/test')
  28. async test(): Promise<string> {
  29. const sensitiveWordTool = new SensitiveWordTool({
  30. useDefaultWords: true,
  31. });
  32. const str = '<p>在群里,我们不能讨论有关于党,d,国家,gj,政府,zf,不能建政,jz,不能搞黄色,淫秽色情</p>';
  33. const res = sensitiveWordTool.filter(str);
  34. return res;
  35. }
  36. @Get('/initKeys')
  37. async makePairCrypto() {
  38. await this.pemService.createKeys();
  39. return 'makePairCrypto';
  40. }
  41. /**
  42. * es请求远程字典内容, 约1分钟来同步一次
  43. * @returns 返回扩充字典内容
  44. */
  45. @Get('/es_dict')
  46. async getEsDict() {
  47. const result = await this.esDict.getDictContent();
  48. const ranStr = this.esDict.randomStr();
  49. try {
  50. this.ctx.response.etag = ranStr;
  51. this.ctx.response.lastModified = new Date();
  52. } catch (error) {
  53. console.log(error);
  54. }
  55. return result;
  56. }
  57. @All('/**')
  58. async proxy() {
  59. // TODO:检查请求是否在白名单
  60. const inWhiteList = this.singleSignOnService.inWhiteList();
  61. if (!inWhiteList) {
  62. // 不在白名单上则检查登录
  63. await this.singleSignOnService.index();
  64. // 暂时不要检查权限
  65. // await this.permissionService.index();
  66. // 通过检查(不报异常中断程序)即可以发送请求
  67. return await this.service.index();
  68. }
  69. // 3.发送请求
  70. return await this.service.index();
  71. }
  72. }