2019-07-26 21:33:40 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-07-27 18:14:13 +02:00
|
|
|
# file: setstatus.py.py
|
2019-07-26 21:33:40 +02:00
|
|
|
# date: 26.07.2019
|
|
|
|
# email: berhsi@web.de
|
|
|
|
|
2019-07-27 18:14:13 +02:00
|
|
|
# client, who connects to the statusserver at port 10001 to update the krautspace
|
2019-07-27 00:44:53 +02:00
|
|
|
# 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
|
|
|
|
|
|
|
import socket
|
2019-07-29 18:27:53 +02:00
|
|
|
import ssl
|
2019-07-27 00:44:53 +02:00
|
|
|
from sys import exit, argv, byteorder
|
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())
|
|
|
|
print('Set value to {}'.format(i))
|
|
|
|
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
|
|
|
|
|
|
|
|
while status == None:
|
|
|
|
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)])
|
|
|
|
print('Read status: {}'.format(status))
|
|
|
|
return status
|
2019-07-26 21:33:40 +02:00
|
|
|
|
|
|
|
|
2019-07-27 16:49:08 +02:00
|
|
|
def main(*status):
|
2019-07-26 21:33:40 +02:00
|
|
|
|
2019-07-29 18:27:53 +02:00
|
|
|
HOST = 'localhost'
|
2019-07-27 00:44:53 +02:00
|
|
|
PORT = 10001
|
2019-07-29 18:27:53 +02:00
|
|
|
SERVER_NAME = 'server.status.kraut.space'
|
|
|
|
CLIENT_CERT = './certs/client.crt'
|
|
|
|
CLIENT_KEY = './certs/client.key'
|
|
|
|
SERVER_CERT = './certs/server.crt'
|
2019-07-27 00:44:53 +02:00
|
|
|
BOM = byteorder
|
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
|
|
|
|
2019-07-27 16:49:08 +02:00
|
|
|
if status:
|
|
|
|
STATUS = status[0]
|
|
|
|
STATUS = bytes([STATUS])
|
|
|
|
print('Status: {}'.format(STATUS))
|
2019-07-26 21:33:40 +02:00
|
|
|
else:
|
2019-07-27 16:49:08 +02:00
|
|
|
if len(argv) == 1:
|
|
|
|
STATUS = read_argument()
|
|
|
|
else:
|
|
|
|
STATUS = check_arguments(argv)
|
2019-07-27 18:14:13 +02:00
|
|
|
if STATUS == None:
|
|
|
|
STATUS = read_argument()
|
2019-07-26 21:33:40 +02:00
|
|
|
|
2019-07-29 18:27:53 +02:00
|
|
|
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile = SERVER_CERT)
|
|
|
|
context.options &= ~ssl.PROTOCOL_TLS
|
|
|
|
context.verify_mode = ssl.CERT_OPTIONAL
|
|
|
|
# context.set_ciphers('HIGHT:!aNULL:!RC4:!DSS')
|
|
|
|
context.load_cert_chain(certfile = CLIENT_CERT, keyfile = CLIENT_KEY)
|
|
|
|
print('SSL context created')
|
|
|
|
|
2019-07-26 21:33:40 +02:00
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
|
|
|
print('Socket created')
|
2019-07-27 00:44:53 +02:00
|
|
|
try:
|
2019-07-29 18:27:53 +02:00
|
|
|
conn = context.wrap_socket(mySocket, server_side = False, \
|
|
|
|
server_hostname = SERVER_NAME)
|
|
|
|
print('Connection wrapped with ssl.context')
|
|
|
|
except Exception as e:
|
|
|
|
print('Context wrapper failed: [}'.format(e))
|
|
|
|
try:
|
|
|
|
conn.connect((HOST, PORT))
|
|
|
|
print('SSL established: {}'.format(conn.getpeercert()))
|
2019-07-27 00:44:53 +02:00
|
|
|
except Exception as e:
|
2019-07-29 18:27:53 +02:00
|
|
|
print('SSL handshake failed: {}'.format(e))
|
2019-07-27 00:44:53 +02:00
|
|
|
exit(1)
|
2019-07-26 21:33:40 +02:00
|
|
|
try:
|
|
|
|
print('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:
|
|
|
|
print('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)
|
2019-07-27 00:44:53 +02:00
|
|
|
print('Server returns: {}'.format(RESPONSE))
|
|
|
|
if RESPONSE == STATUS:
|
|
|
|
print('Status sucessfull updated')
|
|
|
|
else:
|
|
|
|
print('Failed to update status')
|
2019-07-26 22:02:01 +02:00
|
|
|
print('Disconnect from server')
|
2019-07-27 00:44:53 +02:00
|
|
|
except Exception as e:
|
|
|
|
print('Error: {}'.format(e))
|
|
|
|
exit(3)
|
2019-07-26 21:33:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|