Config.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const API = require('../../../../lib/API.js')
  2. const { BaseStdResponse } = require('../../../../BaseStdResponse.js')
  3. const AccessControl = require('../../../../lib/AccessControl.js')
  4. const db = require('../../../../plugin/DataBase/db.js')
  5. const Redis = require('../../../../plugin/DataBase/Redis.js')
  6. const QgProxyManager = require('../../../../lib/Lepao/QgProxyManager')
  7. class AdminLepaoProxyConfig extends API {
  8. constructor() {
  9. super()
  10. this.setPath('/Admin/Lepao/Proxy/Config')
  11. this.setMethod('POST')
  12. }
  13. async onRequest(req, res) {
  14. const {
  15. uuid,
  16. session,
  17. proxy_enabled,
  18. area,
  19. area_ex,
  20. isp,
  21. distinct_extract,
  22. invalidate_cache
  23. } = req.body
  24. if ([uuid, session].some(v => v === '' || v == null) || proxy_enabled === undefined || proxy_enabled === null)
  25. return res.json({ ...BaseStdResponse.MISSING_PARAMETER })
  26. if (!await AccessControl.checkSession(uuid, session))
  27. return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED })
  28. const permission = await AccessControl.getPermission(uuid)
  29. if (!permission.includes('admin') && !permission.includes('service'))
  30. return res.json({ ...BaseStdResponse.PERMISSION_DENIED })
  31. const enabled =
  32. proxy_enabled === true || proxy_enabled === 1 || proxy_enabled === '1' ? 1 : 0
  33. const areaStr = area == null ? '' : String(area).trim()
  34. const areaExStr = area_ex == null ? '' : String(area_ex).trim()
  35. let ispVal = null
  36. if (isp !== '' && isp !== undefined && isp !== null) {
  37. const n = Number(isp)
  38. if (n === 1 || n === 2 || n === 3) ispVal = n
  39. }
  40. const distinct = Number(distinct_extract) === 0 ? 0 : 1
  41. const now = Date.now()
  42. try {
  43. await QgProxyManager.ensureSettingsRow()
  44. await db.query(
  45. `UPDATE lepao_proxy_settings SET proxy_enabled = ?, area = ?, area_ex = ?, isp = ?, distinct_extract = ?, updated_at = ? WHERE id = 1`,
  46. [enabled, areaStr, areaExStr, ispVal, distinct, now]
  47. )
  48. await db.query(
  49. `INSERT INTO lepao_proxy_project_settings (scope_key, proxy_enabled, updated_at)
  50. VALUES (?, ?, ?)
  51. ON DUPLICATE KEY UPDATE proxy_enabled = VALUES(proxy_enabled), updated_at = VALUES(updated_at)`,
  52. [QgProxyManager.getProjectKey(), enabled, now]
  53. )
  54. if (invalidate_cache === true || invalidate_cache === 1 || invalidate_cache === '1') {
  55. await Redis.del(QgProxyManager.REDIS_CURRENT)
  56. }
  57. await QgProxyManager.recordLog({
  58. event: 'config_change',
  59. detail: {
  60. proxy_enabled: enabled,
  61. project_scope_key: QgProxyManager.getProjectKey(),
  62. area: areaStr,
  63. area_ex: areaExStr,
  64. isp: ispVal,
  65. distinct_extract: distinct,
  66. invalidate_cache: !!invalidate_cache,
  67. operator: uuid
  68. }
  69. })
  70. return res.json({ ...BaseStdResponse.OK })
  71. } catch (e) {
  72. this.logger?.error?.(`AdminLepaoProxyConfig: ${e.stack || e}`)
  73. return res.json({ ...BaseStdResponse.ERR, msg: '保存配置失败' })
  74. }
  75. }
  76. }
  77. module.exports.AdminLepaoProxyConfig = AdminLepaoProxyConfig