修复激活时 sudo 无法从 stdin 获取密码的问题,改为管道传密并后台拉起注册工具。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
24kycj
2026-08-05 16:35:31 +08:00
parent 669ee2dcd3
commit 1d0d39995e
3 changed files with 32 additions and 9 deletions
+4 -1
View File
@@ -31,9 +31,12 @@ export function openHelp() {
export function runCmd(cmd) { export function runCmd(cmd) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.log('[runCmd] exec:', cmd)
exec(cmd, (err, stdout, stderr) => { exec(cmd, (err, stdout, stderr) => {
// 仅按退出码判定失败;Qt 等工具常向 stderr 打警告(如 QStandardPaths console.log('[runCmd] done', { err: err && (err.message || err), stdout, stderr })
if (err) reject(err.message || err) if (err) reject(err.message || err)
// sudo 失败常在 stderr 且 exit 0(后台 & 时)
else if (stderr && /sudo:\s*no password was provided/i.test(stderr)) reject(stderr.trim())
else resolve(stdout || '') else resolve(stdout || '')
}) })
}) })
+18 -5
View File
@@ -75,18 +75,31 @@ export function stopService(vm) {
} }
export function handleActive(vm) { export function handleActive(vm) {
const platform = vm.$platform || require('@/platform').default
const activePath = getActivePath(platform)
console.log('[激活] 点击前往激活, activePath=', activePath, 'appRoot=', platform.getAppRoot && platform.getAppRoot())
vm.$prompt('请输入管理员密码并点击确定以前往激活设备', '温馨提示', { vm.$prompt('请输入管理员密码并点击确定以前往激活设备', '温馨提示', {
confirmButtonText: '确定', confirmButtonText: '确定',
cancelButtonText: '取消', cancelButtonText: '取消',
inputType: 'password' inputType: 'password'
}) })
.then(({ value }) => { .then(({ value }) => {
const activePath = getActivePath(vm.$platform || require('@/platform').default) // regist.sh 内 sudo -S 从 stdin 读密码;后台 & 会导致 stdin 为空(sudo: no password was provided
vm.$runCmd(`bash "${activePath}" '${String(value).replace(/'/g, `'\\''`)}'`).catch((err) => { const pass = String(value).replace(/'/g, `'\\''`)
vm.$message({ message: err, type: 'error' }) const cmd = `( echo '${pass}' | bash ${activePath} '${pass}' ) &`
}) console.log('[激活] 确认密码, len=', (value || '').length, 'cmd=', cmd)
vm.$runCmd(cmd)
.then((res) => {
console.log('[激活] runCmd 成功', res)
})
.catch((err) => {
console.log('[激活] runCmd 失败', err)
vm.$message({ message: err, type: 'error' })
})
})
.catch((e) => {
console.log('[激活] 取消或关闭弹窗', e)
}) })
.catch(() => {})
} }
export function openWork(vm, platform) { export function openWork(vm, platform) {
+10 -3
View File
@@ -897,6 +897,7 @@ export default {
// 激活设备 // 激活设备
handleActive() { handleActive() {
let that = this let that = this
console.log('[激活] 点击前往激活, activePath=', that.activePath, 'appRoot=', platform.getAppRoot && platform.getAppRoot())
that that
.$prompt('请输入管理员密码并点击确定以前往激活设备', '温馨提示', { .$prompt('请输入管理员密码并点击确定以前往激活设备', '温馨提示', {
confirmButtonText: '确定', confirmButtonText: '确定',
@@ -904,19 +905,25 @@ export default {
inputType: 'password' inputType: 'password'
}) })
.then(({ value }) => { .then(({ value }) => {
// regist.sh 内 sudo -S 从 stdin 读密码;后台 & 会导致 stdin 为空(sudo: no password was provided
const pass = String(value).replace(/'/g, `'\\''`)
const cmd = `( echo '${pass}' | bash ${that.activePath} '${pass}' ) &`
console.log('[激活] 确认密码, len=', (value || '').length, 'cmd=', cmd)
that that
.$runCmd(`bash "${that.activePath}" '${String(value).replace(/'/g, `'\\''`)}'`) .$runCmd(cmd)
.then((res) => { .then((res) => {
console.log('[激活] runCmd 成功', res)
}) })
.catch((err) => { .catch((err) => {
console.log('[激活] runCmd 失败', err)
that.$message({ that.$message({
message: err, message: err,
type: 'error' type: 'error'
}) })
}) })
}) })
.catch(() => { .catch((e) => {
console.log('[激活] 取消或关闭弹窗', e)
}) })
}, },
// 执行 // 执行