From d049e272ecf44e70810c213ed045779212adb662 Mon Sep 17 00:00:00 2001 From: kzolotarev95 Date: Sun, 28 Jun 2026 22:35:26 +0400 Subject: [PATCH] Remove Lua dependency from LuCI integration --- .gitattributes | 3 +- README.md | 41 +- .../lua/luci/controller/owrt_full_backup.lua | 23 - files/usr/sbin/owrt-full-backup-upload | 433 ++++++++++-------- .../menu.d/luci-app-owrt-full-backup.json | 8 +- .../rpcd/acl.d/luci-app-owrt-full-backup.json | 17 + .../resources/view/owrt_full_backup.js | 43 ++ install.sh | 4 +- uninstall.sh | 2 + 9 files changed, 338 insertions(+), 236 deletions(-) delete mode 100644 files/usr/lib/lua/luci/controller/owrt_full_backup.lua create mode 100644 files/usr/share/rpcd/acl.d/luci-app-owrt-full-backup.json create mode 100644 files/www/luci-static/resources/view/owrt_full_backup.js diff --git a/.gitattributes b/.gitattributes index e844d67..6df6e89 100644 --- a/.gitattributes +++ b/.gitattributes @@ -12,5 +12,6 @@ files/usr/sbin/owrt-full-backup text eol=lf files/usr/sbin/owrt-full-backup-upload text eol=lf files/etc/config/fullbackup text eol=lf files/www/cgi-bin/owrt-full-backup text eol=lf -files/usr/lib/lua/luci/controller/owrt_full_backup.lua text eol=lf files/usr/share/luci/menu.d/luci-app-owrt-full-backup.json text eol=lf +files/usr/share/rpcd/acl.d/luci-app-owrt-full-backup.json text eol=lf +files/www/luci-static/resources/view/owrt_full_backup.js text eol=lf diff --git a/README.md b/README.md index 5210e10..a164884 100644 --- a/README.md +++ b/README.md @@ -67,18 +67,20 @@ wget -O - "https://raw.githubusercontent.com/kzolotarev95/luci-app-owrt-full-bac │ ├── sbin │ │ ├── owrt-full-backup │ │ └── owrt-full-backup-upload - │ ├── lib - │ │ └── lua - │ │ └── luci - │ │ └── controller - │ │ └── owrt_full_backup.lua │ └── share - │ └── luci - │ └── menu.d + │ ├── luci + │ │ └── menu.d + │ │ └── luci-app-owrt-full-backup.json + │ └── rpcd + │ └── acl.d │ └── luci-app-owrt-full-backup.json └── www - └── cgi-bin - └── owrt-full-backup + ├── cgi-bin + │ └── owrt-full-backup + └── luci-static + └── resources + └── view + └── owrt_full_backup.js ``` ## Что ставится на роутер @@ -86,10 +88,11 @@ wget -O - "https://raw.githubusercontent.com/kzolotarev95/luci-app-owrt-full-bac | Путь | Назначение | | --- | --- | | `/usr/sbin/owrt-full-backup` | CLI-команда для создания, просмотра и восстановления архива | -| `/usr/sbin/owrt-full-backup-upload` | Потоковая загрузка `.tar.gz` из браузера без удержания всего файла в RAM | +| `/usr/sbin/owrt-full-backup-upload` | Потоковая загрузка `.tar.gz` из браузера без Lua | | `/www/cgi-bin/owrt-full-backup` | Веб-панель | -| `/usr/lib/lua/luci/controller/owrt_full_backup.lua` | Редирект из LuCI в веб-панель с приватным ключом | +| `/www/luci-static/resources/view/owrt_full_backup.js` | LuCI-страница-редирект без Lua runtime | | `/usr/share/luci/menu.d/luci-app-owrt-full-backup.json` | Пункт меню LuCI для OpenWrt 22-25 | +| `/usr/share/rpcd/acl.d/luci-app-owrt-full-backup.json` | Доступ LuCI к приватному веб-ключу | | `/etc/config/fullbackup` | Настройки по умолчанию | | `/etc/owrt-full-backup/web.key` | Приватный ключ веб-панели | @@ -159,6 +162,22 @@ rm -rf /tmp/luci-indexcache /tmp/luci-modulecache /tmp/luci-indexcache.* /tmp/lu Потом обнови страницу LuCI или выйди из LuCI и зайди снова. Прямая приватная ссылка из вывода `install.sh` тоже всегда работает. +## Если в LuCI ошибка `No Lua runtime installed` + +Поставь свежую версию. Модуль больше не использует Lua-controller для пункта меню LuCI: + +```sh +wget -O - "https://raw.githubusercontent.com/kzolotarev95/luci-app-owrt-full-backup/main/install.sh?v=$(date +%s)" | sh +``` + +Установщик удалит старый файл: + +```text +/usr/lib/lua/luci/controller/owrt_full_backup.lua +``` + +И заменит его на LuCI JS-view + rpcd ACL. + ## Если ошибка `No space left on device` Причина почти всегда одна: архив создается в `/tmp`, а там RAM закончилась. diff --git a/files/usr/lib/lua/luci/controller/owrt_full_backup.lua b/files/usr/lib/lua/luci/controller/owrt_full_backup.lua deleted file mode 100644 index 7dca09d..0000000 --- a/files/usr/lib/lua/luci/controller/owrt_full_backup.lua +++ /dev/null @@ -1,23 +0,0 @@ -module("luci.controller.owrt_full_backup", package.seeall) - -function index() - entry({"admin", "services", "owrt-full-backup"}, call("action_open"), "OpenWrt Full Backup", 90).dependent = false -end - -function action_open() - local http = require "luci.http" - local fs = require "nixio.fs" - local key = fs.readfile("/etc/owrt-full-backup/web.key") or "" - - key = key:gsub("%s+", "") - if key == "" then - http.prepare_content("text/html; charset=utf-8") - http.write("OpenWrt Full Backup") - http.write("

