From d22d972de0af96508b251f68774032426b4a3207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0=20=D0=A4=D0=B0?= =?UTF-8?q?=D1=80=D0=B1=D0=B5=D1=80?= Date: Wed, 21 Oct 2026 16:26:57 +0400 Subject: init --- nginx.conf | 19 +++++++++++++++++++ server.lua | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 nginx.conf create mode 100644 server.lua diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..a037ce3 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,19 @@ +user root; +worker_processes auto; +worker_rlimit_nofile 16000; + +events { + worker_connections 1024; +} + +stream { + lua_code_cache on; + server { + listen 1965 ssl; + ssl_alpn off; + ssl_certificate /etc/letsencrypt/live/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/privkey.pem; + ssl_protocols TLSv1.2 TLSv1.3; + content_by_lua_file /home/gemini/server.lua; + } +} diff --git a/server.lua b/server.lua new file mode 100644 index 0000000..3625423 --- /dev/null +++ b/server.lua @@ -0,0 +1,48 @@ +local root_path = "/home/gemini" + +local function handle_gemini() + local sock = ngx.req.socket(true) + if not sock then return end + + sock:settimeout(5000) + local line, err = sock:receive("*l") + if not line then return end + + -- Декодируем %20 (пробелы) из URL, прилетевшего от Amfora + local url = line:match("gemini://[^/]+(.*)") + if not url or url == "" or url == "/" then + url = "/index.gmi" + end + local path = url:gsub("%%20", " ") + + local local_path = root_path .. path + local file = io.open(local_path, "rb") -- Бинарное чтение rb + + if not file then + sock:send("51 Resource not found\r\n") + return + end + + local content = file:read("*all") + file:close() + + -- Выставляем правильный MIME-тип по расширению + local mime = "text/plain; charset=utf-8" -- Дефолт для неизвестных файлов/логов + + if path:match("%.gmi$") or path:match("%.gemini$") then + mime = "text/gemini; charset=utf-8" + elseif path:match("%.md$") or path:match("%.markdown$") then + mime = "text/markdown; charset=utf-8" + elseif path:match("%.jpg$") or path:match("%.jpeg$") then + mime = "image/jpeg" + elseif path:match("%.png$") then + mime = "image/png" + elseif path:match("%.pdf$") then + mime = "application/pdf" + end + + sock:send("20 " .. mime .. "\r\n") + sock:send(content) +end + +handle_gemini() -- cgit v1.2.3