From e22f084940962a0be0ddeb08365859899b8fde0c Mon Sep 17 00:00:00 2001 From: Kirill Date: Sat, 23 May 2026 11:00:02 +0500 Subject: [PATCH] feat: add IP gate plugin with SITE_ACCESS_IPS env var support --- server/src/plugins/ip-gate.js | 95 +++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 server/src/plugins/ip-gate.js diff --git a/server/src/plugins/ip-gate.js b/server/src/plugins/ip-gate.js new file mode 100644 index 0000000..c69e6ba --- /dev/null +++ b/server/src/plugins/ip-gate.js @@ -0,0 +1,95 @@ +const EXCLUDED_PATHS = [ + '/api/auth/oauth/vk/callback', + '/api/auth/oauth/yandex/callback', + '/api/webhooks/yookassa', + '/api/admin/notifications/telegram/webhook', +] + +function build403Html(ip) { + const safeIp = ip || 'не определён' + return ` + + + + +Любимый Креатив — Доступ запрещён + + + +
+

Любимый Креатив

+

Изделия ручной работы: вещи с характером и вниманием к деталям

+

Сайт находится в разработке
и скоро будет доступен

+

Ваш IP: ${safeIp}

+
+ +` +} + +export async function registerIpGate(fastify) { + fastify.addHook('onRequest', async (request, reply) => { + const allowed = process.env.SITE_ACCESS_IPS + if (!allowed) return + + const allowedIps = allowed + .split(',') + .map((s) => s.trim()) + .filter(Boolean) + + if (allowedIps.length === 0) return + + const urlPath = request.url.split('?')[0] + + if (EXCLUDED_PATHS.includes(urlPath)) return + + if (allowedIps.includes(request.ip)) return + + return reply.code(403).type('text/html').send(build403Html(request.ip)) + }) +}