From c217b83ad89e791dfe4a7affc2f80f7ffa8803be Mon Sep 17 00:00:00 2001 From: Petro1990 Date: Mon, 24 Nov 2025 23:22:11 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20PWA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install.sh | 11 ++++++++++ mihomo_editor.py | 43 +++++++++++++++++++++++++++++++++++++--- public/service-worker.js | 28 ++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 public/service-worker.js diff --git a/install.sh b/install.sh index aead6da..b0e3ce2 100644 --- a/install.sh +++ b/install.sh @@ -1,5 +1,12 @@ #!/bin/sh +# Проверка наличия openssl +if ! command -v openssl &> /dev/null; then + echo "openssl not found. Please install it first." + # opkg update && opkg install openssl-utils (example for OpenWrt) + exit 1 +fi + # Определяем директорию, в которой находится скрипт SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) @@ -70,6 +77,10 @@ wget --no-check-certificate -O "$ICONS_DIR/icon-192x192.png" "$ICON192_URL" echo "Загрузка icon-512x512.png..." wget --no-check-certificate -O "$ICONS_DIR/icon-512x512.png" "$ICON512_URL" +# Загружаем service-worker.js +echo "Загрузка service-worker.js..." +wget -O "$PWA_DIR/service-worker.js" "$BASE_URL/public/service-worker.js" + # Скачиваем основной скрипт echo "Загрузка $PY_SCRIPT..." wget --no-check-certificate -O "$INSTALL_DIR/$PY_SCRIPT" "$BASE_URL/$PY_SCRIPT" diff --git a/mihomo_editor.py b/mihomo_editor.py index 0ca725b..cea264b 100644 --- a/mihomo_editor.py +++ b/mihomo_editor.py @@ -12,6 +12,7 @@ import time import shutil import glob import json +import ssl from datetime import datetime # --- НАСТРОЙКИ --- @@ -677,6 +678,15 @@ function doRename() { }) .catch(e => alert("Сетевая ошибка: " + e)); } + if ('serviceWorker' in navigator) { + window.addEventListener('load', () => { + navigator.serviceWorker.register('/service-worker.js').then(registration => { + console.log('ServiceWorker registration successful with scope: ', registration.scope); + }, err => { + console.log('ServiceWorker registration failed: ', err); + }); + }); + } """ @@ -779,6 +789,17 @@ class H(http.server.SimpleHTTPRequestHandler): pass # Silent fail to avoid crashing def do_GET(s): + if s.path == '/service-worker.js': + try: + with open('public/service-worker.js', 'rb') as f: + s.send_response(200) + s.send_header('Content-type', 'application/javascript') + s.end_headers() + s.wfile.write(f.read()) + except FileNotFoundError: + s.send_error(404, "File not found") + return + if s.path == '/manifest.json': try: with open('public/manifest.json', 'rb') as f: @@ -1007,7 +1028,23 @@ class H(http.server.SimpleHTTPRequestHandler): try: - socketserver.TCPServer.allow_reuse_address = True; - socketserver.TCPServer(("", PORT), H).serve_forever() + # Создаем самоподписанный сертификат "на лету" + certfile = '/tmp/mihomo_cert.pem' + keyfile = '/tmp/mihomo_key.pem' + if not os.path.exists(certfile): + subprocess.call('openssl req -new -x509 -keyout {k} -out {c} -days 365 -nodes -subj "/C=US/ST=CA/L=./O=./CN=localhost"'.format(k=keyfile, c=certfile), shell=True, stderr=subprocess.DEVNULL) + + server_address = ('', PORT) + httpd = http.server.HTTPServer(server_address, H) + httpd.socket = ssl.wrap_socket(httpd.socket, + server_side=True, + certfile=certfile, + keyfile=keyfile, + ssl_version=ssl.PROTOCOL_TLS) + print(f"Server running on https://localhost:{PORT}") + httpd.serve_forever() except Exception as e: - print(e) \ No newline at end of file + print(f"Failed to start HTTPS server: {e}") + print("Fallback to HTTP...") + socketserver.TCPServer.allow_reuse_address = True + socketserver.TCPServer(("", PORT), H).serve_forever() \ No newline at end of file diff --git a/public/service-worker.js b/public/service-worker.js new file mode 100644 index 0000000..abbb679 --- /dev/null +++ b/public/service-worker.js @@ -0,0 +1,28 @@ +// Список ресурсов для кэширования +const CACHE_NAME = 'mihomo-studio-cache-v1'; +const urlsToCache = [ + '/', + '/manifest.json', + '/icons/icon-192x192.png', + '/icons/icon-512x512.png' +]; + +// Установка Service Worker и кэширование ресурсов +self.addEventListener('install', event => { + event.waitUntil( + caches.open(CACHE_NAME) + .then(cache => { + console.log('Opened cache'); + return cache.addAll(urlsToCache); + }) + ); +}); + +// Обработка запросов: сначала из сети, потом из кэша +self.addEventListener('fetch', event => { + event.respondWith( + fetch(event.request).catch(() => { + return caches.match(event.request); + }) + ); +}); \ No newline at end of file