ClipSync/Clients/Linux/client.py

106 lines
3.2 KiB
Python
Executable File

"""
This script is a daemon that, on event, send and sync the clipboard with a
distant one
"""
import threading
import time
import sys
import json
import pyperclip
import requests
import socketio
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"
SIGNATURE = "[AllSync] "
KEY = b'n63cPRiZ5DBIBgIR8oiDhnzE5oAj-6xsU1ed8gdPr8A='
fernet = Fernet(KEY)
IP = f"http://{IP}:{PORT}"
sio = socketio.Client()
sio.connect(IP)
print("[Debug] Connected to Server .w.")
auth = requests.post(f"{IP}/user",
data={"username": USERNAME, "password": PASSWORD},
timeout=10000)
if auth.status_code != 200:
print("invalid credentials")
sys.exit()
token = json.loads(auth.content.decode())['token']
notify_stop_event = threading.Event()
# clipboard_stop_event = threading.Event()
def send_notification():
notif.start_monitoring()
while not notify_stop_event.is_set():
while not notif.notification_queue.empty():
notification = notif.notification_queue.get()
if notification.title.find(SIGNATURE) != -1:
continue
requests.put(f"{IP}/notification",
data={"token": token,
"title":
fernet.encrypt(notification.title.encode()),
"content":
fernet.encrypt(notification.content.encode()),
"deviceName": HOSTNAME},
timeout=5000)
print("[NotificationEvent] data sent")
time.sleep(1)
notification_thread = threading.Thread(target=send_notification)
# clipboard_thread = threading.Thread(target=)
@sio.event
def NotificationUpdate(data):
if data["device_name"] == HOSTNAME:
return
response = requests.get(f"{IP}/notification/-1?token={token}",
timeout=2000)
response = json.loads(response.content.decode())["notifications"]
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:
return
clipboard = requests.get(f"{IP}/clipboard/-1?token={token}",
timeout=2000)
clipboard = json.loads(clipboard.content.decode())
clipboard = clipboard["clipboard"]
pyperclip.copy(clipboard)
print("[ClipboardEvent] received data")
@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)