Add tokens and faceclaw-input script, which allows using a CLI utility as an input device for the glasses

This commit is contained in:
James Babcock committed 2026-09-20 00:34:58 -07:00
1 parent 313ba85708
commit ccb320c66f
32 files changed
+1785 -12

No files matched your search

+70
View File
@@ -0,0 +1,70 @@
/** Interface selection never returns wildcard, Wi-Fi, cellular or LAN addresses. */
export type InterfaceAddress = { name: string; address: string; pointToPoint: boolean };
export const INTERFACE_STORAGE_KEY = 'remoteInput.interface';
export function isTunnelInterface(item: InterfaceAddress): boolean {
if (/^(wlan|wifi|wl|en\d|eth\d|rmnet|ccmni|pdp_ip|ap\d|p2p)/i.test(item.name)) return false;
return item.pointToPoint || /^(tailscale|utun\d|tun\d|wg\d)/i.test(item.name);
}
function isTailnetAddress(address: string): boolean {
const parts = address.split('.').map(Number);
return /^fd7a:115c:a1e0:/i.test(address) || (parts.length === 4 && parts[0] === 100 && parts[1] >= 64 && parts[1] <= 127);
}
export function listenerAddresses(interfaces: InterfaceAddress[], selection: string): string[] {
const addresses = ['127.0.0.1'];
if (selection === 'localhost') return addresses;
const tunnels = interfaces.filter(isTunnelInterface);
// A generic tun/utun name alone does not identify Tailscale. Its dedicated
// IPv6 prefix does; CGNAT IPv4 alone could belong to an unrelated VPN.
const tailscaleNames = new Set(tunnels.filter(item => /^tailscale/i.test(item.name) ||
/^fd7a:115c:a1e0:/i.test(item.address)).map(item => item.name));
for (const item of tunnels) {
const selected = selection === 'tailscale'
? tailscaleNames.has(item.name) && isTailnetAddress(item.address)
: selection === `interface:${item.name}`;
if (selected && item.address !== '0.0.0.0' && item.address !== '::' && !item.address.startsWith('fe80:')) addresses.push(item.address);
}
return [...new Set(addresses)].sort();
}
export type NativeListener = {
start(port: number, address: string): string;
stop(): void;
nextRequest(): string | null;
complete(id: number, response: string): void;
};
/** Independent sockets preserve localhost access while adding/removing a VPN. */
export function createListenerPool(create: () => NativeListener) {
let nextId = 0, cursor = 0;
const entries = new Map<string, { id: number; listener: NativeListener }>();
return {
start(port: number, addresses: string[]): string {
for (const [address, entry] of entries) if (!addresses.includes(address)) { entry.listener.stop(); entries.delete(address); }
const failures: string[] = [];
for (const address of addresses) if (!entries.has(address)) {
const listener = create();
const error = listener.start(port, address);
if (error) { listener.stop(); failures.push(`${address}: ${error}`); }
else entries.set(address, { id: ++nextId, listener });
}
return failures.join('\n');
},
addresses(): string[] { return [...entries.keys()]; },
stop(): void { for (const entry of entries.values()) entry.listener.stop(); entries.clear(); },
nextRequest(): string | null {
const active = [...entries.values()];
for (let i = 0; i < active.length; i++) {
const entry = active[cursor++ % active.length];
const raw = entry.listener.nextRequest();
if (raw) {
const request = JSON.parse(String(raw));
return JSON.stringify({ ...request, id: `${entry.id}/${request.id}` });
}
}
return null;
},
complete(id: string, response: string): void {
const [entryId, requestId] = id.split('/').map(Number);
for (const entry of entries.values()) if (entry.id === entryId) entry.listener.complete(requestId, response);
},
};
}
+99
View File
@@ -0,0 +1,99 @@
/** Local input API v1. Transport: one UTF-8 JSON line per TCP connection. */
export const REMOTE_PORT = 8791;
export const PERMISSIONS = ['input', 'text', 'assistant'] as const;
export type Permission = typeof PERMISSIONS[number];
export const GESTURES = ['click', 'double-click', 'long-press', 'short-then-long-press',
'scroll-up', 'scroll-down', 'swipe-up', 'swipe-down', 'swipe-left', 'swipe-right'] as const;
export type RemoteGesture = typeof GESTURES[number];
export type TokenRecord = { id: string; name: string; hash: string; permissions: Permission[]; createdAt: number };
export type RemoteHost = {
ready(): boolean;
locked(): boolean;
input(gesture: RemoteGesture, source: 'watch' | 'ring'): Promise<void>;
acceptsText(): boolean;
text(text: string, submit: boolean): void;
assistantAvailable(): boolean;
assistant(text: string): void;
};
export type Reply = { ok: true } | { ok: false; error: string; message: string };
const error = (code: string, message: string): Reply => ({ ok: false, error: code, message });
export class TokenStore {
constructor(private readonly read: () => string, private readonly write: (json: string) => void,
private readonly random: () => string, private readonly hash: (value: string) => string) {}
list(): TokenRecord[] {
try {
const parsed = JSON.parse(this.read());
if (!Array.isArray(parsed)) return [];
return parsed.filter((r): r is TokenRecord => r && typeof r.id === 'string' &&
typeof r.name === 'string' && /^[a-f0-9]{64}$/.test(r.hash) && typeof r.createdAt === 'number' &&
Array.isArray(r.permissions) && r.permissions.every((p: Permission) => PERMISSIONS.includes(p)));
} catch { return []; }
}
create(name: string, permissions: Permission[]): { token: string; record: TokenRecord } {
if (!name.trim() || name.length > 80) throw new Error('Enter a token name (1–80 characters).');
if (!permissions.length || !permissions.every(p => PERMISSIONS.includes(p))) throw new Error('Select at least one permission.');
const records = this.list();
if (records.length >= 32) throw new Error('Revoke a token before creating more (maximum 32).');
const secret = this.random();
if (!/^[a-f0-9]{64}$/.test(secret)) throw new Error('Secure random token generation failed.');
const token = `fc1_${secret}`;
const hash = this.hash(token);
const record = { id: hash.slice(0, 16), name: name.trim(), hash, permissions: [...new Set(permissions)], createdAt: Date.now() };
this.write(JSON.stringify([...records, record]));
return { token, record };
}
permissions(id: string, permissions: Permission[]): void {
if (!permissions.every(p => PERMISSIONS.includes(p))) throw new Error('Invalid permission.');
this.write(JSON.stringify(this.list().map(r => r.id === id ? { ...r, permissions: [...new Set(permissions)] } : r)));
}
revoke(id: string): void { this.write(JSON.stringify(this.list().filter(r => r.id !== id))); }
authenticate(token: unknown): TokenRecord | undefined {
if (typeof token !== 'string' || !/^fc1_[a-f0-9]{64}$/.test(token)) return undefined;
const digest = this.hash(token);
// Compare fixed-size digests without an early exit on mismatching bytes.
return this.list().find(r => {
let difference = r.hash.length ^ digest.length;
for (let i = 0; i < 64; i++) difference |= r.hash.charCodeAt(i) ^ digest.charCodeAt(i);
return difference === 0;
});
}
}
export async function handleRequest(body: string, tokens: TokenStore, host: RemoteHost): Promise<Reply> {
let request: any;
try { request = JSON.parse(body); } catch { return error('bad_request', 'Expected a JSON object.'); }
if (!request || typeof request !== 'object' || Array.isArray(request)) return error('bad_request', 'Expected a JSON object.');
const token = tokens.authenticate(request.token);
if (!token) return error('unauthorized', 'Invalid or revoked token.');
if (request.version !== 1 || (request.action !== 'ping' && !PERMISSIONS.includes(request.action))) return error('bad_request', 'Expected version 1 and action ping, input, text or assistant.');
if (request.action === 'ping') {
if (request.permission !== undefined && !PERMISSIONS.includes(request.permission)) return error('bad_request', 'Invalid permission.');
if (request.permission && !token.permissions.includes(request.permission)) return error('forbidden', `Token lacks ${request.permission} permission.`);
return { ok: true };
}
if (request.action === 'text' && request.submit !== undefined && typeof request.submit !== 'boolean') return error('bad_request', 'submit must be a boolean.');
if (!token.permissions.includes(request.action)) return error('forbidden', `Token lacks ${request.action} permission.`);
if (request.action === 'input') {
if (!GESTURES.includes(request.gesture) || !['watch', 'ring'].includes(request.source ?? 'watch') ||
(request.source === 'ring' && request.gesture.startsWith('swipe-'))) {
return error('bad_request', 'Invalid gesture or source; directions require watch input.');
}
} else if (typeof request.text !== 'string' || !request.text.trim() || request.text.length > 8000 || request.text.includes('\0')) {
return error('bad_request', 'Text must contain 1–8000 characters without NUL.');
}
if (!host.ready()) return error('unavailable', 'Faceclaw is not ready for input.');
// Gestures follow the normal lock-screen path. Text must never bypass it.
if (request.action !== 'input' && host.locked()) return error('locked', 'The glasses are locked.');
try {
if (request.action === 'input') await host.input(request.gesture, request.source ?? 'watch');
else if (request.action === 'text') {
if (!host.acceptsText()) return error('unavailable', 'The foreground window does not accept text.');
host.text(request.text, request.submit !== false);
} else {
if (!host.assistantAvailable()) return error('unavailable', 'Set up the voice assistant in Settings first.');
host.assistant(request.text);
}
return { ok: true };
} catch { return error('failed', 'Faceclaw could not deliver the request.'); }
}
+54
View File
@@ -0,0 +1,54 @@
import { getStringSetting, setStringSetting, onSettingsStoreChanged } from '../native/settings-store';
import { remoteNative, remoteInterfaces, randomTokenSecret, tokenHash } from '../native/remote-input';
import { INTERFACE_STORAGE_KEY, listenerAddresses } from './listeners';
import { TokenStore, handleRequest, REMOTE_PORT, type RemoteHost } from './protocol';
export const TOKEN_STORAGE_KEY = 'remoteInput.tokens.v1';
export const remoteTokens = new TokenStore(() => getStringSetting(TOKEN_STORAGE_KEY, '[]'),
value => setStringSetting(TOKEN_STORAGE_KEY, value), randomTokenSecret, tokenHash);
let status = 'No tokens; listener stopped.';
export function remoteInputStatus(): string { return status; }
export function remoteInterfaceSelection(): string { return getStringSetting(INTERFACE_STORAGE_KEY, 'tailscale'); }
export function selectRemoteInterface(value: string): void { setStringSetting(INTERFACE_STORAGE_KEY, value); }
/** Owned by the main controller. Native socket work never enters a JS isolate. */
export function startRemoteInput(host: RemoteHost): () => void {
const native = remoteNative();
let timer: ReturnType<typeof setInterval> | null = null;
let busy = false;
let networkTimer: ReturnType<typeof setInterval> | null = null;
const sync = () => {
if (!remoteTokens.list().length) {
native.stop();
if (timer !== null) clearInterval(timer);
timer = null;
if (networkTimer !== null) clearInterval(networkTimer);
networkTimer = null;
status = 'No tokens; listener stopped.';
return;
}
const selection = remoteInterfaceSelection();
const addresses = listenerAddresses(remoteInterfaces(), selection);
const failure = native.start(REMOTE_PORT, addresses);
status = `Listening on ${native.addresses().map(address => `${address.includes(':') ? `[${address}]` : address}:${REMOTE_PORT}`).join(', ') || 'no addresses'}.`;
if (selection !== 'localhost' && addresses.length === 1) status += '\nSelected tunnel not found; localhost only. Enable Tailscale or select its tunnel interface.';
if (failure) status += `\n${failure}`;
// Reconcile after VPN reconnects/address changes; unchanged sockets stay open.
if (networkTimer === null) networkTimer = setInterval(sync, 3000);
if (timer !== null) return;
timer = setInterval(() => {
if (busy) return;
const raw = native.nextRequest();
if (!raw) return;
const request = JSON.parse(String(raw));
if (Date.now() >= request.expiresAt) return;
busy = true;
void handleRequest(request.body, remoteTokens, host)
.then(reply => native.complete(request.id, JSON.stringify(reply)))
.catch(() => native.complete(request.id, JSON.stringify({ ok: false, error: 'failed', message: 'Request failed.' })))
.finally(() => { busy = false; });
}, 15);
};
sync();
const off = onSettingsStoreChanged(key => { if (key === TOKEN_STORAGE_KEY || key === INTERFACE_STORAGE_KEY) sync(); });
return () => { off(); if (timer !== null) clearInterval(timer); if (networkTimer !== null) clearInterval(networkTimer); native.stop(); };
}