refactor code, ẩn thông tin ssh

This commit is contained in:
2026-03-09 11:59:55 +07:00
parent 9f6bd8c35a
commit 1297511122
13 changed files with 1043 additions and 504 deletions

141
core/api_flash.py Normal file
View File

@@ -0,0 +1,141 @@
"""
LuCI HTTP Firmware Flasher for OpenWrt devices.
Tự động hoá 3 bước flash qua web interface LuCI:
1. Login → lấy sysauth cookie + stok token
2. Upload firmware.bin (multipart)
3. Confirm (Proceed) → thiết bị flash và reboot
Tương thích:
- Barrier Breaker 14.07 (field: username/password)
- OpenWrt mới hơn (field: luci_username/luci_password)
"""
import re
import time
import os
from typing import Optional
import requests
def _extract_stok(text: str) -> Optional[str]:
"""Tách stok token từ URL hoặc body HTML. Trả về None nếu không tìm thấy."""
m = re.search(r";stok=([a-f0-9]+)", text)
return m.group(1) if m else None
def flash_device_api(ip, firmware_path, username="root", password="",
keep_settings=False, status_cb=None):
"""
Flash firmware lên thiết bị OpenWrt qua LuCI HTTP.
Args:
ip : địa chỉ IP thiết bị
firmware_path : đường dẫn file .bin trên máy tính
username : LuCI username (mặc định "root")
password : LuCI password (mặc định rỗng — Barrier Breaker không có pass)
keep_settings : True = giữ cấu hình cũ sau flash
status_cb : callback(str) cập nhật tiến độ lên UI
Returns:
"DONE" — flash thành công
"FAIL: …" — thất bại, kèm lý do
"""
base_url = f"http://{ip}"
login_url = f"{base_url}/cgi-bin/luci"
session = requests.Session()
try:
# ── STEP 1: Login ────────────────────────────────────────────
if status_cb:
status_cb("Logging in...")
# Phát hiện field name tương thích Barrier Breaker vs OpenWrt mới
try:
page_html = session.get(login_url, timeout=10).text
except Exception:
page_html = ""
if 'name="luci_username"' in page_html:
login_data = {"luci_username": username, "luci_password": password}
else:
login_data = {"username": username, "password": password}
resp = session.post(login_url, data=login_data,
timeout=10, allow_redirects=True)
if resp.status_code == 403:
return "FAIL: Login denied (403)"
# Lấy stok từ URL → body → redirect history
stok = (_extract_stok(resp.url)
or _extract_stok(resp.text)
or next(
(_extract_stok(h.headers.get("Location", ""))
for h in resp.history
if _extract_stok(h.headers.get("Location", ""))),
None
))
if not stok and "sysauth" not in str(session.cookies):
return "FAIL: Login failed — no session"
flash_url = (
f"{base_url}/cgi-bin/luci/;stok={stok}/admin/system/flashops"
if stok else
f"{base_url}/cgi-bin/luci/admin/system/flashops"
)
# ── STEP 2: Upload Firmware ──────────────────────────────────
if status_cb:
status_cb("Uploading firmware...")
filename = os.path.basename(firmware_path)
extra = {"keep": "on"} if keep_settings else {}
with open(firmware_path, "rb") as f:
resp = session.post(
flash_url,
data=extra,
files={"image": (filename, f, "application/octet-stream")},
timeout=300,
)
if resp.status_code != 200:
return f"FAIL: Upload HTTP {resp.status_code}"
body = resp.text.lower()
if "invalid image" in body or "bad image" in body:
return "FAIL: Invalid firmware image"
if "unsupported" in body or "not compatible" in body:
return "FAIL: Firmware not compatible"
if "verify" not in body or "proceed" not in body:
return ("FAIL: Upload ignored by server"
if 'name="image"' in resp.text
else "FAIL: Unexpected response after upload")
# ── STEP 3: Confirm (Proceed) ────────────────────────────────
if status_cb:
status_cb("Confirming (Proceed)...")
confirm = {"step": "2", "keep": "on" if keep_settings else ""}
try:
session.post(flash_url, data=confirm, timeout=300)
except (requests.exceptions.ConnectionError,
requests.exceptions.ReadTimeout):
pass # Đứt kết nối khi reboot = bình thường
if status_cb:
status_cb("Rebooting...")
time.sleep(3)
return "DONE"
except requests.ConnectionError:
return "FAIL: Cannot connect"
except requests.Timeout:
return "DONE (rebooting)"
except Exception as e:
return f"FAIL: {e}"
finally:
session.close()

79
core/flash_new_worker.py Normal file
View File

@@ -0,0 +1,79 @@
"""
Worker thread cho chế độ "Nạp Mới FW" (New Flash / Factory Reset).
Hỗ trợ 2 method:
- "api" : Flash qua LuCI HTTP (core/api_flash.py)
- "ssh" : Flash qua SSH/SCP (core/ssh_new_flash.py), có tuỳ chọn set_passwd
"""
from PyQt6.QtCore import QThread, pyqtSignal
from concurrent.futures import ThreadPoolExecutor
from core.api_flash import flash_device_api
from core.ssh_new_flash import flash_device_new_ssh
class NewFlashThread(QThread):
"""Flash firmware lên thiết bị OpenWrt vừa reset / chưa có pass."""
device_status = pyqtSignal(int, str) # index, status message
device_done = pyqtSignal(int, str) # index, result ("DONE" / "FAIL: ...")
all_done = pyqtSignal()
def __init__(self, devices, firmware_path, max_workers=10,
method="api",
ssh_user="root", ssh_password="admin123a",
ssh_backup_password="", set_passwd=True):
"""
Args:
devices : list[dict] — [{ip, mac, ...}, ...]
firmware_path : str — đường dẫn file firmware
max_workers : int — số thiết bị flash song song (0 = unlimited)
method : "api"|"ssh"
ssh_user : str — SSH username (chỉ dùng với method="ssh")
ssh_password : str — SSH password mới sẽ đặt / đăng nhập
ssh_backup_password : str — password dự phòng nếu login lần đầu thất bại
set_passwd : bool — True = gọi passwd trước khi flash (thiết bị vừa reset)
"""
super().__init__()
self.devices = devices
self.firmware_path = firmware_path
self.max_workers = max_workers
self.method = method
self.ssh_user = ssh_user
self.ssh_password = ssh_password
self.ssh_backup_password = ssh_backup_password
self.set_passwd = set_passwd
def run(self):
def _flash_one(i, dev):
try:
def on_status(msg):
self.device_status.emit(i, msg)
if self.method == "ssh":
result = flash_device_new_ssh(
dev["ip"], self.firmware_path,
user=self.ssh_user,
password=self.ssh_password,
backup_password=self.ssh_backup_password,
set_passwd=self.set_passwd,
status_cb=on_status,
)
else:
result = flash_device_api(
dev["ip"], self.firmware_path,
status_cb=on_status,
)
self.device_done.emit(i, result)
except Exception as e:
self.device_done.emit(i, f"FAIL: {e}")
workers = self.max_workers if self.max_workers > 0 else len(self.devices)
with ThreadPoolExecutor(max_workers=max(workers, 1)) as executor:
futures = [executor.submit(_flash_one, i, dev)
for i, dev in enumerate(self.devices)]
for f in futures:
f.result()
self.all_done.emit()

View File

@@ -0,0 +1,65 @@
"""
Worker thread cho chế độ "Update Firmware" (thiết bị đã cài sẵn OpenWrt).
Đặc điểm:
- Chỉ dùng SSH (không có LuCI API).
- Credentials cố định: root / admin123a (backup: admin).
- Không thực hiện bước đặt lại mật khẩu (set_passwd=False) vì thiết bị
đã có SSH đang chạy với pass đã biết.
"""
from PyQt6.QtCore import QThread, pyqtSignal
from concurrent.futures import ThreadPoolExecutor
from core.ssh_update_flash import flash_device_update_ssh
# Credentials mặc định cho Update Mode
_UPDATE_SSH_USER = "root"
_UPDATE_SSH_PASSWORD = "admin123a"
_UPDATE_SSH_BACKUP = "admin"
class UpdateFlashThread(QThread):
"""Cập nhật firmware lên thiết bị OpenWrt đang chạy qua SSH / sysupgrade."""
device_status = pyqtSignal(int, str) # index, status message
device_done = pyqtSignal(int, str) # index, result ("DONE" / "FAIL: ...")
all_done = pyqtSignal()
def __init__(self, devices, firmware_path, max_workers=10):
"""
Args:
devices : list[dict] — [{ip, mac, ...}, ...]
firmware_path : str — đường dẫn file firmware
max_workers : int — số thiết bị update song song (0 = unlimited)
"""
super().__init__()
self.devices = devices
self.firmware_path = firmware_path
self.max_workers = max_workers
def run(self):
def _update_one(i, dev):
try:
def on_status(msg):
self.device_status.emit(i, msg)
result = flash_device_update_ssh(
dev["ip"], self.firmware_path,
user=_UPDATE_SSH_USER,
password=_UPDATE_SSH_PASSWORD,
backup_password=_UPDATE_SSH_BACKUP,
status_cb=on_status,
)
self.device_done.emit(i, result)
except Exception as e:
self.device_done.emit(i, f"FAIL: {e}")
workers = self.max_workers if self.max_workers > 0 else len(self.devices)
with ThreadPoolExecutor(max_workers=max(workers, 1)) as executor:
futures = [executor.submit(_update_one, i, dev)
for i, dev in enumerate(self.devices)]
for f in futures:
f.result()
self.all_done.emit()

