runforgeSetZoneProbe.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * 直连 RunForge 切换跑区(与 Worker lepao.setZone 一致),用于 token 探活,不经过 runpy。
  3. */
  4. const axios = require('axios')
  5. const { URLSearchParams } = require('url')
  6. const db = require('../DataBase/db')
  7. const { dataEncrypt, dataDecrypt, dataSign } = require('./Crypto')
  8. const {
  9. getWebVpnCookieHeader,
  10. invalidateWebVpnCookie,
  11. isProbablyVpnLoginHtml,
  12. isWebVpnUnifiedAuthCredentialFailure,
  13. markLepaoUnifiedAuthFailed
  14. } = require('../../lib/Lepao/webvpnCookie')
  15. const { withLepaoAccountProxy } = require('../../lib/Lepao/lepaoOutboundProxy')
  16. const BASE_URL = 'https://lepao.ctbu.edu.cn/v3/api.php'
  17. const DEFAULT_UA =
  18. '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'
  19. const runZoneMap = {
  20. 兰花湖校区跑区: 2,
  21. 主校区北跑区: 3,
  22. 主校区南跑区: 5,
  23. 重庆工商大学茶园校区: 6
  24. }
  25. function lepaoTimestamp() {
  26. return Number((Date.now() / 1000).toFixed(3))
  27. }
  28. /**
  29. * @param {object} p
  30. * @param {string} p.uid
  31. * @param {string} p.token
  32. * @param {string|number} p.school_id
  33. * @param {string} p.student_num
  34. * @param {number} [p.random_id=1] path_data.id,需存在且 run_zone_name 可映射
  35. * @param {string} [p.userAgent]
  36. * @param {string} [p.create_user] 绑定用户 uuid,用于取 WebVPN Cookie(教务登录名与学号 student_num 一致)
  37. * @returns {Promise<object>} RunForge 响应体(与 Worker request 解密后形态一致)
  38. */
  39. async function probeSetZone(p) {
  40. const {
  41. uid,
  42. token,
  43. school_id,
  44. student_num,
  45. random_id = 1,
  46. userAgent,
  47. create_user: createUser
  48. } = p
  49. const record = await db.query('SELECT run_zone_name FROM path_data WHERE id = ?', [random_id])
  50. if (!record || record.length === 0) {
  51. throw new Error('跑区不存在')
  52. }
  53. const runZoneId = runZoneMap[record[0].run_zone_name]
  54. if (!runZoneId) throw new Error('跑区不存在')
  55. const raw = {
  56. uid,
  57. token,
  58. school_id,
  59. term_id: 0,
  60. course_id: 0,
  61. class_id: 0,
  62. student_num,
  63. card_id: student_num,
  64. timestamp: lepaoTimestamp(),
  65. version: 1,
  66. nonce: String(Math.floor(Math.random() * 900000 + 100000)),
  67. ostype: 5,
  68. run_zone_id: String(runZoneId)
  69. }
  70. raw.sign = dataSign(raw)
  71. const form = new URLSearchParams()
  72. form.append('ostype', '5')
  73. form.append('data', dataEncrypt(JSON.stringify(raw)))
  74. let webvpnCookie
  75. if (createUser) {
  76. try {
  77. webvpnCookie = await getWebVpnCookieHeader(createUser, student_num, {
  78. skipPostWebVpnLepaoSync: true
  79. })
  80. } catch (e) {
  81. if (isWebVpnUnifiedAuthCredentialFailure(e)) {
  82. try {
  83. await markLepaoUnifiedAuthFailed(student_num, createUser, student_num)
  84. } catch (_) {
  85. /* ignore */
  86. }
  87. }
  88. throw new Error(e.message || 'WebVPN 登录失败')
  89. }
  90. }
  91. const buildHeaders = () => ({
  92. 'Content-Type': 'application/x-www-form-urlencoded',
  93. Accept: '*/*',
  94. 'Accept-Language': 'zh-CN,zh-Hans;q=0.9',
  95. 'User-Agent': userAgent || DEFAULT_UA,
  96. Referer: 'https://servicewechat.com/wxf94c4ddb63d87ede/32/page-frame.html',
  97. charset: 'utf-8',
  98. ...(webvpnCookie ? { Cookie: webvpnCookie } : {})
  99. })
  100. const postOnce = async () => {
  101. const execPost = (extraAxios) =>
  102. axios.post(`${BASE_URL}/Run/setRunZone`, form, {
  103. headers: buildHeaders(),
  104. timeout: 20000,
  105. ...extraAxios,
  106. responseType: 'text',
  107. transformResponse: [(b) => b]
  108. })
  109. return withLepaoAccountProxy(execPost, {
  110. createUserUuid: createUser || null,
  111. studentNum: student_num || null,
  112. debugAxiosOpts: { proxy: false },
  113. logger: undefined
  114. })
  115. }
  116. let res = await postOnce()
  117. let result
  118. try {
  119. result = JSON.parse(res.data)
  120. } catch {
  121. result = res.data
  122. }
  123. if (
  124. typeof result === 'string' &&
  125. isProbablyVpnLoginHtml(result) &&
  126. createUser
  127. ) {
  128. await invalidateWebVpnCookie(createUser, student_num)
  129. try {
  130. webvpnCookie = await getWebVpnCookieHeader(createUser, student_num, {
  131. skipCache: true,
  132. skipPostWebVpnLepaoSync: true
  133. })
  134. } catch (vpnRefreshErr) {
  135. if (isWebVpnUnifiedAuthCredentialFailure(vpnRefreshErr)) {
  136. try {
  137. await markLepaoUnifiedAuthFailed(student_num, createUser, student_num)
  138. } catch (_) {
  139. /* ignore */
  140. }
  141. }
  142. throw new Error(vpnRefreshErr.message || 'WebVPN 登录失败')
  143. }
  144. res = await postOnce()
  145. try {
  146. result = JSON.parse(res.data)
  147. } catch {
  148. result = res.data
  149. }
  150. }
  151. if (result?.data && result?.is_encrypt === 1) {
  152. result = { ...result, data: JSON.parse(dataDecrypt(result.data)) }
  153. }
  154. return result
  155. }
  156. function isProbeSetZoneOk(result) {
  157. if (!result) return false
  158. const hasData = result.data != null && result.data !== ''
  159. if (!hasData) return false
  160. if (Number(result.status) === 1) return true
  161. const cd = Number(result.code)
  162. if (cd === 1 || cd === 200) return true
  163. return false
  164. }
  165. function getProbeFailMessage(result) {
  166. if (!result) return ''
  167. return (
  168. result.info ||
  169. result.msg ||
  170. result.message ||
  171. (typeof result.data === 'object' && result.data
  172. ? result.data.info || result.data.msg || result.data.message
  173. : '') ||
  174. ''
  175. )
  176. }
  177. module.exports = {
  178. probeSetZone,
  179. isProbeSetZoneOk,
  180. getProbeFailMessage,
  181. BASE_URL
  182. }