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
| import http from "http"; import { createFileSessionStorage } from "@react-router/node"; import { parse } from "querystring";
const storage = createFileSessionStorage({ dir: "./sessions", cookie: { name: "session", httpOnly: true, path: "/", sameSite: "lax", secrets: ["mazesec"], } });
const users = []; const ADMIN_USER = { username: "admin", password: "Maze-Sec2026", role: "admin" };
const css = ` body { font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif; background: #fff0f5; color: #4a4a4a; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; } .container { background: white; padding: 2rem; border-radius: 15px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); width: 400px; text-align: center; border: 2px solid #ffbed2; } h1 { color: #d63384; display: flex; align-items: center; justify-content: center; gap: 10px; } input { display: block; width: 100%; padding: 10px; margin: 10px 0; border: 1px solid #ddd; border-radius: 5px; box-sizing: border-box;} button { background: #ff69b4; color: white; border: none; padding: 10px 20px; border-radius: 20px; cursor: pointer; font-size: 16px; transition: background 0.3s; width: 100%; } button:hover { background: #d63384; } .flower-deco { font-size: 40px; margin: 10px 0; } .links { margin-top: 20px; font-size: 14px; } a { color: #d63384; text-decoration: none; } .error { color: red; margin: 10px 0; font-size: 14px; } .dashboard-content { text-align: left; background: #fffafc; padding: 15px; border-radius: 8px; border: 1px dashed #ffbed2; margin-top: 20px; } `;
function renderPage(content, title = "Flower App") { return ` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>${title}</title> <style>${css}</style> </head> <body> <div class="container"> <div class="flower-deco">🌸 🌺 🌻</div> ${content} </div> </body> </html>`; }
function getBody(req) { return new Promise((resolve) => { let body = ""; req.on("data", chunk => { body += chunk.toString(); }); req.on("end", () => { resolve(parse(body)); }); }); }
async function redirect(res, location, cookie = null) { const headers = { "Location": location }; if (cookie) headers["Set-Cookie"] = cookie; res.writeHead(302, headers); res.end(); }
http.createServer(async (req, res) => { const url = new URL(req.url, `http://${req.headers.host}`); const session = await storage.getSession(req.headers.cookie); const flashError = session.get("error"); if (flashError) session.unset("error");
const setHtmlHeader = () => res.setHeader("Content-Type", "text/html; charset=utf-8");
if (req.method === "GET" && url.pathname === "/") { const userId = session.get("userId"); if (userId) { return redirect(res, "/dashboard"); }
const html = renderPage(` <h1>欢迎来到花园系统</h1> <p>请登录以查看我们的专属花卉收藏。</p> <div class="links"> <a href="/login">登录</a> | <a href="/register">注册</a> </div> `); setHtmlHeader(); res.setHeader("Set-Cookie", await storage.commitSession(session)); return res.end(html); }
if (req.method === "GET" && url.pathname === "/login") { const html = renderPage(` <h1>🌺 登录</h1> ${flashError ? `<div class="error">${flashError}</div>` : ''} <form method="POST" action="/login"> <input type="text" name="username" placeholder="用户名" required /> <input type="password" name="password" placeholder="密码" required /> <button type="submit">进入花园</button> </form> <div class="links"><a href="/register">没有账号?点击注册</a></div> `, "Login"); setHtmlHeader(); res.setHeader("Set-Cookie", await storage.commitSession(session)); return res.end(html); }
if (req.method === "POST" && url.pathname === "/login") { const body = await getBody(req); const { username, password } = body;
let user = null; if (username === ADMIN_USER.username && password === ADMIN_USER.password) { user = ADMIN_USER; } else { user = users.find(u => u.username === username && u.password === password); }
if (user) { session.set("userId", user.username); session.set("role", user.role || "user"); return redirect(res, "/dashboard", await storage.commitSession(session)); } else { session.flash("error", "凭证无效 🥀"); return redirect(res, "/login", await storage.commitSession(session)); } }
if (req.method === "GET" && url.pathname === "/register") { const html = renderPage(` <h1>🌻 注册</h1> ${flashError ? `<div class="error">${flashError}</div>` : ''} <form method="POST" action="/register"> <input type="text" name="username" placeholder="设置用户名" required /> <input type="password" name="password" placeholder="设置密码" required /> <button type="submit">加入俱乐部</button> </form> <div class="links"><a href="/login">已有账号?去登录</a></div> `, "Register");
setHtmlHeader(); res.setHeader("Set-Cookie", await storage.commitSession(session)); return res.end(html); }
if (req.method === "POST" && url.pathname === "/register") { const body = await getBody(req); const { username, password } = body;
if (users.find(u => u.username === username) || username === "admin") { session.flash("error", "用户名已被占用 🌵"); return redirect(res, "/register", await storage.commitSession(session)); }
users.push({ username, password, role: "user" }); session.flash("error", "注册成功!请登录 🌹"); return redirect(res, "/login", await storage.commitSession(session)); }
if (req.method === "POST" && url.pathname === "/logout") { return redirect(res, "/", await storage.destroySession(session)); }
if (req.method === "GET" && url.pathname === "/dashboard") { const userId = session.get("userId"); const role = session.get("role");
if (!userId) { return redirect(res, "/login"); }
let adminContent = ""; if (role === "admin" || userId === "admin") { adminContent = ` <div style="background: #ffe6e6; border: 2px solid red; padding: 10px; margin-top: 10px;"> <h3>👑 管理员面板</h3> <p><strong>Secret Flag:</strong> FLAG{FLOWER_POWER_OVERFLOW}</p> <p>你已获得花园的完全控制权。</p> </div> `; }
const html = renderPage(` <h1>花园仪表盘 🌼</h1> <p>你好, <strong>${userId}</strong>!</p> <div class="dashboard-content"> <p>当前身份: <strong>${role === 'admin' ? '首席园丁 👒' : '访客 🐝'}</strong></p> <p>享受这宁静的氛围吧...</p> ${adminContent} </div> <form method="POST" action="/logout" style="margin-top: 20px;"> <button type="submit" style="background: #999;">退出登录</button> </form> `, "Dashboard");
setHtmlHeader(); return res.end(html); }
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }); res.end("页面未找到");
}).listen(3000, () => { console.log("🌸 Flower Server running on http://localhost:3000"); });
|