Improve voice turns, local commands and Spotify controls
This commit is contained in:
+17
-2
@@ -13,16 +13,31 @@ const store = openStore();
|
||||
const server = http.createServer(async (req, res) => {
|
||||
try {
|
||||
if (req.method === 'GET' && req.url === '/healthz') { res.writeHead(200, { 'content-type': 'application/json', 'cache-control': 'no-store' }); res.end('{"ok":true}'); return; }
|
||||
if (req.url === '/v1/device/status' && req.method === 'POST') {
|
||||
if (!authorizedDevice(req)) { res.writeHead(401); res.end(); return; }
|
||||
let body = '';
|
||||
for await (const chunk of req) {
|
||||
body += chunk;
|
||||
if (body.length > 14000) { res.writeHead(413); res.end(); return; }
|
||||
}
|
||||
const data = JSON.parse(body);
|
||||
if (!data || typeof data !== 'object' || Array.isArray(data)) { res.writeHead(400); res.end(); return; }
|
||||
store.recordDevice(data);
|
||||
res.writeHead(204); res.end(); return;
|
||||
}
|
||||
if (await handleAdmin(req, res, store)) return;
|
||||
res.writeHead(404); res.end();
|
||||
} catch { if (!res.headersSent) res.writeHead(500); res.end(); }
|
||||
});
|
||||
const sockets = new WebSocketServer({ noServer: true, maxPayload: 128 * 1024, perMessageDeflate: false });
|
||||
let active = false;
|
||||
server.on('upgrade', (req, socket, head) => {
|
||||
function authorizedDevice(req) {
|
||||
const token = req.headers.authorization?.startsWith('Bearer ') ? req.headers.authorization.slice(7) : '';
|
||||
const a = Buffer.from(token), b = Buffer.from(DEVICE_TOKEN);
|
||||
if (req.url !== '/v1/voice' || a.length !== b.length || !timingSafeEqual(a, b)) { socket.end('HTTP/1.1 401 Unauthorized\r\nConnection: close\r\n\r\n'); return; }
|
||||
return a.length === b.length && timingSafeEqual(a, b);
|
||||
}
|
||||
server.on('upgrade', (req, socket, head) => {
|
||||
if (req.url !== '/v1/voice' || !authorizedDevice(req)) { socket.end('HTTP/1.1 401 Unauthorized\r\nConnection: close\r\n\r\n'); return; }
|
||||
if (active) { socket.end('HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\n\r\n'); return; }
|
||||
sockets.handleUpgrade(req, socket, head, ws => {
|
||||
active = true;
|
||||
|
||||
Reference in New Issue
Block a user