runforgeSetZoneProbe.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * 直连 RunForge 切换跑区(与 Worker lepao.setZone 一致),用于 token 探活,不经过 runpy。
  3. */
  4. const { URLSearchParams } = require('url')
  5. const { postLepaoSchool } = require('../../lib/Lepao/lepaoSchoolHttp')
  6. const db = require('../DataBase/db')
  7. const { dataEncrypt, dataDecrypt, dataSign } = require('./Crypto')
  8. const BASE_URL = 'https://lepao.ctbu.edu.cn/v3/api.php'
  9. const DEFAULT_UA =
  10. 'Mozilla/5.0 (Linux; Android 16; 2211133C Build/BP2A.250605.031.A3; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/138.0.7204.180 Mobile Safari/537.36 XWEB/1380347 MMWEBSDK/20250202 MMWEBID/1020 wxwork/5.0.6.66174 MicroMessenger/8.0.28.48(0x28001c30) MiniProgramEnv/android Luggage/3.0.2.95ef3f83 NetType/WIFI Language/zh_CN ABI/arm64'
  11. const runZoneMap = {
  12. 兰花湖校区跑区: 2,
  13. 主校区北跑区: 3,
  14. 主校区南跑区: 5,
  15. 重庆工商大学茶园校区: 6
  16. }
  17. function lepaoTimestamp() {
  18. return Number((Date.now() / 1000).toFixed(3))
  19. }
  20. /**
  21. * @param {object} p
  22. * @param {string} p.uid
  23. * @param {string} p.token
  24. * @param {string|number} p.school_id
  25. * @param {string} p.student_num
  26. * @param {number} [p.random_id=1] path_data.id,需存在且 run_zone_name 可映射
  27. * @param {string} [p.userAgent]
  28. * @returns {Promise<object>} RunForge 响应体(与 Worker request 解密后形态一致)
  29. */
  30. async function probeSetZone(p) {
  31. const { uid, token, school_id, student_num, random_id = 1, userAgent } = p
  32. const record = await db.query('SELECT run_zone_name FROM path_data WHERE id = ?', [random_id])
  33. if (!record || record.length === 0) {
  34. throw new Error('跑区不存在')
  35. }
  36. const runZoneId = runZoneMap[record[0].run_zone_name]
  37. if (!runZoneId) throw new Error('跑区不存在')
  38. const raw = {
  39. uid,
  40. token,
  41. school_id,
  42. term_id: 0,
  43. course_id: 0,
  44. class_id: 0,
  45. student_num,
  46. card_id: student_num,
  47. timestamp: lepaoTimestamp(),
  48. version: 1,
  49. nonce: String(Math.floor(Math.random() * 900000 + 100000)),
  50. ostype: 5,
  51. run_zone_id: String(runZoneId)
  52. }
  53. raw.sign = dataSign(raw)
  54. const form = new URLSearchParams()
  55. form.append('ostype', '5')
  56. form.append('data', dataEncrypt(JSON.stringify(raw)))
  57. const res = await postLepaoSchool(`${BASE_URL}/Run/setRunZone`, form, {
  58. headers: {
  59. 'Content-Type': 'application/x-www-form-urlencoded',
  60. Accept: '*/*',
  61. 'Accept-Language': 'zh-CN,zh-Hans;q=0.9',
  62. 'User-Agent': userAgent || DEFAULT_UA,
  63. Referer: 'https://servicewechat.com/wxf94c4ddb63d87ede/32/page-frame.html',
  64. charset: 'utf-8'
  65. },
  66. timeout: 20000
  67. })
  68. let result = res.data
  69. if (result?.data && result?.is_encrypt === 1) {
  70. result = { ...result, data: JSON.parse(dataDecrypt(result.data)) }
  71. }
  72. return result
  73. }
  74. function isProbeSetZoneOk(result) {
  75. if (!result) return false
  76. const hasData = result.data != null && result.data !== ''
  77. if (!hasData) return false
  78. if (Number(result.status) === 1) return true
  79. const cd = Number(result.code)
  80. if (cd === 1 || cd === 200) return true
  81. return false
  82. }
  83. function getProbeFailMessage(result) {
  84. if (!result) return ''
  85. return (
  86. result.info ||
  87. result.msg ||
  88. result.message ||
  89. (typeof result.data === 'object' && result.data
  90. ? result.data.info || result.data.msg || result.data.message
  91. : '') ||
  92. ''
  93. )
  94. }
  95. module.exports = {
  96. probeSetZone,
  97. isProbeSetZoneOk,
  98. getProbeFailMessage,
  99. BASE_URL
  100. }