client auf configparser umgestellt
This commit is contained in:
parent
08af0365b8
commit
be3f9ecb64
2 changed files with 106 additions and 29 deletions
20
setstatus.conf
Normal file
20
setstatus.conf
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
# file: setstatus.conf
|
||||||
|
|
||||||
|
# Setstatus.conf is part of doorstatus - a programm to change the krautspaces
|
||||||
|
# doorstatus. This is the configuration file for the client who triggers the
|
||||||
|
# change.
|
||||||
|
|
||||||
|
[general]
|
||||||
|
timeout = 3.0
|
||||||
|
loglevel = info
|
||||||
|
|
||||||
|
[server]
|
||||||
|
host = nr18.space
|
||||||
|
port = 10001
|
||||||
|
cert = ./certs/server.crt
|
||||||
|
fqdn = server.status.kraut.space
|
||||||
|
|
||||||
|
[client]
|
||||||
|
cert = ./certs/client.crt
|
||||||
|
key = ./certs/client.key
|
||||||
|
|
115
setstatus.py
115
setstatus.py
|
@ -5,12 +5,17 @@
|
||||||
# date: 26.07.2019
|
# date: 26.07.2019
|
||||||
# email: berhsi@web.de
|
# email: berhsi@web.de
|
||||||
|
|
||||||
|
# Setstatus.py is part of doorstatus - a programm to deal with the
|
||||||
|
# krautspaces doorstatus.
|
||||||
|
|
||||||
# client, who connects to the statusserver at port 10001 to update the
|
# 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
|
# krautspace door status. If no status is given as argument, he reads from
|
||||||
# stdin until input is 0 or 1.
|
# stdin until input is 0 or 1.
|
||||||
|
|
||||||
import socket
|
|
||||||
import ssl
|
import ssl
|
||||||
|
import socket
|
||||||
|
import logging
|
||||||
|
import configparser
|
||||||
from sys import exit, argv
|
from sys import exit, argv
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,7 +32,7 @@ def check_arguments(argv):
|
||||||
else:
|
else:
|
||||||
if argv[1].strip() == '0' or argv[1].strip() == '1':
|
if argv[1].strip() == '0' or argv[1].strip() == '1':
|
||||||
i = int(argv[1].strip())
|
i = int(argv[1].strip())
|
||||||
print('Set value to {}'.format(i))
|
logging.debug('Set value to {}'.format(i))
|
||||||
byte_value = bytes([i])
|
byte_value = bytes([i])
|
||||||
else:
|
else:
|
||||||
byte_value = None
|
byte_value = None
|
||||||
|
@ -46,65 +51,117 @@ def read_argument():
|
||||||
buf = input('Enter new status (0/1): ')
|
buf = input('Enter new status (0/1): ')
|
||||||
if buf == '0' or buf == '1':
|
if buf == '0' or buf == '1':
|
||||||
status = bytes([int(buf)])
|
status = bytes([int(buf)])
|
||||||
print('Read status: {}'.format(status))
|
logging.debug('Read status: {}'.format(status))
|
||||||
return status
|
return status
|
||||||
|
|
||||||
|
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]))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
HOST = 'localhost'
|
|
||||||
PORT = 10001
|
|
||||||
SERVER_NAME = 'server.status.kraut.space'
|
|
||||||
CLIENT_CERT = './certs/client.crt'
|
|
||||||
CLIENT_KEY = './certs/client.key'
|
|
||||||
SERVER_CERT = './certs/server.crt'
|
|
||||||
STATUS = None
|
STATUS = None
|
||||||
RESPONSE = None
|
RESPONSE = None
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
STATUS = check_arguments(argv)
|
STATUS = check_arguments(argv)
|
||||||
while STATUS is None:
|
while STATUS is None:
|
||||||
STATUS = read_argument()
|
STATUS = read_argument()
|
||||||
|
|
||||||
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH,
|
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH,
|
||||||
cafile=SERVER_CERT)
|
cafile=config['server']['cert'])
|
||||||
context.set_ciphers('EECDH+AESGCM') # only ciphers for tls 1.2 and 1.3
|
# use only cyphers for tls version 1.2 and 1.3
|
||||||
|
context.set_ciphers('EECDH+AESGCM')
|
||||||
context.options |= getattr(ssl._ssl, 'OP_NO_COMPRESSION', 0)
|
context.options |= getattr(ssl._ssl, 'OP_NO_COMPRESSION', 0)
|
||||||
context.load_cert_chain(certfile=CLIENT_CERT, keyfile=CLIENT_KEY)
|
context.load_cert_chain(certfile=config['client']['cert'],
|
||||||
print('SSL context created')
|
keyfile=config['client']['key'])
|
||||||
|
logging.debug('SSL context created')
|
||||||
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
||||||
print('Socket created')
|
logging.debug('Socket created')
|
||||||
try:
|
try:
|
||||||
conn = context.wrap_socket(mySocket, server_side=False,
|
conn = context.wrap_socket(mySocket, server_side=False,
|
||||||
server_hostname=SERVER_NAME)
|
server_hostname=config['server']['fqdn'])
|
||||||
print('Connection wrapped with ssl.context')
|
logging.debug('Connection wrapped with ssl.context')
|
||||||
conn.settimeout(5.0)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Context wrapper failed: [}'.format(e))
|
logging.error('Context wrapper failed: {}'.format(e))
|
||||||
try:
|
try:
|
||||||
conn.connect((HOST, PORT))
|
conn.settimeout(float(config['general']['timeout']))
|
||||||
print('Connection established: {}'.format(conn.getpeercert()))
|
except Exception as e:
|
||||||
|
logging.debug('Failed to set timeout: {}'.format(e))
|
||||||
|
try:
|
||||||
|
conn.connect((config['server']['host'], int(config['server']['port'])))
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
print('Connection timeout')
|
logging.eror('Connection timeout')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Connection failed: {}'.format(e))
|
logging.error('Connection failed: {}'.format(e))
|
||||||
exit(1)
|
exit(1)
|
||||||
|
logging.debug('Peer certificate commonName: {}'.format(
|
||||||
|
conn.getpeercert()['subject'][5][0][1]))
|
||||||
|
logging.debug('Peer certificate serialNumber: {}'.format(
|
||||||
|
conn.getpeercert()['serialNumber']))
|
||||||
try:
|
try:
|
||||||
print('Send new status: {}'.format(STATUS))
|
logging.debug('Send new status: {}'.format(STATUS))
|
||||||
conn.send(STATUS)
|
conn.send(STATUS)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Error: {}'.format(e))
|
logging.error('Error: {}'.format(e))
|
||||||
exit(2)
|
exit(2)
|
||||||
try:
|
try:
|
||||||
RESPONSE = conn.recv(1)
|
RESPONSE = conn.recv(1)
|
||||||
print('Server returns: {}'.format(RESPONSE))
|
logging.debug('Server returns: {}'.format(RESPONSE))
|
||||||
if RESPONSE == STATUS:
|
if RESPONSE == STATUS:
|
||||||
print('Status sucessfull updated')
|
logging.info('Status sucessfull updated')
|
||||||
else:
|
else:
|
||||||
print('Failed to update status')
|
logging.error('Failed to update status')
|
||||||
print('Disconnect from server')
|
logging.debug('Disconnect from server')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Error: {}'.format(e))
|
logging.error('Error: {}'.format(e))
|
||||||
exit(3)
|
exit(3)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue