Skip to content

Commit

Permalink
Fix SSL errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Dehuysser committed Dec 27, 2020
1 parent 816155f commit b24de1f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
42 changes: 36 additions & 6 deletions app/httpclient.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import usocket, os
import usocket, os, time

class Response:

Expand All @@ -7,13 +7,19 @@ def __init__(self, socket, saveToFile=None):
self._saveToFile = saveToFile
self._encoding = 'utf-8'
if saveToFile is not None:
print('Saving to file', saveToFile)
time.sleep(1)
CHUNK_SIZE = 512 # bytes
with open(saveToFile, 'w') as outfile:
print('Opened file', saveToFile)
time.sleep(1)
data = self._socket.read(CHUNK_SIZE)
while data:
outfile.write(data)
data = self._socket.read(CHUNK_SIZE)
outfile.close()
print('Data written to file', saveToFile)
time.sleep(1)

self.close()

Expand All @@ -39,8 +45,14 @@ def text(self):
return str(self.content, self._encoding)

def json(self):
import ujson
return ujson.loads(self.content)
result = None
try:
import ujson
result = ujson.load(self._socket)
finally:
self.close()
return result



class HttpClient:
Expand All @@ -49,6 +61,8 @@ def __init__(self, headers={}):
self._headers = headers

def request(self, method, url, data=None, json=None, file=None, custom=None, saveToFile=None, headers={}, stream=None):
print('Req', url)

def _write_headers(sock, _headers):
for k in _headers:
sock.write(b'{}: {}\r\n'.format(k, _headers[k]))
Expand All @@ -70,18 +84,34 @@ def _write_headers(sock, _headers):
host, port = host.split(':', 1)
port = int(port)

print('Getting addr info')
time.sleep(1)
ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)
if len(ai) < 1:
raise ValueError('You are not connected to the internet...')
ai = ai[0]

print(ai)

print('Opening socket', ai)
time.sleep(1)
s = usocket.socket(ai[0], ai[1], ai[2])
print('Socket open')
time.sleep(1)
try:
print('Connecting to socket')
time.sleep(1)
s.connect(ai[-1])
print('Connected to socket')
time.sleep(1)
if proto == 'https:':
s = ussl.wrap_socket(s, server_hostname=host)
try:
print('Wrapping socket with ssl')
time.sleep(1)
s = ussl.wrap_socket(s)
print('Wrapped socket with ssl')
time.sleep(1)
except:
print("An error occurred.")

s.write(b'%s /%s HTTP/1.0\r\n' % (method, path))
if not 'Host' in headers:
s.write(b'Host: %s\r\n' % host)
Expand Down
10 changes: 5 additions & 5 deletions app/ota_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ def _download_all_files(self, version, sub_dir=''):
for file in file_list.json():
path = self.modulepath(self.new_version_dir + '/' + file['path'].replace(self.main_dir + '/', '').replace(self.github_src_dir, ''))
if file['type'] == 'file':
download_url = file['download_url']
self._download_file(download_url.replace('refs/tags/', ''), path)
gitPath = file['path']
print('\tDownloading: ', gitPath, 'to', path)
self._download_file(version, gitPath, path)
elif file['type'] == 'dir':
print('Creating dir', path)
self.mkdir(path)
Expand All @@ -150,13 +151,12 @@ def _download_all_files(self, version, sub_dir=''):
file_list.close()

def _download_file(self, version, gitPath, path):
print('\tDownloading: ', gitPath, 'to', path)
repo_name = self.github_repo.replace('https://api.github.com/repos/', '')
try:
self.http_client.get('https://raw.githubusercontent.com/{}/{}/{}'.format(repo_name, version, gitPath), saveToFile=path)
self.http_client.get('https://cdn.jsdelivr.net/gh/{}@{}/{}'.format(repo_name, version, gitPath), saveToFile=path)
except OSError as err:
gc.collect()
self.http_client.get('https://cdn.jsdelivr.net/gh/{}@{}/{}'.format(repo_name, version, gitPath), saveToFile=path)
self.http_client.get('https://raw.githubusercontent.com/{}/{}/{}'.format(repo_name, version, gitPath), saveToFile=path)
pass


Expand Down
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@


def connectToWifiAndUpdate():
import machine, network, gc, app.secrets as secrets
import time, machine, network, gc, app.secrets as secrets
time.sleep(1)
print('Memory free', gc.mem_free())

from app.ota_updater import OTAUpdater
Expand Down

0 comments on commit b24de1f

Please sign in to comment.