OpenWrt Full Backup

") - http.write("

Веб-ключ не найден. Переустанови модуль командой sh install.sh.

") - http.write("") - return - end - - http.redirect("/cgi-bin/owrt-full-backup?key=" .. key) -end diff --git a/files/usr/sbin/owrt-full-backup-upload b/files/usr/sbin/owrt-full-backup-upload index 8403e00..64dc959 100644 --- a/files/usr/sbin/owrt-full-backup-upload +++ b/files/usr/sbin/owrt-full-backup-upload @@ -1,239 +1,278 @@ -#!/usr/bin/lua +#!/usr/bin/ucode +'use strict'; -local default_dir = arg[1] or "/tmp" -local content_type = os.getenv("CONTENT_TYPE") or "" -local content_length = tonumber(os.getenv("CONTENT_LENGTH") or "0") or 0 -local pending = "" -local target_tmp = nil +import * as fs from 'fs'; -local function cleanup() - if target_tmp then - os.remove(target_tmp) - end -end +const default_dir = ARGV[0] || '/tmp'; +const content_type = getenv('CONTENT_TYPE') || ''; +let input_available = +getenv('CONTENT_LENGTH') || 0; +let input = fs.fdopen(0, 'r'); +let output = fs.fdopen(1, 'w'); +let error = fs.fdopen(2, 'w'); +let pending = ''; +let target_tmp = null; -local function fail(message) - cleanup() - io.stderr:write("ОШИБКА: " .. message .. "\n") - os.exit(1) -end +function cleanup() { + if (target_tmp) + fs.unlink(target_tmp); +} -local function ok(message) - io.stdout:write(message .. "\n") -end +function fail(message) { + cleanup(); + error.write('ОШИБКА: ' + message + '\n'); + exit(1); +} -local function valid_path(path) - return type(path) == "string" and path:match("^/[A-Za-z0-9%._/%-]+$") ~= nil -end +function ok(message) { + output.write(message + '\n'); +} -local function shell_quote(value) - return "'" .. tostring(value):gsub("'", "'\\''") .. "'" -end +function valid_path(path) { + return type(path) == 'string' && match(path, /^\/[A-Za-z0-9._\/-]+$/); +} -local function safe_filename(name) - name = tostring(name or ""):gsub("\\", "/") - name = name:match("([^/]+)$") or "" - name = name:gsub("[^A-Za-z0-9%._%-]", "_") - return name -end +function safe_filename(name) { + name = name != null ? '' + name : ''; + name = replace(name, /\\/g, '/'); + name = fs.basename(name); + return replace(name, /[^A-Za-z0-9._-]/g, '_'); +} -local function trim_crlf(line) - return tostring(line or ""):gsub("\r\n$", ""):gsub("\n$", "") -end +function trim_crlf(line) { + line = line != null ? '' + line : ''; + line = replace(line, /\r\n$/, ''); + return replace(line, /\n$/, ''); +} -local function read_from_stdin(size) - return io.stdin:read(size or 8192) -end +function append_array(array, value) { + array[length(array)] = value; +} -local function read_line() - while true do - local pos = pending:find("\n", 1, true) - if pos then - local line = pending:sub(1, pos) - pending = pending:sub(pos + 1) - return line - end +function concat_array(array, separator) { + let result = ''; - local chunk = read_from_stdin(1024) - if not chunk or #chunk == 0 then - if #pending > 0 then - local line = pending - pending = "" - return line - end - return nil - end + for (let i = 0; i < length(array); i++) { + if (i > 0) + result += separator; - pending = pending .. chunk - end -end + result += array[i]; + } -local boundary = content_type:match('boundary="?([^";]+)"?') -if not boundary or boundary == "" then - fail("multipart boundary не найден") -end + return result; +} -if content_length <= 0 then - fail("пустой upload") -end +function read_from_stdin(size) { + if (input_available <= 0) + return null; -local marker = "--" .. boundary -local delimiter = "\r\n" .. marker + size = size != null ? size : 8192; + let chunk = input.read(min(input_available, size, 8192)); -local first_line = trim_crlf(read_line()) -if first_line ~= marker then - fail("multipart boundary не найден в теле запроса") -end + if (chunk == null) { + input_available = 0; + return null; + } -local function read_headers() - local headers = {} + input_available -= length(chunk); + return chunk; +} - while true do - local line = read_line() - if not line then - fail("не удалось прочитать заголовки upload") - end +function read_line() { + while (true) { + let pos = index(pending, '\n'); - line = trim_crlf(line) - if line == "" then - break - end + if (pos != null && pos >= 0) { + let line = substr(pending, 0, pos + 1); + pending = substr(pending, pos + 1); + return line; + } - headers[#headers + 1] = line - end + let chunk = read_from_stdin(1024); - return table.concat(headers, "\n") -end + if (chunk == null) { + if (length(pending) > 0) { + let line = pending; + pending = ''; + return line; + } -local function read_part(out, max_text) - local keep = #delimiter + 4 - local buffer = "" - local text = {} - local text_size = 0 - local part_size = 0 + return null; + } - local function consume(data) - if not data or #data == 0 then - return - end + pending += chunk; + } +} - part_size = part_size + #data - if out then - local wrote, err = out:write(data) - if not wrote then - fail("не удалось записать upload: " .. tostring(err)) - end - return - end +let boundary_match = match(content_type, /boundary="?([^";]+)"?/); +let boundary = boundary_match ? boundary_match[1] : null; - text_size = text_size + #data - if text_size > (max_text or 4096) then - fail("поле формы слишком большое") - end - text[#text + 1] = data - end +if (!boundary) + fail('multipart boundary не найден'); - while true do - local chunk - if #pending > 0 then - chunk = pending - pending = "" - else - chunk = read_from_stdin(8192) - end +if (input_available <= 0) + fail('пустой upload'); - if not chunk then - fail("не найден конец multipart upload") - end +const marker = '--' + boundary; +const delimiter = '\r\n' + marker; - buffer = buffer .. chunk - local start_pos, end_pos = buffer:find(delimiter, 1, true) - if start_pos then - consume(buffer:sub(1, start_pos - 1)) +let first_line = trim_crlf(read_line()); +if (first_line != marker) + fail('multipart boundary не найден в теле запроса'); - local after = end_pos + 1 - local final = false - if buffer:sub(after, after + 1) == "--" then - final = true - after = after + 2 - end - if buffer:sub(after, after + 1) == "\r\n" then - after = after + 2 - end +function read_headers() { + let headers = []; - pending = buffer:sub(after) - return final, table.concat(text), part_size - end + while (true) { + let line = read_line(); - if #buffer > keep then - consume(buffer:sub(1, #buffer - keep)) - buffer = buffer:sub(#buffer - keep + 1) - end - end -end + if (line == null) + fail('не удалось прочитать заголовки upload'); -local upload_dir = default_dir -local saved_path = nil -local saved_size = 0 -local final = false + line = trim_crlf(line); -while not final do - local headers = read_headers() - local disposition = headers:match("[Cc]ontent%-[Dd]isposition:%s*([^\n]+)") - local name = disposition and disposition:match('name="([^"]+)"') - local filename = disposition and disposition:match('filename="([^"]*)"') + if (line == '') + break; - if name == "upload_dir" and not filename then - local value - final, value = read_part(nil, 4096) - upload_dir = tostring(value or ""):gsub("[%z\r\n]", "") - elseif name == "archive" and filename and filename ~= "" then - if not valid_path(upload_dir) then - fail("неверная папка для загрузки: " .. tostring(upload_dir)) - end + append_array(headers, line); + } - local file_name = safe_filename(filename) - if file_name == "" then - fail("файл архива не выбран") - end - if not file_name:match("%.tar%.gz$") then - fail("можно загружать только архивы .tar.gz") - end + return concat_array(headers, '\n'); +} - os.execute("mkdir -p " .. shell_quote(upload_dir)) +function read_part(out, max_text) { + let keep = length(delimiter) + 4; + let buffer = ''; + let text = []; + let text_size = 0; + let part_size = 0; - local target = upload_dir:gsub("/+$", "") .. "/" .. file_name - target_tmp = target .. ".uploading." .. tostring(os.time()) .. "." .. tostring(content_length) - local out, err = io.open(target_tmp, "wb") - if not out then - fail("не удалось открыть файл для записи: " .. target .. ": " .. tostring(err)) - end + function consume(data) { + if (data == null || length(data) == 0) + return; - local _ - final, _, saved_size = read_part(out, 0) - local closed, close_err = out:close() - if not closed then - fail("не удалось закрыть файл: " .. target .. ": " .. tostring(close_err)) - end - if saved_size <= 0 then - fail("файл пустой") - end + part_size += length(data); - local renamed, rename_err = os.rename(target_tmp, target) - if not renamed then - fail("не удалось сохранить файл: " .. target .. ": " .. tostring(rename_err)) - end + if (out) { + out.write(data); + return; + } - target_tmp = nil - saved_path = target - else - final = read_part(nil, 1024) - end -end + text_size += length(data); -if not saved_path then - fail("файл архива не выбран") -end + max_text = max_text != null ? max_text : 4096; -ok("Архив загружен: " .. saved_path) -ok("Размер: " .. tostring(saved_size) .. " байт") + if (text_size > max_text) + fail('поле формы слишком большое'); + + append_array(text, data); + } + + while (true) { + let chunk; + + if (length(pending) > 0) { + chunk = pending; + pending = ''; + } + else { + chunk = read_from_stdin(8192); + } + + if (chunk == null) + fail('не найден конец multipart upload'); + + buffer += chunk; + + let start_pos = index(buffer, delimiter); + + if (start_pos != null && start_pos >= 0) { + consume(substr(buffer, 0, start_pos)); + + let after = start_pos + length(delimiter); + let final = false; + + if (substr(buffer, after, 2) == '--') { + final = true; + after += 2; + } + + if (substr(buffer, after, 2) == '\r\n') + after += 2; + + pending = substr(buffer, after); + return [ final, concat_array(text, ''), part_size ]; + } + + if (length(buffer) > keep) { + consume(substr(buffer, 0, length(buffer) - keep)); + buffer = substr(buffer, length(buffer) - keep); + } + } +} + +let upload_dir = default_dir; +let saved_path = null; +let saved_size = 0; +let final = false; + +while (!final) { + let headers = read_headers(); + let disposition_match = match(headers, /[Cc]ontent-[Dd]isposition:\s*([^\n]+)/); + let disposition = disposition_match ? disposition_match[1] : null; + let name_match = disposition ? match(disposition, /name="([^"]+)"/) : null; + let filename_match = disposition ? match(disposition, /filename="([^"]*)"/) : null; + let name = name_match ? name_match[1] : null; + let filename = filename_match ? filename_match[1] : null; + + if (name == 'upload_dir' && filename == null) { + let part = read_part(null, 4096); + final = part[0]; + upload_dir = part[1] != null ? '' + part[1] : ''; + upload_dir = replace(upload_dir, /[\r\n]/g, ''); + } + else if (name == 'archive' && filename != null && filename != '') { + if (!valid_path(upload_dir)) + fail('неверная папка для загрузки: ' + upload_dir); + + let file_name = safe_filename(filename); + + if (!file_name) + fail('файл архива не выбран'); + + if (!match(file_name, /\.tar\.gz$/)) + fail('можно загружать только архивы .tar.gz'); + + fs.mkdir(upload_dir, 0755); + + let target = replace(upload_dir, /\/+$/, '') + '/' + file_name; + target_tmp = target + '.uploading.' + time() + '.' + input_available; + + let out = fs.open(target_tmp, 'w', 384); + if (!out) + fail('не удалось открыть файл для записи: ' + target); + + let part = read_part(out, 0); + final = part[0]; + saved_size = part[2]; + out.close(); + + if (saved_size <= 0) + fail('файл пустой'); + + if (!fs.rename(target_tmp, target)) + fail('не удалось сохранить файл: ' + target); + + target_tmp = null; + saved_path = target; + } + else { + let part = read_part(null, 1024); + final = part[0]; + } +} + +if (!saved_path) + fail('файл архива не выбран'); + +ok('Архив загружен: ' + saved_path); +ok('Размер: ' + saved_size + ' байт'); diff --git a/files/usr/share/luci/menu.d/luci-app-owrt-full-backup.json b/files/usr/share/luci/menu.d/luci-app-owrt-full-backup.json index ff3ff44..fa7053d 100644 --- a/files/usr/share/luci/menu.d/luci-app-owrt-full-backup.json +++ b/files/usr/share/luci/menu.d/luci-app-owrt-full-backup.json @@ -3,11 +3,13 @@ "title": "OpenWrt Full Backup", "order": 90, "action": { - "type": "call", - "module": "luci.controller.owrt_full_backup", - "function": "action_open" + "type": "view", + "path": "owrt_full_backup" }, "depends": { + "acl": [ + "luci-app-owrt-full-backup" + ], "fs": { "/www/cgi-bin/owrt-full-backup": "executable" } diff --git a/files/usr/share/rpcd/acl.d/luci-app-owrt-full-backup.json b/files/usr/share/rpcd/acl.d/luci-app-owrt-full-backup.json new file mode 100644 index 0000000..7f8979b --- /dev/null +++ b/files/usr/share/rpcd/acl.d/luci-app-owrt-full-backup.json @@ -0,0 +1,17 @@ +{ + "luci-app-owrt-full-backup": { + "description": "Grant access to OpenWrt Full Backup LuCI helper", + "read": { + "ubus": { + "file": [ + "read" + ] + }, + "file": { + "/etc/owrt-full-backup/web.key": [ + "read" + ] + } + } + } +} diff --git a/files/www/luci-static/resources/view/owrt_full_backup.js b/files/www/luci-static/resources/view/owrt_full_backup.js new file mode 100644 index 0000000..7998201 --- /dev/null +++ b/files/www/luci-static/resources/view/owrt_full_backup.js @@ -0,0 +1,43 @@ +'use strict'; +'require view'; +'require fs'; +'require ui'; + +return view.extend({ + load: function() { + return fs.read('/etc/owrt-full-backup/web.key').catch(function() { + return ''; + }); + }, + + render: function(key) { + key = (key || '').trim(); + + if (!key) { + return E('div', { 'class': 'cbi-map' }, [ + E('h2', {}, _('OpenWrt Full Backup')), + E('div', { 'class': 'alert-message warning' }, [ + _('Веб-ключ не найден. Переустанови модуль командой install.sh.') + ]) + ]); + } + + var url = '/cgi-bin/owrt-full-backup?key=' + encodeURIComponent(key); + + window.setTimeout(function() { + window.location.replace(url); + }, 100); + + return E('div', { 'class': 'cbi-map' }, [ + E('h2', {}, _('OpenWrt Full Backup')), + E('p', {}, _('Открываю веб-панель полного бэкапа...')), + E('p', {}, [ + E('a', { 'class': 'btn cbi-button cbi-button-apply', 'href': url }, _('Открыть панель')) + ]) + ]); + }, + + handleSaveApply: null, + handleSave: null, + handleReset: null +}); diff --git a/install.sh b/install.sh index 7cbf428..13fcab5 100644 --- a/install.sh +++ b/install.sh @@ -106,8 +106,10 @@ install_file "usr/sbin/owrt-full-backup-upload" 0755 ln -sf owrt-full-backup "$(target_path usr/sbin/owrt-backup)" install_config install_file "www/cgi-bin/owrt-full-backup" 0755 -install_file "usr/lib/lua/luci/controller/owrt_full_backup.lua" 0644 install_file "usr/share/luci/menu.d/luci-app-owrt-full-backup.json" 0644 +install_file "usr/share/rpcd/acl.d/luci-app-owrt-full-backup.json" 0644 +install_file "www/luci-static/resources/view/owrt_full_backup.js" 0644 +rm -f "$(target_path usr/lib/lua/luci/controller/owrt_full_backup.lua)" 2>/dev/null || true rm -rf "$(target_path tmp/luci-indexcache)" "$(target_path tmp/luci-modulecache)" "$(target_path tmp/luci-indexcache.)"* "$(target_path tmp/luci-modulecache.)"* 2>/dev/null || true diff --git a/uninstall.sh b/uninstall.sh index 19a07c3..aa22c69 100644 --- a/uninstall.sh +++ b/uninstall.sh @@ -19,6 +19,8 @@ rmf usr/sbin/owrt-backup rmf www/cgi-bin/owrt-full-backup rmf usr/lib/lua/luci/controller/owrt_full_backup.lua rmf usr/share/luci/menu.d/luci-app-owrt-full-backup.json +rmf usr/share/rpcd/acl.d/luci-app-owrt-full-backup.json +rmf www/luci-static/resources/view/owrt_full_backup.js if [ "$PURGE" = "1" ]; then rmf etc/config/fullbackup