2019-07-27 16:51:56 +02:00
|
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
2022-03-06 11:55:29 +01:00
|
|
|
|
# file: apistatusd.py
|
2019-07-27 16:51:56 +02:00
|
|
|
|
# date: 26.07.2019
|
2022-07-14 21:47:16 +02:00
|
|
|
|
# mail: berhsi@web.de
|
2019-07-27 16:51:56 +02:00
|
|
|
|
|
2020-07-09 12:08:06 +02:00
|
|
|
|
# Status server, listening for door status updates. The IPv4 address and port
|
|
|
|
|
# to listen on are configurable, by default localhost:10001 is used. The
|
|
|
|
|
# connection is secured by TLS and client side authentication.
|
2019-07-27 16:51:56 +02:00
|
|
|
|
|
2022-07-14 21:47:16 +02:00
|
|
|
|
try:
|
|
|
|
|
import json
|
|
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
|
import socket
|
|
|
|
|
import ssl
|
|
|
|
|
import sys
|
|
|
|
|
import requests
|
|
|
|
|
import threading
|
|
|
|
|
from time import time, localtime, strftime, sleep
|
|
|
|
|
import configparser
|
2023-10-13 20:03:12 +02:00
|
|
|
|
except Exception as e:
|
2022-07-14 21:47:16 +02:00
|
|
|
|
print('Import error: {}'.format(e))
|
2022-07-12 21:57:20 +02:00
|
|
|
|
|
|
|
|
|
|
2019-07-30 22:06:07 +02:00
|
|
|
|
def certs_readable(config):
|
2019-07-30 22:20:45 +02:00
|
|
|
|
'''
|
2019-09-19 10:21:33 +02:00
|
|
|
|
checks at start, if the needed certificates defined (no nullstring) and
|
|
|
|
|
readable.
|
2019-07-30 22:20:45 +02:00
|
|
|
|
param 1: dictionary
|
|
|
|
|
return: boolean
|
|
|
|
|
'''
|
2020-07-09 12:08:06 +02:00
|
|
|
|
for i in (config['server']['key'], config['server']['cert'],
|
|
|
|
|
config['client']['cert']):
|
2019-09-19 10:21:33 +02:00
|
|
|
|
if i == '' or os.access(i, os.R_OK) is False:
|
2020-07-09 12:08:06 +02:00
|
|
|
|
logging.error('Cannot read {}'.format(i))
|
2019-07-30 22:06:07 +02:00
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
2020-07-09 12:08:06 +02:00
|
|
|
|
def print_config(config):
|
2019-07-29 18:32:27 +02:00
|
|
|
|
'''
|
2020-07-09 12:08:06 +02:00
|
|
|
|
Logs the config with level debug.
|
2019-07-27 16:51:56 +02:00
|
|
|
|
'''
|
|
|
|
|
logging.debug('Using config:')
|
2020-07-09 12:08:06 +02:00
|
|
|
|
for section in config.sections():
|
|
|
|
|
logging.debug('Section {}'.format(section))
|
|
|
|
|
for i in config[section]:
|
2022-07-14 21:47:16 +02:00
|
|
|
|
if i == 'token':
|
|
|
|
|
logging.debug(' {}: {}'.format(i, 'aaaaa-bbbbb-ccccc-ddddd-eeeee'))
|
|
|
|
|
else:
|
|
|
|
|
logging.debug(' {}: {}'.format(i, config[section][i]))
|
2019-07-27 16:51:56 +02:00
|
|
|
|
|
2022-07-30 12:09:38 +02:00
|
|
|
|
def create_ssl_context(config):
|
|
|
|
|
'''
|
|
|
|
|
Creates the ssl context.
|
|
|
|
|
return: context object or None
|
|
|
|
|
'''
|
2023-10-23 23:50:29 +02:00
|
|
|
|
requirement = ssl.CERT_REQUIRED
|
2023-10-25 00:35:28 +02:00
|
|
|
|
match config['client']['required'].lower():
|
|
|
|
|
case 'false':
|
|
|
|
|
requirement = ssl.CERT_NONE
|
|
|
|
|
case 'may':
|
|
|
|
|
requirement = ssl.CERT_OPTIONAL
|
2023-10-23 23:50:29 +02:00
|
|
|
|
|
2022-07-30 12:09:38 +02:00
|
|
|
|
try:
|
|
|
|
|
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
|
|
|
|
|
context.verify_mode = requirement
|
|
|
|
|
context.load_cert_chain(certfile=config['server']['cert'],
|
|
|
|
|
keyfile=config['server']['key'])
|
|
|
|
|
context.load_verify_locations(cafile=config['client']['cert'])
|
2023-10-25 00:35:28 +02:00
|
|
|
|
context.set_ciphers("ECDHE-RSA-AES256-GCM-SHA384 ECDHE-RSA-AES128-GCM-SHA256")
|
|
|
|
|
context.set_ecdh_curve("secp384r1")
|
|
|
|
|
context.minimum_version = ssl.TLSVersion.TLSv1_2
|
|
|
|
|
context.maximum_version = ssl.TLSVersion.TLSv1_2
|
2022-07-30 12:09:38 +02:00
|
|
|
|
# ensure, compression is disabled (disabled by default anyway at the moment)
|
|
|
|
|
context.options |= ssl.OP_NO_COMPRESSION
|
2023-10-23 23:50:29 +02:00
|
|
|
|
context.options |= ssl.OP_CIPHER_SERVER_PREFERENCE
|
2023-10-25 00:35:28 +02:00
|
|
|
|
context.options |= ssl.OP_SINGLE_ECDH_USE
|
2022-07-30 12:09:38 +02:00
|
|
|
|
logging.debug('SSL context created')
|
2023-10-25 00:35:28 +02:00
|
|
|
|
return context
|
2022-07-30 12:09:38 +02:00
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error('Failed to create SSL context')
|
|
|
|
|
logging.error('Error: {}'.format(e))
|
|
|
|
|
return None
|
|
|
|
|
|
2019-09-10 15:33:27 +02:00
|
|
|
|
def print_ciphers(cipherlist):
|
2019-09-13 10:29:38 +02:00
|
|
|
|
'''
|
2020-07-09 12:08:06 +02:00
|
|
|
|
Prints the list of allowed ciphers.
|
2019-09-13 10:29:38 +02:00
|
|
|
|
param1: dictionary
|
|
|
|
|
return: boolean
|
|
|
|
|
'''
|
2022-03-11 13:28:03 +01:00
|
|
|
|
logging.debug('Available ciphers')
|
2019-09-10 15:33:27 +02:00
|
|
|
|
for i in cipherlist:
|
|
|
|
|
for j in i.keys():
|
2022-03-11 13:28:03 +01:00
|
|
|
|
if j in ('name', 'protocol'):
|
|
|
|
|
logging.debug('{}: {}'.format(j, i[j]))
|
2019-09-10 15:33:27 +02:00
|
|
|
|
|
2022-03-11 13:28:03 +01:00
|
|
|
|
def print_context(ctx):
|
|
|
|
|
'''
|
|
|
|
|
Prints the ssl settings for the given ssl context.
|
|
|
|
|
param1: context object
|
|
|
|
|
'''
|
|
|
|
|
logging.debug('----------- context ----------------')
|
|
|
|
|
logging.debug('Minimum version ssl: {}'.format(ctx.minimum_version))
|
|
|
|
|
logging.debug('Maximum version ssl: {}'.format(ctx.maximum_version))
|
|
|
|
|
logging.debug('SSL options enabled: {}'.format(ctx.options))
|
2023-10-23 23:50:29 +02:00
|
|
|
|
logging.debug('Protocol: {}'.format(ssl.get_protocol_name(ctx.protocol)))
|
2022-03-11 13:28:03 +01:00
|
|
|
|
logging.debug('Verify flags certificates: {}'.format(ctx.verify_flags))
|
|
|
|
|
logging.debug('Verify mode: {}'.format(ctx.verify_mode))
|
|
|
|
|
print_ciphers(ctx.get_ciphers())
|
|
|
|
|
logging.debug('------------------------------------')
|
2019-09-10 15:33:27 +02:00
|
|
|
|
|
2019-07-29 18:32:27 +02:00
|
|
|
|
def display_peercert(cert):
|
2019-09-13 10:29:38 +02:00
|
|
|
|
'''
|
2020-07-09 12:08:06 +02:00
|
|
|
|
Displays the values of a given certificate.
|
2022-03-11 13:28:03 +01:00
|
|
|
|
param1: dictionary or none
|
2019-09-13 10:29:38 +02:00
|
|
|
|
'''
|
2022-03-11 13:28:03 +01:00
|
|
|
|
if cert == None:
|
|
|
|
|
logging.debug('Peer does not offer a certificate')
|
|
|
|
|
elif len(cert) == 0:
|
|
|
|
|
logging.debug('Peer certificate was not valid')
|
|
|
|
|
else:
|
2023-10-13 20:03:12 +02:00
|
|
|
|
logging.debug('--- Peer cert ---')
|
|
|
|
|
for key in cert.keys():
|
|
|
|
|
if key in ('subject', 'serialNumber', 'notBefore', 'notAfter'):
|
|
|
|
|
if key == 'subject':
|
|
|
|
|
logging.debug(f'Subject: {cert[key]}')
|
|
|
|
|
for i in cert[key]:
|
|
|
|
|
logging.debug(f'{i[0][0]}: {i[0][1]}')
|
|
|
|
|
else:
|
|
|
|
|
logging.debug(f'{key}: {cert[key]}')
|
|
|
|
|
logging.debug('-----------------')
|
2019-07-29 18:32:27 +02:00
|
|
|
|
|
2019-07-27 16:51:56 +02:00
|
|
|
|
def receive_buffer_is_valid(raw_data):
|
|
|
|
|
'''
|
2020-07-09 12:08:06 +02:00
|
|
|
|
Checks validity of the received buffer contents.
|
2022-03-06 11:55:29 +01:00
|
|
|
|
param 1: byte object
|
2019-07-27 16:51:56 +02:00
|
|
|
|
return: boolean
|
|
|
|
|
'''
|
2023-07-17 19:07:37 +02:00
|
|
|
|
try:
|
|
|
|
|
if raw_data.decode('utf-8', 'strict') in ('0', '1'):
|
|
|
|
|
logging.debug('Argument is valid: {}'.format(raw_data))
|
|
|
|
|
return True
|
|
|
|
|
except UnicodeDecodeError as err:
|
|
|
|
|
logging.error('Argument is not valid unicode')
|
|
|
|
|
logging.error(err)
|
|
|
|
|
return False
|
|
|
|
|
except Exception as err:
|
|
|
|
|
logging.error('Exception occurred')
|
|
|
|
|
logging.error(err)
|
|
|
|
|
return False
|
2020-07-09 12:08:06 +02:00
|
|
|
|
logging.debug('Argument is not valid: {}'.format(raw_data))
|
|
|
|
|
return False
|
2019-07-27 16:51:56 +02:00
|
|
|
|
|
2022-07-30 10:32:03 +02:00
|
|
|
|
def read_api(api):
|
|
|
|
|
'''
|
|
|
|
|
Reads the Space API JSON into a dict. Returns the dict on success and
|
|
|
|
|
False on failure.
|
|
|
|
|
|
|
|
|
|
param 1: string
|
|
|
|
|
return: dict or boolean
|
|
|
|
|
'''
|
|
|
|
|
logging.debug('Open API file: {}'.format(api))
|
|
|
|
|
# return early if the API JSON cannot be read
|
|
|
|
|
if not os.access(api, os.R_OK):
|
|
|
|
|
logging.error('Failed to read API file')
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
logging.debug('API is readable')
|
|
|
|
|
with open(api, 'r') as api_file:
|
|
|
|
|
logging.debug('API file successfully readable opened')
|
|
|
|
|
try:
|
|
|
|
|
api_json_data = json.load(api_file)
|
|
|
|
|
logging.debug('API file successfully read')
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error('Failed to read API file: {}'.format(e))
|
|
|
|
|
return False
|
|
|
|
|
return api_json_data
|
|
|
|
|
|
2023-10-13 20:03:12 +02:00
|
|
|
|
def change_status(status, timestamp, api_template, filename):
|
2019-07-27 16:51:56 +02:00
|
|
|
|
'''
|
2020-07-09 12:08:06 +02:00
|
|
|
|
Write the new status together with a timestamp into the Space API JSON.
|
2022-03-06 11:55:29 +01:00
|
|
|
|
param 1: byte object
|
2019-07-27 16:51:56 +02:00
|
|
|
|
param 2: string
|
|
|
|
|
return: boolean
|
|
|
|
|
'''
|
|
|
|
|
logging.debug('Change status API')
|
2022-07-10 18:17:33 +02:00
|
|
|
|
if os.access(filename, os.W_OK):
|
2020-07-09 12:08:06 +02:00
|
|
|
|
logging.debug('API file is writable')
|
2022-07-10 18:17:33 +02:00
|
|
|
|
with open(filename, 'w') as api_file:
|
2022-07-30 10:32:03 +02:00
|
|
|
|
logging.debug('API file successfull writable opened')
|
2023-10-13 20:03:12 +02:00
|
|
|
|
api_template["state"]["open"] = status
|
|
|
|
|
api_template["state"]["lastchange"] = timestamp
|
2020-07-09 12:08:06 +02:00
|
|
|
|
try:
|
2023-10-13 20:03:12 +02:00
|
|
|
|
# json.dump(data, api_file, indent=4)
|
|
|
|
|
json.dump(api_template, api_file, indent=4)
|
2020-07-09 12:08:06 +02:00
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error('Failed to change API file')
|
|
|
|
|
logging.error('{}'.format(e))
|
2022-03-06 11:55:29 +01:00
|
|
|
|
return False
|
2020-07-09 12:08:06 +02:00
|
|
|
|
logging.debug('API file changed')
|
|
|
|
|
else:
|
|
|
|
|
logging.error('API file is not writable. Wrong permissions?')
|
|
|
|
|
return False
|
2022-07-10 18:17:33 +02:00
|
|
|
|
logging.info('API file successfull changed to {}'.format(status))
|
2020-07-09 12:08:06 +02:00
|
|
|
|
return True
|
2019-07-27 16:51:56 +02:00
|
|
|
|
|
2022-07-10 18:17:33 +02:00
|
|
|
|
def get_status_and_time(raw_data):
|
2019-07-27 16:51:56 +02:00
|
|
|
|
'''
|
|
|
|
|
Create a timestamp, changes the value of the given byte into a string
|
|
|
|
|
and returns both.
|
2022-03-06 11:55:29 +01:00
|
|
|
|
param 1: byte object
|
2022-07-10 19:38:29 +02:00
|
|
|
|
return: tuple (boolean, integer)
|
2019-07-27 16:51:56 +02:00
|
|
|
|
'''
|
2023-10-25 01:08:16 +02:00
|
|
|
|
status = raw_data.decode('utf-8', 'strict') == '1'
|
2020-11-22 22:35:27 +01:00
|
|
|
|
timestamp = int(str(time()).split('.')[0])
|
2020-07-09 12:08:06 +02:00
|
|
|
|
|
2019-09-19 10:21:33 +02:00
|
|
|
|
logging.debug('Set values for timestamp: {} and status: {}'.format(
|
2020-11-22 22:35:27 +01:00
|
|
|
|
str(timestamp), str(status)))
|
2019-07-27 16:51:56 +02:00
|
|
|
|
return (status, timestamp)
|
|
|
|
|
|
2022-07-14 21:47:16 +02:00
|
|
|
|
def join_path(host, api):
|
|
|
|
|
'''
|
|
|
|
|
Becomes two parts (host and api) of the mastodon url and concanate them
|
|
|
|
|
param1: string
|
|
|
|
|
param2: string
|
|
|
|
|
return: string
|
|
|
|
|
'''
|
|
|
|
|
url = ''
|
|
|
|
|
try:
|
|
|
|
|
if host[-1] == '/' and api[0] == '/':
|
|
|
|
|
url = ''.join((host, api[1:]))
|
|
|
|
|
elif host[-1] != '/' and api[0] != '/':
|
|
|
|
|
url = '/'.join((host, api))
|
|
|
|
|
else:
|
|
|
|
|
url = ''.join((host, api))
|
|
|
|
|
except TypeError as e:
|
2023-10-17 21:33:24 +02:00
|
|
|
|
logging.error("Can't join path: {}".format(e))
|
2022-07-14 21:47:16 +02:00
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
class Toot(threading.Thread):
|
|
|
|
|
'''
|
|
|
|
|
The thread to toot the status to mastodon.
|
|
|
|
|
'''
|
|
|
|
|
def __init__(self, status, timestamp, config):
|
|
|
|
|
'''
|
|
|
|
|
param1: boolean
|
|
|
|
|
param2: integer
|
|
|
|
|
param3: dictionary
|
|
|
|
|
'''
|
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
self.status = status
|
|
|
|
|
self.config = config
|
|
|
|
|
self.timestamp = timestamp
|
|
|
|
|
self.api = '/api/v1/statuses'
|
|
|
|
|
self.auth = {'Authorization': ''}
|
|
|
|
|
self.data = {'status': ''}
|
|
|
|
|
self.url = ''
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
|
'''
|
|
|
|
|
return: boolean
|
|
|
|
|
'''
|
|
|
|
|
timeformat = '%d.%m.%Y %H:%M Uhr'
|
|
|
|
|
# convert seconds into timestring
|
|
|
|
|
try:
|
|
|
|
|
timestring = strftime(timeformat, localtime(self.timestamp))
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error('Can not convert timestamp into timestring')
|
|
|
|
|
return False
|
|
|
|
|
# set status message
|
2023-10-25 01:08:16 +02:00
|
|
|
|
if self.status:
|
2022-07-14 21:47:16 +02:00
|
|
|
|
self.data['status'] = 'Krautspace is open since: {}'.format(timestring)
|
2023-10-25 01:08:16 +02:00
|
|
|
|
else:
|
2022-07-14 21:47:16 +02:00
|
|
|
|
self.data['status'] = 'Krautspace is closed since: {}'.format(timestring)
|
|
|
|
|
logging.debug('Message: {}'.format(self.data['status']))
|
|
|
|
|
# build mastodon api url
|
|
|
|
|
self.url = join_path(self.config['mastodon']['host'], self.api)
|
|
|
|
|
# build authentcation header
|
|
|
|
|
self.auth['Authorization'] = 'Bearer {}'.format(
|
|
|
|
|
self.config['mastodon']['token'])
|
|
|
|
|
# and finaly send request to mastodon
|
|
|
|
|
try:
|
|
|
|
|
logging.debug('Try to toot status')
|
|
|
|
|
response = requests.post(self.url, data = self.data,
|
|
|
|
|
headers = self.auth)
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
logging.info('Toot successful send')
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
logging.error('Failed to toot. Response: {}'.format(response.status_code))
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error('Exception occurred: {}'.format(e))
|
|
|
|
|
return False
|
2019-07-27 16:51:56 +02:00
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
'''
|
2020-07-09 12:08:06 +02:00
|
|
|
|
The main function - open a socket, create a ssl context, load certs and
|
|
|
|
|
listen for connections. At SSL context we set only one available cipher
|
2019-09-19 10:21:33 +02:00
|
|
|
|
suite and disable compression.
|
2020-07-09 12:08:06 +02:00
|
|
|
|
OP_NO_COMPRESSION: prevention against CRIME attack
|
|
|
|
|
OP_DONT_ISERT_EMPTY_FRAGMENTS: prevention agains CBC 4 attack
|
2019-09-19 10:21:33 +02:00
|
|
|
|
(cve-2011-3389)
|
2019-07-27 16:51:56 +02:00
|
|
|
|
'''
|
2022-03-06 11:55:29 +01:00
|
|
|
|
answer = '3'.encode(encoding='utf-8', errors='strict')
|
2022-07-18 20:15:25 +02:00
|
|
|
|
loglevel = logging.INFO
|
2019-09-11 21:24:28 +02:00
|
|
|
|
formatstring = '%(asctime)s: %(levelname)s: %(message)s'
|
|
|
|
|
logging.basicConfig(format=formatstring, level=loglevel)
|
|
|
|
|
|
2020-07-09 12:08:06 +02:00
|
|
|
|
default_config = {
|
|
|
|
|
'general': {
|
|
|
|
|
'timeout': 3.0,
|
2022-07-18 20:15:25 +02:00
|
|
|
|
'loglevel': 'info'
|
2020-07-09 12:08:06 +02:00
|
|
|
|
},
|
|
|
|
|
'server': {
|
|
|
|
|
'host': 'localhost',
|
|
|
|
|
'port': 10001,
|
|
|
|
|
'cert': './certs/server.crt',
|
|
|
|
|
'key': './certs/server.key'
|
|
|
|
|
},
|
|
|
|
|
'client': {
|
2022-07-30 12:09:38 +02:00
|
|
|
|
'cert': './certs/client.crt',
|
|
|
|
|
'required': 'true'
|
2020-07-09 12:08:06 +02:00
|
|
|
|
},
|
|
|
|
|
'api': {
|
|
|
|
|
'api': './api',
|
|
|
|
|
'template': './api_template'
|
2022-07-10 18:17:33 +02:00
|
|
|
|
},
|
|
|
|
|
'mastodon': {
|
|
|
|
|
'send': 'false',
|
|
|
|
|
'host': 'localhost',
|
|
|
|
|
'token': 'aaaaa-bbbbb-ccccc-ddddd-eeeee'
|
2019-07-27 16:51:56 +02:00
|
|
|
|
}
|
2020-07-09 12:08:06 +02:00
|
|
|
|
}
|
2023-10-13 20:03:12 +02:00
|
|
|
|
|
|
|
|
|
api_template = {
|
|
|
|
|
"api": "0.13",
|
2023-10-20 00:08:14 +02:00
|
|
|
|
"space": "Krautspace – Hackspace Jena e.V.",
|
2023-10-13 20:03:12 +02:00
|
|
|
|
"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": {
|
2023-10-17 21:05:03 +02:00
|
|
|
|
"open":"https://status.krautspace.de/images/status-open.png",
|
|
|
|
|
"closed":"https://status.krautspace.de/images/status-closed.png"
|
2023-10-13 20:03:12 +02:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"feeds": {
|
|
|
|
|
"calendar": {
|
|
|
|
|
"type": "ical",
|
2023-10-17 21:05:03 +02:00
|
|
|
|
"url": "https://cloud.kraut.space/remote.php/dav/public-calendars/2EkPGt3PF6WwYsA3?export"
|
2023-10-20 00:08:14 +02:00
|
|
|
|
},
|
|
|
|
|
"blog": {
|
|
|
|
|
"type": "rss",
|
|
|
|
|
"url": "https://wiki.kraut.space/feed.php?mode=list&ns=blog:content"
|
2023-10-13 20:03:12 +02:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"contact": {
|
2023-10-20 00:08:14 +02:00
|
|
|
|
"email": "office@kraut.space",
|
2023-10-17 21:05:03 +02:00
|
|
|
|
"mastodon": "@krautspace@chaos.social",
|
2023-10-20 00:08:14 +02:00
|
|
|
|
"matrix": "#krautchan:matrix.kraut.space"
|
2023-10-13 20:03:12 +02:00
|
|
|
|
},
|
|
|
|
|
"issue_report_channels": [
|
|
|
|
|
"email"
|
|
|
|
|
],
|
|
|
|
|
"projects": [
|
2023-10-17 21:05:03 +02:00
|
|
|
|
"https://git.kraut.space/Krautspace"
|
2023-10-13 20:03:12 +02:00
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-18 20:15:25 +02:00
|
|
|
|
logging.info('Try to read config file')
|
2022-03-06 11:55:29 +01:00
|
|
|
|
configfile = './apistatusd.conf'
|
2020-07-09 12:08:06 +02:00
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
|
config.read_dict(default_config)
|
|
|
|
|
if not config.read(configfile):
|
|
|
|
|
logging.warning('Configuration file %s not found or not readable. Using default values.',
|
|
|
|
|
configfile)
|
2019-09-11 21:24:28 +02:00
|
|
|
|
|
2020-07-09 12:08:06 +02:00
|
|
|
|
logger = logging.getLogger()
|
|
|
|
|
if not config['general']['loglevel'] in ('critical', 'error', 'warning', 'info', 'debug'):
|
|
|
|
|
logging.warning('Invalid loglevel %s given. Using default level %s.',
|
|
|
|
|
config['general']['loglevel'],
|
|
|
|
|
default_config['general']['loglevel'])
|
|
|
|
|
config.set('general', 'loglevel', default_config['general']['loglevel'])
|
2019-07-27 16:51:56 +02:00
|
|
|
|
|
2022-07-18 20:15:25 +02:00
|
|
|
|
logging.info('Set loglevel to {}'.format(config['general']['loglevel'].upper()))
|
2020-07-09 12:08:06 +02:00
|
|
|
|
logger.setLevel(config['general']['loglevel'].upper())
|
2019-09-17 19:07:07 +02:00
|
|
|
|
|
2020-07-09 12:08:06 +02:00
|
|
|
|
print_config(config)
|
|
|
|
|
|
|
|
|
|
# todo: zertifikate sollten nur lesbar sein!
|
|
|
|
|
if not certs_readable(config):
|
2019-07-30 22:06:07 +02:00
|
|
|
|
logging.error('Cert check failed\nExit')
|
2020-07-09 12:08:06 +02:00
|
|
|
|
sys.exit(1)
|
2019-07-30 22:06:07 +02:00
|
|
|
|
|
2022-07-30 12:09:38 +02:00
|
|
|
|
# ssl context erstellen
|
|
|
|
|
context = create_ssl_context(config)
|
2023-10-23 23:50:29 +02:00
|
|
|
|
if context is None:
|
|
|
|
|
sys.exit(2)
|
|
|
|
|
print_context(context)
|
2019-07-29 18:32:27 +02:00
|
|
|
|
|
2022-07-23 01:42:10 +02:00
|
|
|
|
try:
|
2022-07-30 10:05:32 +02:00
|
|
|
|
# tcp socket öffnen => MySocket
|
2022-07-23 01:42:10 +02:00
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as MySocket:
|
2022-07-30 10:05:32 +02:00
|
|
|
|
logging.debug('TCP Socket created')
|
|
|
|
|
MySocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2023-10-23 23:50:29 +02:00
|
|
|
|
# MySocket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
|
|
|
|
# keep = MySocket.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE)
|
|
|
|
|
# logging.debug('Socket keepalive: {}'.format(keep))
|
2019-07-27 16:51:56 +02:00
|
|
|
|
try:
|
2022-07-23 01:42:10 +02:00
|
|
|
|
MySocket.bind((config['server']['host'], int(config['server']['port'])))
|
|
|
|
|
MySocket.listen(5)
|
|
|
|
|
logging.info('Listening on {} at Port {}'.format(config['server']['host'],
|
|
|
|
|
config['server']['port']))
|
2019-07-27 16:51:56 +02:00
|
|
|
|
except Exception as e:
|
2022-07-23 01:42:10 +02:00
|
|
|
|
logging.error('Unable to bind and listen')
|
2019-07-27 16:51:56 +02:00
|
|
|
|
logging.error('{}'.format(e))
|
2022-07-30 12:09:38 +02:00
|
|
|
|
sys.exit(3)
|
2022-07-30 10:05:32 +02:00
|
|
|
|
# endlos auf verbindungen warten => ClientSocket
|
|
|
|
|
while True:
|
|
|
|
|
ClientSocket, ClientAddress = MySocket.accept()
|
|
|
|
|
logging.info('Client connected: {}:{}'.format(ClientAddress[0], ClientAddress[1]))
|
|
|
|
|
# die verbindung in den ssl-context verpacken => Connection
|
2022-08-03 17:58:27 +02:00
|
|
|
|
try:
|
2023-10-23 23:50:29 +02:00
|
|
|
|
ClientSocket.settimeout(float(config['general']['timeout']))
|
|
|
|
|
logging.debug('set ssl handshake timeout to {}s'.format(ClientSocket.gettimeout()))
|
2022-08-03 17:58:27 +02:00
|
|
|
|
Connection = context.wrap_socket(ClientSocket, server_side=True)
|
2023-10-25 01:08:16 +02:00
|
|
|
|
logging.debug('SSL Connection established')
|
|
|
|
|
display_peercert(Connection.getpeercert(binary_form=False))
|
2022-08-03 17:58:27 +02:00
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error('Unexpected error: {}'.format(e))
|
|
|
|
|
continue
|
2023-10-25 01:08:16 +02:00
|
|
|
|
|
2022-08-03 17:58:27 +02:00
|
|
|
|
# empfangen und antworten
|
|
|
|
|
raw_data = Connection.recv(1)
|
2023-10-25 01:08:16 +02:00
|
|
|
|
if receive_buffer_is_valid(raw_data):
|
2022-08-03 17:58:27 +02:00
|
|
|
|
status, timestamp = get_status_and_time(raw_data)
|
2023-10-25 01:08:16 +02:00
|
|
|
|
if change_status(status, timestamp, api_template, config['api']['api']):
|
2022-08-03 17:58:27 +02:00
|
|
|
|
answer = raw_data
|
|
|
|
|
if config['mastodon']['send'].lower() == 'true':
|
|
|
|
|
logging.debug('Toot is set to true')
|
|
|
|
|
try:
|
|
|
|
|
toot_thread = Toot(status, timestamp, config)
|
|
|
|
|
toot_thread.run()
|
|
|
|
|
except InitException as e:
|
|
|
|
|
logging.error('InitException: {}'.format(e))
|
|
|
|
|
except Exception as ex:
|
|
|
|
|
logging.debug('Exception: {}'.format(ex))
|
2023-10-23 23:50:29 +02:00
|
|
|
|
else:
|
|
|
|
|
logging.debug('Toot is set to false')
|
2022-08-03 17:58:27 +02:00
|
|
|
|
logging.debug('Send {} back'.format(raw_data))
|
|
|
|
|
Connection.send(answer)
|
|
|
|
|
Connection.close()
|
2022-07-23 01:42:10 +02:00
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
logging.info('Keyboard interrupt received')
|
2022-08-03 17:58:27 +02:00
|
|
|
|
if MySocket:
|
|
|
|
|
MySocket.close()
|
|
|
|
|
logging.debug('TCP socket closed')
|
2022-07-30 12:09:38 +02:00
|
|
|
|
sys.exit(255)
|
2022-07-23 01:42:10 +02:00
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error('{}'.format(e))
|
2022-07-30 10:05:32 +02:00
|
|
|
|
if MySocket:
|
|
|
|
|
MySocket.close()
|
|
|
|
|
logging.debug('TCP socket closed')
|
2022-08-03 17:58:27 +02:00
|
|
|
|
sys.exit(254)
|
2019-07-27 16:51:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|