The HID failure came down to the endpoint type map being indexed by endpoint number without the direction bit. A composite device can have endpoint 1 as both interrupt IN (0x81) and bulk OUT (0x01); the last one read won, so interrupt URBs were submitted as bulk and the kernel rejected them. The device attached and stayed silent. Endpoint data now comes from the raw descriptors read from /dev/bus/usb rather than sysfs, which only ever exposes the active alternate setting — a webcam's isochronous endpoints are invisible there because they only exist after SET_INTERFACE. Two sysfs parsing bugs fell out of that too: the numeric endpoint attributes are hex without a prefix (wMaxPacketSize "0040" was read as 40, not 64), and bInterval was never read at all. Reliability: three places could freeze the whole process. The share path fed io.Pipe from the WebSocket read loop, so one slow USB transfer stalled every tunnel and the keepalives with them. The relay wrote to client sockets while holding the hub lock, so one peer that stopped reading blocked routing and registration for everyone. Control transfers ran inline in the protocol loop behind a 5s timeout. Also fixed: a use-after- free where a discarded URB's memory could be collected while the kernel still owned it, a reap loop that spun at 100% CPU on ioctl errors, a missing attach timeout, a double close(done) panic, and Hash[:8] in the relay's log line, which let a client with a short hash take the server down. Adds mode "both", so one client can offer and consume devices at once. The tunnel and client-left callbacks became multicast for it: as plain fields the second manager to register silently unhooked the first. Tunnel traffic is now AES-256-GCM end to end, on the relay path as well as directly. The key is derived from the three tokens, not from the group hash — the relay is told the hash, so a key derived from it would protect nothing from the one party in the middle. Group IDs are unchanged, so existing setups keep working; only clients configured without the tokens drop to unencrypted, relay-only operation. Peers now try to connect directly, with the relay supplying the public address neither side can determine for itself. Candidates are raced because an unreachable address hangs until timeout rather than refusing. Falling back to the relay is not an error. Platform reach: cross-compiled targets for ARM, MIPS and RISC-V (the Linux client needed no code changes — usbdevfs is not architecture specific), multi-arch Docker images, an Android bridge that accepts devices over SCM_RIGHTS because apps cannot open /dev/bus/usb, and macOS builds via system_profiler enumeration. Adds a Windows KMDF filter driver under driver/windows with its Go side. UNTESTED: it has never been compiled or run, needs the WDK to build and an EV certificate to distribute. Treat it as a starting point. Adds "usb-client diag": says per machine whether sharing and using are possible, what stands in the way, and what fixes it. Reports can be uploaded to a relay to get them off machines that are awkward to copy from. 96 tests, all green under -race. Builds for linux, windows and darwin on amd64 and arm64. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
407 lines
16 KiB
JavaScript
407 lines
16 KiB
JavaScript
// USB Server Web UI
|
|
|
|
const API_BASE = '';
|
|
|
|
// Tab navigation
|
|
document.querySelectorAll('.tab').forEach(tab => {
|
|
tab.addEventListener('click', () => {
|
|
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
|
|
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
|
|
tab.classList.add('active');
|
|
document.getElementById('tab-' + tab.dataset.tab).classList.add('active');
|
|
});
|
|
});
|
|
|
|
// Status updates
|
|
async function updateStatus() {
|
|
try {
|
|
const resp = await fetch(API_BASE + '/api/status');
|
|
const data = await resp.json();
|
|
|
|
const el = document.getElementById('status');
|
|
if (data.connected) {
|
|
el.textContent = 'Verbunden';
|
|
el.className = 'status connected';
|
|
} else {
|
|
el.textContent = 'Nicht verbunden';
|
|
el.className = 'status disconnected';
|
|
}
|
|
|
|
const modeNames = {
|
|
share: 'Freigeben',
|
|
use: 'Empfangen',
|
|
both: 'Freigeben und Empfangen',
|
|
};
|
|
document.getElementById('mode-info').innerHTML =
|
|
`<strong>Modus:</strong> ${escapeHtml(modeNames[data.mode] || data.mode)} | ` +
|
|
`<strong>Name:</strong> ${escapeHtml(data.name)} | ` +
|
|
`<strong>Client ID:</strong> ${data.client_id ? escapeHtml(data.client_id.substring(0, 8)) + '...' : '-'}` +
|
|
(data.encrypted ? ' | <strong>Tunnel:</strong> verschluesselt'
|
|
: ' | <strong>Tunnel:</strong> unverschluesselt (keine Tokens)');
|
|
} catch (e) {
|
|
const el = document.getElementById('status');
|
|
el.textContent = 'Fehler';
|
|
el.className = 'status disconnected';
|
|
}
|
|
}
|
|
|
|
// Device list
|
|
async function updateDevices() {
|
|
try {
|
|
const resp = await fetch(API_BASE + '/api/devices');
|
|
const data = await resp.json();
|
|
renderDevices(data);
|
|
} catch (e) {
|
|
document.getElementById('device-list').innerHTML =
|
|
'<p class="loading">Fehler beim Laden der Geraete</p>';
|
|
}
|
|
}
|
|
|
|
function renderDevices(data) {
|
|
const container = document.getElementById('device-list');
|
|
|
|
// In "both" mode show remote devices first — those are the ones you act
|
|
// on — then the local devices this machine offers.
|
|
let html = '';
|
|
if (data.available_devices || data.attached_devices) {
|
|
html += renderUseSection(data.available_devices || [], data.attached_devices || []);
|
|
}
|
|
if (data.local_devices) {
|
|
if (html) {
|
|
html += '<div class="client-header">Eigene Geraete (freigegeben)</div>';
|
|
}
|
|
html += renderShareSection(data.local_devices);
|
|
}
|
|
|
|
container.innerHTML = html || '<p class="no-devices">Keine Geraete</p>';
|
|
}
|
|
|
|
function renderShareSection(devices) {
|
|
if (!devices || devices.length === 0) {
|
|
return '<p class="no-devices">Keine USB-Geraete gefunden</p>';
|
|
}
|
|
|
|
return devices.map(dev => `
|
|
<div class="device-card">
|
|
<div class="device-info">
|
|
<div class="device-name">${escapeHtml(dev.name)}</div>
|
|
<div class="device-details">
|
|
<span>Bus: ${escapeHtml(dev.bus_id)}</span>
|
|
<span>VID:PID: ${escapeHtml(dev.vendor_id)}:${escapeHtml(dev.product_id)}</span>
|
|
<span>Speed: ${speedName(dev.speed)}</span>
|
|
</div>
|
|
</div>
|
|
<div class="device-status">
|
|
<span class="badge ${dev.status === 'available' ? 'available' : 'in-use'}">
|
|
${dev.status === 'available' ? 'Verfuegbar' : 'In Benutzung'}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
`).join('');
|
|
}
|
|
|
|
function renderUseSection(available, attached) {
|
|
let html = '';
|
|
|
|
// Attached devices first
|
|
if (attached && attached.length > 0) {
|
|
html += '<div class="client-header">Verbundene Geraete</div>';
|
|
html += attached.map(dev => `
|
|
<div class="device-card">
|
|
<div class="device-info">
|
|
<div class="device-name">${escapeHtml(dev.name || dev.bus_id)}</div>
|
|
<div class="device-details">
|
|
<span>Von: ${escapeHtml(dev.client_name || dev.client_id)}</span>
|
|
${dev.vendor_id ? `<span>VID:PID: ${escapeHtml(dev.vendor_id)}:${escapeHtml(dev.product_id)}</span>` : ''}
|
|
<span>VHCI Port: ${escapeHtml(dev.vhci_port)}</span>
|
|
</div>
|
|
</div>
|
|
<div class="device-status">
|
|
<label class="auto-connect-label" title="Beim Start automatisch verbinden">
|
|
<input type="checkbox" ${dev.auto_connect ? 'checked' : ''}
|
|
onchange="toggleAutoConnect('${jsArg(dev.vendor_id)}', '${jsArg(dev.product_id)}', this.checked)">
|
|
Autostart
|
|
</label>
|
|
<span class="badge attached">Verbunden</span>
|
|
<button class="btn small danger" onclick="detachDevice('${jsArg(dev.client_id)}', '${jsArg(dev.bus_id)}')">Trennen</button>
|
|
</div>
|
|
</div>
|
|
`).join('');
|
|
}
|
|
|
|
// Group available by client
|
|
const byClient = {};
|
|
(available || []).forEach(dev => {
|
|
const key = dev.client_id;
|
|
if (!byClient[key]) {
|
|
byClient[key] = { name: dev.client_name, devices: [] };
|
|
}
|
|
byClient[key].devices.push(dev);
|
|
});
|
|
|
|
if (Object.keys(byClient).length === 0 && (!attached || attached.length === 0)) {
|
|
return '<p class="no-devices">Keine fremden Geraete verfuegbar. Warte auf Share-Clients...</p>';
|
|
}
|
|
|
|
for (const [clientId, info] of Object.entries(byClient)) {
|
|
html += `<div class="client-header">${escapeHtml(info.name)} (${escapeHtml(clientId.substring(0, 8))}...)</div>`;
|
|
html += info.devices.map(dev => {
|
|
const isAttached = (attached || []).some(a =>
|
|
a.bus_id === dev.bus_id && a.client_id === clientId
|
|
);
|
|
return `
|
|
<div class="device-card">
|
|
<div class="device-info">
|
|
<div class="device-name">${escapeHtml(dev.name)}</div>
|
|
<div class="device-details">
|
|
<span>Bus: ${escapeHtml(dev.bus_id)}</span>
|
|
<span>VID:PID: ${escapeHtml(dev.vendor_id)}:${escapeHtml(dev.product_id)}</span>
|
|
<span>Speed: ${speedName(dev.speed)}</span>
|
|
</div>
|
|
</div>
|
|
<div class="device-status">
|
|
${dev.status === 'in_use'
|
|
? `<span class="badge in-use">In Benutzung</span>
|
|
${dev.allow_force_detach ? `<button class="btn small danger" onclick="forceDetach('${jsArg(clientId)}', '${jsArg(dev.bus_id)}')">Trennen</button>` : ''}`
|
|
: isAttached
|
|
? '<span class="badge attached">Verbunden</span>'
|
|
: `<span class="badge available">Verfuegbar</span>
|
|
<button class="btn small primary" onclick="attachDevice('${jsArg(clientId)}', '${jsArg(dev.bus_id)}')">Verbinden</button>`
|
|
}
|
|
</div>
|
|
</div>
|
|
`;
|
|
}).join('');
|
|
}
|
|
|
|
return html;
|
|
}
|
|
|
|
// Attach/Detach
|
|
async function attachDevice(clientId, busId) {
|
|
try {
|
|
const resp = await fetch(API_BASE + '/api/attach', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ client_id: clientId, bus_id: busId })
|
|
});
|
|
const data = await resp.json();
|
|
if (!data.ok) {
|
|
alert('Fehler: ' + (data.error || 'Unbekannt'));
|
|
}
|
|
updateDevices();
|
|
} catch (e) {
|
|
alert('Verbindungsfehler: ' + e.message);
|
|
}
|
|
}
|
|
|
|
async function detachDevice(clientId, busId) {
|
|
try {
|
|
const resp = await fetch(API_BASE + '/api/detach', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ client_id: clientId, bus_id: busId })
|
|
});
|
|
const data = await resp.json();
|
|
if (!data.ok) {
|
|
alert('Fehler: ' + (data.error || 'Unbekannt'));
|
|
}
|
|
updateDevices();
|
|
} catch (e) {
|
|
alert('Verbindungsfehler: ' + e.message);
|
|
}
|
|
}
|
|
|
|
// Force detach
|
|
async function forceDetach(clientId, busId) {
|
|
try {
|
|
const resp = await fetch(API_BASE + '/api/force-detach', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ client_id: clientId, bus_id: busId })
|
|
});
|
|
const data = await resp.json();
|
|
if (!data.ok) {
|
|
alert('Fehler: ' + (data.error || 'Unbekannt'));
|
|
}
|
|
updateDevices();
|
|
} catch (e) {
|
|
alert('Verbindungsfehler: ' + e.message);
|
|
}
|
|
}
|
|
|
|
// Auto-connect toggle
|
|
async function toggleAutoConnect(vendorId, productId, enabled) {
|
|
try {
|
|
const resp = await fetch(API_BASE + '/api/auto-connect', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ vendor_id: vendorId, product_id: productId, enabled: enabled })
|
|
});
|
|
const data = await resp.json();
|
|
if (!data.ok) {
|
|
alert('Fehler: ' + (data.error || 'Unbekannt'));
|
|
updateDevices();
|
|
}
|
|
} catch (e) {
|
|
alert('Fehler: ' + e.message);
|
|
updateDevices();
|
|
}
|
|
}
|
|
|
|
// Settings
|
|
async function loadSettings() {
|
|
try {
|
|
const resp = await fetch(API_BASE + '/api/config');
|
|
const cfg = await resp.json();
|
|
document.getElementById('relay-addr').value = cfg.relay_addr || '';
|
|
document.getElementById('hash').value = cfg.hash || '';
|
|
document.getElementById('mode').value = cfg.mode || 'use';
|
|
document.getElementById('client-name').value = cfg.name || '';
|
|
document.getElementById('web-port').value = cfg.web_port || 8080;
|
|
document.getElementById('allow-force-detach').checked = cfg.allow_force_detach || false;
|
|
document.getElementById('direct-port').value = cfg.direct_port || 0;
|
|
document.getElementById('disable-direct').checked = cfg.disable_direct || false;
|
|
document.getElementById('token1').value = cfg.token1 || '';
|
|
document.getElementById('token2').value = cfg.token2 || '';
|
|
document.getElementById('token3').value = cfg.token3 || '';
|
|
if (cfg.hash) {
|
|
document.getElementById('computed-hash').textContent = cfg.hash;
|
|
}
|
|
} catch (e) {
|
|
console.error('Failed to load settings:', e);
|
|
}
|
|
}
|
|
|
|
document.getElementById('settings-form').addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
try {
|
|
const resp = await fetch(API_BASE + '/api/config', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
relay_addr: document.getElementById('relay-addr').value,
|
|
mode: document.getElementById('mode').value,
|
|
name: document.getElementById('client-name').value,
|
|
web_port: parseInt(document.getElementById('web-port').value),
|
|
allow_force_detach: document.getElementById('allow-force-detach').checked,
|
|
direct_port: parseInt(document.getElementById('direct-port').value) || 0,
|
|
disable_direct: document.getElementById('disable-direct').checked,
|
|
})
|
|
});
|
|
const data = await resp.json();
|
|
if (data.ok) {
|
|
alert('Einstellungen gespeichert. Neustart erforderlich fuer Aenderungen.');
|
|
} else {
|
|
alert('Fehler: ' + (data.error || 'Unbekannt'));
|
|
}
|
|
} catch (e) {
|
|
alert('Fehler: ' + e.message);
|
|
}
|
|
});
|
|
|
|
// Token generation
|
|
document.getElementById('generate-tokens').addEventListener('click', async () => {
|
|
try {
|
|
const resp = await fetch(API_BASE + '/api/generate-token', { method: 'POST' });
|
|
const data = await resp.json();
|
|
document.getElementById('token1').value = data.token1;
|
|
document.getElementById('token2').value = data.token2;
|
|
document.getElementById('token3').value = data.token3;
|
|
document.getElementById('computed-hash').textContent = data.hash;
|
|
document.getElementById('hash').value = data.hash;
|
|
} catch (e) {
|
|
alert('Fehler: ' + e.message);
|
|
}
|
|
});
|
|
|
|
document.getElementById('apply-tokens').addEventListener('click', async () => {
|
|
const token1 = document.getElementById('token1').value;
|
|
const token2 = document.getElementById('token2').value;
|
|
const token3 = document.getElementById('token3').value;
|
|
|
|
if (!token1 || !token2 || !token3) {
|
|
alert('Bitte alle 3 Tokens eingeben');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const resp = await fetch(API_BASE + '/api/apply-tokens', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ token1, token2, token3 })
|
|
});
|
|
const data = await resp.json();
|
|
if (data.ok) {
|
|
document.getElementById('computed-hash').textContent = data.hash;
|
|
document.getElementById('hash').value = data.hash;
|
|
alert('Tokens angewandt. Hash: ' + data.hash.substring(0, 16) + '...');
|
|
} else {
|
|
alert('Fehler: ' + (data.error || 'Unbekannt'));
|
|
}
|
|
} catch (e) {
|
|
alert('Fehler: ' + e.message);
|
|
}
|
|
});
|
|
|
|
// Service management
|
|
document.getElementById('install-service').addEventListener('click', async () => {
|
|
try {
|
|
const resp = await fetch(API_BASE + '/api/service/install', { method: 'POST' });
|
|
const data = await resp.json();
|
|
document.getElementById('service-status').textContent = data.message || data.error;
|
|
} catch (e) {
|
|
document.getElementById('service-status').textContent = 'Fehler: ' + e.message;
|
|
}
|
|
});
|
|
|
|
document.getElementById('uninstall-service').addEventListener('click', async () => {
|
|
try {
|
|
const resp = await fetch(API_BASE + '/api/service/uninstall', { method: 'POST' });
|
|
const data = await resp.json();
|
|
document.getElementById('service-status').textContent = data.message || data.error;
|
|
} catch (e) {
|
|
document.getElementById('service-status').textContent = 'Fehler: ' + e.message;
|
|
}
|
|
});
|
|
|
|
// Helpers
|
|
function speedName(speed) {
|
|
const names = { 1: 'Low (1.5M)', 2: 'Full (12M)', 3: 'High (480M)', 5: 'Super (5G)', 6: 'Super+ (10G)' };
|
|
return names[speed] || 'Unknown';
|
|
}
|
|
|
|
function escapeHtml(str) {
|
|
if (str === null || str === undefined) return '';
|
|
return String(str)
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''');
|
|
}
|
|
|
|
// jsArg escapes a value for use inside a single-quoted JavaScript string that
|
|
// itself sits in an HTML attribute. Bus IDs, client IDs and vendor strings all
|
|
// arrive from remote peers, so interpolating them raw would let another client
|
|
// in the group inject script into this UI.
|
|
function jsArg(str) {
|
|
if (str === null || str === undefined) return '';
|
|
return String(str)
|
|
.replace(/\\/g, '\\\\')
|
|
.replace(/'/g, "\\'")
|
|
.replace(/"/g, '"')
|
|
.replace(/</g, '\\x3c')
|
|
.replace(/>/g, '\\x3e')
|
|
.replace(/&/g, '\\x26');
|
|
}
|
|
|
|
// Init
|
|
loadSettings();
|
|
updateStatus();
|
|
updateDevices();
|
|
|
|
// Periodic updates
|
|
setInterval(updateStatus, 5000);
|
|
setInterval(updateDevices, 3000);
|