Something went wrong. Try again.
tangled on ur phone
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124import http from "node:http";
const PORT = Number(process.env.PORT) || 3000;const PUBLIC_URL = (process.env.PUBLIC_URL || "https://untangle.mozzius.dev").replace(/\/+$/, "");
// atproto native apps use a reversed-domain URI scheme, e.g. dev.mozzius.untangle:/oauth/callbackconst SCHEME = new URL(PUBLIC_URL).hostname.split(".").reverse().join(".");const REDIRECT_URI = `${SCHEME}:/oauth/callback`;
const CLIENT_METADATA = { client_id: `${PUBLIC_URL}/oauth-client-metadata.json`, client_name: "Untangle", client_uri: PUBLIC_URL, application_type: "native", grant_types: ["authorization_code", "refresh_token"], response_types: ["code"], redirect_uris: [REDIRECT_URI], // Keep in sync with OAuthConfig.scope in the app. scope: [ "atproto", "repo:org.tangled.feed.star?action=create&action=delete", "repo:org.tangled.graph.follow?action=create&action=delete", "rpc:com.atproto.repo.createRecord?aud=*", "rpc:com.atproto.repo.putRecord?aud=*", ].join(" "), token_endpoint_auth_method: "none", dpop_bound_access_tokens: true,};
const CSS = ` :root { color-scheme: light dark; --bg: #fbfbfa; --fg: #1d1d1f; --muted: #6e6e73; --link: #0a66c2; } @media (prefers-color-scheme: dark) { :root { --bg: #1c1c1e; --fg: #f5f5f7; --muted: #a1a1a6; --link: #4da3ff; } } body { margin: 0; min-height: 100vh; display: grid; place-items: center; background: var(--bg); color: var(--fg); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; } main { max-width: 32rem; padding: 2rem 1.25rem; } h1 { margin: 0 0 0.25rem; font-size: 2.25rem; } .tagline { margin: 0 0 1rem; color: var(--muted); font-size: 1.1rem; } p { line-height: 1.5; } a { color: var(--link); } nav { margin-top: 1.5rem; display: flex; gap: 1rem; flex-wrap: wrap; }`;
const page = (body) => `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Untangle</title><style>${CSS}</style></head><body><main>${body}</main></body></html>`;
const LANDING = page(` <h1>Untangle</h1> <p class="tagline">A native iOS client for Tangled</p> <p>Untangle is an independent app for browsing and interacting with repositories on <a href="https://tangled.org">Tangled</a>, a git forge built on atproto.</p> <nav> <a href="https://tangled.org">Tangled</a> <a href="https://github.com/mozzius/untangle">Source on GitHub</a> </nav>`);
const CALLBACK = page(`<p>Return to the Untangle app to finish signing in.</p>`);
const ROUTES = { "/": { type: "text/html; charset=utf-8", body: LANDING }, "/healthz": { type: "text/plain; charset=utf-8", body: "ok" }, "/oauth/callback": { type: "text/html; charset=utf-8", body: CALLBACK }, "/oauth-client-metadata.json": { type: "application/json; charset=utf-8", body: JSON.stringify(CLIENT_METADATA, null, 2), headers: { "Cache-Control": "public, max-age=300", "Access-Control-Allow-Origin": "*", }, },};
function send(req, res, status, type, body, headers = {}) { res.writeHead(status, { "Content-Type": type, "Content-Length": Buffer.byteLength(body), ...headers, }); res.end(req.method === "HEAD" ? undefined : body);}
const json = (req, res, status, obj, headers) => send(req, res, status, "application/json; charset=utf-8", JSON.stringify(obj), headers);
const server = http.createServer((req, res) => { const { pathname } = new URL(req.url, "http://localhost"); res.on("finish", () => console.log(`${req.method} ${pathname} ${res.statusCode}`));
if (req.method !== "GET" && req.method !== "HEAD") { return json(req, res, 405, { error: "MethodNotAllowed" }, { Allow: "GET, HEAD" }); }
const route = ROUTES[pathname]; if (!route) return json(req, res, 404, { error: "NotFound" });
send(req, res, 200, route.type, route.body, route.headers);});
server.listen(PORT, "0.0.0.0", () => { console.log(`Untangle server listening on 0.0.0.0:${PORT} (${PUBLIC_URL})`);});
process.on("SIGTERM", () => { console.log("SIGTERM received, shutting down"); server.close(() => process.exit(0)); setTimeout(() => process.exit(0), 10_000).unref();});