Server: unchanged shape, just added a "signal" message type to the existing /api/send + /api/stream bus. Now carries both "clipboard" (payload) and "signal" (offer/answer/ICE) over the same envelope. Client: -rtc flag turns the Go listener into a Pion peer. Posts an SDP offer at startup, accepts the browser's answer through the signaling bus, exchanges ICE, then receives clipboard text over a DataChannel named "tether". On message: writes to OS clipboard same as SSE path. Web UI: acts as the answerer. Listens for "signal" SSE events, replies to offers, exchanges ICE. When DataChannel opens, the send button uses RTCDataChannel.send() instead of POST /api/send — data no longer traverses the server after pairing. Pill in the header flips sse → negotiating → rtc to make this visible. Toolchain: bumped go.mod to go 1.26, switched to pion/webrtc v4 and prometheus/client_golang v1.23.x. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
234 lines
8.3 KiB
HTML
234 lines
8.3 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover" />
|
|
<meta name="theme-color" content="#0a0a0a" />
|
|
<title>tether</title>
|
|
<style>
|
|
:root { color-scheme: dark; }
|
|
* { box-sizing: border-box; }
|
|
html, body { margin: 0; height: 100%; }
|
|
body {
|
|
font: 15px -apple-system, "SF Pro Text", system-ui, sans-serif;
|
|
background: #0a0a0a; color: #e5e5e5;
|
|
display: flex; flex-direction: column;
|
|
padding: 18px 16px env(safe-area-inset-bottom);
|
|
gap: 14px;
|
|
}
|
|
header { display: flex; align-items: baseline; gap: 8px; }
|
|
h1 { margin: 0; font-size: 22px; font-weight: 600; letter-spacing: -0.5px; }
|
|
.tag { font-size: 11px; color: #888; letter-spacing: 0.5px; text-transform: uppercase; }
|
|
.pill {
|
|
font-size: 10px; padding: 2px 8px; border-radius: 999px;
|
|
background: #1f1f1f; color: #888; letter-spacing: 0.4px;
|
|
}
|
|
.pill.live { background: #052e16; color: #4ade80; }
|
|
.pill.connecting { background: #1f2937; color: #fbbf24; }
|
|
.row { display: flex; flex-direction: column; gap: 6px; }
|
|
label { font-size: 11px; color: #888; letter-spacing: 0.4px; text-transform: uppercase; }
|
|
textarea {
|
|
width: 100%; min-height: 130px; resize: vertical;
|
|
font: 15px -apple-system, ui-monospace, "SF Mono", monospace;
|
|
background: #161616; color: #f5f5f5;
|
|
border: 1px solid #232323; border-radius: 10px;
|
|
padding: 12px; outline: none;
|
|
}
|
|
textarea:focus { border-color: #3a3a3a; }
|
|
.actions { display: flex; gap: 8px; }
|
|
button {
|
|
flex: 1; padding: 12px 16px; font: 600 14px -apple-system, system-ui, sans-serif;
|
|
background: #1a1a1a; color: #f5f5f5;
|
|
border: 1px solid #2a2a2a; border-radius: 10px;
|
|
cursor: pointer; transition: background 0.15s;
|
|
}
|
|
button.primary { background: #4f46e5; border-color: #6366f1; }
|
|
button.primary:active { background: #4338ca; }
|
|
button:active { background: #232323; }
|
|
.status {
|
|
font-size: 13px; color: #888;
|
|
padding: 8px 12px; background: #131313;
|
|
border: 1px solid #1f1f1f; border-radius: 8px;
|
|
min-height: 38px; display: flex; align-items: center;
|
|
}
|
|
.status.ok { color: #4ade80; }
|
|
.status.err { color: #f87171; }
|
|
.feed { display: flex; flex-direction: column; gap: 6px; }
|
|
.feed h2 { font-size: 11px; color: #888; letter-spacing: 0.5px; text-transform: uppercase; margin: 0; }
|
|
.msg {
|
|
background: #131313; border: 1px solid #1f1f1f; border-radius: 8px;
|
|
padding: 10px 12px; font: 13px ui-monospace, "SF Mono", monospace;
|
|
color: #d4d4d4; white-space: pre-wrap; word-break: break-word;
|
|
max-height: 200px; overflow: auto;
|
|
}
|
|
.meta { font-size: 11px; color: #666; margin-top: 4px; }
|
|
.meta .rtc-badge { color: #4ade80; }
|
|
footer {
|
|
margin-top: auto; padding-top: 8px;
|
|
font-size: 11px; color: #555; text-align: center;
|
|
}
|
|
footer a { color: #888; text-decoration: none; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<header>
|
|
<h1>tether</h1>
|
|
<span class="tag">phone ↔ laptop</span>
|
|
<span class="pill" id="rtcPill">sse</span>
|
|
</header>
|
|
|
|
<div class="row">
|
|
<label for="out">send to laptop</label>
|
|
<textarea id="out" placeholder="paste or type something…"></textarea>
|
|
<div class="actions">
|
|
<button id="pasteBtn">paste clipboard</button>
|
|
<button class="primary" id="sendBtn">send →</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="status" id="status">idle</div>
|
|
|
|
<div class="feed">
|
|
<h2>received from laptop</h2>
|
|
<div id="incoming"></div>
|
|
</div>
|
|
|
|
<footer>
|
|
<a href="https://gitea.pecord.io/pecord/tether">tether on gitea</a> · v0.3
|
|
</footer>
|
|
|
|
<script>
|
|
const $ = (id) => document.getElementById(id);
|
|
const peerID = "tether-browser-" + Math.random().toString(36).slice(2, 8);
|
|
let rtcChannel = null; // open RTCDataChannel, when we have one
|
|
|
|
const status = (msg, cls) => {
|
|
const s = $("status");
|
|
s.textContent = msg;
|
|
s.className = "status" + (cls ? " " + cls : "");
|
|
};
|
|
const setPill = (mode) => {
|
|
const p = $("rtcPill");
|
|
p.textContent = mode;
|
|
p.className = "pill " + (mode === "rtc" ? "live" : mode === "negotiating" ? "connecting" : "");
|
|
};
|
|
|
|
function addIncoming(text, source, ts, viaRTC) {
|
|
const el = document.createElement("div");
|
|
el.className = "msg";
|
|
el.textContent = text;
|
|
const meta = document.createElement("div");
|
|
meta.className = "meta";
|
|
const badge = viaRTC ? '<span class="rtc-badge">via rtc</span> · ' : '';
|
|
meta.innerHTML = badge + (source || "client") + " @ " + new Date(ts || Date.now()).toLocaleTimeString();
|
|
el.appendChild(meta);
|
|
const feed = $("incoming");
|
|
feed.insertBefore(el, feed.firstChild);
|
|
}
|
|
|
|
$("pasteBtn").addEventListener("click", async () => {
|
|
try {
|
|
$("out").value = await navigator.clipboard.readText();
|
|
status("pasted from clipboard", "ok");
|
|
} catch (e) {
|
|
status("clipboard read denied — paste manually", "err");
|
|
}
|
|
});
|
|
|
|
$("sendBtn").addEventListener("click", async () => {
|
|
const text = $("out").value;
|
|
if (!text) { status("empty", "err"); return; }
|
|
if (rtcChannel && rtcChannel.readyState === "open") {
|
|
rtcChannel.send(text);
|
|
status("sent via rtc ✓", "ok");
|
|
} else {
|
|
status("sending via http…");
|
|
try {
|
|
const r = await fetch("/api/send", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ type: "clipboard", text, source: "phone" }),
|
|
});
|
|
status(r.ok ? "delivered ✓" : "server returned " + r.status, r.ok ? "ok" : "err");
|
|
} catch (e) { status("network error", "err"); }
|
|
}
|
|
});
|
|
|
|
// ── WebRTC peer ────────────────────────────────────────────────────────
|
|
// Browser acts as ANSWERER. Listens for offers via SSE, replies via /api/send.
|
|
async function postSignal(payload) {
|
|
await fetch("/api/send", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
type: "signal",
|
|
from: peerID,
|
|
source: "phone",
|
|
signal: JSON.stringify(payload),
|
|
}),
|
|
});
|
|
}
|
|
|
|
let pc = null;
|
|
async function handleOffer(sdp) {
|
|
if (pc) try { pc.close(); } catch (_) {}
|
|
pc = new RTCPeerConnection({
|
|
iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
|
|
});
|
|
setPill("negotiating");
|
|
pc.onicecandidate = (ev) => {
|
|
if (ev.candidate) postSignal({ kind: "ice", candidate: ev.candidate.toJSON() });
|
|
};
|
|
pc.onconnectionstatechange = () => {
|
|
if (pc.connectionState === "connected") setPill("rtc");
|
|
else if (pc.connectionState === "failed" || pc.connectionState === "disconnected") setPill("sse");
|
|
};
|
|
pc.ondatachannel = (ev) => {
|
|
const ch = ev.channel;
|
|
ch.onopen = () => { rtcChannel = ch; setPill("rtc"); };
|
|
ch.onclose = () => { rtcChannel = null; setPill("sse"); };
|
|
ch.onmessage = (m) => addIncoming(m.data, "laptop", Date.now(), true);
|
|
};
|
|
await pc.setRemoteDescription({ type: "offer", sdp });
|
|
const answer = await pc.createAnswer();
|
|
await pc.setLocalDescription(answer);
|
|
await postSignal({ kind: "answer", sdp: answer });
|
|
}
|
|
|
|
async function handleIceCandidate(candidate) {
|
|
if (pc) {
|
|
try { await pc.addIceCandidate(candidate); } catch (e) {}
|
|
}
|
|
}
|
|
|
|
// ── SSE main feed ──────────────────────────────────────────────────────
|
|
function connectFeed() {
|
|
const es = new EventSource("/api/stream");
|
|
es.addEventListener("clipboard", (ev) => {
|
|
try {
|
|
const m = JSON.parse(ev.data);
|
|
if (m.source !== "phone") {
|
|
addIncoming(m.text, m.source, m.ts, false);
|
|
}
|
|
} catch (e) {}
|
|
});
|
|
es.addEventListener("signal", (ev) => {
|
|
try {
|
|
const m = JSON.parse(ev.data);
|
|
if (m.from === peerID) return; // ignore our own
|
|
let payload = m.signal;
|
|
if (typeof payload === "string") payload = JSON.parse(payload);
|
|
if (payload.kind === "offer" && payload.sdp) {
|
|
handleOffer(payload.sdp.sdp);
|
|
} else if (payload.kind === "ice" && payload.candidate) {
|
|
handleIceCandidate(payload.candidate);
|
|
}
|
|
} catch (e) {}
|
|
});
|
|
es.onerror = () => setTimeout(connectFeed, 2000);
|
|
}
|
|
connectFeed();
|
|
</script>
|
|
</body>
|
|
</html>
|