mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-24 04:19:48 +01:00
配置注释与测试页面跟上认证改动
吊销时刻的注释原来写成改大之后设备会自动换发新凭证,实际是线之前的 凭证一律拒,没有激活码就换不到新的,全网设备会一起下线,注释改成 真实语义并提醒先备好激活码。 测试页面加上激活码输入,设备地址统一去掉分隔符后使用,让 OTA 请求头和 websocket 子协议是同一个值,签名才对得上。客户端地址的占位提示原来 说"需与设备上报的一致",实际服务端只比对设备地址,改成如实说明。
This commit is contained in:
parent
ed4bbff6cb
commit
83149d3a00
6 changed files with 65 additions and 20 deletions
|
|
@ -30,6 +30,17 @@ python start.py
|
|||
- 事件桥地址:ws://127.0.0.1:8006/wakeword-ws
|
||||
- 健康检查:http://127.0.0.1:8006/health
|
||||
|
||||
# 开启认证后的用法
|
||||
|
||||
服务端 `config.yaml` 里 `server.auth.enabled` 设为 `true` 时,本页面需要额外的凭证才能连上:
|
||||
|
||||
1. **激活码**:向服务端运维要一个码,填进页面的「激活码」输入框。页面请求 OTA 时带上它,服务端换发一次 websocket token,用掉即失效。
|
||||
2. **设备MAC**:填无分隔符或带冒号的 12 位 MAC 都行,页面会统一去掉分隔符转小写后再发给服务端。OTA 请求头和 websocket 子协议用的是同一个值,签名才对得上。
|
||||
|
||||
websocket 的凭证走子协议头而不是 URL:`Sec-WebSocket-Protocol: xiaozhi, t-<token>, d-<deviceId>, c-<clientId>`。浏览器不能自定义 websocket 请求头,服务端握手时只回显 `xiaozhi`。放进 URL 查询串的凭证不再被采信,理由是查询串会原样落进反代访问日志。
|
||||
|
||||
不开启认证时以上都不用填,行为与之前一致。
|
||||
|
||||
# 目录说明
|
||||
|
||||
- `start.py`:模块启动入口
|
||||
|
|
|
|||
|
|
@ -155,8 +155,14 @@
|
|||
<div class="config-row">
|
||||
<div class="config-item">
|
||||
<label for="clientId">客户端ID:</label>
|
||||
<input type="text" id="clientId" placeholder="client-id(需与设备上报的一致)">
|
||||
<input type="text" id="clientId" placeholder="client-id(接口要求填写,服务端不校验)">
|
||||
</div>
|
||||
<div class="config-item">
|
||||
<label for="activationCode">激活码:</label>
|
||||
<input type="text" id="activationCode" placeholder="仅 auth.enabled: true 时需要">
|
||||
</div>
|
||||
</div>
|
||||
<div class="config-row">
|
||||
<div class="config-item">
|
||||
<label for="deviceName">设备名称:</label>
|
||||
<input type="text" id="deviceName" value="Web测试设备" maxlength="50"
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { getAudioPlayer } from './core/audio/player.js?v=0205';
|
|||
import { checkMicrophoneAvailability, isHttpNonLocalhost } from './core/audio/recorder.js?v=0205';
|
||||
import { initMcpTools } from './core/mcp/tools.js?v=0205';
|
||||
import { startWakewordBridgeListener } from './core/network/wakeword-bridge.js?v=0205';
|
||||
import { normalizeDeviceId } from './core/network/ota-connector.js?v=0205';
|
||||
import { uiController } from './ui/controller.js?v=0205';
|
||||
import { log } from './utils/logger.js?v=0205';
|
||||
|
||||
|
|
@ -303,10 +304,10 @@ class App {
|
|||
|
||||
log(`正在发送图片到视觉分析接口: ${url}`, 'info');
|
||||
|
||||
const deviceId = document.getElementById('deviceMac')?.value || '';
|
||||
const deviceId = normalizeDeviceId(document.getElementById('deviceMac')?.value || '');
|
||||
const clientId = document.getElementById('clientId')?.value || '';
|
||||
if (!deviceId || !clientId) {
|
||||
throw new Error('视觉分析失败:请填写设备MAC与客户端ID,两者需与设备上报的一致');
|
||||
throw new Error('视觉分析失败:请填写设备MAC与客户端ID');
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
|
|
|
|||
|
|
@ -86,6 +86,10 @@ export function saveConfig() {
|
|||
localStorage.setItem('xz_tester_deviceMac', deviceMacInput.value);
|
||||
localStorage.setItem('xz_tester_deviceName', deviceNameInput.value);
|
||||
localStorage.setItem('xz_tester_clientId', clientIdInput.value);
|
||||
const activationCodeInput = document.getElementById('activationCode');
|
||||
if (activationCodeInput) {
|
||||
localStorage.setItem('xz_tester_activationCode', activationCodeInput.value.trim());
|
||||
}
|
||||
const emojiEnabledInput = document.getElementById('emojiEnabled');
|
||||
if (emojiEnabledInput) {
|
||||
localStorage.setItem('xz_tester_emojiEnabled', emojiEnabledInput.value);
|
||||
|
|
@ -107,6 +111,7 @@ export function getConfig() {
|
|||
const deviceMac = document.getElementById('deviceMac')?.value.trim() || '';
|
||||
const deviceName = document.getElementById('deviceName')?.value.trim() || '';
|
||||
const clientId = document.getElementById('clientId')?.value.trim() || '';
|
||||
const activationCode = document.getElementById('activationCode')?.value.trim() || '';
|
||||
const emojiEnabled = document.getElementById('emojiEnabled')?.value !== 'false';
|
||||
|
||||
return {
|
||||
|
|
@ -114,6 +119,7 @@ export function getConfig() {
|
|||
deviceName,
|
||||
deviceMac,
|
||||
clientId,
|
||||
activationCode,
|
||||
emojiEnabled
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,11 @@
|
|||
import { log } from '../../utils/logger.js?v=0205';
|
||||
|
||||
// 子协议值必须是合法的 token 字符,冒号不在其中,所以 Device-Id 去掉分隔符。
|
||||
// OTA 请求头与 websocket 子协议必须用同一个值,否则 token 签名对不上
|
||||
export function normalizeDeviceId(deviceId) {
|
||||
return (deviceId || '').replace(/[:-_]/g, '').toLowerCase();
|
||||
}
|
||||
|
||||
// WebSocket 连接
|
||||
export async function webSocketConnect(otaUrl, config) {
|
||||
|
||||
|
|
@ -22,21 +28,23 @@ export async function webSocketConnect(otaUrl, config) {
|
|||
}
|
||||
|
||||
// 使用OTA返回的websocket URL
|
||||
let connUrl = new URL(websocket.url);
|
||||
const connUrl = new URL(websocket.url);
|
||||
|
||||
// 添加token参数(从OTA响应中获取)
|
||||
// 浏览器不能自定义 websocket 请求头,凭证和设备标识改走子协议头:
|
||||
// xiaozhi, t-<token>, d-<deviceId>, c-<clientId>,服务端只回显 xiaozhi。
|
||||
// 不再拼进 URL 查询串,那会原样落进反代访问日志
|
||||
const subprotocols = [
|
||||
'xiaozhi',
|
||||
`d-${normalizeDeviceId(config.deviceId)}`,
|
||||
`c-${config.clientId}`
|
||||
];
|
||||
if (websocket.token) {
|
||||
if (websocket.token.startsWith("Bearer ")) {
|
||||
connUrl.searchParams.append('authorization', websocket.token);
|
||||
} else {
|
||||
connUrl.searchParams.append('authorization', 'Bearer ' + websocket.token);
|
||||
}
|
||||
const token = websocket.token.startsWith('Bearer ')
|
||||
? websocket.token.slice(7)
|
||||
: websocket.token;
|
||||
subprotocols.splice(1, 0, `t-${token}`);
|
||||
}
|
||||
|
||||
// 添加认证参数(保持原有逻辑)
|
||||
connUrl.searchParams.append('device-id', config.deviceId);
|
||||
connUrl.searchParams.append('client-id', config.clientId);
|
||||
|
||||
const wsurl = connUrl.toString()
|
||||
|
||||
log(`正在连接: ${wsurl}`, 'info');
|
||||
|
|
@ -45,7 +53,7 @@ export async function webSocketConnect(otaUrl, config) {
|
|||
document.getElementById('serverUrl').value = wsurl;
|
||||
}
|
||||
|
||||
return new WebSocket(connUrl.toString());
|
||||
return new WebSocket(connUrl.toString(), subprotocols);
|
||||
}
|
||||
|
||||
// 验证配置
|
||||
|
|
@ -68,8 +76,12 @@ async function sendOTA(otaUrl, config) {
|
|||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Device-Id': config.deviceId,
|
||||
'Client-Id': config.clientId
|
||||
'Device-Id': normalizeDeviceId(config.deviceId),
|
||||
'Client-Id': config.clientId,
|
||||
// 开了认证时,OTA 要拿激活码换首次 token;用掉即失效
|
||||
...(config.activationCode
|
||||
? { 'Authorization': `Bearer ${config.activationCode}` }
|
||||
: {})
|
||||
},
|
||||
body: JSON.stringify({
|
||||
version: 0,
|
||||
|
|
|
|||
|
|
@ -38,14 +38,23 @@ server:
|
|||
# 当长期标识会导致设备直接被锁在门外
|
||||
allowed_devices: []
|
||||
# 白名单设备的 token 有效期(秒)。留空表示与 expire_seconds 一致,
|
||||
# 不因进白名单就放宽;确有需要时可设置,但上限建议 90 天
|
||||
# 不因进白名单就放宽;确有需要时可设置,硬上限 90 天,超出会被截断
|
||||
allowed_devices_expire_seconds: null
|
||||
# 白名单设备首次接入免凭证换 token,每台设备只放行一次。
|
||||
# 升级前靠白名单免 token 的存量设备,靠它换到凭证,否则升级即掉线。
|
||||
# 白名单是运维显式列出的可信 MAC,这里不新增任何信任来源。
|
||||
# 若配了 activation_codes 或不想要这条自举路径,设成 false
|
||||
allowed_devices_bootstrap: true
|
||||
# 一次性激活码,供没有 token 的设备首次接入时换取 token,用掉即失效
|
||||
# 新设备接入流程:在此列表中预先加入一个码,设备带着该码请求 OTA 接口,
|
||||
# 服务端签发 token 后自动从列表移除该码
|
||||
# 写成 "码: Device-Id" 表示该码只能给这台设备用,例如 ["a1b2c3: aa:bb:cc:dd:ee:ff"];
|
||||
# 裸字符串表示不限设备。码本身不能带冒号
|
||||
activation_codes: []
|
||||
# token 签发时刻下界(Unix 秒)。改成当前时间即可一次性作废所有存量 token,
|
||||
# 设备下次开机请求 OTA 时会自动换发新 token,无需维护黑名单
|
||||
# token 吊销线(Unix 秒):签发时刻早于它的 token 一律判为无效。
|
||||
# 这是作废旧凭证的开关,不是"让设备换发新凭证"的开关——改大之后,
|
||||
# 存量设备带旧 token 请求 OTA 会被拒,没有激活码就换不到新 token,
|
||||
# 全网设备会一起下线。作废前请先确认 activation_codes 里有可用的码
|
||||
issued_after: 0
|
||||
# CORS 允许的来源列表,默认空表示不做跨域放行
|
||||
# 需要跨域时填具体来源,例如 ["https://your-console.example.com"]
|
||||
|
|
|
|||
Loading…
Reference in a new issue