api wird aus einer template variable gelesen, ausgabe des peer cert überarbeitet

This commit is contained in:
bernd 2023-10-13 20:03:12 +02:00
parent 06c4d75f27
commit 4b19063b66

View file

@ -19,7 +19,7 @@ try:
import threading import threading
from time import time, localtime, strftime, sleep from time import time, localtime, strftime, sleep
import configparser import configparser
except ImportException as e: except Exception as e:
print('Import error: {}'.format(e)) print('Import error: {}'.format(e))
@ -117,14 +117,28 @@ def display_peercert(cert):
elif len(cert) == 0: elif len(cert) == 0:
logging.debug('Peer certificate was not valid') logging.debug('Peer certificate was not valid')
else: else:
logging.debug('Peer certificate commonName: {}'.format( logging.debug('--- Peer cert ---')
cert['subject'][5][0][1])) for key in cert.keys():
logging.debug('Peer certificate serialNumber: {}'.format( if key in ('subject', 'serialNumber', 'notBefore', 'notAfter'):
cert['serialNumber'])) if key == 'subject':
logging.debug('Peer certificate notBefore: {}'.format( logging.debug(f'Subject: {cert[key]}')
cert['notBefore'])) for i in cert[key]:
logging.debug('Peer certificate notAfter: {}'.format( logging.debug(f'{i[0][0]}: {i[0][1]}')
cert['notAfter'])) else:
logging.debug(f'{key}: {cert[key]}')
logging.debug('-----------------')
try:
logging.debug('Peer certificate commonName: {}'.format(
cert['subject'][5][0][1]))
logging.debug('Peer certificate serialNumber: {}'.format(
cert['serialNumber']))
logging.debug('Peer certificate notBefore: {}'.format(
cert['notBefore']))
logging.debug('Peer certificate notAfter: {}'.format(
cert['notAfter']))
except Exception as error:
logging.debug('Cert display failed')
logging.debug(error)
def receive_buffer_is_valid(raw_data): def receive_buffer_is_valid(raw_data):
''' '''
@ -172,7 +186,7 @@ def read_api(api):
return False return False
return api_json_data return api_json_data
def change_status(status, timestamp, filename): def change_status(status, timestamp, api_template, filename):
''' '''
Write the new status together with a timestamp into the Space API JSON. Write the new status together with a timestamp into the Space API JSON.
param 1: byte object param 1: byte object
@ -181,18 +195,19 @@ def change_status(status, timestamp, filename):
''' '''
logging.debug('Change status API') logging.debug('Change status API')
# todo: use walrus operator := when migrating to python >= 3.8 # todo: use walrus operator := when migrating to python >= 3.8
data = read_api(filename) # data = read_api(filename)
if data is False: # if data is False:
return False # return False
if os.access(filename, os.W_OK): if os.access(filename, os.W_OK):
logging.debug('API file is writable') logging.debug('API file is writable')
with open(filename, 'w') as api_file: with open(filename, 'w') as api_file:
logging.debug('API file successfull writable opened') logging.debug('API file successfull writable opened')
data["state"]["open"] = status api_template["state"]["open"] = status
data["state"]["lastchange"] = timestamp api_template["state"]["lastchange"] = timestamp
try: try:
json.dump(data, api_file, indent=4) # json.dump(data, api_file, indent=4)
json.dump(api_template, api_file, indent=4)
except Exception as e: except Exception as e:
logging.error('Failed to change API file') logging.error('Failed to change API file')
logging.error('{}'.format(e)) logging.error('{}'.format(e))
@ -335,6 +350,47 @@ def main():
'token': 'aaaaa-bbbbb-ccccc-ddddd-eeeee' 'token': 'aaaaa-bbbbb-ccccc-ddddd-eeeee'
} }
} }
api_template = {
"api": "0.13",
"space": "Krautspace",
"url": "https://kraut.space",
"logo": "https://status.krautspace.de/images/krautspace_pixelbanner.png",
"location": {
"address": "Hackspace Jena e. V., Krautgasse 26, 07743 Jena, Germany",
"lat": 50.9292,
"lon": 11.5826
},
"state": {
"open": False,
"lastchange": 1563499131,
"icon": {
"open":"https://status.krautspace.de/images/krautspace_pixelicon_open.png",
"closed":"https://status.krautspace.de/images/krautspace_pixelicon_closed.png"
}
},
"feeds": {
"calendar": {
"type": "ical",
"url": "https://kraut.space/krautspace.ics"
}
},
"contact": {
"mastodon": "https://chaos.social/@krautspace",
"matrix": "#krautchan:matrix.kraut.space",
"email": "office@krautspace.de"
},
"issue_report_channels": [
"mastodon",
"matrix",
"email"
],
"projects": [
"https://git.kraut.space",
"https://github.com/HackspaceJena/"
]
}
logging.info('Try to read config file') logging.info('Try to read config file')
configfile = './apistatusd.conf' configfile = './apistatusd.conf'
config = configparser.ConfigParser() config = configparser.ConfigParser()
@ -402,7 +458,7 @@ def main():
raw_data = Connection.recv(1) raw_data = Connection.recv(1)
if receive_buffer_is_valid(raw_data) is True: if receive_buffer_is_valid(raw_data) is True:
status, timestamp = get_status_and_time(raw_data) status, timestamp = get_status_and_time(raw_data)
if change_status(status, timestamp, config['api']['api']) is True: if change_status(status, timestamp, api_template, config['api']['api']) is True:
answer = raw_data answer = raw_data
if config['mastodon']['send'].lower() == 'true': if config['mastodon']['send'].lower() == 'true':
logging.debug('Toot is set to true') logging.debug('Toot is set to true')