View File

@@ -1,164 +0,0 @@
"""
OpenWrt LuCI Firmware Flasher (Barrier Breaker 14.07)
Automates the 3-step firmware flash process via LuCI web interface:
Step 1: POST /cgi-bin/luci → username=root&password= → get sysauth cookie + stok
Step 2: POST /cgi-bin/luci/;stok=XXX/admin/system/flashops
→ multipart: image=firmware.bin → get verification page
Step 3: POST /cgi-bin/luci/;stok=XXX/admin/system/flashops
→ step=2&keep= → device flashes and reboots
"""
import requests
import re
import time
import os
def flash_device(ip, firmware_path, username="root", password="",
keep_settings=False, status_cb=None):
"""
Flash firmware to an OpenWrt device via LuCI.
Returns:
"DONE" on success, "FAIL: reason" on error
"""
base_url = f"http://{ip}"
session = requests.Session()
try:
# ═══════════════════════════════════════════
# STEP 1: Login
# ═══════════════════════════════════════════
if status_cb:
status_cb("Logging in...")
# GET login page to detect form field names
login_url = f"{base_url}/cgi-bin/luci"
try:
get_resp = session.get(login_url, timeout=10)
page_html = get_resp.text
except Exception:
page_html = ""
# Barrier Breaker uses "username"/"password"
# Newer LuCI uses "luci_username"/"luci_password"
if 'name="luci_username"' in page_html:
login_data = {"luci_username": username, "luci_password": password}
else:
login_data = {"username": username, "password": password}
resp = session.post(login_url, data=login_data,
timeout=10, allow_redirects=True)
if resp.status_code == 403:
return "FAIL: Login denied (403)"
# Extract stok from response body or URL
stok = None
for source in [resp.url, resp.text]:
match = re.search(r";stok=([a-f0-9]+)", source)
if match:
stok = match.group(1)
break
# Also check redirect history
if not stok:
for hist in resp.history:
match = re.search(r";stok=([a-f0-9]+)",
hist.headers.get("Location", ""))
if match:
stok = match.group(1)
break
# Verify login succeeded
has_cookie = "sysauth" in str(session.cookies)
if not stok and not has_cookie:
return "FAIL: Login failed — no session"
# Build flash URL with stok
if stok:
flash_url = f"{base_url}/cgi-bin/luci/;stok={stok}/admin/system/flashops"
else:
flash_url = f"{base_url}/cgi-bin/luci/admin/system/flashops"
# ═══════════════════════════════════════════
# STEP 2: Upload firmware image
# ═══════════════════════════════════════════
if status_cb:
status_cb("Uploading firmware...")
filename = os.path.basename(firmware_path)
with open(firmware_path, "rb") as f:
# Don't send "keep" field = uncheck "Keep settings"
# Send "keep": "on" only if keep_settings is True
data = {}
if keep_settings:
data["keep"] = "on"
resp = session.post(
flash_url,
data=data,
files={"image": (filename, f, "application/octet-stream")},
timeout=300,
)
if resp.status_code != 200:
return f"FAIL: Upload HTTP {resp.status_code}"
resp_lower = resp.text.lower()
# Check for errors
if "invalid image" in resp_lower or "bad image" in resp_lower:
return "FAIL: Invalid firmware image"
if "unsupported" in resp_lower or "not compatible" in resp_lower:
return "FAIL: Firmware not compatible"
# Verify we got the "Flash Firmware - Verify" page
if "verify" not in resp_lower or "proceed" not in resp_lower:
# Still on flash form = upload was ignored
if 'name="image"' in resp.text:
return "FAIL: Upload ignored by server"
return "FAIL: Unexpected response after upload"
# ═══════════════════════════════════════════
# STEP 3: Click "Proceed" to start flash
# ═══════════════════════════════════════════
if status_cb:
status_cb("Confirming (Proceed)...")
# The Proceed form from the verification page:
# <input type="hidden" name="step" value="2" />
# <input type="hidden" name="keep" value="" />
confirm_data = {
"step": "2",
"keep": "",
}
if keep_settings:
confirm_data["keep"] = "on"
try:
session.post(flash_url, data=confirm_data, timeout=300)
except requests.exceptions.ConnectionError:
# Device rebooting — this is expected and means SUCCESS
pass
except requests.exceptions.ReadTimeout:
# Also expected during reboot
pass
if status_cb:
status_cb("Rebooting...")
time.sleep(3)
return "DONE"
except requests.ConnectionError:
return "FAIL: Cannot connect"
except requests.Timeout:
return "DONE (rebooting)"
except Exception as e:
return f"FAIL: {e}"
finally:
session.close()

228
core/ssh_new_flash.py Normal file
View File

@@ -0,0 +1,228 @@
"""
Luồng SSH cho chế độ "Nạp Mới FW" (Factory Reset / Raw device).
Đặc điểm:
- Thiết bị vừa reset cứng → Telnet port 23 mở, SSH chưa có pass.
- Bước 0 (tuỳ chọn): đặt password qua Telnet → SSH (set_passwd=True).
- Bước 14: kết nối SSH, upload SCP, verify, sync + sysupgrade.
Public API:
set_device_password(ip, user, old_password, new_password, status_cb)
flash_device_new_ssh(ip, firmware_path, user, password,
backup_password, set_passwd, status_cb)
"""
import socket
import time
import random
import paramiko
from core.ssh_utils import (
_create_ssh_client,
_upload_firmware,
_verify_firmware,
_sync_and_sysupgrade,
)
# ──────────────────────────────────────────────────
# Telnet client thủ công (telnetlib bị xoá Python 3.13+)
# ──────────────────────────────────────────────────
class _SimpleTelnet:
"""Minimal Telnet client dùng raw socket."""
def __init__(self, host, port=23, timeout=10):
self._sock = socket.create_connection((host, port), timeout=timeout)
def read_very_eager(self):
try:
self._sock.setblocking(False)
data = b""
while True:
try:
chunk = self._sock.recv(4096)
if not chunk:
break
data += chunk
except (BlockingIOError, OSError):
break
self._sock.setblocking(True)
return data
except Exception:
return b""
def write(self, data: bytes):
self._sock.sendall(data)
def close(self):
try:
self._sock.close()
except Exception:
pass
# ──────────────────────────────────────────────────
# Đặt mật khẩu thiết bị (Telnet → SSH fallback)
# ──────────────────────────────────────────────────
def set_device_password(ip, user="root", old_password="",
new_password="admin123a", status_cb=None):
"""
Đặt mật khẩu thiết bị OpenWrt.
Thứ tự thử:
1. Telnet port 23 — thiết bị vừa reset (chưa có pass, SSH chưa mở)
2. SSH — thiết bị cũ có SSH nhưng cần đổi pass
Returns:
"DONE" — thành công
"FAIL: …" — thất bại sau tất cả các retry
"""
# Jitter để không hammering khi chạy song song nhiều thiết bị
time.sleep(random.uniform(0.1, 1.5))
# ── 1. Thử Telnet ──────────────────────────────────────────────
if status_cb:
status_cb("Checking Telnet port for raw device...")
try:
tn = _SimpleTelnet(ip, timeout=5)
if status_cb:
status_cb("Telnet connected! Setting password...")
time.sleep(1)
tn.read_very_eager() # Flush banner OpenWrt
tn.write(b"passwd\n"); time.sleep(1)
tn.write(new_password.encode("ascii") + b"\n"); time.sleep(1)
tn.write(new_password.encode("ascii") + b"\n"); time.sleep(1)
tn.write(b"exit\n"); time.sleep(0.5)
tn.close()
if status_cb:
status_cb("Password set via Telnet. Waiting for SSH to start...")
time.sleep(3) # Chờ Dropbear (SSH daemon) khởi động
return "DONE"
except Exception:
# Telnet không truy cập được → thử SSH bên dưới
pass
# ── 2. Fallback SSH ─────────────────────────────────────────────
if status_cb:
status_cb("Connecting SSH for password update...")
last_err = None
for attempt in range(3):
try:
client = _create_ssh_client(ip, user, old_password, timeout=10)
if status_cb:
status_cb("Setting password via SSH...")
shell = client.invoke_shell()
time.sleep(1)
if shell.recv_ready():
shell.recv(65535)
shell.send("passwd\n"); time.sleep(2)
shell.send(f"{new_password}\n"); time.sleep(1)
shell.send(f"{new_password}\n"); time.sleep(2)
if shell.recv_ready():
shell.recv(65535)
shell.send("exit\n"); time.sleep(0.5)
shell.close()
client.close()
if status_cb:
status_cb("Password set ✓")
return "DONE"
except paramiko.AuthenticationException as e:
return f"FAIL: Cannot connect SSH (Old pass incorrect?) — {e}"
except Exception as e:
last_err = e
try:
client.close()
except Exception:
pass
time.sleep(2)
return f"FAIL: Cannot connect SSH after 3 attempts — {last_err}"
# ──────────────────────────────────────────────────
# Flash firmware — Nạp Mới (Factory Reset)
# ──────────────────────────────────────────────────
def flash_device_new_ssh(ip, firmware_path, user="root", password="admin123a",
backup_password="", set_passwd=False, status_cb=None):
"""
Flash firmware lên thiết bị OpenWrt vừa reset / chưa có mật khẩu.
Luồng:
0. (Tuỳ chọn) Đặt mật khẩu qua Telnet / SSH
1. Kết nối SSH
2. Upload firmware qua SCP lên /tmp/
3. Verify file tồn tại
4. sync + sysupgrade
Args:
ip : địa chỉ IP thiết bị
firmware_path : đường dẫn file .bin trên máy tính
user : SSH username (mặc định "root")
password : mật khẩu SSH (hoặc mật khẩu mới sẽ đặt)
backup_password : mật khẩu dự phòng nếu password chính không vào được
set_passwd : True = chạy bước đặt mật khẩu trước khi flash
status_cb : callback(str) để cập nhật trạng thái lên UI
Returns:
"DONE" — flash thành công
"FAIL: …" — thất bại, kèm lý do
"""
# ── STEP 0: Đặt mật khẩu (tuỳ chọn) ──────────────────────────
if set_passwd:
result = set_device_password(ip, user, "", password, status_cb)
if result.startswith("FAIL"):
# Thử với backup_password nếu có
if backup_password:
result = set_device_password(ip, user, backup_password, password, status_cb)
# Thử xem password đã được đặt chưa (idempotent)
if result.startswith("FAIL"):
result = set_device_password(ip, user, password, password, status_cb)
if result.startswith("FAIL"):
return result
else:
time.sleep(random.uniform(0.1, 1.5)) # Jitter khi không set_passwd
# ── STEP 1: Kết nối SSH ─────────────────────────────────────────
if status_cb:
status_cb("Connecting SSH...")
try:
client = _create_ssh_client(ip, user, password)
except paramiko.AuthenticationException:
return "FAIL: SSH authentication failed"
except paramiko.SSHException as e:
return f"FAIL: SSH error — {e}"
except Exception as e:
return f"FAIL: Cannot connect — {e}"
# ── STEP 24: Upload → Verify → sysupgrade ─────────────────────
try:
remote_path = _upload_firmware(client, firmware_path, status_cb)
_verify_firmware(client, remote_path, status_cb)
return _sync_and_sysupgrade(client, remote_path, status_cb)
except RuntimeError as e:
return f"FAIL: {e}"
except Exception as e:
return f"FAIL: {e}"
finally:
try:
client.close()
except Exception:
pass

