-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagement-api.js
More file actions
172 lines (158 loc) · 6.17 KB
/
Copy pathmanagement-api.js
File metadata and controls
172 lines (158 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Optional, authenticated localhost-only management API.
// The Windows launcher writes credentials directly and does not need this
// endpoint. It exists for advanced local automation and is disabled unless
// both FREEBUFF_MANAGEMENT_API=1 and a random management token are configured.
import { timingSafeEqual, randomBytes } from "node:crypto";
import { readFileSync, writeFileSync, renameSync, copyFileSync, existsSync, readdirSync, openSync, closeSync, fsyncSync, unlinkSync } from "node:fs";
import { resolve } from "node:path";
import { parseProxyUrl, redactProxyUrl } from "./local-proxy.js";
export function createManagementApi({ credentialDirectory, token, reload = () => {} }) {
const expected = Buffer.from(String(token || ""), "utf8");
if (expected.length < 24) throw new Error("management token must contain at least 24 bytes");
return async function managementApi(req, res, url) {
if (url.pathname !== "/_freebuff/accounts") return false;
if (!isAuthorized(req.headers["x-freebuff-management-token"], expected)) {
send(res, 401, { error: "unauthorized" });
return true;
}
if (!isLocalRequest(req.socket?.remoteAddress)) {
send(res, 403, { error: "localhost_only" });
return true;
}
if (req.method === "GET") {
send(res, 200, { accounts: readSafeAccounts(credentialDirectory) });
return true;
}
if (req.method === "POST") {
let body;
try {
body = JSON.parse((await readBody(req, 64 * 1024)).toString("utf8") || "{}");
} catch {
send(res, 400, { error: "invalid_json" });
return true;
}
const key = String(body.key || "");
const proxy = String(body.proxy || "").trim();
if (!key) {
send(res, 400, { error: "missing_key" });
return true;
}
try {
if (proxy) parseProxyUrl(proxy);
} catch (error) {
send(res, 400, { error: "invalid_proxy", message: error.message });
return true;
}
const updated = updateAccountProxy(credentialDirectory, key, proxy);
if (!updated) {
send(res, 404, { error: "account_not_found" });
return true;
}
reload();
send(res, 200, { ok: true, key, proxy: redactProxyUrl(proxy) });
return true;
}
send(res, 405, { error: "method_not_allowed" });
return true;
};
}
export function createManagementToken() {
return randomBytes(32).toString("base64url");
}
export function isLocalHost(hostname) {
const value = String(hostname || "").trim().toLowerCase();
return value === "127.0.0.1" || value === "localhost" || value === "::1" || value === "[::1]";
}
export function isLocalRequest(address) {
const value = String(address || "").toLowerCase();
return value === "127.0.0.1" || value === "::1" || value === "::ffff:127.0.0.1";
}
function isAuthorized(received, expected) {
const actual = Buffer.from(Array.isArray(received) ? received[0] || "" : String(received || ""), "utf8");
return actual.length === expected.length && timingSafeEqual(actual, expected);
}
function readSafeAccounts(directory) {
const accounts = [];
for (const file of credentialFiles(directory)) {
try {
const root = JSON.parse(readFileSync(file, "utf8"));
if (root.accounts && typeof root.accounts === "object") {
for (const [key, account] of Object.entries(root.accounts)) {
if (!account?.authToken) continue;
accounts.push({ key, name: String(account.name || ""), email: maskEmail(account.email), proxy: redactProxyUrl(account.proxy || "") });
}
} else if (root.authToken) {
accounts.push({ key: "default", name: String(root.name || ""), email: maskEmail(root.email), proxy: redactProxyUrl(root.proxy || "") });
}
} catch {}
}
return accounts;
}
function updateAccountProxy(directory, key, proxy) {
for (const file of credentialFiles(directory)) {
let root;
try { root = JSON.parse(readFileSync(file, "utf8")); } catch { continue; }
if (!root.accounts || typeof root.accounts !== "object" || !root.accounts[key]) continue;
if (proxy) root.accounts[key].proxy = proxy;
else delete root.accounts[key].proxy;
writeJsonAtomically(file, root);
return true;
}
for (const file of credentialFiles(directory)) {
let root;
try { root = JSON.parse(readFileSync(file, "utf8")); } catch { continue; }
if (key !== "default" || !root.authToken) continue;
if (proxy) root.proxy = proxy;
else delete root.proxy;
writeJsonAtomically(file, root);
return true;
}
return false;
}
function credentialFiles(directory) {
if (!existsSync(directory)) return [];
return readdirSync(directory).filter((file) => file.endsWith(".json")).map((file) => resolve(directory, file));
}
function writeJsonAtomically(path, value) {
const temporary = `${path}.${process.pid}.${Date.now()}.tmp`;
const backup = `${path}.bak`;
try {
writeFileSync(temporary, JSON.stringify(value, null, 2) + "\n", { encoding: "utf8", mode: 0o600, flag: "wx" });
const handle = openSync(temporary, "r+");
try {
try { fsyncSync(handle); } catch (error) {
// Some Windows filesystems reject fsync on read-only handles. The
// writeFileSync call is complete and rename remains atomic.
if (error?.code !== "EPERM" && error?.code !== "EINVAL") throw error;
}
} finally { closeSync(handle); }
if (existsSync(path)) copyFileSync(path, backup);
renameSync(temporary, path);
} finally {
try { if (existsSync(temporary)) unlinkSync(temporary); } catch {}
}
}
async function readBody(req, maximum) {
const chunks = [];
let size = 0;
for await (const chunk of req) {
size += chunk.length;
if (size > maximum) throw new Error("body_too_large");
chunks.push(chunk);
}
return Buffer.concat(chunks);
}
function send(res, status, body) {
res.writeHead(status, {
"content-type": "application/json; charset=utf-8",
"cache-control": "no-store",
"x-content-type-options": "nosniff",
});
res.end(JSON.stringify(body));
}
function maskEmail(value) {
const email = String(value || "");
const at = email.indexOf("@");
if (at <= 1) return email ? "***" : "";
return `${email.slice(0, Math.min(2, at))}***${email.slice(at)}`;
}