app.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import express from "express";
  2. import logger from "morgan";
  3. import { AES, enc, mode, pad } from "crypto-js";
  4. const { JSEncrypt } = require("js-encrypt");
  5. interface IReportLog {
  6. client: string;
  7. webSource?: string;
  8. deviceId: string;
  9. environment?: string;
  10. customInfo: string;
  11. logPageNo: number;
  12. fileDate: string;
  13. logArray: string;
  14. }
  15. interface ILogItem {
  16. l: string;
  17. iv?: string;
  18. k?: string;
  19. v?: number;
  20. }
  21. interface ILogContent {
  22. /**log type */
  23. t: number;
  24. /**log content */
  25. c: string;
  26. /**js timestamp */
  27. d: string;
  28. }
  29. const port = '9002';
  30. const privateKey = '-----BEGIN PRIVATE KEY-----\n' +
  31. 'MIICXAIBAAKBgG2m5VVtZ4mHml3FB9foDRpDW7PwFoa+1eYN777rNmIdnmezQqHW\n' +
  32. 'IRVcnTRVjrgGt2ndP2cYT7MgmWpvr8IjgN0PZ6ngMmKYGpapMqkxsnS/6Q8UZO4P\n' +
  33. 'QNlnsK2hSPoIDeJcHxDvo6Nelg+mRHEpD6K+1FIqzvdwVPCcgK7UbZElAgMBAAEC\n' +
  34. 'gYAXQM9dgGf2iGU6AXCaXsF4klQ+ImoEhS/DK61t5V+RCwrunttAirJVX2CPGp27\n' +
  35. 'dOEseBjb+hHcwMsIAUtadkD7VqDoLg0C63pP6Yr91zoLSq7ru7FL4j8ZDGgHV2tE\n' +
  36. '6TbtIRGbxuuF+EmztKqrMCvN4qcxqDvTtU6Xq9Us7xC+uQJBANoFtsuTqDaFFOJ0\n' +
  37. 'p0S3+w4lzUcfp+XboVb4+q7wcFumfDCLIuvOTEiCFj5Tj0o2eHtEo3ARHWIcNZqB\n' +
  38. 'OgYPPdMCQQCAwJzubpjr7oXxINLERcQ1PXvjD5HD9Q4A20p6pFkcEYTlDYW/nm60\n' +
  39. 'PMr7JWG54TH0e6w8IfJZVR2xonVasoInAkEAjdIfuUdgqa5iCnkFgb8IEYjngneG\n' +
  40. 'GRCIX/Hv57JB9GxU5qLrYWa92oC8hWiHkifisZTRmAmaCoL9H3cmTmDFvwJAJjwM\n' +
  41. '3mmNlBLDR/YdYRfuyni1v5oyCWVOgUad+YmwxLsXIgY//8WGzpN3G9ngCZksgpPv\n' +
  42. 'c/QIyiqSpNu/ye1U5QJBAIgSfWXvx+varXagGojcCH8mVtT/E4/w3R+QTLAp6s0L\n' +
  43. 'QTQUDPnDGrxvT4sDoU6ib+nn0FAr/kTyJptdlvaXfeo=' +
  44. '-----END PRIVATE KEY-----';
  45. const app = express();
  46. app.use(logger('dev'));
  47. app.use(express.json());
  48. app.use(express.urlencoded({ extended: false }));
  49. app.use('/', (req, res, next) => {
  50. const reportLog: IReportLog = req.body;
  51. const logArray: Array<ILogItem> = reportLog.logArray.split(',').map(it => JSON.parse(decodeURIComponent(it))) as Array<ILogItem>;
  52. let logContent: Array<ILogContent> = logArray.map(it => {
  53. let logContent: ILogContent, content: string;
  54. if (it.iv && it.k && it.v) {
  55. /**
  56. * 如果加密过
  57. * 则先使用RSA解密出AES加密的key
  58. * 再使用得到的key使用AES解密得到内容
  59. */
  60. /**
  61. * if log with encryption
  62. * first, use RSA decrypt to get the AES encryption key
  63. * then, use the key do AES decrypt can get our log content
  64. */
  65. const en = new JSEncrypt();
  66. en.setPrivateKey(privateKey);
  67. const key = en.decrypt(it.k).toString(enc.Utf8);
  68. content = AES.decrypt(it.l, enc.Utf8.parse(key), { iv: enc.Utf8.parse(it.iv), mode: mode.CTR, padding: pad.NoPadding }).toString(enc.Utf8);
  69. } else {
  70. content = enc.Base64.parse(it.l).toString(enc.Utf8);
  71. }
  72. logContent = JSON.parse(content);
  73. logContent.c = decodeURIComponent(logContent.c);
  74. return logContent;
  75. });
  76. res.status(200).json(logContent);
  77. });
  78. app.listen(port, () => { console.log(`server run at ${port}`); });