mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-12 01:53:07 +03:00
Add MD5 to core, Fix OTA examples and Digest Authentication to OTA and espota.py
This commit is contained in:
@ -4,11 +4,12 @@
|
||||
# https://gist.github.com/igrr/d35ab8446922179dc58c
|
||||
#
|
||||
# Modified since 2015-09-18 from Pascal Gollor (https://github.com/pgollor)
|
||||
# Modified since 2015-11-09 from Hristo Gochkov (https://github.com/me-no-dev)
|
||||
#
|
||||
# This script will push an OTA update to the ESP
|
||||
# use it like: python espota.py -i <ESP_IP_address> -p <ESP_port> -f <sketch.bin>
|
||||
# use it like: python espota.py -i <ESP_IP_address> -p <ESP_port> [-a password] -f <sketch.bin>
|
||||
# Or to upload SPIFFS image:
|
||||
# python espota.py -i <ESP_IP_address> -p <ESP_port> -s -f <spiffs.bin>
|
||||
# python espota.py -i <ESP_IP_address> -p <ESP_port> [-a password] -s -f <spiffs.bin>
|
||||
#
|
||||
# Changes
|
||||
# 2015-09-18:
|
||||
@ -16,6 +17,11 @@
|
||||
# - Add logging.
|
||||
# - Send command to controller to differ between flashing and transmitting SPIFFS image.
|
||||
#
|
||||
# Changes
|
||||
# 2015-11-09:
|
||||
# - Added digest authentication
|
||||
# - Enchanced error tracking and reporting
|
||||
#
|
||||
|
||||
from __future__ import print_function
|
||||
import socket
|
||||
@ -23,6 +29,7 @@ import sys
|
||||
import os
|
||||
import optparse
|
||||
import logging
|
||||
import hashlib
|
||||
|
||||
# Commands
|
||||
FLASH = 0
|
||||
@ -43,14 +50,55 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
|
||||
return 1
|
||||
|
||||
content_size = os.path.getsize(filename)
|
||||
f = open(filename,'rb')
|
||||
file_md5 = hashlib.md5(f.read()).hexdigest()
|
||||
f.close()
|
||||
logging.info('Upload size: %d', content_size)
|
||||
message = '%s %d %d %d\n' % (password, command, serverPort, content_size)
|
||||
message = '%d %d %d %s\n' % (command, serverPort, content_size, file_md5)
|
||||
|
||||
# Wait for a connection
|
||||
logging.info('Sending invitation to: %s', remoteAddr)
|
||||
sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
remote_address = (remoteAddr, int(remotePort))
|
||||
sent = sock2.sendto(message, remote_address)
|
||||
sock2.settimeout(10)
|
||||
try:
|
||||
data = sock2.recv(37)
|
||||
except:
|
||||
logging.error('No Answer')
|
||||
sock2.close()
|
||||
return 1
|
||||
if (data != "OK"):
|
||||
if(data.startswith('AUTH')):
|
||||
nonce = data.split()[1]
|
||||
cnonce_text = '%s%u%s%s' % (filename, content_size, file_md5, remoteAddr)
|
||||
cnonce = hashlib.md5(cnonce_text).hexdigest()
|
||||
passmd5 = hashlib.md5(password).hexdigest()
|
||||
result_text = '%s:%s:%s' % (passmd5 ,nonce, cnonce)
|
||||
result = hashlib.md5(result_text).hexdigest()
|
||||
sys.stderr.write('Authenticating...')
|
||||
sys.stderr.flush()
|
||||
message = '%s %s\n' % (cnonce, result)
|
||||
sock2.sendto(message, remote_address)
|
||||
sock2.settimeout(10)
|
||||
try:
|
||||
data = sock2.recv(32)
|
||||
except:
|
||||
sys.stderr.write('FAIL\n')
|
||||
logging.error('No Answer to our Authentication')
|
||||
sock2.close()
|
||||
return 1
|
||||
if (data != "OK"):
|
||||
sys.stderr.write('FAIL\n')
|
||||
logging.error('%s', data)
|
||||
sock2.close()
|
||||
sys.exit(1);
|
||||
return 1
|
||||
sys.stderr.write('OK\n')
|
||||
else:
|
||||
logging.error('Bad Answer: %s', data)
|
||||
sock2.close()
|
||||
return 1
|
||||
sock2.close()
|
||||
|
||||
logging.info('Waiting for device...')
|
||||
@ -94,9 +142,13 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
|
||||
connection.close()
|
||||
f.close()
|
||||
sock.close()
|
||||
if (data != "OK"):
|
||||
sys.stderr.write('\n')
|
||||
logging.error('%s', data)
|
||||
return 1;
|
||||
return 0
|
||||
except:
|
||||
logging.error('Result: No Answer!')
|
||||
logging.error('No Result!')
|
||||
connection.close()
|
||||
f.close()
|
||||
sock.close()
|
||||
|
Reference in New Issue
Block a user