接通双端加密语音录制与播放

This commit is contained in:
xhgit committed 2026-11-13 23:39:13 +08:00
1 parent 149028267e
commit 87676ae983
13 files changed
+217 -10

No files matched your search

+23 -1
View File
@@ -49,7 +49,7 @@ async function start() {
url.pathname.endsWith('.wasm') ? 'application/wasm' : 'application/javascript'
return new Response(bytes, { headers: {
'Content-Type': type,
'Content-Security-Policy': browserAsset ? policy : policy + "; connect-src 'self' " + api + ' ' + websocket,
'Content-Security-Policy': browserAsset ? policy : policy + "; media-src blob:; connect-src 'self' " + api + ' ' + websocket,
'X-Content-Type-Options': 'nosniff',
} })
} catch {
@@ -73,6 +73,28 @@ async function start() {
return !win.isDestroyed() && event.sender === win.webContents && frame && frame === win.webContents.mainFrame &&
frame.url.split('#')[0] === appURL
}
let microphoneUntil = 0
function voicePermission(contents: Electron.WebContents | null, url: string, mainFrame: boolean, permission: string, types: string[]) {
return !win.isDestroyed() && contents === win.webContents && mainFrame &&
url.split('#')[0] === appURL && win.webContents.getURL().startsWith(appURL + '#/chat/') &&
Date.now() <= microphoneUntil && permission === 'media' && types.length === 1 && types[0] === 'audio'
}
trusted.setPermissionCheckHandler((contents, permission, _origin, details) =>
voicePermission(contents, details.requestingUrl ?? '', details.isMainFrame, permission,
details.mediaType ? [details.mediaType] : []))
trusted.setPermissionRequestHandler((contents, permission, callback, details) =>
callback(voicePermission(contents, details.requestingUrl, details.isMainFrame, permission,
'mediaTypes' in details ? details.mediaTypes ?? [] : [])))
ipcMain.handle('voice:allow', (event) => {
if (!trustedSender(event) || !event.senderFrame?.url.startsWith(appURL + '#/chat/')) {
throw new Error('录音请求来源无效')
}
microphoneUntil = Date.now() + 30_000
})
ipcMain.handle('voice:end', (event) => {
if (!trustedSender(event)) throw new Error('录音请求来源无效')
microphoneUntil = 0
})
ipcMain.handle('device:sign', (event, username: unknown, challenge: unknown) => {
if (!trustedSender(event) || typeof username !== 'string' || typeof challenge !== 'string') {
throw new Error('设备签名请求来源无效')
+2
View File
@@ -6,6 +6,8 @@ contextBridge.exposeInMainWorld('xhchat', {
apiBase: apiArgument?.slice('--xhchat-api='.length),
signChallenge: (username: string, challenge: string) => ipcRenderer.invoke('device:sign', username, challenge),
openBrowser: (url?: string) => ipcRenderer.invoke('browser:open', url),
allowVoiceRecording: () => ipcRenderer.invoke('voice:allow'),
endVoiceRecording: () => ipcRenderer.invoke('voice:end'),
signMls: (username: string, kind: string, user: string, device: string, value: Uint8Array) =>
ipcRenderer.invoke('device:sign-mls', username, kind, user, device, value),
protectMls: (key: string) => ipcRenderer.invoke('device:protect-mls', key),