92
core/ssh_update_flash.py Normal file
View File

@@ -0,0 +1,92 @@
"""
Luồng SSH cho chế độ "Update Firmware" (thiết bị đã cài sẵn OpenWrt).
Đặc điểm:
- Thiết bị đang chạy bình thường, SSH đã mở sẵn với pass đã biết.
- Không có bước Telnet / đặt lại mật khẩu.
- Kết nối trực tiếp → upload SCP → verify → sync + sysupgrade.
Public API:
flash_device_update_ssh(ip, firmware_path, user, password,
backup_password, status_cb)
"""
import time
import paramiko
from core.ssh_utils import (
_create_ssh_client,
_upload_firmware,
_verify_firmware,
_sync_and_sysupgrade,
)
def flash_device_update_ssh(ip, firmware_path, user="root",
password="admin123a", backup_password="",
status_cb=None):
"""
Cập nhật firmware lên thiết bị OpenWrt đang chạy qua SSH / sysupgrade.
Luồng:
1. Kết nối SSH (thử password, nếu lỗi auth thử backup_password)
2. Upload firmware qua SCP lên /tmp/
3. Verify file tồn tại
4. sync + sysupgrade
Args:
ip : địa chỉ IP thiết bị
firmware_path : đường dẫn file .bin trên máy tính
user : SSH username (mặc định "root")
password : SSH password chính
backup_password : SSH password dự phòng nếu password chính sai
status_cb : callback(str) để cập nhật trạng thái lên UI
Returns:
"DONE" — update thành công
"FAIL: …" — thất bại, kèm lý do
"""
# ── STEP 1: Kết nối SSH ─────────────────────────────────────────
if status_cb:
status_cb("Connecting SSH...")
client = None
# Thử password chính trước
try:
client = _create_ssh_client(ip, user, password)
except paramiko.AuthenticationException:
# Thử backup_password nếu có
if backup_password:
try:
client = _create_ssh_client(ip, user, backup_password)
except paramiko.AuthenticationException:
return "FAIL: SSH authentication failed (wrong password)"
except paramiko.SSHException as e:
return f"FAIL: SSH error — {e}"
except Exception as e:
return f"FAIL: Cannot connect — {e}"
else:
return "FAIL: SSH authentication failed"
except paramiko.SSHException as e:
return f"FAIL: SSH error — {e}"
except Exception as e:
return f"FAIL: Cannot connect — {e}"
# ── STEP 24: Upload → Verify → sysupgrade ─────────────────────
try:
remote_path = _upload_firmware(client, firmware_path, status_cb)
_verify_firmware(client, remote_path, status_cb)
return _sync_and_sysupgrade(client, remote_path, status_cb)
except RuntimeError as e:
return f"FAIL: {e}"
except Exception as e:
return f"FAIL: {e}"
finally:
try:
client.close()
except Exception:
pass

120
core/ssh_utils.py Normal file
View File

@@ -0,0 +1,120 @@
"""
SSH/SCP helper functions dùng chung cho cả 2 luồng flash.
Không chứa logic nghiệp vụ — chỉ là transport layer:
_create_ssh_client() — kết nối SSH với retry
_upload_firmware() — upload file qua SCP lên /tmp/
_verify_firmware() — kiểm tra file tồn tại trên device
_sync_and_sysupgrade() — sync + sysupgrade, trả về "DONE" / "FAIL: ..."
"""
import os
import time
import paramiko
from scp import SCPClient
def _create_ssh_client(ip, user, password, timeout=15):
"""Tạo SSH client với AutoAddPolicy, có retry 3 lần."""
last_err = None
for attempt in range(3):
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
ip, username=user, password=password,
timeout=timeout, look_for_keys=False, allow_agent=False,
)
return client
except paramiko.AuthenticationException:
# Sai password — retry không giúp ích gì
raise
except Exception as e:
last_err = e
if attempt < 2:
time.sleep(1)
raise last_err
def _upload_firmware(client, firmware_path, status_cb=None):
"""
Upload firmware qua SCP lên /tmp/<filename>.
Trả về remote_path khi thành công, raise RuntimeError nếu thất bại.
"""
if status_cb:
status_cb("Uploading firmware via SCP...")
filename = os.path.basename(firmware_path)
remote_path = f"/tmp/{filename}"
last_err = None
for attempt in range(3):
try:
scp = SCPClient(client.get_transport(), socket_timeout=350)
scp.put(firmware_path, remote_path)
scp.close()
time.sleep(2)
return remote_path
except Exception as e:
last_err = e
time.sleep(1.5)
raise RuntimeError(f"SCP upload failed (Check Network) — {last_err}")
def _verify_firmware(client, remote_path, status_cb=None):
"""
Xác nhận file firmware tồn tại trên thiết bị.
Raise RuntimeError nếu file không có.
"""
if status_cb:
status_cb("Verifying firmware...")
_, stdout, _ = client.exec_command(
f"test -f {remote_path} && ls -lh {remote_path}", timeout=10
)
output = stdout.read().decode("utf-8", errors="ignore").strip()
if not output:
raise RuntimeError("Firmware file not found on device after upload")
def _sync_and_sysupgrade(client, remote_path, status_cb=None):
"""
Sync filesystem rồi chạy sysupgrade.
Trả về "DONE" khi thành công, "FAIL: ..." khi sysupgrade lỗi sớm.
Connection drop trong lúc sysupgrade = thành công (thiết bị đang reboot).
"""
if status_cb:
status_cb("Syncing filesystem...")
client.exec_command("sync", timeout=10)
time.sleep(2)
if status_cb:
status_cb("Flashing firmware (sysupgrade)...")
try:
# -F: bỏ qua kiểm tra metadata (cần cho uImage)
# -v: verbose -n: không giữ cấu hình cũ
_, stdout, _ = client.exec_command(
f"sysupgrade -F -v -n {remote_path} > /tmp/sysup.log 2>&1"
)
# Chờ tối đa 4 giây — nếu exit quá sớm thì coi như lỗi
time.sleep(4)
if stdout.channel.exit_status_ready():
exit_code = stdout.channel.recv_exit_status()
_, log_out, _ = client.exec_command("cat /tmp/sysup.log")
err_msg = log_out.read().decode("utf-8", errors="ignore").strip()
return (
f"FAIL: sysupgrade terminated early "
f"(Code {exit_code}). Details:\n{err_msg}"
)
except Exception:
# Connection drop = device đang reboot = thành công
pass
if status_cb:
status_cb("Rebooting...")
time.sleep(3)
return "DONE"

