CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
Squash the tree into one commit, drop docs/architecture.html, reset the version line to 0.3.0, consolidate the changelog, enable Renovate automerge for non-major updates and add AGENTS.md.
89 lines
4.4 KiB
JavaScript
89 lines
4.4 KiB
JavaScript
const { Router } = require("../utils/router");
|
|
const packageJson = require("../../package.json");
|
|
const { evaluate, accountAllowed, cookieFor } = require("../lib/access");
|
|
const { verifyPassword } = require("../utils/password");
|
|
const { tokenFromRequest: sessionToken } = require("../lib/sessions");
|
|
const { classifyHost, QUERY_PARAM } = require("../lib/proxy");
|
|
const logger = require("../utils/logger");
|
|
|
|
const app = Router();
|
|
|
|
app.get("/info", (req, res) => {
|
|
res.json({ name: "frislit", version: packageJson.version, baseDomain: req.config.baseDomain, publicUrl: req.config.publicUrl });
|
|
});
|
|
|
|
app.get("/whoami", async (req, res) => {
|
|
const device = req.config.ready ? await req.auth.deviceFor(req.auth.bearerFromHeaders(req.headers)) : null;
|
|
if (!device) return res.status(401).json({ error: "unauthorized" });
|
|
const account = await req.auth.accountFor(device);
|
|
if (!account) return res.status(401).json({ error: "unauthorized" });
|
|
res.json({ ok: true, role: "owner", username: account.username, guest: device.guest?.label ?? null });
|
|
});
|
|
|
|
app.get("/status/:id", (req, res) => {
|
|
const tunnel = req.registry.get(String(req.params.id || "").toLowerCase());
|
|
res.json({ exists: !!tunnel, online: !!tunnel && tunnel.online });
|
|
});
|
|
|
|
app.post("/gate/:id", async (req, res) => {
|
|
const tunnel = req.registry.get(String(req.params.id || "").toLowerCase());
|
|
if (!tunnel) return res.status(404).json({ error: "not_found", message: "Tunnel not found" });
|
|
|
|
const policy = tunnel.policy;
|
|
if (evaluate(policy, req.info.clientIp, req.intel.lookup(req.info.clientIp))) {
|
|
return res.status(403).json({ error: "forbidden", message: "Your address is not allowed to use this tunnel" });
|
|
}
|
|
if (policy.auth !== "password") return res.status(400).json({ error: "bad_request", message: "This tunnel does not use a password" });
|
|
|
|
const key = `gate:${tunnel.id}:${req.info.clientIp}`;
|
|
if (req.attempts.blocked(key)) {
|
|
return res.status(429).json({ error: "too_many_attempts", message: "Too many attempts. Please try again later." });
|
|
}
|
|
|
|
if (!await verifyPassword(String(req.body.password || ""), policy.passwordHash)) {
|
|
req.attempts.record(key);
|
|
logger.warn(`Failed gate attempt for ${tunnel.id}`, { ip: req.info.clientIp });
|
|
return res.status(401).json({ error: "unauthorized", message: "Wrong password" });
|
|
}
|
|
|
|
req.attempts.forget(key);
|
|
const token = req.access.create(tunnel.id);
|
|
res.header("Set-Cookie", cookieFor(token, req.info.secure)).json({ ok: true });
|
|
});
|
|
|
|
const redirect = (res, location) => res.raw.writeHead(302, { Location: location, "Cache-Control": "no-store" }).end();
|
|
|
|
const returnTarget = (req, tunnel, raw) => {
|
|
const url = URL.parse(raw);
|
|
if (!url || !["http:", "https:"].includes(url.protocol)) return null;
|
|
const host = classifyHost(url.hostname, req.config.baseDomain);
|
|
if (host.kind === "base") {
|
|
url.searchParams.delete(QUERY_PARAM);
|
|
return { url, to: `/@${tunnel.id}${url.pathname}${url.search}` };
|
|
}
|
|
const owner = host.kind === "subdomain" ? host.id : req.domains.tunnelFor(url.hostname);
|
|
return owner === tunnel.id ? { url, to: `${url.pathname}${url.search}` } : null;
|
|
};
|
|
|
|
app.get("/gate/:id/authorize", async (req, res) => {
|
|
const tunnel = req.registry.get(req.params.id.toLowerCase());
|
|
if (!tunnel || tunnel.policy.auth !== "frislit") return res.status(404).json({ error: "not_found", message: "Tunnel not found" });
|
|
if (evaluate(tunnel.policy, req.info.clientIp, req.intel.lookup(req.info.clientIp))) {
|
|
return res.status(403).json({ error: "forbidden", message: "Your address is not allowed to use this tunnel" });
|
|
}
|
|
const target = returnTarget(req, tunnel, req.query.get("return"));
|
|
if (!target) return res.status(400).json({ error: "bad_request", message: "That return address does not belong to this tunnel" });
|
|
|
|
if (!await req.sessions.get(sessionToken(req.raw))) return redirect(res, `/@frislit/login?next=${encodeURIComponent(req.raw.url)}`);
|
|
|
|
const session = await req.sessions.get(sessionToken(req.raw));
|
|
if (!accountAllowed(tunnel.policy, session.account.id, tunnel.accountId)) {
|
|
return res.status(403).json({ error: "forbidden", message: "Your account is not allowed to use this tunnel" });
|
|
}
|
|
|
|
const code = req.access.issueCode(tunnel.id, target.url.hostname);
|
|
redirect(res, `${target.url.origin}/@frislit/gate?code=${code}&to=${encodeURIComponent(target.to)}`);
|
|
});
|
|
|
|
module.exports = app;
|