接通 Windows 与 Web 设备间加密文字聊天

This commit is contained in:
xhgit committed 2026-11-13 18:43:04 +08:00
1 parent b04034ee89
commit d1efdedc57
27 files changed
+880 -22

No files matched your search

+135
View File
@@ -0,0 +1,135 @@
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useSession } from '../session'
import { enableChat, send, synchronize, trust } from '../chat/chat'
const session = useSession()
const route = useRoute()
const ownCode = ref('')
const code = ref('')
const selected = ref('')
const draft = ref('')
const error = ref('')
const busy = ref(false)
const view = ref<Awaited<ReturnType<typeof synchronize>>>()
const target = computed(() => typeof route.params.user === 'string' ? route.params.user : '')
const peer = computed(() => view.value?.peers.find((item) => item.id === selected.value))
const trusted = computed(() => peer.value && view.value?.pinned[peer.value.id] === peer.value.publicKey)
const changed = computed(() => peer.value && view.value?.pinned[peer.value.id] && !trusted.value)
const lines = computed(() => (view.value?.lines ?? []).filter((line) => line.peer === selected.value))
let timer: ReturnType<typeof setInterval>
async function execute(action: () => Promise<void>) {
if (busy.value || !session.identity) return
busy.value = true
error.value = ''
try {
await action()
} catch (cause) {
error.value = cause instanceof Error ? cause.message : '加密聊天暂不可用'
} finally {
busy.value = false
}
}
async function refresh() {
const identity = session.identity
if (!identity || !target.value || !ownCode.value) return
const contact = target.value
const current = await synchronize(identity, contact, session.authorized)
if (target.value !== contact) return
view.value = current
if (!current.peers.some((device) => device.id === selected.value)) selected.value = current.peers[0]?.id ?? ''
}
function enable() {
void execute(async () => {
if (!session.identity) return
ownCode.value = await enableChat(session.identity, session.authorized)
await refresh()
})
}
function confirm() {
void execute(async () => {
if (!session.identity || !peer.value) return
await trust(session.identity, target.value, peer.value, code.value)
code.value = ''
await refresh()
})
}
function submit() {
void execute(async () => {
if (!session.identity || !peer.value) return
await send(session.identity, target.value, peer.value, draft.value, session.authorized)
draft.value = ''
await refresh()
})
}
function sync() {
if (ownCode.value) void execute(refresh)
}
watch(target, () => {
view.value = undefined
selected.value = ''
code.value = ''
draft.value = ''
sync()
})
watch(() => [session.inboxChanges, session.contactChanges, session.connection], sync)
onMounted(() => { timer = setInterval(sync, 10_000) })
onBeforeUnmount(() => clearInterval(timer))
</script>
<template>
<section class="device-page">
<header class="page-header">
<h1>加密聊天</h1>
<button v-if="ownCode" class="plain" :disabled="busy" @click="sync">刷新</button>
</header>
<div class="device-content chat-page">
<p v-if="error" class="error" role="alert">{{ error }}</p>
<button v-if="!ownCode" type="button" class="primary" :disabled="busy" @click="enable">启用本机加密聊天</button>
<template v-else>
<details open>
<summary>本机安全码</summary>
<p>请通过另一可信渠道将此安全码告知对方。双方都需验证。</p>
<code class="security-code">{{ ownCode }}</code>
</details>
<template v-if="target">
<label v-if="view?.peers.length">对方设备
<select v-model="selected" :disabled="busy">
<option v-for="device in view.peers" :key="device.id" :value="device.id">{{ device.name }}</option>
</select>
</label>
<p v-else>对方设备出现后可开始验证。</p>
<p v-if="changed" class="error" role="alert">设备身份已变更,请重新核验。</p>
<p v-if="peer && !peer.mls">等待对方启用本机加密聊天。</p>
<form v-if="peer?.mls && !trusted" class="contact-preferences" @submit.prevent="confirm">
<label>对方安全码<input v-model="code" required maxlength="100" autocomplete="off"></label>
<button class="plain" :disabled="busy">核验设备</button>
</form>
<template v-if="trusted">
<p class="verified-label">设备已验证</p>
<p v-if="view?.deliveryError" class="error">{{ view.deliveryError }}</p>
<p v-if="view?.rejected" class="muted">已拒绝 {{ view.rejected }} 个无法验证的封包。</p>
<ol class="chat-lines" aria-label="消息记录" aria-live="polite">
<li v-for="line in lines" :key="line.id" :class="{ own: line.own }">
<span>{{ line.text }}</span><small v-if="view?.unsent.includes(line.id)">待发送</small>
</li>
</ol>
<form class="chat-compose" @submit.prevent="submit">
<label>消息<textarea v-model="draft" required rows="3" maxlength="4000"></textarea></label>
<button class="primary" :disabled="busy || !draft.trim()">发送</button>
</form>
</template>
</template>
<p v-else>在联系人详情打开聊天。</p>
</template>
</div>
</section>
</template>
+1
View File
@@ -115,6 +115,7 @@ async function change(action: ContactAction) {
<button v-if="detail.state === 'outgoing'" class="plain" :disabled="busy" @click="change('cancel')">取消申请</button>
<button v-if="detail.state === 'blocked'" class="plain" :disabled="busy" @click="change('unblock')">解除拉黑</button>
<form v-if="detail.state === 'friend'" class="contact-preferences" @submit.prevent="change('preferences')">
<RouterLink class="plain" :to="'/chat/' + detail.id">打开加密聊天</RouterLink>
<label>备注<input v-model="alias" maxlength="64"></label>
<label>分组<input v-model="category" maxlength="32"></label>
<button class="plain" :disabled="busy">保存</button>
+2
View File
@@ -24,6 +24,7 @@ async function act(action: () => Promise<void>) {
<main v-if="session.identity" class="workspace">
<nav class="rail" aria-label="主导航">
<RouterLink class="mark" to="/devices" aria-label="XHChat">X</RouterLink>
<RouterLink class="rail-link" to="/chat" title="消息" aria-label="消息">◇</RouterLink>
<RouterLink class="rail-link" to="/contacts" title="联系人" aria-label="联系人">人</RouterLink>
<RouterLink class="rail-link" to="/devices" title="设备管理" aria-label="设备管理">▣</RouterLink>
<button v-if="browser" type="button" class="rail-link browser-button" title="浏览器" aria-label="浏览器" @click="act(browser)">◎</button>
@@ -34,6 +35,7 @@ async function act(action: () => Promise<void>) {
<div><strong>{{ session.identity.user.username }}</strong><small>我的账号</small></div>
</div>
<nav class="panel-links" aria-label="工作区">
<RouterLink to="/chat">消息</RouterLink>
<RouterLink to="/contacts">联系人</RouterLink>
<RouterLink to="/devices">设备管理</RouterLink>
<button v-if="browser" class="plain mobile-browser" @click="act(browser)">浏览器</button>