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-09-14 07:02:17 +01:00
parent 8961d0401a
commit 1e919c9627
6 changed files with 70 additions and 173 deletions

View File

@ -373,6 +373,7 @@ class Keymaker(Logger):
def load_qr(self,name):
if not name: return
# try to load?
contact_fnfn = os.path.join(PATH_QRCODES,name+'.png')
if not os.path.exists(contact_fnfn): return ''

View File

@ -3,11 +3,50 @@ from komrade import *
from komrade.backend import *
from komrade.backend.keymaker import *
class Komrade(Caller):
# BEGIN PHONE BOOK (in memory singleton mapping)
PHONEBOOK = {}
# Factory constructor
def Komrade(name,pubkey=None,*x,**y):
from komrade.backend.the_operator import TheOperator
from komrade.backend.the_telephone import TheTelephone
from komrade.backend.komrades import KomradeX
global PHONEBOOK
# already have?
if not name and not pubkey: return KomradeX()
if name in PHONEBOOK: return PHONEBOOK[name]
pk64 = None if not pubkey else b64enc(pubkey)
if pk64 in PHONEBOOK: return PHONEBOOK[pk64]
print(f'finding Komrade {name} / {pubkey} for the first time!')
# operator?
if name==OPERATOR_NAME:
kommie = TheOperator() #(*x,**y)
if name==TELEPHONE_NAME:
kommie = TheTelephone() #(*x,**y)
else:
kommie = KomradeX(name,*x,**y)
# print('found!',name,PHONEBOOK[name],PHONEBOOK[name].keychain())
PHONEBOOK[name] = kommie
if kommie.pubkey:
PHONEBOOK[kommie.pubkey.data_b64] = kommie
return kommie
class KomradeX(Caller):
def __init__(self, name=None, passphrase=DEBUG_DEFAULT_PASSPHRASE):
super().__init__(name=name,passphrase=passphrase)
self.log(f'booted komrade with {name} and {passphrase} and\n\n{dict_format(self.keychain())}')
# self.log(f'booted komrade with {name} and {passphrase} and\n\n{dict_format(self.keychain())}')
# if SHOW_STATUS:
# from komrade.cli import CLI
# self.cli = CLI(name=name, komrade=self)
@ -65,6 +104,8 @@ class Komrade(Caller):
# if keys.get('pubkey') and keys.get('privkey')
def register(self, name = None, passphrase = None, is_group=None, show_intro=0,show_body=True):
# global PHONEBOOK
# print('got name:',name)
## Defaults
if name and not self.name: self.name=name
@ -141,6 +182,9 @@ class Komrade(Caller):
self.crypt_keys.set(pubkey.data_b64, name, prefix='/name/')
self.crypt_keys.set(pubkey.data_b64, privkey_encr_obj.data, prefix='/privkey_encr/')
# storing myself in memory phonebook
# PHONEBOOK[name]=self
## 7) Save data to server
data = {
'name':name,
@ -289,7 +333,7 @@ def test_register():
botname=f'marx{str(num).zfill(3)}'
marxbot = Komrade(botname)
# marxbot=Komrade()
marxbot.register()
marxbot.register(passphrase='boo')

View File

@ -15,41 +15,19 @@ def is_valid_msg_d(msg_d):
class Message(Logger):
def __init__(self,msg_d,from_whom=None,to_whom=None,messenger=None,embedded_msg=None,is_encrypted=False):
def __init__(self,msg_d,from_whom=None,to_whom=None,embedded_msg=None,is_encrypted=False):
# check input
if not is_valid_msg_d(msg_d):
raise KomradeException('This is not a valid msg_d:',msg_d)
# set fields
self.msg_d=msg_d
self.to_name=msg_d.get('to_name')
self.to_pubkey=msg_d.get('to')
self.from_name=msg_d.get('from_name')
self.from_pubkey=msg_d.get('from')
self.msg=msg_d.get('msg')
self.embedded_msg=embedded_msg # only if this message has an embedded one
self._route=msg_d.get(ROUTE_KEYNAME)
self._from_whom=from_whom
self._to_whom=to_whom
self.messenger=None
self._is_encrypted=None
# get operators straight away?
if not self._from_whom or not self._to_whom:
self.get_whoms()
if not self.from_name:
self.from_name=self.from_whom.name
if not self.to_name:
self.to_name=self.to_whom.name
if not self.to_pubkey:
self.to_pubkey=self.to_whom.pubkey.data
if not self.from_pubkey:
self.from_pubkey=self.from_whom.pubkey.data
if isBase64(self.to_pubkey): self.to_pubkey = b64decode(self.to_pubkey)
if isBase64(self.from_pubkey): self.from_pubkey = b64decode(self.from_pubkey)
if hasattr(self.to_pubkey,'data'): self.to_pubkey=self.to_pubkey.data
if hasattr(self.from_pubkey,'data'): self.from_pubkey=self.from_pubkey.data
# self.log('loaded message: to pub',self.to_pubkey,'from pub',self.from_pubkey)
def __repr__(self):
# self.log('my type??',type(self.msg),self.msg)
@ -87,18 +65,6 @@ class Message(Logger):
del md[ROUTE_KEYNAME]
return md
# def mark_return_to_sender1(self,new_msg=None):
# self.log('making return to sender. v1:',self)
# self._from_whom,self._to_whom = self._to_whom,self._from_whom
# self.msg_d['from'],self.msg_d['to'] = self.msg_d['to'],self.msg_d['from'],
# if 'from_name' in self.msg_d and 'to_name' in self.msg_d:
# self.msg_d['from_name'],self.msg_d['to_name'] = self.msg_d['to_name'],self.msg_d['from_name']
# if new_msg:
# self.msg=self.msg_d['msg']=new_msg
# self.log('making return to sender. v2:',self)
def return_to_sender(self,new_msg=None):
@ -111,71 +77,22 @@ class Message(Logger):
'from_name':self.msg_d.get('to_name'),
'to_name':self.msg_d.get('from_name'),
'msg':new_msg if new_msg else self.msg_d.get('msg')
},
from_whom = self.to_whom,
to_whom = self.from_whom
}
)
self.log('returning:',new_msg)
return new_msg
def get_whom(self,name=None,pubkey=None):
from komrade.backend.operators import locate_an_operator
return locate_an_operator(name=None,pubkey=None)
@property
def from_whom(self):
if not self._from_whom:
self._from_whom,self._to_whom = self.get_whoms()
return self._from_whom
from komrade.backend.komrades import Komrade
return Komrade(self.from_name, pubkey=self.from_pubkey)
@property
def to_whom(self):
if not self._to_whom:
self._from_whom,self._to_whom = self.get_whoms()
return self._to_whom
from komrade.backend.komrades import Komrade
return Komrade(self.to_name, pubkey=self.to_pubkey)
def get_whoms(self):
if self._from_whom is None or self._to_whom is None:
self._from_whom = locate_an_operator(self.from_name,self.from_pubkey)
self._to_whom = locate_an_operator(self.to_name,self.to_pubkey)
return self._from_whom,self._to_whom
## loading messages
def get_whoms1(self):
if self._from_whom is not None and self._to_whom is not None:
return (self._from_whom,self._to_whom)
alleged_from_whom = self.get_whom(self.from_name)
alleged_to_whom = self.get_whom(self.to_name)
if not self.whom_records_match(alleged_from_whom,alleged_to_whom):
raise KomradeException('Records of from_whoms on The Operator and the from_whom do not match. Something fishy going on?')
else:
self._from_whom = alleged_from_whom
self._to_whom = alleged_to_whom
return (self._from_whom,self._to_whom)
def whom_records_match(self,alleged_from_whom,alleged_to_whom):
alleged_from_whom_name = self.from_name
alleged_from_whom_pubkey = self.from_pubkey
alleged_to_whom_name = self.to_name
alleged_to_whom_pubkey = self.to_pubkey
# self.log('from_whom names:',alleged_from_whom.name, alleged_from_whom_name)
# self.log('from_whom pubs:',alleged_from_whom.pubkey, alleged_from_whom_pubkey)
# self.log('to_whom names:',alleged_to_whom.name, alleged_to_whom_name)
# self.log('to_whom pubs:',alleged_to_whom.pubkey, alleged_to_whom_pubkey)
if alleged_to_whom.name != alleged_to_whom_name:
return False
if alleged_from_whom.name != alleged_from_whom_name:
return False
if alleged_to_whom.pubkey != alleged_to_whom_pubkey:
return False
if alleged_from_whom.pubkey != alleged_from_whom_pubkey:
return False
return True
def decrypt(self,recursive=False):
# check if needs decryption
@ -186,9 +103,9 @@ class Message(Logger):
self.log(f'Attempting to decrypt:\n{self}')
# decrypt msg
# self.log('attempting to decrypt',self.msg,'from',self.from_pubkey,'to',self.to_whom,self.to_whom.keychain(),self.to_whom.assemble(self.to_whom.keychain()))
self.log('attempting to decrypt',self.msg,'from',self.from_pubkey,'to',self.to_pubkey, self.to_whom,dict_format(self.to_whom.keychain()),self.to_whom.assemble(self.to_whom.keychain()))
if not self.to_whom.privkey:
self.log(f'{self.to_whom} cannot decrypt this message! {dict_format(self.to_whom.keychain())}!')
self.log(f'{self.to_whom} cannot decrypt this message! {dict_format(self.to_whom.keychain())}!\n\n{self.to_whom.name} {self.to_whom.pubkey} {self.to_name} {self.to_pubkey} {Komrade(self.to_name).keychain()}')
return
self.msg = self.msg_d['msg'] = decr_msg_b = SMessage(

View File

@ -1,59 +1,17 @@
# internal imports
import os,sys; sys.path.append(os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')),'..')))
from komrade import *
# from komrade.backend.crypt import *
# from komrade.backend.keymaker import *
# from komrade.backend.mazes import *
# from komrade.backend.switchboard import *
from komrade.backend import *
def locate_an_operator_somehow(str_or_byte_or_obj):
if issubclass(type(str_or_byte_or_obj),Operator): return str_or_byte_or_obj
if type(str_or_byte_or_obj)==str: return locate_an_operator(name=str_or_byte_or_obj)
if type(str_or_byte_or_obj)==bytes: return locate_an_operator(pubkey=str_or_byte_or_obj)
raise KomradeException(type(str_or_byte_or_obj),'???')
def comlink(name_or_pubkey):
return locate_an_operator_somehow(name_or_pubkey)
PHONEBOOK = {}
def locate_an_operator(name=None,pubkey=None):
global OPERATOR,TELEPHONE,PHONEBOOK
if name in PHONEBOOK: return PHONEBOOK[name]
# if pubkey in PHONEBOOK: return PHONEBOOK[pubkey]
from komrade.backend.the_operator import TheOperator
from komrade.backend.the_telephone import TheTelephone
from komrade.backend.callers import Caller
if not OPERATOR: OPERATOR = TheOperator()
if not TELEPHONE: TELEPHONE = TheTelephone()
if pubkey:
assert type(pubkey)==bytes
if not isBase64(pubkey): pubkey=b64encode(pubkey)
if name == OPERATOR_NAME:
return OPERATOR
if pubkey and pubkey == OPERATOR.pubkey:
return OPERATOR
if name==TELEPHONE_NAME:
return TELEPHONE
if pubkey and pubkey == TELEPHONE.pubkey:
return TELEPHONE
print('name????',name)
from komrade.backend.komrades import Komrade
PHONEBOOK[name] = caller = Komrade(name=name)
return caller
from komrade.constants import OPERATOR_ROUTES
class Operator(Keymaker):
ROUTES = OPERATOR_ROUTES
def __init__(self, name=None, passphrase=DEBUG_DEFAULT_PASSPHRASE, pubkey=None, keychain = {}, path_crypt_keys=PATH_CRYPT_CA_KEYS, path_crypt_data=PATH_CRYPT_CA_DATA):
# print('booting opertor with ...',name,pubkey,'??')
@ -69,25 +27,7 @@ class Operator(Keymaker):
super().__init__(name=name,passphrase=passphrase, keychain=keychain,
path_crypt_keys=path_crypt_keys, path_crypt_data=path_crypt_data)
# self.find_pubkey_and_name(name,pubkey)
# self.log('booted with operator with:',self.name,self.pubkey,self.find_pubkey(name),'??')
# def boot(self):
# ## get both name and pubkey somehow
# if not self.pubkey and self.name:
# self._keychain['pubkey'] = self.find_pubkey()
# elif self.pubkey and not self.name:
# def boot(self,create=False):
# # Do I have my keys?
# have_keys = self.exists()
# # If not, forge them -- only once!``
# if not have_keys and create:
# self.get_new_keys()
@property
def phone(self):
from komrade.backend.the_telephone import TheTelephone
@ -140,7 +80,7 @@ class Operator(Keymaker):
# self.log(f'I am {self} packaging a message to {another}: {msg_d}')
from komrade.backend.messages import Message
msg_obj = Message(msg_d,from_whom=self,to_whom=another)
msg_obj = Message(msg_d)
# encrypt!
# msg_obj.encrypt()
@ -231,20 +171,7 @@ class Operator(Keymaker):
# ring ring
from komrade.cli.artcode import ART_PHONE_SM1
import textwrap as tw
nxt=get_class_that_defined_method(get_resp_from).__name__
nxtfunc=get_resp_from.__name__
# if from_whom != self:
# self.status(f'''ring ring!
# @{self}: *picks up phone*
# @{from_whom}: I have a message I need you to send for me.
# @{self}: To whom?
# @{from_whom}: To @{to_whom}. But not directly.
# @{self}: Who should it I pass it through?
# @{from_whom}: Pass it to {nxt}. Tell them to use "{nxtfunc}".
# @{self}: Got it... So what's the message?
# @{from_whom}: The message is:
# {dict_format(msg,tab=4)}
# ''')
if caller!=self:
from komrade.cli.artcode import ART_PHONE_SM1
self.log(f'ring ring! I the {self} have received a message from {caller},\n which I will now encrypt and send along to {to_whom}.\n {ART_PHONE_SM1} ')

View File

@ -10,6 +10,10 @@ from komrade.backend import *
# print(PATH_OPERATOR_WEB_KEYS_URL)
# def TheOperator(*x,**y):
# from komrade.backend.operators import Komrade
# return Komrade(OPERATOR_NAME,*x,**y)
class TheOperator(Operator):
"""
@ -78,7 +82,7 @@ class TheOperator(Operator):
}
# msg_d = pickle.loads(data_b)
# self.log('msg_d',msg_d)
msg_obj = Message(msg_d,from_whom=self.phone,to_whom=self)
msg_obj = Message(msg_d)
self.log(f'Decoding the binary, I discovered an encrypted message from {self.phone}\n: {msg_obj}')

View File

@ -4,6 +4,10 @@ from komrade import *
from komrade.backend import *
from komrade.backend.phonelines import *
# def TheTelephone(*x,**y):
# return Komrade(TELEPHONE_NAME,*x,**y)
### ACTUAL PHONE CONNECTIONS
class TheTelephone(Operator):
"""
@ -59,7 +63,7 @@ class TheTelephone(Operator):
# unseal
from komrade.backend.messages import Message
resp_msg_obj = Message(resp_msg_d,from_whom=self.op,to_whom=self) #self.unseal_msg(resp_msg_b)
resp_msg_obj = Message(resp_msg_d)
# res = resp_msg_b_unsealed
self.log('Decoding binary, message discovered:\n',resp_msg_obj)