From 5a114c546fb5b8a6813f1babfd9e78cb8cb20516 Mon Sep 17 00:00:00 2001 From: Djalim Simaila Date: Thu, 23 Mar 2023 13:19:05 +0100 Subject: [PATCH] client side encrption+ pid lock for sigle instance --- client.py | 68 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/client.py b/client.py index cc38777..53d1457 100644 --- a/client.py +++ b/client.py @@ -1,5 +1,5 @@ """ -This script is a daemon that, on event, send and sync the clipboard with a +This script is a daemon that, on event, send and sync the clipboard with a distant one """ import threading @@ -9,24 +9,27 @@ import json import pyperclip import requests import socketio -import zc.lockfile +from pid.decorator import pidfile +from cryptography.fernet import Fernet import notification as notif # to put in a conf file -ip = 'localhost' -port = "9564" -hostname = "WarMachine" -username = "neotaku67" -password = "un bon mot de passe de prefererance mais en sah tant qu'il est hashe ca passe" -sign = "[AllSync] " +IP = 'localhost' +PORT = "9564" +HOSTNAME = "WarMachine" +USERNAME = "neotaku67" +PASSWORD = "un bon mot de passe de prefererance mais en sah tant qu'il est hashe ca passe" +SIGNATURE = "[AllSync] " +KEY = b'n63cPRiZ5DBIBgIR8oiDhnzE5oAj-6xsU1ed8gdPr8A=' +fernet = Fernet(KEY) -ip = f"http://{ip}:{port}" +IP = f"http://{IP}:{PORT}" sio = socketio.Client() -sio.connect(ip) +sio.connect(IP) print("[Debug] Connected to Server .w.") -auth = requests.post(f"{ip}/user", - data={"username": username, "password": password}, +auth = requests.post(f"{IP}/user", + data={"username": USERNAME, "password": PASSWORD}, timeout=10000) if auth.status_code != 200: print("invalid credentials") @@ -40,14 +43,17 @@ def send_notification(): while not notify_stop_event.is_set(): while not notif.notification_queue.empty(): notification = notif.notification_queue.get() - if notification.title.find(sign) == -1: + if notification.title.find(SIGNATURE) != -1: continue - requests.put(f"{ip}/notification", + requests.put(f"{IP}/notification", data={"token": token, - "title": notification.title, - "content": notification.content, - "deviceName": hostname}, + "title": + fernet.encrypt(notification.title.encode()), + "content": + fernet.encrypt(notification.content.encode()), + "deviceName": HOSTNAME}, timeout=5000) + print("[NotificationEvent] data sent") time.sleep(1) @@ -57,22 +63,26 @@ notification_thread = threading.Thread(target=send_notification) @sio.event def NotificationUpdate(data): - if data["device_name"] == hostname: + if data["device_name"] == HOSTNAME: return - response = requests.get(f"{ip}/notification/-1?token={token}", + response = requests.get(f"{IP}/notification/-1?token={token}", timeout=2000) response = json.loads(response.content.decode())["notifications"] - notification = notif.Notification(title=sign+response["title"], - content=response["content"]) + notification = notif.Notification(title=SIGNATURE+fernet + .decrypt(response["title"]) + .decode(), + content=fernet + .decrypt(response["content"]) + .decode()) notification.show() print("[NotificationEvent] received data") @sio.event def ClipboardUpdate(data): - if data["device_name"] == hostname: + if data["device_name"] == HOSTNAME: return - clipboard = requests.get(f"{ip}/clipboard/-1?token={token}", + clipboard = requests.get(f"{IP}/clipboard/-1?token={token}", timeout=2000) clipboard = json.loads(clipboard.content.decode()) clipboard = clipboard["clipboard"] @@ -80,6 +90,16 @@ def ClipboardUpdate(data): print("[ClipboardEvent] received data") -if __name__ == "__main__": +@pidfile() +def main(): notification_thread.start() sio.wait() + + +if __name__ == "__main__": + try: + main() + except: + print("cant lock pid file, is an another instance running?") + sio.disconnect() + sys.exit(1)