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)) + }) +}