- domain/rules: 计分与排名解耦、破纪录按组别、tieBreak 生效、调整分不丢、移除 aggregate 假能力 - realtime: 新建 internal/dto,快照三项契约对齐,/ws 只认会话身份 - httpapi: 流水幂等、standings 整表载荷、广播接线、强制改密、XFF 防伪造、records 写入、审计、赛事包内容、发布前备份、result.points、实例列表 - web: FormData 上传、YAML 解析、快照归一化、大屏解析层、bootstrap 降级、访客判定、主题切换 - console: 优雅关闭握手、中文化、免验证改管理员账号、选屏与日志轮转修复 - docs: 用户文档三份 + README - 主代理: 破纪录加分接线(levels 未加载导致恒 0)
78 lines
2.6 KiB
JavaScript
78 lines
2.6 KiB
JavaScript
'use strict'
|
|
|
|
const path = require('path')
|
|
const { spawn } = require('child_process')
|
|
|
|
// 用户名 / 密码长度上限:passwd 自己也会限制,这里只是把明显不合法的挡在 UI 侧
|
|
const FIELD_MAX = 64
|
|
|
|
// 返回错误文案,null 表示通过。required 为 false 时允许空串(= 不改 / 随机生成)
|
|
function validateAccountField(value, label, required) {
|
|
if (typeof value !== 'string') return `${label}必须是文本`
|
|
if (value === '') return required ? `${label}不能为空` : null
|
|
// spawn 用参数数组、不经 shell,不存在命令注入;但空格会把参数拆成两段、
|
|
// 换行会截断输出,所以在入口直接拒掉,别让 passwd 报看不懂的错。
|
|
if (/\s/.test(value)) return `${label}不能包含空格、制表符或换行`
|
|
if (value.length > FIELD_MAX) return `${label}最多 ${FIELD_MAX} 个字符`
|
|
return null
|
|
}
|
|
|
|
// 空的新用户名 / 空密码直接传空串:passwd 的语义就是「留空则不改 / 随机生成」
|
|
function buildPasswdArgs({ configArg, currentUsername, newUsername, password }) {
|
|
return [
|
|
'passwd',
|
|
'-config', configArg,
|
|
'-user', currentUsername,
|
|
'-new-username', newUsername,
|
|
'-password', password
|
|
]
|
|
}
|
|
|
|
// 跑一次 passwd,实时把 stdout/stderr 交给 onOutput,结束时返回完整输出与退出码
|
|
function runPasswd({ exePath, args, onOutput }) {
|
|
return new Promise((resolve) => {
|
|
let child
|
|
try {
|
|
// cwd 取 exe 所在目录:config.yaml 与 data/ 都跟 exe 平级
|
|
child = spawn(exePath, args, {
|
|
cwd: path.dirname(exePath),
|
|
windowsHide: true,
|
|
stdio: ['ignore', 'pipe', 'pipe']
|
|
})
|
|
} catch (err) {
|
|
resolve({ ok: false, error: `启动 passwd 失败:${err.message}`, output: '' })
|
|
return
|
|
}
|
|
|
|
let output = ''
|
|
let settled = false
|
|
const finish = (result) => {
|
|
if (settled) return
|
|
settled = true
|
|
resolve(result)
|
|
}
|
|
const onChunk = (chunk) => {
|
|
const text = String(chunk)
|
|
output += text
|
|
if (typeof onOutput === 'function') onOutput(text)
|
|
}
|
|
|
|
if (child.stdout) {
|
|
child.stdout.setEncoding('utf8')
|
|
child.stdout.on('data', onChunk)
|
|
}
|
|
if (child.stderr) {
|
|
child.stderr.setEncoding('utf8')
|
|
child.stderr.on('data', onChunk)
|
|
}
|
|
|
|
child.on('error', (err) => finish({ ok: false, error: err.message, output }))
|
|
// 用 close 而不是 exit:等两个 stdio 都排空,输出才是完整的
|
|
child.on('close', (code, signal) => {
|
|
finish({ ok: code === 0, exitCode: code, signal: signal || null, output })
|
|
})
|
|
})
|
|
}
|
|
|
|
module.exports = { FIELD_MAX, validateAccountField, buildPasswdArgs, runPasswd }
|