2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-03 23:15:33 +00:00
This commit is contained in:
quadrismegistus 2020-08-22 09:07:46 +01:00
parent 670115a795
commit eba12f7f41
2 changed files with 22 additions and 50 deletions

View File

@ -210,7 +210,7 @@ class Api(object):
authentic = verify_signature(signature, value_bytes, public_sender_key)
if not authentic:
self.log('message is inauthentic for set??',authentic)
raise Exception('message is inauthentic for set??' +str(authentic))
return None
# encrypt?
@ -229,7 +229,9 @@ class Api(object):
payload = sep2.join(WDV)
res = aes_rsa_encrypt(payload,encrypt_for_pubkey)
if res is None: return None
if res is None:
raise Exception('encryption result does not exist')
return None
payload_encr_aes, payload_encr_aes_key, payload_encr_aes_iv = res
decryption_tools = sep2.join([
@ -373,6 +375,9 @@ class Api(object):
res = await node.set(key,proc(key,value))
#node.stop()
self.log('reconnecting ...',self._node)
#await self._node.stop()
#await self.connect()
return res
return await _set()
@ -398,7 +403,6 @@ class Api(object):
self.log(f'jsonize_res({res0} [{type(res0)}] --> {res} [{type(res)}')
return res
# if key_or_keys.startsiwth('/post/'):
res = await self.get(key_or_keys,decode_data=decode_data)
self.log('get_json() got from get():',res)
#self.log('get_json() got',res)
@ -451,9 +455,11 @@ class Api(object):
# if not (name and passkey): return {'error':'Name and password needed'}
person = await self.get_person(name)
if person is not None:
self.log('register() person <-',person)
# try to log in
self.log('my keys',self.keys.keys())
if not name in self.keys:
self.log('!! person already exists')
return {'error':'Person already exists'}
# test 3 conditions
@ -462,6 +468,7 @@ class Api(object):
if simple_lock_test(privkey,pubkey):
self.username=name
self.log('!! logging into',name)
return {'success':'Logging back in...'}
private_key = generate_rsa_key()
@ -491,40 +498,6 @@ class Api(object):
self.log('!!',e)
return {'error':'Incorrect password'}
## LOGIN
async def login(self,name,passkey):
# verify input
if not (name and passkey):
return {'error':'Name and password required'}
# try to load private key
private_key_dat = self.load_private_key(passkey)
if 'error' in private_key_dat:
return {'error':private_key_dat['error']}
if not 'success' in private_key_dat:
return {'error':'Incorrect password?'}
self._private_key = private_key = private_key_dat['success']
# see if user exists
person = await self.get_person(name)
# self.log(person)
if person is None:
return {'error':'Login failed'}
# verify keys
# self.log('got person =',person)
person_public_key_pem = person['public_key']
public_key = load_pubkey(person_public_key_pem) #load_public_key(person_public_key_pem.encode())
self._public_key = real_public_key = private_key.public_key()
#log('PUBLIC',public_key.public_numbers())
#log('REAL PUBLIC',real_public_key.public_numbers())
if public_key.public_numbers() != real_public_key.public_numbers():
return {'error':'Keys do not match!'}
return {'success':'Login successful', 'username':name}
#@property
def get_keys(self):
@ -626,14 +599,6 @@ class Api(object):
file_store['parts_data']=pieces
return file_store
#def get_current_event_id(self):
# return self.get_json_val(self,'/current/event/id')
# def get_uri(self):
# event_id = self.get_current_event_id()
# event_id=1 if event_id is None else int(event_id)
# return f'/post/{event_id}'
async def flush(self):
self.log('saving back to db file...')
node = await self.node

View File

@ -182,8 +182,8 @@ class Server:
nearest = self.protocol.router.find_neighbors(node)
self.log(f'nearest = {nearest}')
if not nearest:
self.log("There are no known neighbors to get key %s" % key)
return None
raise Exception("There are no known neighbors to get key %s" % key)
spider = ValueSpiderCrawl(self.protocol, node, nearest,
@ -192,10 +192,14 @@ class Server:
found = await spider.find()
self.log(f"Eventually found for key {key} value {found}")
if not found:
return None
raise Exception('nothing found!')
# set it locally? @EDIT
if store_anywhere and found:
self.storage.set(dkey,found)
self.log(f'storing anywhere: {dkey} -> {found}')
self.storage[dkey]=found
return found
@ -219,8 +223,10 @@ class Server:
"""
node = Node(dkey)
self.log('set_digest()',node)
nearest = self.protocol.router.find_neighbors(node)
self.log('set_digest() nearest -->',nearest)
if not nearest:
self.log.warning("There are no known neighbors to set key %s",
dkey.hex())
@ -234,7 +240,7 @@ class Server:
# if this node is close too, then store here as well
if store_anywhere:
self.log(f'store_anywhere -> {dkey} --> {value}')
self.storage.set(dkey,value)
self.storage[dkey]=value
else:
biggest = max([n.distance_to(node) for n in nodes])
if self.node.distance_to(node) < biggest:
@ -243,9 +249,10 @@ class Server:
results = [self.protocol.call_store(n, dkey, value) for n in nodes]
results = await asyncio.gather(*results)
self.log(f'--> set() results --> {results}')
# return true only if at least one store call succeeded
return any(await asyncio.gather(*results))
return any(results)
def save_state(self, fname):
"""