2019-07-26 21:33:40 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-09-14 14:01:52 +02:00
|
|
|
# file: setstatus.py
|
2019-07-26 21:33:40 +02:00
|
|
|
# date: 26.07.2019
|
|
|
|
# email: berhsi@web.de
|
|
|
|
|
2020-07-09 20:12:49 +02:00
|
|
|
# Setstatus.py is part of doorstatus - a programm to deal with the
|
|
|
|
# krautspaces doorstatus.
|
|
|
|
|
2019-09-19 09:44:52 +02:00
|
|
|
# client, who connects to the statusserver at port 10001 to update the
|
|
|
|
# krautspace door status. If no status is given as argument, he reads from
|
|
|
|
# stdin until input is 0 or 1.
|
2019-07-26 21:33:40 +02:00
|
|
|
|
2019-07-29 18:27:53 +02:00
|
|
|
import ssl
|
2020-07-09 20:12:49 +02:00
|
|
|
import socket
|
|
|
|
import logging
|
|
|
|
import configparser
|
2019-09-19 09:44:52 +02:00
|
|
|
from sys import exit, argv
|
2019-07-26 21:33:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_arguments(argv):
|
2019-07-27 00:44:53 +02:00
|
|
|
'''
|
|
|
|
Checks length and validity of command line argument vectors. If there is
|
|
|
|
no argument or argument is not valid, it returns None. Otherwise it
|
|
|
|
converts the string value into a byte value.
|
|
|
|
param 1: array of strings
|
|
|
|
return: None or byte value
|
|
|
|
'''
|
2019-07-26 21:33:40 +02:00
|
|
|
if len(argv) == 1:
|
2019-07-27 00:44:53 +02:00
|
|
|
byte_value = None
|
2019-07-26 21:33:40 +02:00
|
|
|
else:
|
|
|
|
if argv[1].strip() == '0' or argv[1].strip() == '1':
|
2019-07-27 00:44:53 +02:00
|
|
|
i = int(argv[1].strip())
|
2020-07-09 20:12:49 +02:00
|
|
|
logging.debug('Set value to {}'.format(i))
|
2019-07-27 00:44:53 +02:00
|
|
|
byte_value = bytes([i])
|
2019-07-26 21:33:40 +02:00
|
|
|
else:
|
2019-07-27 00:44:53 +02:00
|
|
|
byte_value = None
|
|
|
|
return byte_value
|
2019-07-26 21:33:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def read_argument():
|
2019-07-27 00:44:53 +02:00
|
|
|
'''
|
|
|
|
Reads from stdin until the given value is valid. Convert the given
|
|
|
|
string to a byte value and return this value.
|
|
|
|
return: byte value
|
|
|
|
'''
|
2019-07-26 21:33:40 +02:00
|
|
|
status = None
|
|
|
|
|
2019-09-19 09:44:52 +02:00
|
|
|
while status is None:
|
2019-07-26 21:33:40 +02:00
|
|
|
buf = input('Enter new status (0/1): ')
|
|
|
|
if buf == '0' or buf == '1':
|
2019-07-27 00:44:53 +02:00
|
|
|
status = bytes([int(buf)])
|
2020-07-09 20:12:49 +02:00
|
|
|
logging.debug('Read status: {}'.format(status))
|
2019-07-27 00:44:53 +02:00
|
|
|
return status
|
2019-07-26 21:33:40 +02:00
|
|
|
|
2020-07-09 20:12:49 +02:00
|
|
|
def print_config(config):
|
|
|
|
'''
|
|
|
|
Logs the config with level debug.
|
|
|
|
'''
|
|
|
|
logging.debug('Using config:')
|
|
|
|
for section in config.sections():
|
|
|
|
logging.debug('Section {}'.format(section))
|
|
|
|
for i in config[section]:
|
|
|
|
logging.debug(' {}: {}'.format(i, config[section][i]))
|
|
|
|
|
2019-07-26 21:33:40 +02:00
|
|
|
|
2019-09-19 09:44:52 +02:00
|
|
|
def main():
|
2019-07-26 21:33:40 +02:00
|
|
|
|
|
|
|
STATUS = None
|
2019-07-27 00:44:53 +02:00
|
|
|
RESPONSE = None
|
2019-07-26 21:33:40 +02:00
|
|
|
|
2020-07-09 20:12:49 +02:00
|
|
|
loglevel = logging.DEBUG
|
|
|
|
formatstring = '%(asctime)s: %(levelname)s: %(message)s'
|
|
|
|
logging.basicConfig(format=formatstring, level=loglevel)
|
|
|
|
|
|
|
|
default_config = {
|
|
|
|
'general': {
|
|
|
|
'timeout': 5.0,
|
|
|
|
'loglevel': 'warning'
|
|
|
|
},
|
|
|
|
'server': {
|
|
|
|
'host': 'localhost',
|
|
|
|
'port': 10001,
|
|
|
|
'cert': './certs/server.crt',
|
|
|
|
'fqdn': 'server.status.kraut.space'
|
|
|
|
},
|
|
|
|
'client': {
|
|
|
|
'cert': './certs/client.crt',
|
|
|
|
'key': './certs/client.key'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
configfile = './setstatus.conf'
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read_dict(default_config)
|
|
|
|
if not config.read(configfile):
|
|
|
|
logging.warning('Configuration file {} not found or not readable.'.format(
|
|
|
|
configfile))
|
|
|
|
logging.warning('Using default values.')
|
|
|
|
|
|
|
|
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'])
|
|
|
|
|
|
|
|
logger.setLevel(config['general']['loglevel'].upper())
|
|
|
|
|
|
|
|
print_config(config)
|
|
|
|
|
2019-09-19 09:44:52 +02:00
|
|
|
STATUS = check_arguments(argv)
|
|
|
|
while STATUS is None:
|
|
|
|
STATUS = read_argument()
|
2019-07-26 21:33:40 +02:00
|
|
|
|
2019-09-19 09:44:52 +02:00
|
|
|
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH,
|
2020-07-09 20:12:49 +02:00
|
|
|
cafile=config['server']['cert'])
|
|
|
|
# use only cyphers for tls version 1.2 and 1.3
|
|
|
|
context.set_ciphers('EECDH+AESGCM')
|
2019-09-19 10:36:48 +02:00
|
|
|
context.options |= getattr(ssl._ssl, 'OP_NO_COMPRESSION', 0)
|
2020-07-09 20:12:49 +02:00
|
|
|
context.load_cert_chain(certfile=config['client']['cert'],
|
|
|
|
keyfile=config['client']['key'])
|
|
|
|
logging.debug('SSL context created')
|
2019-07-29 18:27:53 +02:00
|
|
|
|
2019-07-26 21:33:40 +02:00
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
2020-07-09 20:12:49 +02:00
|
|
|
logging.debug('Socket created')
|
2019-07-27 00:44:53 +02:00
|
|
|
try:
|
2019-09-19 09:44:52 +02:00
|
|
|
conn = context.wrap_socket(mySocket, server_side=False,
|
2020-07-09 20:12:49 +02:00
|
|
|
server_hostname=config['server']['fqdn'])
|
|
|
|
logging.debug('Connection wrapped with ssl.context')
|
|
|
|
except Exception as e:
|
|
|
|
logging.error('Context wrapper failed: {}'.format(e))
|
|
|
|
try:
|
|
|
|
conn.settimeout(float(config['general']['timeout']))
|
2019-07-29 18:27:53 +02:00
|
|
|
except Exception as e:
|
2020-07-09 20:12:49 +02:00
|
|
|
logging.debug('Failed to set timeout: {}'.format(e))
|
2019-07-29 18:27:53 +02:00
|
|
|
try:
|
2020-07-09 20:12:49 +02:00
|
|
|
conn.connect((config['server']['host'], int(config['server']['port'])))
|
2019-09-14 14:01:52 +02:00
|
|
|
except socket.timeout:
|
2020-07-09 20:12:49 +02:00
|
|
|
logging.eror('Connection timeout')
|
2019-07-27 00:44:53 +02:00
|
|
|
except Exception as e:
|
2020-07-09 20:12:49 +02:00
|
|
|
logging.error('Connection failed: {}'.format(e))
|
2019-07-27 00:44:53 +02:00
|
|
|
exit(1)
|
2020-07-09 20:12:49 +02:00
|
|
|
logging.debug('Peer certificate commonName: {}'.format(
|
|
|
|
conn.getpeercert()['subject'][5][0][1]))
|
|
|
|
logging.debug('Peer certificate serialNumber: {}'.format(
|
|
|
|
conn.getpeercert()['serialNumber']))
|
2019-07-26 21:33:40 +02:00
|
|
|
try:
|
2020-07-09 20:12:49 +02:00
|
|
|
logging.debug('Send new status: {}'.format(STATUS))
|
2019-07-29 18:27:53 +02:00
|
|
|
conn.send(STATUS)
|
2019-07-26 21:33:40 +02:00
|
|
|
except Exception as e:
|
2020-07-09 20:12:49 +02:00
|
|
|
logging.error('Error: {}'.format(e))
|
2019-07-27 00:44:53 +02:00
|
|
|
exit(2)
|
2019-07-26 22:02:01 +02:00
|
|
|
try:
|
2019-07-29 18:27:53 +02:00
|
|
|
RESPONSE = conn.recv(1)
|
2020-07-09 20:12:49 +02:00
|
|
|
logging.debug('Server returns: {}'.format(RESPONSE))
|
2019-07-27 00:44:53 +02:00
|
|
|
if RESPONSE == STATUS:
|
2020-07-09 20:12:49 +02:00
|
|
|
logging.info('Status sucessfull updated')
|
2019-07-27 00:44:53 +02:00
|
|
|
else:
|
2020-07-09 20:12:49 +02:00
|
|
|
logging.error('Failed to update status')
|
|
|
|
logging.debug('Disconnect from server')
|
2019-07-27 00:44:53 +02:00
|
|
|
except Exception as e:
|
2020-07-09 20:12:49 +02:00
|
|
|
logging.error('Error: {}'.format(e))
|
2019-07-27 00:44:53 +02:00
|
|
|
exit(3)
|
2019-07-26 21:33:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|