-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
345 lines (295 loc) · 8.87 KB
/
server.js
File metadata and controls
345 lines (295 loc) · 8.87 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
const http = require("http");
const path = require("path");
const fs = require("fs");
const crypto = require("crypto");
const HOST = "0.0.0.0";
const PORT = Number(process.env.PORT || 8080);
const ADMIN_KEY = process.env.ADMIN_KEY || "clawfish-admin";
const DATA_DIR = path.join(__dirname, "data");
const DATA_FILE = path.join(DATA_DIR, "agents.json");
const PUBLIC_DIR = __dirname;
const MIME_TYPES = {
".html": "text/html; charset=utf-8",
".css": "text/css; charset=utf-8",
".js": "application/javascript; charset=utf-8",
".json": "application/json; charset=utf-8",
".ico": "image/x-icon",
".svg": "image/svg+xml",
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".webp": "image/webp"
};
function ensureDataFile() {
if (!fs.existsSync(DATA_DIR)) {
fs.mkdirSync(DATA_DIR, { recursive: true });
}
if (!fs.existsSync(DATA_FILE)) {
fs.writeFileSync(DATA_FILE, JSON.stringify({ agents: [] }, null, 2), "utf8");
}
}
function loadData() {
ensureDataFile();
const raw = fs.readFileSync(DATA_FILE, "utf8");
const parsed = JSON.parse(raw);
if (!parsed.agents || !Array.isArray(parsed.agents)) {
return { agents: [] };
}
return parsed;
}
function saveData(data) {
fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2), "utf8");
}
function sendJson(res, statusCode, payload) {
const body = JSON.stringify(payload);
res.writeHead(statusCode, {
"Content-Type": "application/json; charset=utf-8",
"Content-Length": Buffer.byteLength(body)
});
res.end(body);
}
function sendFile(res, filePath) {
if (!fs.existsSync(filePath)) {
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Not found");
return;
}
const ext = path.extname(filePath).toLowerCase();
const contentType = MIME_TYPES[ext] || "application/octet-stream";
const content = fs.readFileSync(filePath);
res.writeHead(200, {
"Content-Type": contentType,
"Content-Length": content.length
});
res.end(content);
}
function parseJsonBody(req) {
return new Promise((resolve, reject) => {
const chunks = [];
let size = 0;
req.on("data", (chunk) => {
size += chunk.length;
if (size > 1024 * 1024) {
reject(new Error("Request body is too large."));
return;
}
chunks.push(chunk);
});
req.on("end", () => {
if (chunks.length === 0) {
resolve({});
return;
}
try {
const raw = Buffer.concat(chunks).toString("utf8");
resolve(JSON.parse(raw));
} catch (error) {
reject(new Error("Invalid JSON body."));
}
});
req.on("error", reject);
});
}
function createInviteCode(length = 8) {
const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
const bytes = crypto.randomBytes(length);
let code = "";
for (let i = 0; i < length; i += 1) {
code += chars[bytes[i] % chars.length];
}
return code;
}
function createVerificationCode() {
const value = Math.floor(100000 + Math.random() * 900000);
return String(value);
}
function createInviteAccessToken() {
return crypto.randomBytes(20).toString("hex");
}
function hashPassword(password) {
return crypto.createHash("sha256").update(password).digest("hex");
}
function getBaseUrl(req) {
const protocol = req.headers["x-forwarded-proto"] || "http";
return `${protocol}://${req.headers.host}`;
}
function maskContact(contact) {
if (contact.includes("@")) {
const [name, domain] = contact.split("@");
const shownName = `${name.slice(0, 2)}***`;
return `${shownName}@${domain}`;
}
if (contact.length >= 7) {
return `${contact.slice(0, 3)}****${contact.slice(-2)}`;
}
return `${contact.slice(0, 2)}***`;
}
function handleAdminStats(req, res) {
const adminKey = String(req.headers["x-admin-key"] || "");
if (!adminKey || adminKey !== ADMIN_KEY) {
sendJson(res, 401, { message: "后台密钥无效。" });
return;
}
const data = loadData();
const verificationIssuedAgents = data.agents.filter((item) => item.verificationCode).length;
const recentAgents = data.agents
.slice(-10)
.reverse()
.map((item) => ({
contactMasked: maskContact(item.contact),
registeredAt: item.registeredAt
}));
sendJson(res, 200, {
registeredAgents: data.agents.length,
verificationIssuedAgents,
recentAgents
});
}
async function handleRegister(req, res) {
let payload;
try {
payload = await parseJsonBody(req);
} catch (error) {
sendJson(res, 400, { message: error.message });
return;
}
const contact = String(payload.contact || "").trim();
const password = String(payload.password || "").trim();
const referralCode = String(payload.inviteCode || "").trim();
if (!contact) {
sendJson(res, 400, { message: "联系方式不能为空。" });
return;
}
if (password.length < 6) {
sendJson(res, 400, { message: "密码至少 6 位。" });
return;
}
const data = loadData();
const existingAgent = data.agents.find((agent) => agent.contact === contact);
if (existingAgent) {
sendJson(res, 409, { message: "该联系方式已注册。" });
return;
}
const inviteCode = createInviteCode(8);
const inviteAccessToken = createInviteAccessToken();
const agent = {
agentId: crypto.randomUUID(),
contact,
passwordHash: hashPassword(password),
inviteCode,
inviteAccessToken,
inviteTokenIssuedAt: new Date().toISOString(),
referredByCode: referralCode,
verificationCode: "",
verificationIssuedAt: "",
registeredAt: new Date().toISOString()
};
data.agents.push(agent);
saveData(data);
sendJson(res, 201, {
message: "注册成功。",
nextPath: `/invite-center?token=${agent.inviteAccessToken}`
});
}
function handleInviteSession(req, res, requestUrl) {
const token = String(requestUrl.searchParams.get("token") || "").trim();
if (!token) {
sendJson(res, 400, { message: "缺少邀请码访问凭证。" });
return;
}
const data = loadData();
const agent = data.agents.find((item) => item.inviteAccessToken === token);
if (!agent) {
sendJson(res, 404, { message: "邀请码访问凭证无效,请重新注册。" });
return;
}
sendJson(res, 200, {
agentId: agent.agentId,
inviteCode: agent.inviteCode,
inviteLink: `${getBaseUrl(req)}/invite/${agent.inviteCode}`
});
}
async function handleVerificationCode(req, res) {
let payload;
try {
payload = await parseJsonBody(req);
} catch (error) {
sendJson(res, 400, { message: error.message });
return;
}
const agentId = String(payload.agentId || "").trim();
if (!agentId) {
sendJson(res, 400, { message: "缺少 agentId。" });
return;
}
const data = loadData();
const agent = data.agents.find((item) => item.agentId === agentId);
if (!agent) {
sendJson(res, 404, { message: "未找到该注册用户,请先注册。" });
return;
}
agent.verificationCode = createVerificationCode();
agent.verificationIssuedAt = new Date().toISOString();
saveData(data);
sendJson(res, 200, {
verificationCode: agent.verificationCode,
issuedAt: agent.verificationIssuedAt
});
}
function handleStatic(req, res, pathname) {
if (pathname === "/" || pathname.startsWith("/invite/")) {
sendFile(res, path.join(PUBLIC_DIR, "index.html"));
return;
}
if (pathname === "/invite-center") {
sendFile(res, path.join(PUBLIC_DIR, "invite.html"));
return;
}
if (pathname === "/admin") {
sendFile(res, path.join(PUBLIC_DIR, "admin.html"));
return;
}
if (pathname.startsWith("/assets/")) {
const relativePath = pathname.replace(/^\/+/, "");
const normalizedPath = path.normalize(relativePath);
if (normalizedPath.startsWith("..")) {
res.writeHead(403, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Forbidden");
return;
}
sendFile(res, path.join(PUBLIC_DIR, normalizedPath));
return;
}
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Not found");
}
const server = http.createServer(async (req, res) => {
const requestUrl = new URL(req.url, `http://${req.headers.host}`);
const pathname = requestUrl.pathname;
if (req.method === "GET" && pathname === "/api/admin/stats") {
handleAdminStats(req, res);
return;
}
if (req.method === "GET" && pathname === "/api/invite-session") {
handleInviteSession(req, res, requestUrl);
return;
}
if (req.method === "POST" && pathname === "/api/register") {
await handleRegister(req, res);
return;
}
if (req.method === "POST" && pathname === "/api/verification-code") {
await handleVerificationCode(req, res);
return;
}
if (req.method === "GET") {
handleStatic(req, res, pathname);
return;
}
res.writeHead(405, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Method not allowed");
});
ensureDataFile();
server.listen(PORT, HOST, () => {
console.log(`Clawfish server running on http://${HOST}:${PORT}`);
});