Revert "wol-gate: WAKE_KEY 설정 시 키(쿠키/쿼리)가 있는 요청과 LAN만 기상시킴"
배포 안 하기로 결정(키 관리 부담). 운영 중인 wol-gate는 변경 없음. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7a29708743
commit
d47aed5a9a
1 file changed
+4
-70
+4
-70
@@ -17,11 +17,6 @@ const LISTEN_PORT = parseInt(process.env.LISTEN_PORT || '8099', 10);
|
||||
const HEALTH_TIMEOUT_MS = parseInt(process.env.HEALTH_TIMEOUT_MS || '1200', 10);
|
||||
const WAKE_COOLDOWN_MS = parseInt(process.env.WAKE_COOLDOWN_MS || '30000', 10);
|
||||
const REFRESH_SECONDS = parseInt(process.env.REFRESH_SECONDS || '5', 10);
|
||||
// 선택: WAKE_KEY가 설정되면 매직패킷은 키를 가진 요청(쿠키 wg_key 또는 ?wake=키)과 사설망(LAN)에서만 보낸다.
|
||||
// 외부 봇/스캐너가 ai.applecherry.net을 두드려 서버를 깨우는 것을 막는다(2026-10-15 SK IDC 대역 사례).
|
||||
// 비어 있으면 기존 동작(국내 IP면 깨움) 그대로.
|
||||
const WAKE_KEY = process.env.WAKE_KEY || '';
|
||||
const WAKE_COOKIE = 'wg_key';
|
||||
|
||||
let lastWakeAt = 0;
|
||||
|
||||
@@ -199,53 +194,16 @@ function describeRequester(req) {
|
||||
return `${ip} "${req.method} ${host}${req.url}" UA="${ua}"`;
|
||||
}
|
||||
|
||||
function parseCookies(header) {
|
||||
const out = {};
|
||||
for (const part of String(header || '').split(';')) {
|
||||
const i = part.indexOf('=');
|
||||
if (i > 0) out[part.slice(0, i).trim()] = part.slice(i + 1).trim();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function safeEqual(a, b) {
|
||||
const ab = Buffer.from(String(a));
|
||||
const bb = Buffer.from(String(b));
|
||||
return ab.length === bb.length && require('crypto').timingSafeEqual(ab, bb);
|
||||
}
|
||||
|
||||
function isPrivateIp(ip) {
|
||||
const n = ipToInt(ip || '');
|
||||
return n !== null && inSortedRanges(n, PRIVATE_V4_RANGES);
|
||||
}
|
||||
|
||||
// 키 확인: 쿠키 또는 쿼리. 쿼리로 들어온 경우 쿠키를 심어 이후 요청이 자동 통과하게 한다.
|
||||
function wakeKeyState(req) {
|
||||
if (!WAKE_KEY) return { required: false, ok: true, setCookie: false };
|
||||
const cookieVal = parseCookies(req.headers['cookie'])[WAKE_COOKIE];
|
||||
if (cookieVal && safeEqual(cookieVal, WAKE_KEY)) return { required: true, ok: true, setCookie: false };
|
||||
try {
|
||||
const q = new URL(req.url, 'http://x').searchParams.get('wake');
|
||||
if (q && safeEqual(q, WAKE_KEY)) return { required: true, ok: true, setCookie: true };
|
||||
} catch { /* 잘못된 URL은 키 없음으로 */ }
|
||||
return { required: true, ok: false, setCookie: false };
|
||||
}
|
||||
|
||||
function maybeWake(req) {
|
||||
const ip = extractClientIp(req);
|
||||
const key = wakeKeyState(req);
|
||||
if (key.required && !key.ok && !isPrivateIp(ip)) {
|
||||
console.log(`[wol-gate] wake denied (no key) - triggered by ${describeRequester(req)}`);
|
||||
return false;
|
||||
}
|
||||
if (!isDomesticIp(ip)) {
|
||||
console.log(`[wol-gate] wake blocked (non-KR) - triggered by ${describeRequester(req)}`);
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
const now = Date.now();
|
||||
if (now - lastWakeAt <= WAKE_COOLDOWN_MS) {
|
||||
console.log(`[wol-gate] wake suppressed (cooldown) - triggered by ${describeRequester(req)}`);
|
||||
return true;
|
||||
return;
|
||||
}
|
||||
lastWakeAt = now;
|
||||
console.log(`[wol-gate] waking - triggered by ${describeRequester(req)}`);
|
||||
@@ -254,20 +212,6 @@ function maybeWake(req) {
|
||||
} catch (e) {
|
||||
console.error('[wol-gate]', e.message);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function renderSleepingPage() {
|
||||
return `<!doctype html>
|
||||
<html lang="ko"><head><meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>서버 절전 중</title>
|
||||
<style>
|
||||
body{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;background:#111;color:#eee;
|
||||
display:flex;flex-direction:column;align-items:center;justify-content:center;height:100vh;margin:0;gap:12px;padding:0 16px;text-align:center}
|
||||
p{color:#999;font-size:14px}
|
||||
</style></head>
|
||||
<body><div>서버가 절전 중입니다.</div><p>자동 깨우기는 승인된 기기에서만 동작합니다.</p></body></html>`;
|
||||
}
|
||||
|
||||
function proxyRequest(req, res) {
|
||||
@@ -290,18 +234,8 @@ const server = http.createServer(async (req, res) => {
|
||||
proxyRequest(req, res);
|
||||
return;
|
||||
}
|
||||
const woke = maybeWake(req);
|
||||
if (!woke) {
|
||||
res.writeHead(403, { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store' });
|
||||
res.end(renderSleepingPage());
|
||||
return;
|
||||
}
|
||||
const headers = { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store' };
|
||||
const key = wakeKeyState(req);
|
||||
if (key.setCookie) {
|
||||
headers['Set-Cookie'] = `${WAKE_COOKIE}=${WAKE_KEY}; Max-Age=31536000; Path=/; Secure; HttpOnly; SameSite=Lax`;
|
||||
}
|
||||
res.writeHead(200, headers);
|
||||
maybeWake(req);
|
||||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
||||
res.end(renderWakingPage());
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user