From 4d756f58f393ac988d87de91027871dca06a81b1 Mon Sep 17 00:00:00 2001 From: mindsuru Date: Fri, 24 Nov 2023 11:40:41 +0100 Subject: [PATCH] =?UTF-8?q?=09ge=C3=A4ndert:=20=20=20=20=20=20=20platformi?= =?UTF-8?q?o=5Fupload.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platformio_upload.py | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/platformio_upload.py b/platformio_upload.py index b22e8d5..929ff76 100644 --- a/platformio_upload.py +++ b/platformio_upload.py @@ -16,6 +16,7 @@ import hashlib from urllib.parse import urlparse import time +from requests.auth import HTTPDigestAuth Import("env") try: @@ -29,6 +30,8 @@ def on_upload(source, target, env): firmware_path = str(source[0]) + + auth = None upload_url_compatibility = env.GetProjectOption('upload_url') upload_url = upload_url_compatibility.replace("/update", "") @@ -50,12 +53,37 @@ def on_upload(source, target, env): 'Referer': f'{upload_url}/update', 'Connection': 'keep-alive' } - - start_response = requests.get(start_url, headers=start_headers) - if start_response.status_code != 200: - print("start-request faild " + str(start_response.status_code)) - return + checkAuthResponse = requests.get(f"{upload_url_compatibility}/update") + + if checkAuthResponse.status_code == 401: + try: + username = env.GetProjectOption('username') + password = env.GetProjectOption('password') + except: + username = None + password = None + print("No authentication values specified.") + print('Please, add some Options in your .ini file like: "username=yourchoosenusername"') + if username is None or password is None: + print("Authentication required, but no credentials provided.") + return + print("Serverconfiguration: authentication needed.") + auth = HTTPDigestAuth(username, password) + doUpdateAuth = requests.get(start_url, headers=start_headers, auth=auth) + + if doUpdateAuth.status_code != 200: + print("authentication faild " + str(doUpdateAuth.status_code)) + return + print("Authentication successfull") + else: + auth = None + print("Serverconfiguration: autentication not needed.") + doUpdate = requests.get(start_url, headers=start_headers) + + if doUpdate.status_code != 200: + print("start-request faild " + str(doUpdate.status_code)) + return firmware.seek(0) encoder = MultipartEncoder(fields={ @@ -87,7 +115,7 @@ def on_upload(source, target, env): } - response = requests.post(f"{upload_url}/ota/upload", data=monitor, headers=post_headers) + response = requests.post(f"{upload_url}/ota/upload", data=monitor, headers=post_headers, auth=auth) bar.close() time.sleep(0.1)