qr code now ascii, makes functions private

This commit is contained in:
Raphael Maenle 2022-01-12 19:21:05 +01:00
parent a84ed4851d
commit 4c6ca75fa8

View File

@ -6,6 +6,7 @@ import qrcode
import requests import requests
import base64 import base64
import re import re
import os
SENDER_LENGTH = 4 SENDER_LENGTH = 4
@ -52,17 +53,20 @@ def password_decrypt(token: bytes, password: str) -> bytes:
return Fernet(key).decrypt(token) return Fernet(key).decrypt(token)
class Bump: class Bump:
def __init__(self, secret=None, secret_file='.bump_secrets'): def __init__(self, secret=None, secrets_file=None):
self.secrets = self.load_secrets(secret, secret_file) 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 == []: if self.secrets == []:
self.generate_secret(secret_file) print("you seem to not have a secret in your secrets file! Creating one now...")
self.show_secrets() 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: try:
with open(secrets_file, 'r') as f: with open(secrets_file, 'r') as f:
secrets = f.read().splitlines() secrets = f.read().splitlines()
@ -73,7 +77,7 @@ class Bump:
return secrets return secrets
def generate_secret(self, secret_file): def generate_secret(self, secrets_file):
WORDS = r.get_random_words() WORDS = r.get_random_words()
secret = "" secret = ""
for _ in range(SENDER_LENGTH + PASSWORD_LENGTH): for _ in range(SENDER_LENGTH + PASSWORD_LENGTH):
@ -84,74 +88,76 @@ class Bump:
self.secrets.append(secret) 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') 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: 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) print(secret)
def get_password(self, index=0): def _get_password(self, index=0):
secret = self.secrets[index].split('-') secret = self.secrets[index].split('-')
return "-".join(secret[SENDER_LENGTH:]) return "-".join(secret[SENDER_LENGTH:])
def get_sender(self, index=0): def _get_sender(self, index=0):
words = self.secrets[index].split('-') words = self.secrets[index].split('-')
return "-".join(words[0:SENDER_LENGTH]) return "-".join(words[0:SENDER_LENGTH])
def encrypt(self, data): def _encrypt(self, data):
password = self.get_password() password = self._get_password()
return password_encrypt(data.encode(), password) return password_encrypt(data.encode(), password)
def decrypt(self, data): def _decrypt(self, data):
password = self.get_password() password = self._get_password()
return password_decrypt(data, password).decode() return password_decrypt(data, password).decode()
def push_fake(self, data):
print(self.encrypt(data))
def push(self, title='', data=''): def push(self, title='', data=''):
params = { params = {
'sender': self.get_sender(), 'sender': self._get_sender(),
'title': title, 'title': title,
'data': self.encrypt(data) 'data': self._encrypt(data)
} }
return self.set_post("push", params) return self._set_post("push", params)
def firebase(self):
params = {
'token': "dTm8S2bfTdKYQTjrxnwFFg:APA91bHGgM7IdRS5uxD0ljmwmP6cAec2icX0VBs69iRB2ApsohyOWzTzontO7cBkjNYbWV87zvxrXIs5jHkJ-8mSWa_-RiU2Y8-XEy3g-Fep3z6dhDeM3KazP58jDRgbdB5cVpDcIEWL"
}
return self.set_post("firebase", params)
def peek(self): def peek(self):
return self.get_post("peek") return self._get_post("peek")
def pop(self): def pop(self):
return self.get_post("pop") return self._get_post("pop")
def list(self): def list(self):
params = { params = {
'minutes': 2 'minutes': 2
} }
return self.get_post("list", params) return self._get_post("list", params)
def clear(self): def clear(self):
return self.get_post("clear") return self._get_post("clear")
def deleteSender(self): def delete_sender(self):
return self.set_post("delete_sender") 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 url = self.URL + mechanism
params = { params = {
'sender': self.get_sender(), 'sender': self._get_sender(),
} }
if add_params != None: if add_params != None:
@ -160,11 +166,11 @@ class Bump:
messages = requests.post(url, params).json() messages = requests.post(url, params).json()
print(messages) print(messages)
def get_post(self, mechanism, add_params = None): def _get_post(self, mechanism, add_params = None):
url = self.URL + mechanism url = self.URL + mechanism
params = { params = {
'sender': self.get_sender() 'sender': self._get_sender()
} }
if add_params != None: if add_params != None:
@ -177,38 +183,37 @@ class Bump:
return [{}] return [{}]
elif "messages" in messages: elif "messages" in messages:
for message in messages.get("messages"): for message in messages.get("messages"):
message['data'] = self.decrypt(message.get('data')) message['data'] = self._decrypt(message.get('data'))
return messages return messages
else: else:
messages['data'] = self.decrypt(messages.get('data')) messages['data'] = self._decrypt(messages.get('data'))
return [messages] return [messages]
def load_log(self): def _load_log(self):
with open('.bump_log', "r+") as f: with open('.bump_log', "r+") as f:
return f.readlines() return f.readlines()
def save_log(self, message): def _save_log(self, message):
with open('.bump_log', "a") as f: 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" return json.dumps(message) + "\n"
def alert(self, sleep_time=1): def alert(self, sleep_time=1):
log = self.load_log() log = self._load_log()
while True: while True:
time.sleep(sleep_time) time.sleep(sleep_time)
messages = self.list() messages = self.list()
for message in messages: 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']) print(message['data'])
self.save_log(message) self._save_log(message)
log = self.load_log() log = self.load_log()
if __name__ == '__main__': if __name__ == '__main__':
b = Bump() b = Bump()
b.decrypt("M1dEAxKZ5HUHCJoRkgGOvAABhqCAAAAAAGG2eKTSlKXWLDQx5B_wssZsNwsanzQID2UyUm4KKuKYKgfwH5MG2N-qzt6K4mg3pfZmWPaiDB9PiqlX236k6zo9Yvvq")