// Message signing ("WAA" / BotGuard). Google Voice's web client attaches a // signature, computed by an obfuscated script Google serves, to every text it // sends; sends without one work but get rate-limited sooner. Like // mautrix-gvoice's electron.mjs, we run Google's script in a hidden window, // locked down so it can't load anything else, and ask it for signatures. // // Everything here is best-effort: on any failure the send goes out unsigned. import { BrowserWindow, session } from 'electron' import crypto from 'node:crypto' import { USER_AGENT, type GVoice } from './client' const EXPIRY = 60 * 60_000 const TIMEOUT = 10_000 const ABOUT_URLS = new Set(['https://voice.google.com/', 'https://voice.google.com/about', 'https://voice.google.com/u/0/about']) // Runs inside the hidden page. const LOAD_SCRIPT = (src: string): Promise => new Promise((resolve, reject) => { const s = document.createElement('script') s.src = src s.onload = () => resolve() s.onerror = () => reject(new Error('script failed to load')) document.head.appendChild(s) }) type Payload = { message_ids: string; destinations: string; thread_id: string } | undefined const RUN_PROGRAM = (globalName: string, program: string, payload: Payload): Promise => new Promise((resolve, reject) => { const vm = (window as unknown as Record void }>)[globalName] if (!vm) return reject(new Error('signer not loaded')) vm.a( program, (fn1: (cb: (r: string) => void, args: unknown[]) => void) => fn1((r) => resolve(r), [payload, undefined, undefined, undefined]), true, undefined, () => undefined ) }) const b64sha256 = (s: string): string => crypto.createHash('sha256').update(s).digest('base64') const withTimeout = (p: Promise, ms: number): Promise => Promise.race([p, new Promise((_, reject) => setTimeout(() => reject(new Error('timed out')), ms))]) export class WaaSigner { private win?: BrowserWindow private ready?: Promise private initedAt = 0 private program = '' private globalName = '' private scriptUrl = '' constructor(private gv: GVoice) {} private async open(): Promise { if (this.win && !this.win.isDestroyed()) return this.win const ses = session.fromPartition('gvoice-waa') // in-memory, no Google cookies ses.webRequest.onBeforeRequest((d, cb) => cb({ cancel: !(ABOUT_URLS.has(d.url) || d.url === this.scriptUrl) })) ses.webRequest.onHeadersReceived((d, cb) => { const headers = { ...d.responseHeaders } for (const k of Object.keys(headers)) if (k.toLowerCase() === 'content-security-policy') delete headers[k] cb({ responseHeaders: headers }) }) const win = new BrowserWindow({ show: false, webPreferences: { session: ses, sandbox: true, contextIsolation: true, nodeIntegration: false } }) await win.loadURL('https://voice.google.com/about', { userAgent: USER_AGENT }) this.win = win return win } private init(): Promise { this.ready = (async () => { const waa = await this.gv.createWaa() const win = await this.open() this.program = waa.program this.globalName = waa.globalName const url = waa.interpreterUrl.startsWith('//') ? 'https:' + waa.interpreterUrl : waa.interpreterUrl if (url !== this.scriptUrl) { this.scriptUrl = url await win.webContents.executeJavaScript(`(${LOAD_SCRIPT.toString()})(${JSON.stringify(url)})`) } this.initedAt = Date.now() // The web client checks in once with an empty payload. void this.run(undefined) .then((sig) => this.gv.pingWaa(sig)) .catch(() => undefined) })() this.ready.catch(() => (this.ready = undefined)) return this.ready } private async run(payload: Payload): Promise { const js = `(${RUN_PROGRAM.toString()})(${JSON.stringify(this.globalName)}, ${JSON.stringify(this.program)}, ${JSON.stringify(payload)})` return withTimeout(this.win!.webContents.executeJavaScript(js) as Promise, TIMEOUT) } // Signature for a send, or undefined if one couldn't be had. async sign(threadId: string, recipients: string[], transactionId: number): Promise { try { if (!this.ready || Date.now() - this.initedAt > EXPIRY) await this.init() else await this.ready const sig = await this.run({ message_ids: b64sha256(String(transactionId)), destinations: b64sha256(recipients.join('|')), thread_id: b64sha256(threadId) }) return sig || undefined } catch (e) { console.warn('[gvoice] signing failed, sending unsigned:', (e as Error).message) return undefined } } close(): void { if (this.win && !this.win.isDestroyed()) this.win.destroy() this.win = undefined this.ready = undefined } }