feat(mobile): add local QR scanning and desktop pairing invitations

This commit is contained in:
yilsem committed 2026-09-08 21:28:24 +03:00
1 parent c1929957a2
commit 4115f1b4c0
17 files changed
+178 -17

No files matched your search

+23 -2
View File
@@ -1,6 +1,8 @@
import { AccountClient, openPhoneConnection } from './transport.mjs'
import { VoiceSession } from './voice.mjs'
import { taskState } from './behavior.mjs'
import { parseInvite } from './invite.mjs'
import { InviteScanner } from './qr.mjs'
const $ = id => document.getElementById(id)
if (globalThis.Capacitor?.isNativePlatform()) globalThis.Capacitor.registerPlugin('MobileSecrets')
let credentials = {}, account, connection, selected, approval, voice, pendingTask, usageTimer, voiceGeneration = 0
@@ -8,6 +10,23 @@ let connectedDesktopId = null
let connectionEpoch = 0, selectionEpoch = 0
const status = text => { $('status').textContent = text }
const native = () => window.Capacitor?.Plugins?.MobileSecrets
const scanner = new InviteScanner($('qr-video'), async raw => {
if (!account || credentials.deviceId) throw new Error('Önce giriş yapın; kayıtlı cihaz varsa önce mevcut eşleşmeyi kontrol edin.')
const targetAccount = account, epoch = connectionEpoch
const invite = parseInvite(raw)
const result = await account.call(`/v1/aiturk/mobile/invites/${invite.inviteId}/claim`, {
secret: invite.secret, display_name: $('device-name').value.trim() })
if (account !== targetAccount || epoch !== connectionEpoch) return
Object.assign(credentials, { deviceId: result.device_id, deviceToken: result.device_token,
pairingId: result.pairing_id, preferredDesktopId: invite.desktopId })
await saveCredentials()
$('pair-code').textContent = `Bilgisayarda bekleyen telefonu getirip şu kodu onaylayın: ${result.user_code}`
status('QR okundu. Bilgisayardaki açık telefon onayı bekleniyor.')
})
scanner.onError = error => status(error.message)
action('scan-invite', async () => { if (!account || credentials.deviceId) throw new Error('Önce giriş yapın; bu telefon henüz eşleştirilmemiş olmalı.'); await scanner.start() })
action('stop-scan', async () => scanner.stop())
$('qr-file').onchange = async event => { try { await scanner.image(event.target.files[0]) } catch (error) { status(error.message) } finally { event.target.value = '' } }
async function saveCredentials() { if (native()) await native().save({ value: JSON.stringify(credentials) }) }
function addText(container, role, text) { const p = document.createElement('p'); p.textContent = `${role}: ${text}`; $(container).append(p); while ($(container).children.length > 150) $(container).firstChild.remove() }
function action(id, work) { $(id).onclick = async () => { $(id).disabled = true; try { await work() } catch (error) { status(error.message) } finally { $(id).disabled = false } } }
@@ -41,6 +60,7 @@ action('login-start', async () => {
status('Tarayıcıda giriş yaptıktan sonra buraya dönüp girişi kontrol edin.')
})
action('login-check', async () => {
if (credentials.accountToken) throw new Error('Hesaba zaten giriş yapılmış; mevcut hesap değiştirilmeyecek.')
if (!credentials.pendingLogin) throw new Error('Önce giriş başlatın.')
const result = await new AccountClient({ accountToken: credentials.pendingLogin.deviceCode }).call('/v1/aiturk/mobile-auth/poll', {})
if (result.state !== 'approved') { status('Tarayıcıdaki kod onayı bekleniyor.'); return }
@@ -49,6 +69,7 @@ action('login-check', async () => {
status('Hesap bağlı. Şimdi telefonun eşleştirme kodunu oluşturun.')
})
action('logout', async () => {
scanner.stop()
const token = credentials.accountToken || credentials.pendingLogin?.deviceCode
// Revoke the app-specific credential before dropping the local copy. A
// network failure leaves it available for an explicit retry.
@@ -166,6 +187,6 @@ action('history', async () => { const result = await rpc('session.history', { se
action('clear-history', async () => { $('discussion').replaceChildren(); $('result').replaceChildren(); status('Telefondaki görünür metin temizlendi. Google bağlantı bağlamını da temizlemek için görüşmeyi kapatın.') })
action('disconnect', async () => { connectionEpoch++; stopVoice(); connection?.close(); connection = null; selected = null; $('work').hidden = true; status('Telefon bağlantısı kapalı. IDE işi devam edebilir.') })
action('revoke', async () => { const data = await account.call('/v1/aiturk/mobile/devices'); const self = data.devices.find(d => d.self); await account.call(`/v1/aiturk/devices/${self.id}/revoke`, { expected_version: self.version }); stopVoice(); connection?.close(); connection = null; credentials = {}; await saveCredentials(); account = null; $('work').hidden = true; status('Bu telefonun erişimi iptal edildi.') })
document.addEventListener('visibilitychange', () => { if (document.hidden) stopVoice() })
window.addEventListener('pagehide', () => { stopVoice(); connection?.close() })
document.addEventListener('visibilitychange', () => { if (document.hidden) { stopVoice(); scanner.stop() } })
window.addEventListener('pagehide', () => { stopVoice(); scanner.stop(); connection?.close() })
try { const saved = await native()?.load(); if (saved?.value) { credentials = JSON.parse(saved.value); if (credentials.accountToken) { account = new AccountClient(credentials); status('Kayıtlı cihaz bulundu. Bilgisayarları yenileyerek bağlanın.') } } } catch { status('Android güvenli kasası açılamadı. Hesabınızı yeniden eşleştirin.') }
+5 -1
View File
@@ -13,6 +13,10 @@
<label>AITURK hesap API anahtarı<input id="account-token" type="password" autocomplete="off" autocapitalize="none" spellcheck="false"></label>
</details>
<label>Telefon adı<input id="device-name" maxlength="80" value="Android telefonum"></label><button id="pair">Eşleştirme kodu oluştur</button>
<p>Bilgisayarda “Telefon için QR oluştur” seçin. Giriş yaptıktan sonra buradan tarayın. Görüntü sunucuya yüklenmez.</p>
<button id="scan-invite">Bilgisayarın QR kodunu tara</button><button id="stop-scan" class="secondary">Kamerayı kapat</button>
<video id="qr-video" hidden muted playsinline aria-label="QR kamera önizlemesi"></video>
<label>Alternatif: QR resmi seç<input id="qr-file" type="file" accept="image/*"></label>
<pre id="pair-code" aria-live="polite"></pre><p>Kodu bilgisayardaki AITURK Mobil bağlantı penceresinde onaylayın. Eşleştirme kimliğini kopyalayabilirsiniz.</p>
<button id="devices" class="secondary">Onaydan sonra bilgisayarları bul</button>
<label>Bilgisayar<select id="desktop"><option value="">Önce cihazları bulun</option></select></label><button id="connect">Bilgisayara bağlan</button>
@@ -39,4 +43,4 @@
<button id="revoke" class="danger">Bu telefonun erişimini iptal et</button>
</section><hr><p>Ses kaydı saklanmaz. Google görüşme metni yalnız bu ekranın belleğinde kalır. IDE'ye gönderilen görev IDE geçmişinde saklanır. Ekrandan çıkmak mikrofonu kapatır; IDE işini durdurmaz.</p>
<p><a href="privacy.html">Veri akışı ve geliştirme sürümünün sınırları</a></p>
</main><script src="capacitor.js"></script><script type="module" src="app.mjs"></script></body></html>
</main><script src="capacitor.js"></script><script src="vendor/jsQR.js"></script><script type="module" src="app.mjs"></script></body></html>
+11
View File
@@ -0,0 +1,11 @@
export function parseInvite(raw, now = Date.now()) {
if (typeof raw !== 'string' || raw.length > 800) throw new Error('Bu bir AITURK eşleştirme QR kodu değil.')
let data
try { data = JSON.parse(raw) } catch { throw new Error('Bu bir AITURK eşleştirme QR kodu değil.') }
if (!data || data.kind !== 'aiturk-mobile-invite' || data.version !== 1 ||
!/^[0-9a-f-]{36}$/.test(data.inviteId) || !/^[0-9a-f-]{36}$/.test(data.desktopId) ||
!/^[A-Za-z0-9_-]{43}$/.test(data.secret) || !Number.isFinite(Date.parse(data.expiresAt)) ||
Date.parse(data.expiresAt) <= now || Date.parse(data.expiresAt) > now + 11 * 60000)
throw new Error('QR geçersiz veya süresi dolmuş. Bilgisayarda yeni kod oluşturun.')
return { inviteId: data.inviteId, desktopId: data.desktopId, secret: data.secret }
}
+50
View File
@@ -0,0 +1,50 @@
import qrcode from './vendor/qrcode.mjs'
export function drawQr(canvas, text) {
const qr = qrcode(0, 'M'); qr.addData(text); qr.make()
const count = qr.getModuleCount(), scale = 5, margin = 4
canvas.width = canvas.height = (count + margin * 2) * scale
const ctx = canvas.getContext('2d'); ctx.fillStyle = '#fff'; ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = '#000'
for (let y = 0; y < count; y++) for (let x = 0; x < count; x++)
if (qr.isDark(y, x)) ctx.fillRect((x + margin) * scale, (y + margin) * scale, scale, scale)
}
export class InviteScanner {
constructor(video, onCode) { this.video = video; this.onCode = onCode; this.epoch = 0; this.canvas = document.createElement('canvas') }
decode(source, width, height) {
if (!width || !height) return null
const scale = Math.min(1, 1280 / Math.max(width, height))
this.canvas.width = Math.round(width * scale); this.canvas.height = Math.round(height * scale)
const ctx = this.canvas.getContext('2d', { willReadFrequently: true })
ctx.drawImage(source, 0, 0, this.canvas.width, this.canvas.height)
const pixels = ctx.getImageData(0, 0, this.canvas.width, this.canvas.height)
return globalThis.jsQR(pixels.data, pixels.width, pixels.height)?.data
}
async image(file) {
this.stop(); const epoch = this.epoch
if (!file || file.size > 8 * 1024 * 1024) throw new Error('QR resmi en fazla 8 MB olabilir.')
const bitmap = await createImageBitmap(file)
let result
try { result = this.decode(bitmap, bitmap.width, bitmap.height) } finally { bitmap.close() }
if (epoch !== this.epoch) return
if (!result) throw new Error('Resimde QR okunamadı; daha net bir görüntü deneyin.')
await this.onCode(result)
}
async start() {
this.stop(); const epoch = this.epoch
const stream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' }, audio: false })
if (epoch !== this.epoch) { stream.getTracks().forEach(t => t.stop()); return }
this.stream = stream; this.video.srcObject = stream; this.video.hidden = false
try {
await this.video.play()
if (epoch !== this.epoch) return
this.timer = setInterval(() => {
const result = this.decode(this.video, this.video.videoWidth, this.video.videoHeight)
if (result) { this.stop(); void this.onCode(result).catch(error => this.onError?.(error)) }
}, 250)
this.limit = setTimeout(() => this.stop(), 60000)
} catch (error) { this.stop(); throw error }
}
stop() { this.epoch++; clearInterval(this.timer); clearTimeout(this.limit); this.stream?.getTracks().forEach(t => t.stop()); this.stream = null; this.video.srcObject = null; this.video.hidden = true }
}
+1
View File
@@ -8,3 +8,4 @@ button.secondary { background:var(--surface); color:var(--text); border:1px soli
:focus-visible { outline:3px solid var(--accent); outline-offset:4px } a { color:var(--accent) } hr { border:0; border-top:1px solid var(--stroke); margin:30px 0 } pre { white-space:pre-wrap; word-break:break-word; font:inherit } [hidden] { display:none!important }
.status { border-left:3px solid var(--accent); padding:10px 14px; color:var(--text); background:var(--surface) } .row { display:flex; align-items:center; flex-wrap:wrap; gap:4px } .row button { flex:1 } .history { max-height:340px; overflow:auto; padding:8px 0 } .history p { white-space:pre-wrap; overflow-wrap:anywhere } small { color:var(--muted) } input[type=checkbox] { width:24px; height:24px; vertical-align:middle; margin:0 8px 0 0 }
@media(prefers-reduced-motion:no-preference) { button { transition:background-color .1s } }
canvas,video{max-width:100%;height:auto}video{border-radius:12px}canvas{image-rendering:pixelated}