d4-core/server/server.py

139 lines
4.0 KiB
Python
Raw Normal View History

2018-12-05 16:24:10 +01:00
#!/usr/bin/env python3
import os
2018-12-05 16:24:10 +01:00
import sys
import hmac
import stat
2018-12-13 16:03:05 +01:00
import redis
2018-12-05 16:24:10 +01:00
from twisted.internet import ssl, task, protocol, endpoints, defer
from twisted.python import log
from twisted.python.modules import getModule
from twisted.internet.protocol import Protocol
2018-12-05 17:05:46 +01:00
from ctypes import *
from uuid import UUID
2018-12-05 17:05:46 +01:00
hmac_reset = '0000000000000000000000000000000000000000000000000000000000000000'
hmac_key = b'private key to change\n'
2018-12-13 16:03:05 +01:00
redis_server = redis.StrictRedis(
host="localhost",
port=6379,
db=0,
decode_responses=True)
2018-12-05 16:24:10 +01:00
class Echo(Protocol):
#def __init__(self, factory):
# self.factory = factory
def dataReceived(self, data):
process_header(data)
2018-12-13 16:03:05 +01:00
print(self.transport.client)
#print(data[72:])
2018-12-05 17:05:46 +01:00
class D4Header(Structure):
_fields_ = [
("version", c_uint8),
("type", c_uint8),
("uuid1", c_uint64),
("uuid2", c_uint64),
("timestamp", c_uint64),
("hmac1", c_uint64),
("hmac2", c_uint64),
("hmac3", c_uint64),
("hmac4", c_uint64),
("size", c_uint32),
]
def process_header(data):
if len(data) > 73:
d4_header = data[:72].hex()
2018-12-13 16:03:05 +01:00
print(d4_header)
uuid_header = d4_header[4:36]
hmac_header = d4_header[64:128]
d4_header = d4_header.replace(hmac_header, hmac_reset)
temp = bytes.fromhex(d4_header)
data = data.replace(data[:72], temp)
print(data)
data_header = unpack(D4Header, data)
2018-12-13 16:03:05 +01:00
### Debug ###
print('version: {}'.format( data_header['version'] ))
print('type: {}'.format( data_header['type'] ))
print('uuid: {}'.format(uuid_header))
print('timestamp: {}'.format( data_header['timestamp'] ))
print('hmac: {}'.format( hmac_header ))
print('size: {}'.format( data_header['size'] ))
print('size: {}'.format( len(data) - data_header['struct_size']))
#print(d4_header)
### ###
# check if is valid uuid v4
if not is_valid_uuid_v4(uuid_header):
print('not uuid v4')
# verify timestamp
#elif :
# print('not valid timestamp')
elif data_header['size'] != (len(data) - data_header['struct_size']):
print('invalid size')
print('size: {}'.format(data_header['size']))
print(len(data) - data_header['struct_size']) # sizeof(d)=72
else:
# verify hmac sha256
HMAC = hmac.new(hmac_key, msg=data, digestmod='sha256')
2018-12-13 16:03:05 +01:00
print('hexdigest: {}'.format( HMAC.hexdigest() ))
print(data)
if hmac_header == HMAC.hexdigest():
print('hmac match')
2018-12-13 16:03:05 +01:00
redis_server.xadd('stream:{}'.format(data_header['type']), {'message': data[72:], 'uuid':uuid_header, 'timestamp': data_header['timestamp'], 'version': data_header['version']})
print('END')
print()
# discard data
else:
print("hmac don't match")
else:
print('incomplete data')
2018-12-05 17:05:46 +01:00
def unpack(ctype, buffer):
c_str = create_string_buffer(buffer)
d = cast(pointer(c_str), POINTER(ctype)).contents
data_header = {}
data_header['version'] = d.version
data_header['type'] = d.type
data_header['timestamp'] = d.timestamp
data_header['size'] = d.size
data_header['struct_size'] = sizeof(d)
return data_header
2018-12-05 16:24:10 +01:00
def is_valid_uuid_v4(header_uuid):
#try:
uuid_test = UUID(hex=header_uuid, version=4)
return uuid_test.hex == header_uuid
#except:
# return False
2018-12-05 16:24:10 +01:00
def main(reactor):
log.startLogging(sys.stdout)
certData = getModule(__name__).filePath.sibling('server.pem').getContent()
certificate = ssl.PrivateCertificate.loadPEM(certData)
factory = protocol.Factory.forProtocol(Echo)
reactor.listenSSL(4443, factory, certificate.options())
return defer.Deferred()
if __name__ == "__main__":
task.react(main)