mirror of https://github.com/D4-project/d4-core
chg: [server] test with multiple clients + cleaning
todo: - timeout buffers - handle multiple d4 on the same client (with tcp port number)pull/23/head
parent
e19e301038
commit
1ab9deb777
146
server/server.py
146
server/server.py
|
@ -34,8 +34,7 @@ class Echo(Protocol):
|
||||||
|
|
||||||
def dataReceived(self, data):
|
def dataReceived(self, data):
|
||||||
self.process_header(data)
|
self.process_header(data)
|
||||||
print(self.transport.client)
|
#print(self.transport.client)
|
||||||
#print(data[72:])
|
|
||||||
|
|
||||||
#def timeoutConnection(self):
|
#def timeoutConnection(self):
|
||||||
# self.transport.abortConnection()
|
# self.transport.abortConnection()
|
||||||
|
@ -52,9 +51,16 @@ class Echo(Protocol):
|
||||||
|
|
||||||
return data_header
|
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
|
# # TODO: check timestamp
|
||||||
def is_valid_header(self, uuid):
|
def is_valid_header(self, uuid):
|
||||||
if is_valid_uuid_v4(uuid):
|
if self.is_valid_uuid_v4(uuid):
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
@ -130,138 +136,26 @@ class Echo(Protocol):
|
||||||
data_header['hmac_header'] = data_header['hmac_header'].hex()
|
data_header['hmac_header'] = data_header['hmac_header'].hex()
|
||||||
|
|
||||||
### Debug ###
|
### Debug ###
|
||||||
print('hexdigest: {}'.format( HMAC.hexdigest() ))
|
#print('hexdigest: {}'.format( HMAC.hexdigest() ))
|
||||||
print('version: {}'.format( data_header['version'] ))
|
#print('version: {}'.format( data_header['version'] ))
|
||||||
print('type: {}'.format( data_header['type'] ))
|
#print('type: {}'.format( data_header['type'] ))
|
||||||
print('uuid: {}'.format(data_header['uuid_header']))
|
#print('uuid: {}'.format(data_header['uuid_header']))
|
||||||
print('timestamp: {}'.format( data_header['timestamp'] ))
|
#print('timestamp: {}'.format( data_header['timestamp'] ))
|
||||||
print('hmac: {}'.format( data_header['hmac_header'] ))
|
#print('hmac: {}'.format( data_header['hmac_header'] ))
|
||||||
print('size: {}'.format( data_header['size'] ))
|
#print('size: {}'.format( data_header['size'] ))
|
||||||
#print(d4_header)
|
#print(d4_header)
|
||||||
### ###
|
### ###
|
||||||
|
|
||||||
if data_header['hmac_header'] == HMAC.hexdigest():
|
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:
|
else:
|
||||||
print('hmac do not match')
|
print('hmac do not match')
|
||||||
print(data)
|
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):
|
def main(reactor):
|
||||||
log.startLogging(sys.stdout)
|
log.startLogging(sys.stdout)
|
||||||
|
|
Loading…
Reference in New Issue