View File

@@ -1,18 +1,28 @@
"""
Workers module — chứa các QThread dùng cho tác vụ nền.
Scan:
ScanThread — quét mạng LAN tìm thiết bị OpenWrt.
Flash (tách thành 2 module riêng):
core.flash_new_worker → NewFlashThread (Nạp Mới FW)
core.flash_update_worker → UpdateFlashThread (Update FW)
"""
from PyQt6.QtCore import QThread, pyqtSignal from PyQt6.QtCore import QThread, pyqtSignal
from concurrent.futures import ThreadPoolExecutor, as_completed from concurrent.futures import ThreadPoolExecutor, as_completed
# We need these imports inside workers since they use them
from core.scanner import scan_network from core.scanner import scan_network
from core.flasher import flash_device
from core.ssh_flasher import flash_device_ssh
from utils.network import _resolve_hostname from utils.network import _resolve_hostname
class ScanThread(QThread): class ScanThread(QThread):
"""Run network scan in a background thread so UI doesn't freeze.""" """Quét mạng LAN trong background thread để không đóng băng UI."""
finished = pyqtSignal(list)
error = pyqtSignal(str) finished = pyqtSignal(list)
error = pyqtSignal(str)
scan_progress = pyqtSignal(int, int) # done, total (ping sweep) scan_progress = pyqtSignal(int, int) # done, total (ping sweep)
stage = pyqtSignal(str) # current scan phase stage = pyqtSignal(str) # tên giai đoạn hiện tại
def __init__(self, network): def __init__(self, network):
super().__init__() super().__init__()
@@ -29,7 +39,8 @@ class ScanThread(QThread):
results = scan_network(self.network, results = scan_network(self.network,
progress_cb=on_ping_progress, progress_cb=on_ping_progress,
stage_cb=on_stage) stage_cb=on_stage)
# Resolve hostnames in parallel
# Resolve hostname song song
self.stage.emit("hostname") self.stage.emit("hostname")
with ThreadPoolExecutor(max_workers=50) as executor: with ThreadPoolExecutor(max_workers=50) as executor:
future_to_dev = { future_to_dev = {
@@ -42,61 +53,7 @@ class ScanThread(QThread):
dev["name"] = future.result(timeout=3) dev["name"] = future.result(timeout=3)
except Exception: except Exception:
dev["name"] = "" dev["name"] = ""
self.finished.emit(results) self.finished.emit(results)
except Exception as e: except Exception as e:
self.error.emit(str(e)) self.error.emit(str(e))
class FlashThread(QThread):
"""Run firmware flash in background so UI stays responsive."""
device_status = pyqtSignal(int, str) # index, status message
device_done = pyqtSignal(int, str) # index, result
all_done = pyqtSignal()
def __init__(self, devices, firmware_path, max_workers=10,
method="api", ssh_user="root", ssh_password="admin123a",
ssh_backup_password="", set_passwd=False):
super().__init__()
self.devices = devices
self.firmware_path = firmware_path
self.max_workers = max_workers
self.method = method
self.ssh_user = ssh_user
self.ssh_password = ssh_password
self.ssh_backup_password = ssh_backup_password
self.set_passwd = set_passwd
def run(self):
def _flash_one(i, dev):
try:
def on_status(msg):
self.device_status.emit(i, msg)
if self.method == "ssh":
result = flash_device_ssh(
dev["ip"], self.firmware_path,
user=self.ssh_user,
password=self.ssh_password,
backup_password=self.ssh_backup_password,
set_passwd=self.set_passwd,
status_cb=on_status
)
else:
result = flash_device(
dev["ip"], self.firmware_path,
status_cb=on_status
)
self.device_done.emit(i, result)
except Exception as e:
self.device_done.emit(i, f"FAIL: {e}")
# Use configured max_workers (0 = unlimited = one per device)
workers = self.max_workers if self.max_workers > 0 else len(self.devices)
with ThreadPoolExecutor(max_workers=workers) as executor:
futures = []
for i, dev in enumerate(self.devices):
futures.append(executor.submit(_flash_one, i, dev))
for f in futures:
f.result()
self.all_done.emit()

View File

@@ -1,155 +0,0 @@
# Tài liệu Flash Firmware — IoT Firmware Loader
## Tổng quan
Ứng dụng tự động hóa quá trình nạp firmware cho thiết bị **OpenWrt Barrier Breaker 14.07** thông qua giao diện web **LuCI**. Thay vì thao tác thủ công trên trình duyệt, ứng dụng thực hiện 3 bước HTTP tự động cho mỗi thiết bị.
---
## Luồng hoạt động
```mermaid
flowchart TD
A[Chọn Firmware .bin] --> B[Scan LAN]
B --> C[Hiển thị danh sách thiết bị]
C --> D{Chọn thiết bị ☑}
D --> E[Nhấn Flash Selected Devices]
E --> F[FlashThread chạy background]
F --> G["ThreadPoolExecutor\n(max_workers = N)"]
G --> H1[Device 1 → flash_device]
G --> H2[Device 2 → flash_device]
G --> H3[Device N → flash_device]
H1 & H2 & H3 --> I[All Done → Thông báo]
```
### Chi tiết `flash_device()` cho mỗi thiết bị
```mermaid
flowchart TD
S1["STEP 1: Login\nGET /cgi-bin/luci\nPOST username=root, password=empty"] --> C1{Thành công?}
C1 -->|Có cookie sysauth + stok| S2
C1 -->|403 Denied| F1["FAIL: Login denied (403)"]
C1 -->|Không có session| F2["FAIL: Login failed — no session"]
S2["STEP 2: Upload Firmware\nPOST /flashops\nField: image=firmware.bin\nkeep=KHÔNG gửi (bỏ tích)"] --> C2{Response?}
C2 -->|Trang Verify + Proceed| S3
C2 -->|HTTP ≠ 200| F3["FAIL: Upload HTTP xxx"]
C2 -->|invalid image| F4["FAIL: Invalid firmware image"]
C2 -->|unsupported| F5["FAIL: Firmware not compatible"]
C2 -->|Vẫn hiện form upload| F6["FAIL: Upload ignored by server"]
S3["STEP 3: Proceed\nPOST step=2, keep=empty\nXác nhận flash"] --> C3{Response?}
C3 -->|200 Flashing...| R["DONE ✅\nThiết bị đang reboot"]
C3 -->|Connection dropped| R
C3 -->|Read timeout| R
```
---
## Bảng Status
### Status trên cột "Status" trong bảng thiết bị
| Icon | Status | Điều kiện hiển thị |
| ---- | ---------------------------------------- | ------------------------------------------------------------- |
| — | `READY` | Sau khi scan, thiết bị chưa được flash |
| ⏳ | `Logging in...` | Đang POST login vào LuCI |
| ⏳ | `Uploading firmware...` | Đang upload file .bin (~30MB) lên thiết bị |
| ⏳ | `Confirming (Proceed)...` | Đã upload xong, đang gửi lệnh xác nhận flash |
| ⏳ | `Rebooting...` | Thiết bị đang reboot sau khi flash |
| ✅ | `DONE` | Flash thành công, thiết bị đang khởi động lại |
| ✅ | `DONE (rebooting)` | Flash thành công nhưng timeout khi chờ response (bình thường) |
| ❌ | `FAIL: Cannot connect` | Không kết nối được tới thiết bị (sai IP, khác mạng) |
| ❌ | `FAIL: Login denied (403)` | Thiết bị từ chối đăng nhập (sai mật khẩu) |
| ❌ | `FAIL: Login failed — no session` | Login không trả về cookie hoặc token |
| ❌ | `FAIL: Upload HTTP xxx` | Server trả mã lỗi HTTP khi upload |
| ❌ | `FAIL: Invalid firmware image` | File firmware không hợp lệ |
| ❌ | `FAIL: Firmware not compatible` | Firmware không tương thích với thiết bị |
| ❌ | `FAIL: Upload ignored by server` | Server nhận file nhưng không xử lý (sai form field) |
| ❌ | `FAIL: Unexpected response after upload` | Không nhận được trang xác nhận Verify |
---
## Chi tiết kỹ thuật HTTP
### Step 1: Login
```
GET http://{IP}/cgi-bin/luci → Lấy trang login, phát hiện field name
POST http://{IP}/cgi-bin/luci → Gửi username=root&password=
```
| Kết quả | Điều kiện |
| -------------- | ------------------------------------------------------------------ |
| **Thành công** | Response chứa `sysauth` cookie VÀ/HOẶC `stok` token trong URL/body |
| **Thất bại** | HTTP 403, hoặc không có cookie/token |
**Field names tự động phát hiện:**
- OpenWrt Barrier Breaker 14.07: `username` / `password`
- OpenWrt mới hơn: `luci_username` / `luci_password`
### Step 2: Upload Firmware
```
POST http://{IP}/cgi-bin/luci/;stok={TOKEN}/admin/system/flashops
Content-Type: multipart/form-data
Fields:
image = [firmware.bin] (file upload)
keep = (KHÔNG gửi) (bỏ tích Keep Settings)
```
| Kết quả | Điều kiện |
| -------------- | ----------------------------------------------------------------------- |
| **Thành công** | Response chứa "Flash Firmware - Verify" + "Proceed" + checksum |
| **Thất bại** | Response chứa "invalid image", "unsupported", hoặc vẫn hiện form upload |
### Step 3: Proceed (Xác nhận)
```
POST http://{IP}/cgi-bin/luci/;stok={TOKEN}/admin/system/flashops
Fields:
step = 2
keep = (empty string)
```
| Kết quả | Điều kiện |
| -------------- | ------------------------------------------------------------------------------- |
| **Thành công** | Response "The system is flashing now" HOẶC connection bị ngắt (thiết bị reboot) |
| **Thất bại** | Hiếm khi xảy ra — nếu đã qua Step 2 thì Step 3 gần như luôn thành công |
---
## Xử lý song song
```
FlashThread (QThread - background)
└── ThreadPoolExecutor (max_workers = N)
├── Thread 1 → flash_device(IP_1)
├── Thread 2 → flash_device(IP_2)
├── ...
└── Thread N → flash_device(IP_N)
```
| Config | Giá trị | Ý nghĩa |
| ------------------------- | --------- | ------------------------------ |
| Concurrent devices = `10` | Mặc định | Flash 10 thiết bị song song |
| Concurrent devices = `0` | Unlimited | Flash tất cả thiết bị cùng lúc |
| Concurrent devices = `1` | Tuần tự | Flash từng thiết bị một |
**Mỗi thiết bị có session HTTP riêng** → không bị lẫn cookie/token giữa các thiết bị.
---
## Files liên quan
| File | Chức năng |
| --------------- | --------------------------------------------- |
| `flasher.py` | Logic flash 3 bước (login → upload → proceed) |
| `main.py` | UI PyQt6, FlashThread, quản lý song song |
| `debug_full.py` | Script debug — chạy 3 bước với log chi tiết |
| `scanner.py` | Scan mạng LAN tìm thiết bị |
""", "Complexity": 3, "Description": "Created flash documentation file with workflow diagrams, status conditions, HTTP details, and parallel processing explanation.", "EmptyFile": false, "IsArtifact": false, "Overwrite": false, "TargetFile": "/Users/nguyennhatminh/Documents/file code/Smatec/iot_fw_loader/FLASH_DOC.md

123
docs/api_flash_docs.md Normal file
View File

@@ -0,0 +1,123 @@
# Tài liệu Kỹ thuật: Flash Firmware qua LuCI API (`core/api_flash.py`)
Module `api_flash.py` tự động hoá quá trình nạp firmware cho thiết bị **OpenWrt** thông qua giao diện web **LuCI HTTP**. Được dùng trong chế độ **Nạp Mới FW** với method `"api"`.
---
## 1. Kiến Trúc — Vai Trò File
| File | Vai trò |
| -------------------------- | ----------------------------------------------------------------------- |
| `core/api_flash.py` | Logic flash 3 bước (login → upload → proceed) qua LuCI |
| `core/flash_new_worker.py` | `NewFlashThread` — dispatch tới `flash_device_api()` khi `method="api"` |
| `main.py` | UI PyQt6, chọn mode/method, truyền tham số vào worker |
---
## 2. Sơ Đồ Luồng
```mermaid
flowchart TD
A[NewFlashThread - method=api] --> B[flash_device_api]
B --> S1["STEP 1: Login\nGET /cgi-bin/luci — phát hiện field name\nPOST username=root & password="]
S1 --> C1{Thành công?}
C1 -->|sysauth cookie + stok| S2
C1 -->|HTTP 403| F1["FAIL: Login denied (403)"]
C1 -->|Không có session| F2["FAIL: Login failed — no session"]
S2["STEP 2: Upload Firmware\nPOST /flashops\nmultipart: image=firmware.bin"] --> C2{Response?}
C2 -->|Trang Verify + Proceed| S3
C2 -->|HTTP ≠ 200| F3["FAIL: Upload HTTP xxx"]
C2 -->|invalid image| F4["FAIL: Invalid firmware image"]
C2 -->|unsupported| F5["FAIL: Firmware not compatible"]
C2 -->|Vẫn hiện form upload| F6["FAIL: Upload ignored by server"]
S3["STEP 3: Proceed\nPOST step=2 & keep=empty"] --> C3{Response?}
C3 -->|Connection dropped / Timeout| R["DONE ✅ — Device đang reboot"]
C3 -->|200 OK| R
```
---
## 3. Chi Tiết Kỹ Thuật HTTP
### Step 1 — Login
```
GET http://{IP}/cgi-bin/luci → Lấy HTML login, phát hiện field name
POST http://{IP}/cgi-bin/luci → Đăng nhập
```
**Tự động phát hiện field name tương thích:**
| Phiên bản | Field |
| --------------------- | --------------------------------- |
| Barrier Breaker 14.07 | `username` / `password` |
| OpenWrt mới hơn | `luci_username` / `luci_password` |
**Lấy session:** `stok` token được tìm tuần tự trong URL → body HTML → redirect history. Cookie `sysauth` là fallback nếu không có `stok`.
### Step 2 — Upload Firmware
```
POST http://{IP}/cgi-bin/luci/;stok={TOKEN}/admin/system/flashops
Content-Type: multipart/form-data
image = firmware.bin (file upload)
keep = (không gửi) → bỏ tích "Keep Settings" = Clean Flash
```
Thành công khi response trả về trang **"Flash Firmware - Verify"** chứa từ khoá `verify` + `proceed`.
### Step 3 — Confirm (Proceed)
```
POST http://{IP}/cgi-bin/luci/;stok={TOKEN}/admin/system/flashops
step = 2
keep = (empty)
```
**Đứt kết nối = Thành công:** Device bắt đầu flash → SSH/HTTP request bị drop là hành vi mong muốn. `ConnectionError``ReadTimeout` đều được bắt và trả về `"DONE"`.
---
## 4. Bảng Status UI
| Icon | Status | Điều kiện |
| ---- | ---------------------------------------- | ------------------------------------------ |
| ⏳ | `Logging in...` | Đang POST login vào LuCI |
| ⏳ | `Uploading firmware...` | Đang upload file .bin |
| ⏳ | `Confirming (Proceed)...` | Đang gửi lệnh xác nhận flash |
| ⏳ | `Rebooting...` | Chờ device khởi động lại |
| ✅ | `DONE` | Flash thành công |
| ✅ | `DONE (rebooting)` | Flash thành công, timeout khi chờ response |
| ❌ | `FAIL: Cannot connect` | Không kết nối được (sai IP / khác mạng) |
| ❌ | `FAIL: Login denied (403)` | Sai mật khẩu LuCI |
| ❌ | `FAIL: Login failed — no session` | Không có cookie/token sau login |
| ❌ | `FAIL: Upload HTTP xxx` | Server trả lỗi HTTP khi upload |
| ❌ | `FAIL: Invalid firmware image` | File firmware không hợp lệ |
| ❌ | `FAIL: Firmware not compatible` | Firmware không tương thích thiết bị |
| ❌ | `FAIL: Upload ignored by server` | Server không xử lý file (sai form field) |
| ❌ | `FAIL: Unexpected response after upload` | Không nhận được trang Verify |
---
## 5. Xử Lý Song Song
```
NewFlashThread (QThread)
└── ThreadPoolExecutor (max_workers = N)
├── Thread 1 → flash_device_api(IP_1)
├── Thread 2 → flash_device_api(IP_2)
└── Thread N → flash_device_api(IP_N)
```
| Concurrent devices | Ý nghĩa |
| ------------------ | --------------------------------- |
| `10` (mặc định) | Flash 10 thiết bị song song |
| `0` | Unlimited — flash tất cả cùng lúc |
| `1` | Tuần tự — từng thiết bị một |
Mỗi thiết bị có `requests.Session()` riêng — không bị lẫn cookie/token.

View File

@@ -1,60 +1,166 @@
# Tài liệu Kỹ thuật: Nạp Firmware qua SSH (`core/ssh_flasher.py`) # Tài liệu Kỹ thuật: Flash Firmware qua SSH
Module `ssh_flasher.py` chịu trách nhiệm nạp Firmware lên các thiết bị OpenWrt nạp mới hoặc nạp lại thông qua hai giao thức Telnet và SSH. Nó được thiết kế với độ an toàn cao để xử lý đa luồng (ví dụ: quét và nạp hàng loạt thiết bị cùng lúc), có cơ chế tự động thử lại (Retry) và cơ chế dự phòng mt khẩu khi thiết bị bị kẹt mật khẩu cũ. Tài liệu này gộp toàn bộ logic SSH cho cả hai quy trình: **Nạp Mới FW** (Factory Reset)**Update FW** (thiết bị đang chạy). Luồng SSH được tách thành nhiều file với trách nhiệm rõ ràng thay vì gói gọn trong mt module duy nhất.
## 1. Sơ đồ Luồng Hoạt Động (Operational Flow) ---
## 1. Kiến Trúc Tổng Quan — Vai Trò Từng File
```
core/
├── ssh_utils.py ← Transport layer dùng chung (không có business logic)
├── ssh_new_flash.py ← Luồng SSH Nạp Mới FW (Telnet → SSH → sysupgrade)
├── ssh_update_flash.py ← Luồng SSH Update FW (SSH trực tiếp → sysupgrade)
├── flash_new_worker.py ← QThread điều phối Nạp Mới (API LuCI hoặc SSH)
└── flash_update_worker.py← QThread điều phối Update FW (SSH only)
```
### `core/ssh_utils.py` — Transport Layer Dùng Chung
Chứa các helper function cấp thấp, **không mang logic nghiệp vụ**, được import bởi cả 2 luồng SSH:
| Hàm | Mô tả |
| ------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------- |
| `_create_ssh_client(ip, user, password, timeout)` | Tạo SSH client với `AutoAddPolicy`, retry 3 lần. Nếu lỗi `AuthenticationException` thì raise ngay (không retry vô nghĩa). |
| `_upload_firmware(client, firmware_path, status_cb)` | Upload file `.bin` qua SCP lên `/tmp/<filename>`. Retry 3 lần, timeout SCP là 350 giây. Trả về `remote_path`. |
| `_verify_firmware(client, remote_path, status_cb)` | Chạy `test -f && ls -lh` để xác nhận file tồn tại trên device sau khi upload. |
| `_sync_and_sysupgrade(client, remote_path, status_cb)` | Chạy `sync` rồi `sysupgrade -F -v -n`. Đứt kết nối = thành công. Trả về `"DONE"` hoặc `"FAIL: ..."`. |
### `core/ssh_new_flash.py` — Luồng Nạp Mới FW
Xử lý thiết bị **vừa Factory Reset** hoặc **chưa có mật khẩu**. Bao gồm:
- **`_SimpleTelnet`**: Telnet client thủ công bằng raw socket — thay thế `telnetlib` bị xoá khỏi Python 3.13+.
- **`set_device_password()`**: Đặt mật khẩu thiết bị, thử Telnet port 23 trước, fallback sang SSH nếu Telnet đóng.
- **`flash_device_new_ssh()`**: Hàm flash chính — gọi `set_device_password` (nếu `set_passwd=True`) rồi kết nối SSH và gọi pipeline `_upload → _verify → _sync_and_sysupgrade` từ `ssh_utils`.
### `core/ssh_update_flash.py` — Luồng Update FW
Xử lý thiết bị **đang chạy MiraV3**, SSH đã mở sẵn với pass đã biết. Không có Telnet, không có set_passwd:
- **`flash_device_update_ssh()`**: Kết nối SSH (thử `password`, fallback `backup_password` nếu auth fail), rồi gọi thẳng pipeline `_upload → _verify → _sync_and_sysupgrade` từ `ssh_utils`.
### `core/flash_new_worker.py` — QThread Nạp Mới
`NewFlashThread` — điều phối flash song song cho chế độ **Nạp Mới**:
- Nếu `method="ssh"` → gọi `flash_device_new_ssh()` từ `ssh_new_flash`.
- Nếu `method="api"` → gọi `flash_device()` từ `flasher` (LuCI HTTP).
- Nhận đủ credentials từ UI: `ssh_user`, `ssh_password`, `ssh_backup_password`, `set_passwd`.
### `core/flash_update_worker.py` — QThread Update FW
`UpdateFlashThread` — điều phối flash song song cho chế độ **Update FW**:
- Luôn dùng SSH, credentials hardcode an toàn trong module (`root` / `admin123a`, backup `admin`).
- Không nhận credentials từ UI — tránh nhập sai gây flash nhầm.
---
## 2. Sơ Đồ Luồng — Nạp Mới FW (SSH)
```mermaid ```mermaid
graph TD graph TD
A[Bắt đầu Flash SSH] -->|Jitter Delay 0.1s - 1.5s| B{Có yêu cầu Set Password?} A[NewFlashThread.run] --> B{method = ssh?}
B -- Không --> F[Bước 1: Kết nối SSH] B -- api --> API[flash_device - LuCI HTTP]
B -- --> C[Thử kết nối Telnet port 23] B -- ssh --> C[flash_device_new_ssh]
C -- Thành công --> C1[Gửi lệnh `passwd` > admin123a] --> F C -->|set_passwd=True| D[set_device_password]
C -- Lỗi / Timeout / Bị từ chối --> D[Thử SSH với mật khẩu rỗng] C -->|set_passwd=False| Jitter[Jitter 0.1s - 1.5s]
D -- Thành công --> D1[Gửi lệnh `passwd` > admin123a] --> F D --> D1[Thử Telnet port 23]
D -- Lỗi 'Authentication Failed' --> E[Thử SSH với Backup Password] D1 -- Thành công --> D2[passwd > new_password via Telnet]
D2 --> D3[Chờ 3s để Dropbear khởi động]
D1 -- Lỗi / Timeout --> D4[Fallback SSH với old_password rỗng]
D4 -- Auth Fail --> D5[Thử backup_password]
D5 -- Auth Fail --> D6[Thử chính new_password - idempotent]
D6 -- Fail --> Z[DỪNG - Báo FAIL]
D3 --> F
D4 -- OK --> F
D5 -- OK --> F
D6 -- OK --> F
E -- Thành công --> E1[Gửi lệnh `passwd` > admin123a] --> F Jitter --> F[_create_ssh_client - retry 3 lần]
E -- Thất bại --> E2[Thử SSH với Password cấu hình] F --> G[_upload_firmware via SCP vào /tmp/]
G -->|retry 3 lần| H[_verify_firmware - test -f]
E2 -- Thành công --> F H --> I[_sync_and_sysupgrade]
E2 -- Thất bại --> Z[DỪNG LẠI - Báo Lỗi] I --> J[sync rồi sysupgrade -F -v -n]
J --> K{Kết nối đứt?}
F -->|Kết nối SSH thành công sau Retry 3 lần| G[Bước 2: Push Firmware SCP vào /tmp/] K -- Có --> L[DONE - Device đang Reboot]
G -->|Tối đa 3 lần Retry| H[Bước 3: Xác minh File `test -f`] K -- Không trong 4s --> M[FAIL - Lấy log /tmp/sysup.log]
H --> I[Bước 4: Đồng bộ bộ nhớ `sync`]
I --> J[Bước 5: Gọi `sysupgrade -F -v -n /tmp/...`]
J --> K{Kết nối bị đứt?}
K -- Có --> L[Khởi động lại thành công - Báo DONE]
K -- Không --> M[Báo lỗi Sysupgrade]
``` ```
## 2. Phân Tích Logic Cốt Lõi ## 3. Sơ Đồ Luồng — Update FW (SSH)
### 2.1 Cơ Chế Dự Phòng Mật Khẩu Đa Tầng (Fallback Mechanism) ```mermaid
graph TD
A[UpdateFlashThread.run] --> B[flash_device_update_ssh]
B --> C[_create_ssh_client với password chính]
C -- Auth OK --> F
C -- AuthenticationException --> D[Thử backup_password]
D -- Auth OK --> F
D -- Fail --> Z[DỪNG - Báo FAIL]
Điểm mạnh của thuật toán là khả năng "lì lợm" lấy được quyền Root để đặt mật khẩu cho thiết bị. Các lớp dự phòng bao gồm: F[_upload_firmware via SCP vào /tmp/] -->|retry 3 lần| G[_verify_firmware - test -f]
G --> H[_sync_and_sysupgrade]
H --> I[sync rồi sysupgrade -F -v -n]
I --> J{Kết nối đứt?}
J -- Có --> K[DONE - Device đang Reboot]
J -- Không trong 4s --> L[FAIL - Lấy log /tmp/sysup.log]
```
1. **Telnet (Port 23):** Router OpenWrt khi mới xuất xưởng hoặc ngay sau khi Factory Reset sẽ chặn kết nối SSH bằng tài khoản `root` mất gốc, nhưng lại mở Telnet không có mật khẩu. Script sẽ luôn thử bẻ khóa qua đường này đầu tiên. Bất kỳ ngoại lệ nào phát sinh ở luồng này (Refused, Timeout, Broken Pipe) cũng sẽ kích hoạt fallback. ---
2. **SSH (Mật khẩu rỗng):** Nếu Telnet bị đóng cửa (chứng tỏ thiết bị đã được kích hoạt sơ), phần mềm thử mở cổng 22 với User `root` và mật khẩu rỗng `""`.
3. **SSH (Backup Password):** Nếu mật khẩu rỗng bị từ chối bằng `AuthenticationException`, script định nhận diện máy đã bị kẹt một pass được cài đặt trước đó và chèn `backup_password` ra thử nghiệm.
4. **SSH (Target Password):** Nếu trượt tới lớp thứ 3, script thử nốt với chính mật khẩu mục tiêu của dự án (mặc định: `admin123a`) để chặn trường hợp thiết bị đã đổi Pass thành công từ lần trước nhưng quá trình nạp Firmware chưa diễn ra.
### 2.2 Xử Lý Đa Luồng Chống Nghẽn (Concurrency Limits) ## 4. Phân Tích Logic Cốt Lõi
Thư viện `paramiko` thường dễ đổ vỡ cấu trúc và báo lỗi `[Errno 64] Host is down` hoặc `[Errno 32] Broken Pipe` khi có vài chục luồng (threads) cùng mở port đập vào Network Stack OS (hoặc Switch/Router) ở đúng một chớp mắt. Lỗi này là dạng Transient (có tính tạm thời). Do đó hệ thống được nâng cấp: ### 4.1 Cơ Chế Dự Phòng Mật Khẩu Đa Tầng (Nạp Mới)
- **Jitter (Độ trễ ngẫu nhiên):** Sử dụng `time.sleep(random.uniform(0.1, 1.5))` trước khi chọc vào thiết bị. Sự sai lệch mili-giây này phân tán chùm yêu cầu dội vào mạng, tránh tự gây ra một cuộc tấn công từ chối dịch vụ (DDoS) nội bộ. Thuật toán `set_device_password` có 4 lớp dự phòng để chiếm được quyền Root trên thiết bị dù ở trạng thái nào:
- **Vòng lặp (Retry Hooks):** Quá trình đăng nhập (`_create_ssh_client`) cũng như upload payload (`SCPClient.put`) đều được bọc trong bộ đếm thử lại cực đỉnh (thử lại 3 lần sau mỗi 1-2 giây nghỉ). Việc này triệt hạ 99% các lỗi truyền TCP/IP và Timeout.
### 2.3 Cơ Chế Khôi Phục File (RAM Disk SCP) 1. **Telnet (Port 23):** OpenWrt ngay sau Factory Reset mở Telnet không cần mật khẩu nhưng chặn SSH. Script luôn thử cổng này đầu tiên. Mọi exception (Refused, Timeout, Broken Pipe) đều kích hoạt fallback.
2. **SSH mật khẩu rỗng `""`:** Nếu Telnet đóng, thử SSH với pass rỗng — trường hợp thiết bị đang ở trạng thái semi-configured.
3. **SSH Backup Password:** Nếu pass rỗng bị `AuthenticationException`, thiết bị đang kẹt một pass cũ — thử `backup_password`.
4. **SSH Target Password (Idempotent):** Thử lại với chính `new_password` để chặn trường hợp device đã đổi pass thành công từ lần flash trước nhưng chưa được flash firmware.
- Firmware được tống thẳng tới phân vùng ảo RAM cục bộ đích (ví dụ: `/tmp/<tên_file>.bin`) thông qua giao thức SCP. Ghi file trực tiếp vào RAM cho phép đạt tốc độ tuyệt đối nhanh và tránh gây hao mòn sinh học lên FlashNOR của router. ### 4.2 Cơ Chế Dự Phòng Mật Khẩu (Update FW)
- Gửi lệnh `sync` qua kênh Shell để ép thiết bị lưu transaction xuống vùng nhớ cứng, chuẩn bị môi trường ổn định nhất.
### 2.4 Thủ Thuật Gọi `sysupgrade -F` Đơn giản hơn: thử `password` chính → nếu `AuthenticationException` → thử `backup_password`. Không có Telnet, không có set_passwd. Nếu cả hai đều fail thì trả về `FAIL` ngay.
- OpenWrt đời mới khá kén phần Metadata của Firmware Image. Cờ `-F` cởi trói kiểm duyệt đó và ép hệ điều hành nạp đè File bất chấp sự vắng mặt của metadata uImage. Cờ `-n` khiến hệ thống mất sách cấu hình cũ (Clean Flash). ### 4.3 Xử Lý Đa Luồng Chống Nghẽn
- Lệnh `sysupgrade` sẽ cắn xén cả phần OS dưới lõi máy sau đó Tắt mạng đột ngột. Do đó, script được lập trình để xử lý Exception: Sự kiện **MẤT KẾT NỐI CHỦ ĐỘNG** trong khu vực này là dấu hiệu ăn mừng của việc cài đặt thành công thay vì báo hỏng kết nối.
`paramiko` dễ báo lỗi `[Errno 64] Host is down` hoặc `[Errno 32] Broken Pipe` khi hàng chục thread cùng hit network stack cùng một lúc. Hệ thống chống nghẽn bằng 2 cơ chế:
- **Jitter ngẫu nhiên:** `time.sleep(random.uniform(0.1, 1.5))` phân tán chùm request theo thời gian, tránh tự gây DDoS nội bộ.
- **Retry Hooks:** `_create_ssh_client` retry 3 lần (nghỉ 1s giữa mỗi lần). `_upload_firmware` retry 3 lần (nghỉ 1.5s). Triệt 99% lỗi TCP transient.
### 4.4 RAM Disk SCP
Firmware được upload thẳng vào `/tmp/<tên_file>.bin` — phân vùng RAM ảo của OpenWrt. Lợi ích:
- Tốc độ ghi nhanh nhất (RAM vs Flash).
- Không gây hao mòn FlashNOR của router.
- File biến mất sau reboot, không để lại rác.
### 4.5 Thủ Thuật `sysupgrade -F -v -n`
| Cờ | Tác dụng |
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
| `-F` (Force) | Bỏ qua kiểm tra Image Metadata — bắt buộc với file build Raw / `tim_uImage` để tránh lỗi _"Image metadata not present"_. |
| `-v` (Verbose) | Log chi tiết vào `/tmp/sysup.log` để debug khi cần. |
| `-n` (No-keep) | Clean Flash — không giữ cấu hình cũ, tránh xung đột config giữa các phiên bản. |
**Đứt kết nối = Thành công:** `sysupgrade` phá kernel hiện tại rồi reboot nạp firmware mới → SSH session bị đứt là kết quả tất yếu và mong muốn. Script bẫy exception này và trả về `"DONE"`. Ngược lại, nếu sysupgrade fail sớm (< 4 giây, còn giữ kết nối), script đọc `/tmp/sysup.log` trả về error code đầy đủ.
---
## 5. Trình Gỡ Lỗi Nhanh
Cả hai luồng đều gọi `status_cb(msg)` tại mỗi bước toàn bộ tiến độ như _"Connecting SSH"_, _"Uploading firmware"_, _"Syncing filesystem"_ hiển thị trực tiếp tại cột **Status** trên giao diện chính.
Để debug chi tiết hơn, chạy app qua terminal:
```bash
./run.sh
```
Mọi exception đều bị bắt trả về chuỗi `"FAIL: <lý do>"` hiển thị lên UI không silent failure.

View File

@@ -1,59 +0,0 @@
# Tài liệu Kỹ thuật: Cập Nhật Firmware (Update FW)
Quy trình `Update FW` là phương thức tối ưu hóa để nâng cấp Firmware mới vào các thiết bị OpenWrt Live (đã và đang chạy sẵn Firmware của hệ sinh thái MiraV3). Cơ chế này dùng chính giao thức SSH thông qua `core/ssh_flasher.py` nhưng lược bỏ hoàn toàn các bước dò tìm và thiết lập mật khẩu thừa thãi.
## 1. Sơ đồ Luồng Hoạt Động (Operational Flow)
```mermaid
graph TD
A[Bắt đầu Update FW] --> B{Kiểm tra danh sách IP}
B -- Có IP lạ != 192.168.11.102 --> C[Bật MessageBox cảnh báo]
C -- Người dùng No --> Z[HỦY BỎ]
C -- Người dùng Yes --> D
B -- Chỉ có 192.168.11.102 --> D
D[Truyền tham số cấu hình tĩnh] --> E[ssh_user: root<br>ssh_password: admin123a<br>set_passwd: False]
E -->|Jitter Delay 0.1s - 1.5s| F[Kết nối thẳng SSH Port 22]
F -- Thành công --> G[Push Firmware SCP vào /tmp/]
F -- Lỗi / Sai Pass --> Y[DỪNG LẠI - Báo Lỗi]
G -->|Tối đa 3 lần Retry| H[Xác minh File `test -f`]
H --> I[Đồng bộ bộ nhớ `sync`]
I --> J[Gọi Sysupgrade: `sysupgrade -F -v -n /tmp/...`]
J --> K{Kết nối bị đứt?}
K -- Có --> L[Khởi động lại thành công - Báo DONE]
K -- Không --> M[Báo lỗi Sysupgrade]
```
## 2. Phân Tích Logic Cốt Lõi
### 2.1 Cơ Chế Bỏ Qua Telnet (Skip Set Password)
Điểm khác biệt lớn nhất của luồng Update FW so với Nạp Mới:
- Thông qua UI `Chế độ Flash > Update Firmware`, hệ thống hiểu rằng thiết bị đích đã từng được cài đặt hệ sinh thái MiraV3 và chắc chắn đã sở hữu lớp bảo mật SSH cơ bản (root/admin123a).
- Module `main.py` tự động kích hoạt cờ tuỳ chọn ngầm `set_passwd = False` khi gọi `FlashThread`.
- **Bỏ Bầu Trời Khởi Động:** Hệ thống ngay lập tức rẽ ngang vào luồng SSH trên Port 22. Tool bỏ qua hoàn toàn quy trình truy vấn cổng Telnet (23) chậm chạp cũng như các lệnh Set Password (vốn mất thêm từ 3-5 giây chờ đợi). Tốc độ tổng thể tăng tốc cực đáng kể, phục vụ trực tiếp cho quá trình Mass-Update.
### 2.2 Ràng Buộc An Toàn (Safety Checks)
Được kiểm soát từ tầng UI (`main.py`) để chống Flash nhầm hàng hoạt:
- Khi người dùng chọn chế độ Update, script quét qua danh sách MAC/IP đang tích chọn. Chỉ duy nhất thiết bị có IP cấu hình cứng `192.168.11.102` mới được âm thầm qua cửa (được xem là IP mặc định an toàn cho việc test Firmware).
- Nếu phát hiện **Bất kỳ IP lạ nào** (ví dụ: 192.168.1.5, 192.168.11.9), hệ thống lập tức chặn quy trình Flasher lại và bật MessageBox cảnh báo màu đỏ hỏi kỹ thuật viên xác nhận lại.
### 2.3 Quá Trình Sysupgrade (Hủy diệt và Tái sinh)
Cũng giống như luồng nạp mới, quá trình xả file dùng chung tệp lệnh: `sysupgrade -F -v -n /tmp/<tên_file>`
- **Cờ `-n`**: Clean Flash sạch trơn, không giữ Configuration rác của bản cũ.
- **Cờ `-F` (Force)**: Ép hệ điều hành OpenWrt bỏ qua bước kiểm tra Image Metadata tại Local. Tuỳ chọn này sống còn đối với các tệp tin Firmware trần trụi (như build Raw `tim_uImage`) để tránh việc sysupgrade từ chối file, văng lỗi "Image metadata not present" và ngắt ngang tiến trình.
- Kịch bản hoàn hảo nhất của quá trình này lại chính là **bị đứt kết nối mạng đột ngột**. Script đã bẫy một khoảng thời gian chờ (time.sleep) để canh việc Server SSH sập nguồn. Đứt mạng nghĩa là Kernel chuẩn bị Reboot nạp File, và hệ thống sẽ bắt Catch đánh giá là Thành Công (`DONE`). Ngược lại, nếu Sysupgrade thất bại, nó sẽ phun ra Log và giữ nguyên kết nối, lúc đó Script sẽ chụp lại Error Code và in ra UI.
---
## 3. Trình Gỡ Lỗi Nhanh (Debugging Update flow)
Lưu ý: Bạn đã xóa các file script debug đơn lẻ khỏi thư mục gốc (`debug_ssh.py`). Hiện tại luồng Update FW nên được gỡ lỗi trực tiếp bằng cách in Print/Log ra terminal của lệnh `./run.sh` lúc chạy App UI. Mọi tiến độ như "Đang kết nối SSH", "Đồng bộ file" được hiển thị minh bạch tại cột **Status** trên giao diện chính.

48
main.py
View File

@@ -23,9 +23,9 @@ from PyQt6.QtGui import QFont, QColor, QIcon, QAction
import datetime import datetime
from core.scanner import scan_network from core.scanner import scan_network
from core.flasher import flash_device from core.workers import ScanThread
from core.ssh_flasher import flash_device_ssh from core.flash_new_worker import NewFlashThread
from core.workers import ScanThread, FlashThread from core.flash_update_worker import UpdateFlashThread
from utils.network import _resolve_hostname, get_default_network from utils.network import _resolve_hostname, get_default_network
from utils.system import resource_path, get_machine_info, get_version from utils.system import resource_path, get_machine_info, get_version
@@ -311,7 +311,7 @@ class App(QWidget):
flash_layout.addWidget(self.method_container) flash_layout.addWidget(self.method_container)
# Warning UI for Update Mode # Warning UI for Update Mode
self.update_warning_lbl = QLabel("⚠️ FW Update Mode: Forced to SSH (root/admin123a). Target IP [192.168.11.102]. Other IPs require confirmation.") self.update_warning_lbl = QLabel("⚠️ FW Update Mode: Forced to SSH. Target IP [192.168.11.102]. Other IPs require confirmation.")
self.update_warning_lbl.setStyleSheet("color: #f38ba8; font-size: 13px; font-weight: bold; padding: 8px; border: 1px dotted #f38ba8;") self.update_warning_lbl.setStyleSheet("color: #f38ba8; font-size: 13px; font-weight: bold; padding: 8px; border: 1px dotted #f38ba8;")
self.update_warning_lbl.setWordWrap(True) self.update_warning_lbl.setWordWrap(True)
self.update_warning_lbl.setVisible(False) self.update_warning_lbl.setVisible(False)
@@ -565,9 +565,8 @@ class App(QWidget):
self._on_method_changed(self.method_combo.currentIndex()) self._on_method_changed(self.method_combo.currentIndex())
def _on_method_changed(self, index): def _on_method_changed(self, index):
"""Show/hide SSH credentials based on selected method.""" """SSH credentials are always hidden; credentials are hardcoded."""
method = self.method_combo.currentData() self.ssh_creds_widget.setVisible(False)
self.ssh_creds_widget.setVisible(method == "ssh")
def _get_selected_devices(self): def _get_selected_devices(self):
"""Return list of (table_row_index, device_dict) for checked devices.""" """Return list of (table_row_index, device_dict) for checked devices."""
@@ -639,26 +638,33 @@ class App(QWidget):
set_passwd = False set_passwd = False
else: else:
method = self.method_combo.currentData() method = self.method_combo.currentData()
ssh_user = self.ssh_user_input.text().strip() or "root" ssh_user = "root"
ssh_password = self.ssh_pass_input.text() or "admin123a" ssh_password = "admin123a"
ssh_backup_password = "admin123a" ssh_backup_password = "admin123a"
set_passwd = self.set_passwd_cb.isChecked() if method == "ssh" else False set_passwd = True if method == "ssh" else False
# Run flashing in background thread so UI doesn't freeze # Chọn đúng worker theo mode và chạy trong background thread
self.flash_thread = FlashThread( max_w = self.parallel_spin.value()
flash_devices, self.firmware, if mode == "update":
max_workers=self.parallel_spin.value(), self.flash_thread = UpdateFlashThread(
method=method, flash_devices, self.firmware,
ssh_user=ssh_user, max_workers=max_w,
ssh_password=ssh_password, )
ssh_backup_password=ssh_backup_password, else:
set_passwd=set_passwd self.flash_thread = NewFlashThread(
) flash_devices, self.firmware,
max_workers=max_w,
method=method,
ssh_user=ssh_user,
ssh_password=ssh_password,
ssh_backup_password=ssh_backup_password,
set_passwd=set_passwd,
)
self.flash_thread.device_status.connect(self._on_flash_status) self.flash_thread.device_status.connect(self._on_flash_status)
self.flash_thread.device_done.connect(self._on_flash_done) self.flash_thread.device_done.connect(self._on_flash_done)
self.flash_thread.all_done.connect(self._on_flash_all_done) self.flash_thread.all_done.connect(self._on_flash_all_done)
# Disable flash button during flashing # Disable flash button trong khi đang flash
self.btn_flash.setEnabled(False) self.btn_flash.setEnabled(False)
self.flash_thread.start() self.flash_thread.start()