client side encrption+ pid lock for sigle instance

This commit is contained in:
Djalim Simaila 2023-03-23 13:19:05 +01:00
parent 2bfc30a49a
commit 5a114c546f

View File

@ -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 distant one
""" """
import threading import threading
@ -9,24 +9,27 @@ import json
import pyperclip import pyperclip
import requests import requests
import socketio import socketio
import zc.lockfile from pid.decorator import pidfile
from cryptography.fernet import Fernet
import notification as notif import notification as notif
# to put in a conf file # to put in a conf file
ip = 'localhost' IP = 'localhost'
port = "9564" PORT = "9564"
hostname = "WarMachine" HOSTNAME = "WarMachine"
username = "neotaku67" USERNAME = "neotaku67"
password = "un bon mot de passe de prefererance mais en sah tant qu'il est hashe ca passe" PASSWORD = "un bon mot de passe de prefererance mais en sah tant qu'il est hashe ca passe"
sign = "[AllSync] " SIGNATURE = "[AllSync] "
KEY = b'n63cPRiZ5DBIBgIR8oiDhnzE5oAj-6xsU1ed8gdPr8A='
fernet = Fernet(KEY)
ip = f"http://{ip}:{port}" IP = f"http://{IP}:{PORT}"
sio = socketio.Client() sio = socketio.Client()
sio.connect(ip) sio.connect(IP)
print("[Debug] Connected to Server .w.") print("[Debug] Connected to Server .w.")
auth = requests.post(f"{ip}/user", auth = requests.post(f"{IP}/user",
data={"username": username, "password": password}, data={"username": USERNAME, "password": PASSWORD},
timeout=10000) timeout=10000)
if auth.status_code != 200: if auth.status_code != 200:
print("invalid credentials") print("invalid credentials")
@ -40,14 +43,17 @@ def send_notification():
while not notify_stop_event.is_set(): while not notify_stop_event.is_set():
while not notif.notification_queue.empty(): while not notif.notification_queue.empty():
notification = notif.notification_queue.get() notification = notif.notification_queue.get()
if notification.title.find(sign) == -1: if notification.title.find(SIGNATURE) != -1:
continue continue
requests.put(f"{ip}/notification", requests.put(f"{IP}/notification",
data={"token": token, data={"token": token,
"title": notification.title, "title":
"content": notification.content, fernet.encrypt(notification.title.encode()),
"deviceName": hostname}, "content":
fernet.encrypt(notification.content.encode()),
"deviceName": HOSTNAME},
timeout=5000) timeout=5000)
print("[NotificationEvent] data sent")
time.sleep(1) time.sleep(1)
@ -57,22 +63,26 @@ notification_thread = threading.Thread(target=send_notification)
@sio.event @sio.event
def NotificationUpdate(data): def NotificationUpdate(data):
if data["device_name"] == hostname: if data["device_name"] == HOSTNAME:
return return
response = requests.get(f"{ip}/notification/-1?token={token}", response = requests.get(f"{IP}/notification/-1?token={token}",
timeout=2000) timeout=2000)
response = json.loads(response.content.decode())["notifications"] response = json.loads(response.content.decode())["notifications"]
notification = notif.Notification(title=sign+response["title"], notification = notif.Notification(title=SIGNATURE+fernet
content=response["content"]) .decrypt(response["title"])
.decode(),
content=fernet
.decrypt(response["content"])
.decode())
notification.show() notification.show()
print("[NotificationEvent] received data") print("[NotificationEvent] received data")
@sio.event @sio.event
def ClipboardUpdate(data): def ClipboardUpdate(data):
if data["device_name"] == hostname: if data["device_name"] == HOSTNAME:
return return
clipboard = requests.get(f"{ip}/clipboard/-1?token={token}", clipboard = requests.get(f"{IP}/clipboard/-1?token={token}",
timeout=2000) timeout=2000)
clipboard = json.loads(clipboard.content.decode()) clipboard = json.loads(clipboard.content.decode())
clipboard = clipboard["clipboard"] clipboard = clipboard["clipboard"]
@ -80,6 +90,16 @@ def ClipboardUpdate(data):
print("[ClipboardEvent] received data") print("[ClipboardEvent] received data")
if __name__ == "__main__": @pidfile()
def main():
notification_thread.start() notification_thread.start()
sio.wait() sio.wait()
if __name__ == "__main__":
try:
main()
except:
print("cant lock pid file, is an another instance running?")
sio.disconnect()
sys.exit(1)