diff options
| -rw-r--r-- | nginx.conf | 19 | ||||
| -rw-r--r-- | server.lua | 48 |
2 files changed, 67 insertions, 0 deletions
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() |
