forked from Krautspace/doorstatus
main in funktionen aufgeteilt
This commit is contained in:
parent
5e7afde181
commit
26c695828f
1 changed files with 98 additions and 51 deletions
149
setstatus.py
149
setstatus.py
|
@ -17,6 +17,7 @@ import ssl
|
||||||
import socket
|
import socket
|
||||||
import logging
|
import logging
|
||||||
import configparser
|
import configparser
|
||||||
|
from time import sleep
|
||||||
from sys import exit, argv
|
from sys import exit, argv
|
||||||
|
|
||||||
|
|
||||||
|
@ -53,7 +54,7 @@ def check_arguments(argv):
|
||||||
|
|
||||||
def log_config(config):
|
def log_config(config):
|
||||||
'''
|
'''
|
||||||
Logs the config with level debug.
|
Logs the config if loglevel is debug.
|
||||||
'''
|
'''
|
||||||
logging.debug('Using config:')
|
logging.debug('Using config:')
|
||||||
for section in config.sections():
|
for section in config.sections():
|
||||||
|
@ -61,6 +62,76 @@ def log_config(config):
|
||||||
for i in config[section]:
|
for i in config[section]:
|
||||||
logging.debug(' {}: {}'.format(i, config[section][i]))
|
logging.debug(' {}: {}'.format(i, config[section][i]))
|
||||||
|
|
||||||
|
def create_ssl_context(config):
|
||||||
|
'''
|
||||||
|
'''
|
||||||
|
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH,
|
||||||
|
cafile=config['server']['cert'])
|
||||||
|
if not context:
|
||||||
|
logging.error('Failed to create SSL Context')
|
||||||
|
return False
|
||||||
|
context.set_ciphers('EECDH+AESGCM') # only ciphers for tls 1.2 and 1.3
|
||||||
|
context.options |= getattr(ssl._ssl, 'OP_NO_COMPRESSION', 0)
|
||||||
|
try:
|
||||||
|
context.load_cert_chain(certfile=config['client']['cert'],
|
||||||
|
keyfile=config['client']['key'])
|
||||||
|
except Exception as e:
|
||||||
|
logging.error('Failed to load cert chain')
|
||||||
|
return False;
|
||||||
|
logging.debug('SSL context created')
|
||||||
|
return context
|
||||||
|
|
||||||
|
def create_ssl_socket(config, context):
|
||||||
|
'''
|
||||||
|
'''
|
||||||
|
bare_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
|
||||||
|
if not bare_socket:
|
||||||
|
logging.error('Socket creation failed')
|
||||||
|
return False
|
||||||
|
logging.debug('Socket created')
|
||||||
|
try:
|
||||||
|
secure_socket = context.wrap_socket(bare_socket, server_side=False,
|
||||||
|
server_hostname=config['server']['fqdn'])
|
||||||
|
logging.debug('Socket wrapped with SSL')
|
||||||
|
except Exception as e:
|
||||||
|
logging.error('Context wrapper failed: {}'.format(e))
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
secure_socket.settimeout(float(config['general']['timeout']))
|
||||||
|
except Exception as e:
|
||||||
|
logging.debug('Failed to set timeout: {}'.format(e))
|
||||||
|
return secure_socket
|
||||||
|
|
||||||
|
def create_ssl_connection(config, context):
|
||||||
|
'''
|
||||||
|
'''
|
||||||
|
counter = 0
|
||||||
|
conn = False
|
||||||
|
|
||||||
|
while conn is False and counter < 5:
|
||||||
|
ssl_socket = create_ssl_socket(config, context)
|
||||||
|
if ssl_socket is False:
|
||||||
|
exit(4)
|
||||||
|
try:
|
||||||
|
logging.debug('Connect {}: {}'.format(config['server']['host'],
|
||||||
|
int(config['server']['port'])))
|
||||||
|
conn = ssl_socket.connect((config['server']['host'],
|
||||||
|
int(config['server']['port'])))
|
||||||
|
except socket.timeout:
|
||||||
|
logging.error('Connection timeout')
|
||||||
|
ssl_socket.close()
|
||||||
|
sleep(5)
|
||||||
|
counter += 1
|
||||||
|
except Exception as e:
|
||||||
|
logging.error('Connection failed: {}'.format(e))
|
||||||
|
exit(5)
|
||||||
|
logging.debug('Conection established')
|
||||||
|
logging.debug('Peer certificate commonName: {}'.format(
|
||||||
|
ssl_socket.getpeercert()['subject'][5][0][1]))
|
||||||
|
logging.debug('Peer certificate serialNumber: {}'.format(
|
||||||
|
ssl_socket.getpeercert()['serialNumber']))
|
||||||
|
return ssl_socket
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
|
@ -115,64 +186,40 @@ def main():
|
||||||
config['client']['cert'],
|
config['client']['cert'],
|
||||||
config['client']['key'])
|
config['client']['key'])
|
||||||
if check_certs(certs) is False:
|
if check_certs(certs) is False:
|
||||||
sys.exit(1)
|
exit(1)
|
||||||
|
|
||||||
# check cli arguments
|
# check cli arguments
|
||||||
STATUS = check_arguments(argv)
|
STATUS = check_arguments(argv)
|
||||||
if STATUS is None:
|
if STATUS is None:
|
||||||
logging.error('No valid status given')
|
logging.error('No valid status given')
|
||||||
sys.exit(2)
|
exit(2)
|
||||||
|
|
||||||
# create ssl context
|
# create ssl context
|
||||||
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH,
|
context = create_ssl_context(config)
|
||||||
cafile=config['server']['cert'])
|
if context is False:
|
||||||
context.set_ciphers('EECDH+AESGCM') # only ciphers for tls 1.2 and 1.3
|
exit(3)
|
||||||
context.options |= getattr(ssl._ssl, 'OP_NO_COMPRESSION', 0)
|
|
||||||
context.load_cert_chain(certfile=config['client']['cert'],
|
|
||||||
keyfile=config['client']['key'])
|
|
||||||
logging.debug('SSL context created')
|
|
||||||
|
|
||||||
# create socket and send status bit
|
# get connection
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
conn = create_ssl_connection(config, context)
|
||||||
logging.debug('Socket created')
|
|
||||||
try:
|
# send status
|
||||||
conn = context.wrap_socket(mySocket, server_side=False,
|
try:
|
||||||
server_hostname=config['server']['fqdn'])
|
logging.debug('Send new status: {}'.format(STATUS))
|
||||||
logging.debug('Connection wrapped with ssl.context')
|
conn.send(STATUS)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error('Context wrapper failed: {}'.format(e))
|
logging.error('Error: {}'.format(e))
|
||||||
try:
|
exit(6)
|
||||||
conn.settimeout(float(config['general']['timeout']))
|
try:
|
||||||
except Exception as e:
|
RESPONSE = conn.recv(1)
|
||||||
logging.debug('Failed to set timeout: {}'.format(e))
|
logging.debug('Server returns: {}'.format(RESPONSE))
|
||||||
try:
|
if RESPONSE == STATUS:
|
||||||
conn.connect((config['server']['host'], int(config['server']['port'])))
|
logging.info('Status sucessfull updated')
|
||||||
except socket.timeout:
|
else:
|
||||||
logging.error('Connection timeout')
|
logging.error('Failed to update status')
|
||||||
except Exception as e:
|
logging.debug('Disconnect from server')
|
||||||
logging.error('Connection failed: {}'.format(e))
|
except Exception as e:
|
||||||
exit(3)
|
logging.error('Error: {}'.format(e))
|
||||||
logging.debug('Peer certificate commonName: {}'.format(
|
exit(7)
|
||||||
conn.getpeercert()['subject'][5][0][1]))
|
|
||||||
logging.debug('Peer certificate serialNumber: {}'.format(
|
|
||||||
conn.getpeercert()['serialNumber']))
|
|
||||||
try:
|
|
||||||
logging.debug('Send new status: {}'.format(STATUS))
|
|
||||||
conn.send(STATUS)
|
|
||||||
except Exception as e:
|
|
||||||
logging.error('Error: {}'.format(e))
|
|
||||||
exit(4)
|
|
||||||
try:
|
|
||||||
RESPONSE = conn.recv(1)
|
|
||||||
logging.debug('Server returns: {}'.format(RESPONSE))
|
|
||||||
if RESPONSE == STATUS:
|
|
||||||
logging.info('Status sucessfull updated')
|
|
||||||
else:
|
|
||||||
logging.error('Failed to update status')
|
|
||||||
logging.debug('Disconnect from server')
|
|
||||||
except Exception as e:
|
|
||||||
logging.error('Error: {}'.format(e))
|
|
||||||
exit(5)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue