78 lines
2.2 KiB
Python
78 lines
2.2 KiB
Python
import os, sys
|
|
root_dir = os.path.normpath(__file__.split("libs")[0])
|
|
sys.path.append(root_dir)
|
|
|
|
from libs.fspn.protocol.connection import Connection, EVENTS
|
|
from libs.fspn.utils import sha256_util, aes_util, ecdh_util, ecdsa_util
|
|
from libs.fspn.utils.wrapper_util import singleton, threaded
|
|
import base64, time
|
|
|
|
class UserData:
|
|
def __init__(self):
|
|
self.proof_of_work = None
|
|
|
|
class Pmc:
|
|
def __init__(self):
|
|
self.verifying_key, self.signing_key = ecdsa_util.create_keys(base64.b64decode("I0x1Y2FzR2FicmllbFZhekRvc1NhbnRvc0luYWNpbyE="))
|
|
|
|
def get(self, user) -> UserData:
|
|
user_data = UserData()
|
|
user_data.proof_of_work = "eYnU*@"
|
|
return user_data
|
|
|
|
# Returns a request_id. Callback receives str:request_id str:signature
|
|
def sign(self, data, user, callback) -> str:
|
|
self.send_callback("test", callback, data)
|
|
return "test"
|
|
|
|
def send_callback(self, request_id, callback, data):
|
|
signature = ecdsa_util.sign_message(data, self.signing_key)
|
|
signature = base64.b64encode(signature).decode()
|
|
callback(request_id, signature)
|
|
|
|
|
|
class Test:
|
|
def __init__(self):
|
|
pass
|
|
|
|
def test(self):
|
|
pmc = Pmc()
|
|
|
|
c1 = Connection("A4DZSk+TlR+4w39MbiIAQbti+N0H1QlJEhRH2DI6Iubj", pmc)
|
|
c2 = Connection("A4DZSk+TlR+4w39MbiIAQbti+N0H1QlJEhRH2DI6Iubj", pmc)
|
|
|
|
p1= 5676
|
|
p2 = 5686
|
|
c1.connect(("127.0.0.1", p1), ("127.0.0.1", p2))
|
|
c2.connect(("127.0.0.1", p2), ("127.0.0.1", p1))
|
|
|
|
c1.subscribe_event(EVENTS.ON_MESSAGE, self.on_message_1)
|
|
c2.subscribe_event(EVENTS.ON_MESSAGE, self.on_message_2)
|
|
|
|
time.sleep(2)
|
|
|
|
c1.send_message("Testing", True)
|
|
c1.send_binary(data=b"Test", meta={"test":"test"}, encrypted=True)
|
|
|
|
time.sleep(2)
|
|
|
|
c1.close_connection()
|
|
c2.close_connection()
|
|
|
|
def on_message_1(self, event):
|
|
print("C1 got message: ",event.__dict__)
|
|
|
|
def on_message_2(self, event):
|
|
print("C2 got message: ",event.__dict__)
|
|
|
|
import logging
|
|
import sys
|
|
|
|
logging.basicConfig(
|
|
level=logging.DEBUG,
|
|
format='[%(levelname)s] %(asctime)s - %(message)s',
|
|
stream=sys.stdout
|
|
)
|
|
|
|
t = Test()
|
|
t.test() |