From 4c6ca75fa8f0febaf18aafe10b55953acbfa31e5 Mon Sep 17 00:00:00 2001 From: raphael Date: Wed, 12 Jan 2022 19:21:05 +0100 Subject: [PATCH] qr code now ascii, makes functions private --- main.py => bump.py | 105 ++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 50 deletions(-) rename main.py => bump.py (63%) diff --git a/main.py b/bump.py similarity index 63% rename from main.py rename to bump.py index b5892f7..9947460 100644 --- a/main.py +++ b/bump.py @@ -6,6 +6,7 @@ import qrcode import requests import base64 import re +import os SENDER_LENGTH = 4 @@ -52,17 +53,20 @@ def password_decrypt(token: bytes, password: str) -> bytes: return Fernet(key).decrypt(token) class Bump: - def __init__(self, secret=None, secret_file='.bump_secrets'): - self.secrets = self.load_secrets(secret, secret_file) + def __init__(self, secret=None, secrets_file=None): + if None == secrets_file: + secrets_file = os.path.join(os.path.expanduser('~'), '.config/bump/secrets_file') + self.secrets = self._load_secrets(secret, secrets_file) if self.secrets == []: - self.generate_secret(secret_file) - self.show_secrets() + print("you seem to not have a secret in your secrets file! Creating one now...") + self.generate_secret(secrets_file) + self.show_secret() - self.URL = "http://0.0.0.0:4000/api/" + self.URL = "https://bump.maenle.net/api/" - def load_secrets(self, secret, secrets_file='.bump_secrets'): + def _load_secrets(self, secret, secrets_file): try: with open(secrets_file, 'r') as f: secrets = f.read().splitlines() @@ -73,7 +77,7 @@ class Bump: return secrets - def generate_secret(self, secret_file): + def generate_secret(self, secrets_file): WORDS = r.get_random_words() secret = "" for _ in range(SENDER_LENGTH + PASSWORD_LENGTH): @@ -84,74 +88,76 @@ class Bump: self.secrets.append(secret) - with open(secret_file, 'a+') as f: + ipdb.set_trace() + if not os.path.exists(os.path.dirname(secrets_file)): + os.makedirs(os.path.dirname(secrets_file)) + + with open(secrets_file, 'a+') as f: f.write(secret + '\n') - def show_secrets(self): + def show_secret(self): + print("Scan this QR Code with the Bump app to connect them:") for secret in self.secrets: - qrcode.make(secret).show() + qr = qrcode.QRCode( + version=1, + error_correction=qrcode.constants.ERROR_CORRECT_L, + box_size=10, + border=4, + ) + qr.add_data(secret) + qr.print_ascii() + print("") print(secret) - def get_password(self, index=0): + def _get_password(self, index=0): secret = self.secrets[index].split('-') return "-".join(secret[SENDER_LENGTH:]) - def get_sender(self, index=0): + def _get_sender(self, index=0): words = self.secrets[index].split('-') return "-".join(words[0:SENDER_LENGTH]) - def encrypt(self, data): - password = self.get_password() + def _encrypt(self, data): + password = self._get_password() return password_encrypt(data.encode(), password) - def decrypt(self, data): - password = self.get_password() + def _decrypt(self, data): + password = self._get_password() return password_decrypt(data, password).decode() - def push_fake(self, data): - print(self.encrypt(data)) - - def push(self, title='', data=''): params = { - 'sender': self.get_sender(), + 'sender': self._get_sender(), 'title': title, - 'data': self.encrypt(data) + 'data': self._encrypt(data) } - return self.set_post("push", params) - - def firebase(self): - params = { - 'token': "dTm8S2bfTdKYQTjrxnwFFg:APA91bHGgM7IdRS5uxD0ljmwmP6cAec2icX0VBs69iRB2ApsohyOWzTzontO7cBkjNYbWV87zvxrXIs5jHkJ-8mSWa_-RiU2Y8-XEy3g-Fep3z6dhDeM3KazP58jDRgbdB5cVpDcIEWL" - } - return self.set_post("firebase", params) - + return self._set_post("push", params) def peek(self): - return self.get_post("peek") + return self._get_post("peek") def pop(self): - return self.get_post("pop") + return self._get_post("pop") def list(self): params = { 'minutes': 2 } - return self.get_post("list", params) + return self._get_post("list", params) def clear(self): - return self.get_post("clear") + return self._get_post("clear") - def deleteSender(self): - return self.set_post("delete_sender") + def delete_sender(self): + return self._set_post("delete_sender") - def set_post(self, mechanism, add_params = None): + def _set_post(self, mechanism, add_params = None): url = self.URL + mechanism params = { - 'sender': self.get_sender(), + 'sender': self._get_sender(), } if add_params != None: @@ -160,11 +166,11 @@ class Bump: messages = requests.post(url, params).json() print(messages) - def get_post(self, mechanism, add_params = None): + def _get_post(self, mechanism, add_params = None): url = self.URL + mechanism params = { - 'sender': self.get_sender() + 'sender': self._get_sender() } if add_params != None: @@ -177,38 +183,37 @@ class Bump: return [{}] elif "messages" in messages: for message in messages.get("messages"): - message['data'] = self.decrypt(message.get('data')) + message['data'] = self._decrypt(message.get('data')) return messages else: - messages['data'] = self.decrypt(messages.get('data')) + messages['data'] = self._decrypt(messages.get('data')) return [messages] - def load_log(self): + def _load_log(self): with open('.bump_log', "r+") as f: return f.readlines() - def save_log(self, message): + def _save_log(self, message): with open('.bump_log', "a") as f: - f.write(self.to_log_line(message)) + f.write(self._to_log_line(message)) - def to_log_line(self, message): + def _to_log_line(self, message): return json.dumps(message) + "\n" def alert(self, sleep_time=1): - log = self.load_log() + log = self._load_log() while True: time.sleep(sleep_time) messages = self.list() for message in messages: - if self.to_log_line(message) not in log and message != {}: + if self._to_log_line(message) not in log and message != {}: print(message['data']) - self.save_log(message) + self._save_log(message) log = self.load_log() if __name__ == '__main__': b = Bump() - b.decrypt("M1dEAxKZ5HUHCJoRkgGOvAABhqCAAAAAAGG2eKTSlKXWLDQx5B_wssZsNwsanzQID2UyUm4KKuKYKgfwH5MG2N-qzt6K4mg3pfZmWPaiDB9PiqlX236k6zo9Yvvq")