This commit is contained in:
Matthew Frost 2024-03-24 01:03:42 +01:00
parent 167a4e3811
commit e92e5805d1

View file

@ -1,4 +1,4 @@
# Allows PlatformIO to upload directly to AsyncElegantOTA # Allows PlatformIO to upload directly to ElegantOTA
# #
# To use: # To use:
# - copy this script into the same folder as your platformio.ini # - copy this script into the same folder as your platformio.ini
@ -9,11 +9,15 @@
# upload_url = <your upload URL> # upload_url = <your upload URL>
# #
# An example of an upload URL: # An example of an upload URL:
# upload_URL = http://192.168.1.123/update # upload_url = http://192.168.1.123/update
# also possible: upload_url = http://domainname/update
import requests import requests
import hashlib import hashlib
Import('env') from urllib.parse import urlparse
import time
from requests.auth import HTTPDigestAuth
Import("env")
try: try:
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
@ -26,10 +30,61 @@ except ImportError:
def on_upload(source, target, env): def on_upload(source, target, env):
firmware_path = str(source[0]) firmware_path = str(source[0])
upload_url = env.GetProjectOption('upload_url')
auth = None
upload_url_compatibility = env.GetProjectOption('custom_upload_url')
upload_url = upload_url_compatibility.replace("/update", "")
with open(firmware_path, 'rb') as firmware: with open(firmware_path, 'rb') as firmware:
md5 = hashlib.md5(firmware.read()).hexdigest() md5 = hashlib.md5(firmware.read()).hexdigest()
parsed_url = urlparse(upload_url)
host_ip = parsed_url.netloc
# Führe die GET-Anfrage aus
start_url = f"{upload_url}/ota/start?mode=fr&hash={md5}"
start_headers = {
'Host': host_ip,
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0',
'Accept': '*/*',
'Accept-Language': 'de,en-US;q=0.7,en;q=0.3',
'Accept-Encoding': 'gzip, deflate',
'Referer': f'{upload_url}/update',
'Connection': 'keep-alive'
}
checkAuthResponse = requests.get(f"{upload_url_compatibility}/update")
if checkAuthResponse.status_code == 401:
try:
username = env.GetProjectOption('custom_username')
password = env.GetProjectOption('custom_password')
except:
username = None
password = None
print("No authentication values specified.")
print('Please, add some Options in your .ini file like: \n\ncustom_username=username\ncustom_password=password\n')
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) firmware.seek(0)
encoder = MultipartEncoder(fields={ encoder = MultipartEncoder(fields={
'MD5': md5, 'MD5': md5,
@ -46,8 +101,31 @@ def on_upload(source, target, env):
monitor = MultipartEncoderMonitor(encoder, lambda monitor: bar.update(monitor.bytes_read - bar.n)) monitor = MultipartEncoderMonitor(encoder, lambda monitor: bar.update(monitor.bytes_read - bar.n))
response = requests.post(upload_url, data=monitor, headers={'Content-Type': monitor.content_type}) post_headers = {
'Host': host_ip,
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0',
'Accept': '*/*',
'Accept-Language': 'de,en-US;q=0.7,en;q=0.3',
'Accept-Encoding': 'gzip, deflate',
'Referer': f'{upload_url}/update',
'Connection': 'keep-alive',
'Content-Type': monitor.content_type,
'Content-Length': str(monitor.len),
'Origin': f'{upload_url}'
}
response = requests.post(f"{upload_url}/ota/upload", data=monitor, headers=post_headers, auth=auth)
bar.close() bar.close()
print(response,response.text) time.sleep(0.1)
if response.status_code != 200:
message = "\nUpload faild.\nServer response: " + response.text
tqdm.write(message)
else:
message = "\nUpload successful.\nServer response: " + response.text
tqdm.write(message)
env.Replace(UPLOADCMD=on_upload) env.Replace(UPLOADCMD=on_upload)