chg: [server] test with multiple clients + cleaning

todo: - timeout buffers
      - handle multiple d4 on the same client (with tcp port number)
pull/23/head
Terrtia 2019-01-03 14:53:53 +01:00
parent e19e301038
commit 1ab9deb777
No known key found for this signature in database
GPG Key ID: 1E1B1F50D84613D0
1 changed files with 20 additions and 126 deletions

View File

@ -34,8 +34,7 @@ class Echo(Protocol):
def dataReceived(self, data):
self.process_header(data)
print(self.transport.client)
#print(data[72:])
#print(self.transport.client)
#def timeoutConnection(self):
# self.transport.abortConnection()
@ -52,9 +51,16 @@ class Echo(Protocol):
return data_header
def is_valid_uuid_v4(self, header_uuid):
try:
uuid_test = UUID(hex=header_uuid, version=4)
return uuid_test.hex == header_uuid
except:
return False
# # TODO: check timestamp
def is_valid_header(self, uuid):
if is_valid_uuid_v4(uuid):
if self.is_valid_uuid_v4(uuid):
return True
else:
return False
@ -130,138 +136,26 @@ class Echo(Protocol):
data_header['hmac_header'] = data_header['hmac_header'].hex()
### Debug ###
print('hexdigest: {}'.format( HMAC.hexdigest() ))
print('version: {}'.format( data_header['version'] ))
print('type: {}'.format( data_header['type'] ))
print('uuid: {}'.format(data_header['uuid_header']))
print('timestamp: {}'.format( data_header['timestamp'] ))
print('hmac: {}'.format( data_header['hmac_header'] ))
print('size: {}'.format( data_header['size'] ))
#print('hexdigest: {}'.format( HMAC.hexdigest() ))
#print('version: {}'.format( data_header['version'] ))
#print('type: {}'.format( data_header['type'] ))
#print('uuid: {}'.format(data_header['uuid_header']))
#print('timestamp: {}'.format( data_header['timestamp'] ))
#print('hmac: {}'.format( data_header['hmac_header'] ))
#print('size: {}'.format( data_header['size'] ))
#print(d4_header)
### ###
if data_header['hmac_header'] == HMAC.hexdigest():
print('hmac match')
#print('hmac match')
#redis_server.xadd('stream:{}'.format(data_header['type']), {'message': data[62:], 'uuid': data_header['uuid_header'], 'timestamp': data_header['timestamp'], 'version': data_header['version']})
with open(data_header['uuid_header'], 'ab') as f:
f.write(data[62:])
else:
print('hmac do not match')
print(data)
print()
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) > 63:
d4_header = data[:62]
all_data = data[62:]
#print(d4_header)
version = struct.unpack('B', d4_header[0:1])[0]
type = struct.unpack('B', d4_header[1:2])[0]
uuid_header = d4_header[2:18].hex()
timestamp = struct.unpack('Q', d4_header[18:26])[0]
hmac_header = d4_header[26:58]
size = struct.unpack('I', d4_header[58:62])[0]
print('-------------------------')
print(hmac_header)
print(hmac_reset)
print('***************************')
print(d4_header)
d4_header = d4_header.replace(hmac_header, hmac_reset)
print(d4_header)
data = d4_header + all_data
print(data)
#print(data[:62])
#print(len(data[:62]))
hmac_header = hmac_header.hex()
# verify hmac sha256
HMAC = hmac.new(hmac_key, msg=data, digestmod='sha256')
print('hexdigest: {}'.format( HMAC.hexdigest() ))
#print(data)
#if hmac_header != HMAC.hexdigest():
# print("hmac don't match ...")
# print('hexdigest: {}'.format( HMAC.hexdigest() ))
# print('hexdigest: {}'.format( hmac_header ))
### Debug ###
#print(data)
print('version: {}'.format( version ))
print('type: {}'.format( type ))
print('uuid: {}'.format(uuid_header))
print('timestamp: {}'.format( timestamp ))
print('hmac: {}'.format( hmac_header ))
print('size: {}'.format( size ))
#print(d4_header)
### ###
# check if is valid uuid v4
if not is_valid_uuid_v4(uuid_header):
print('not uuid v4')
print()
print()
sys.exit(1)
# verify timestamp
#elif :
# print('not valid timestamp')
elif size != (len(data) - 62):
print('invalid size')
print('size: {}, expected size: {}'.format(len(data) - 62, size))
print()
print()
print(data[:size])
print()
print()
print(data[size:])
sys.exit(1)
else:
# verify hmac sha256
HMAC = hmac.new(hmac_key, msg=data, digestmod='sha256')
print('hexdigest: {}'.format( HMAC.hexdigest() ))
#print(data)
if hmac_header == HMAC.hexdigest():
print('hmac match')
#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")
print()
print()
else:
print('incomplete data')
print()
print()
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
def main(reactor):
log.startLogging(sys.stdout)