prepare-desktop-macos.mjs 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { createHash } from 'node:crypto'
  2. import { execFileSync } from 'node:child_process'
  3. import { readFileSync, readdirSync, writeFileSync } from 'node:fs'
  4. import { join } from 'node:path'
  5. // macOS local-network privacy also looks up the responsible app by Mach-O UUID.
  6. // Stock Electron binaries share that UUID across otherwise unrelated bundles.
  7. // Run once on freshly staged executables, BEFORE electron-builder signs them.
  8. // Namespace each upstream build UUID by bundle ID; leave framework/dSYM UUIDs alone.
  9. export function namespaceMachOUUID(input, bundleId) {
  10. if (!bundleId?.trim()) throw new Error('缺少 macOS 应用标识')
  11. const bytes = Buffer.from(input), patches = []
  12. function thin(start, size) {
  13. const end = start + size
  14. if (size < 32 || start < 0 || end > bytes.length || bytes.readUInt32LE(start) !== 0xfeedfacf)
  15. throw new Error('不支持或损坏的 Mach-O 64 位可执行文件')
  16. const count = bytes.readUInt32LE(start + 16), commandsEnd = start + 32 + bytes.readUInt32LE(start + 20)
  17. if (commandsEnd > end) throw new Error('Mach-O 加载命令越界')
  18. let offset = start + 32, uuidOffset
  19. for (let i = 0; i < count; i++) {
  20. if (offset + 8 > commandsEnd) throw new Error('Mach-O 加载命令不完整')
  21. const command = bytes.readUInt32LE(offset), length = bytes.readUInt32LE(offset + 4)
  22. if (length < 8 || length % 8 || offset + length > commandsEnd) throw new Error('Mach-O 加载命令长度无效')
  23. if (command === 0x1b) {
  24. if (length !== 24 || uuidOffset !== undefined) throw new Error('Mach-O LC_UUID 不唯一或无效')
  25. uuidOffset = offset + 8
  26. }
  27. offset += length
  28. }
  29. if (offset !== commandsEnd || uuidOffset === undefined) throw new Error('Mach-O 缺少有效的 LC_UUID')
  30. const uuid = createHash('sha256').update('yaoyao-macho-uuid-v1\0').update(bundleId).update('\0')
  31. .update(bytes.subarray(uuidOffset, uuidOffset + 16)).digest().subarray(0, 16)
  32. uuid[6] = (uuid[6] & 0x0f) | 0x80 // RFC 9562 version 8, application-defined UUID
  33. uuid[8] = (uuid[8] & 0x3f) | 0x80
  34. patches.push({ offset: uuidOffset, uuid })
  35. }
  36. if (bytes.length >= 8 && bytes.readUInt32BE(0) === 0xcafebabe) {
  37. const count = bytes.readUInt32BE(4), tableEnd = 8 + count * 20, ranges = []
  38. if (!count || tableEnd > bytes.length) throw new Error('Mach-O universal 架构表无效')
  39. for (let i = 0; i < count; i++) {
  40. const entry = 8 + i * 20, start = bytes.readUInt32BE(entry + 8), size = bytes.readUInt32BE(entry + 12)
  41. if (start < tableEnd || ranges.some(r => start < r.end && start + size > r.start))
  42. throw new Error('Mach-O universal 架构重叠')
  43. thin(start, size)
  44. ranges.push({ start, end: start + size })
  45. }
  46. } else thin(0, bytes.length)
  47. for (const { offset, uuid } of patches) uuid.copy(bytes, offset)
  48. return bytes
  49. }
  50. export default async function prepareMacDesktop(context) {
  51. if (context.electronPlatformName !== 'darwin') return
  52. const app = join(context.appOutDir, context.packager.appInfo.productFilename + '.app')
  53. const plist = (bundle, key) => execFileSync('/usr/libexec/PlistBuddy', ['-c', `Print :${key}`, join(bundle, 'Contents', 'Info.plist')], { encoding: 'utf8' }).trim()
  54. if (!plist(app, 'NSLocalNetworkUsageDescription')) throw new Error('应用缺少局域网权限用途说明')
  55. const frameworks = join(app, 'Contents', 'Frameworks')
  56. const bundles = [app, ...readdirSync(frameworks, { withFileTypes: true })
  57. .filter(entry => entry.isDirectory() && entry.name.endsWith('.app')).map(entry => join(frameworks, entry.name))]
  58. for (const bundle of bundles) {
  59. const id = plist(bundle, 'CFBundleIdentifier'), name = plist(bundle, 'CFBundleExecutable')
  60. if (!name || /[/\\]/.test(name)) throw new Error('应用可执行文件名称无效')
  61. const executable = join(bundle, 'Contents', 'MacOS', name)
  62. writeFileSync(executable, namespaceMachOUUID(readFileSync(executable), id))
  63. console.log(`签名前配置独立的 macOS 网络权限身份:${id}`)
  64. }
  65. }