mirror of
https://github.com/esphome/esphome.git
synced 2025-09-22 21:22:22 +01:00
preen
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
import gzip
|
import gzip
|
||||||
import hashlib
|
import hashlib
|
||||||
import io
|
import io
|
||||||
@@ -55,6 +56,12 @@ UPLOAD_BUFFER_SIZE = UPLOAD_BLOCK_SIZE * 8
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# Authentication method lookup table: response -> (hash_func, nonce_size, name)
|
||||||
|
_AUTH_METHODS: dict[int, tuple[Callable[[], Any], int, str]] = {
|
||||||
|
RESPONSE_REQUEST_SHA256_AUTH: (hashlib.sha256, 64, "SHA256"),
|
||||||
|
RESPONSE_REQUEST_AUTH: (hashlib.md5, 32, "MD5"),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class ProgressBar:
|
class ProgressBar:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -262,18 +269,24 @@ def perform_ota(
|
|||||||
send_check(sock, result, "auth result")
|
send_check(sock, result, "auth result")
|
||||||
receive_exactly(sock, 1, "auth result", RESPONSE_AUTH_OK)
|
receive_exactly(sock, 1, "auth result", RESPONSE_AUTH_OK)
|
||||||
|
|
||||||
|
# Authentication method lookup table
|
||||||
|
auth_methods = {
|
||||||
|
RESPONSE_REQUEST_SHA256_AUTH: (hashlib.sha256, 64, "SHA256"),
|
||||||
|
RESPONSE_REQUEST_AUTH: (hashlib.md5, 32, "MD5"),
|
||||||
|
}
|
||||||
|
|
||||||
(auth,) = receive_exactly(
|
(auth,) = receive_exactly(
|
||||||
sock,
|
sock,
|
||||||
1,
|
1,
|
||||||
"auth",
|
"auth",
|
||||||
[RESPONSE_REQUEST_AUTH, RESPONSE_REQUEST_SHA256_AUTH, RESPONSE_AUTH_OK],
|
[RESPONSE_REQUEST_AUTH, RESPONSE_REQUEST_SHA256_AUTH, RESPONSE_AUTH_OK],
|
||||||
)
|
)
|
||||||
if auth == RESPONSE_REQUEST_SHA256_AUTH:
|
|
||||||
# SHA256 authentication
|
if auth in auth_methods:
|
||||||
perform_auth(sock, password, hashlib.sha256, 64, "SHA256")
|
hash_func, nonce_size, hash_name = auth_methods[auth]
|
||||||
elif auth == RESPONSE_REQUEST_AUTH:
|
perform_auth(sock, password, hash_func, nonce_size, hash_name)
|
||||||
# MD5 authentication (backward compatibility)
|
elif auth != RESPONSE_AUTH_OK:
|
||||||
perform_auth(sock, password, hashlib.md5, 32, "MD5")
|
raise OTAError(f"Unknown authentication method requested: 0x{auth:02X}")
|
||||||
|
|
||||||
# Set higher timeout during upload
|
# Set higher timeout during upload
|
||||||
sock.settimeout(30.0)
|
sock.settimeout(30.0)
|
||||||
|
Reference in New Issue
Block a user