diff --git a/copypasta.py b/copypasta.py index e851264..6a71e99 100644 --- a/copypasta.py +++ b/copypasta.py @@ -24,6 +24,7 @@ from re import findall + # socket io for real time speeeeeed from flask_socketio import SocketIO # necessary to compile -__(°-°)__- @@ -34,6 +35,9 @@ from random import choice from string import printable +# instances discovery server +#from discovery_server import Server as DiscoveryServer + #init flask app and secret key app = Flask(__name__) app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 @@ -274,7 +278,7 @@ def change_tab_settings(json_data:dict): # open files explorer into the the copypasta directory -socketio.on("[OPEN_FILES_EXPLORER]") +@socketio.on("[OPEN_FILES_EXPLORER]") def open_files_explorer(json_data:dict): Process(target=startfile,args=(f"{APP_PATH}/static/files_hist",)).start() @@ -283,7 +287,7 @@ def open_files_explorer(json_data:dict): # open a file with default app -socketio.on("[OPEN_FILE]") +@socketio.on("[OPEN_FILE]") def open_file(json_data:dict): @@ -300,7 +304,7 @@ def open_file(json_data:dict): -socketio.on("[COPY_WIFI_PW]") +@socketio.on("[COPY_WIFI_PW]") def copy_wifi_pw(json_data:dict): @@ -318,7 +322,7 @@ def copy_wifi_pw(json_data:dict): # copy text scan content -socketio.on("[COPY_CONTENT]") +@socketio.on("[COPY_CONTENT]") def copy_text_scan_content(json_data:dict): @@ -378,9 +382,6 @@ def change_accepting_uploads(json_data:dict): else: socketio.emit("[NOTIFY_USER]",{"msg":"CopyPasta is now refusing incoming files !"}) - - - #processes @app.route("/process/") def process(process_id): @@ -613,7 +614,6 @@ def upload(): notify_desktop("New scan Incoming !", "Click to open CopyPasta") socketio.emit("[NOTIFY_USER]",{"msg":"New scan Incoming !"}) - socketio.emit("fill_history_tab",get_history_json()) if r != None: @@ -661,15 +661,12 @@ def upload(): open_browser_if_settings_okay("http://127.0.0.1:21987/scan_preview") - return jsonify({"upload_status" : "true"}) - elif file_type == "keystrokes": keystrokes = r['text'] send_keystrokes(keystrokes) - return jsonify({"upload_status" : "true"}) elif file_type == "wifi": @@ -679,8 +676,6 @@ def upload(): store_to_history({"file_type" : "wifi", "ssid" : f"{ssid}","password" : f"{password}", "enctype" : f"{enctype}", "date" : f"{time}"}) - return jsonify({"upload_status" : "true"}) - elif file_type == "isbn": @@ -688,45 +683,33 @@ def upload(): store_to_history({"file_type" : "isbn", "content" : f"{isbn}", "date" :f"{time}","isbn_lookup":identify_product(isbn)}) - return jsonify({"upload_status" : "true"}) - elif file_type == "email": store_to_history({"file_type" : "email","addr" : f"{r['address']}", "subject" : f"{r['subject']}", "content" : f"{r['content']}", "date" : f"{time}"}) - return jsonify({"upload_status" : "true"}) - elif file_type == "url": store_to_history({"file_type" : "url","url" : f"{r}", "date" : f"{time}"}) - return jsonify({"upload_status" : "true"}) elif file_type == "phone": store_to_history({"file_type" : "phone","phone_number" : f"{r}", "date" : f"{time}"}) - return jsonify({"upload_status" : "true"}) - elif file_type == "sms": store_to_history({"file_type" : "sms","phone_number" : f"{r['number']}", "content": f"{r['content']}", "date" : f"{time}"}) - return jsonify({"upload_status" : "true"}) - elif file_type == "location": lat = r['lattitude'] long = r['longitude'] - - store_to_history({"file_type" : "location", "lat" : f"{lat}", "long" : f"{long}", "date" : f"{time}"}) elif file_type == "contact": store_to_history({"file_type" : "contact", "first_name" : f"{r['firstName']}", "name" : f"{r['name']}", "organization" : f"{r['organization']}", "job" : f"{r['title']}"}) - else: return jsonify({"upload_status" : "false","Error" : "unknown type"}), 400 @@ -765,8 +748,12 @@ def upload(): if is_image(file_type): open_browser_if_settings_okay(f"{COPYPASTA_URL}/image_preview?image_id={get_history_file_last_id()}") - - return jsonify({"upload_status" : "true"}) + + + + socketio.emit("fill_history_tab",get_history_json()) + + return jsonify({"upload_status" : "true"}) @@ -872,7 +859,13 @@ def client(): Process(target=open_link_process, args=(COPYPASTA_URL,)).start() + + + if not is_server_already_running(): + + #d_server = DiscoveryServer() + #d_server.start() socketio.run(app,host="0.0.0.0",port=21987) diff --git a/discovery_server.py b/discovery_server.py new file mode 100644 index 0000000..1071ba5 --- /dev/null +++ b/discovery_server.py @@ -0,0 +1,69 @@ +from util import get_private_ip +import socket +from json import loads +from multiprocessing import Process + + +class Server(): + + + + + + def __init__(self) -> None: + """ + a simple server responding to udb ping on port 21988 + and sending ping requests to discover others copypasta instances on the local network + """ + + self.port = 21987 + + self.sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) + + + + def response_loop(self,PORT:int,sock:socket.socket): + + + + msg = ("[PONG FLAG]{ip_addr:\""+get_private_ip() +"\"}").encode("utf-8") + + PORT = 21988 + sock.bind(('',21988)) + + while True: + if sock.recv(1024) == b"ping": + sock.sendto(msg, ("255.255.255.255", PORT)) + + + def start(self): + + Process(target=self.response_loop,args=(self.port,self.sock)).start() + + + def discover_instances(self) -> set: + """ + returns a set of tuples (ip_addr,hostname) + that corresponds to all copypasta instances running on the local network + """ + + ret = set() + + self.sock.sendto(b"ping", ("255.255.255.255", self.port)) + + resp = self.sock.recv(1024).decode("utf-8") + + if resp.startswith("[PONG FLAG]"): + resp = loads(resp.replace("[PONG_FLAG]","")) + ret.add((resp["ip_addr"],socket.gethostbyaddr(resp["ip_addr"]))) + + + return ret + + + + + + + + diff --git a/launcher.py b/launcher.py index 141e57a..15ea045 100644 --- a/launcher.py +++ b/launcher.py @@ -7,7 +7,7 @@ from zipfile import ZipFile from os import path, chdir, remove,mkdir,environ import sys -from util import create_shortcut, notify_desktop +from util import notify_desktop # to fix pyinstaller error import pywintypes @@ -132,12 +132,6 @@ def move_launcher(): f.write(get("https://github.com/CopyPastaOfficial/CopyPasta/releases/latest/download/launcher.exe").content) f.close() - # 2 - create_shortcut(path="C:\\Users\\Public\\Desktop\\CopyPasta.lnk",target="C:\\Program Files\\CopyPasta\\launcher.exe",wDir="C:\\Program Files\\CopyPasta\\",icon="C:\\Program Files\\CopyPasta\\copypasta\\static\\favicon.ico") - - #3 - create_shortcut(path=f"C:\\Users\\{getuser()}\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\CopyPasta.lnk",target="C:\\Program Files\\CopyPasta\\launcher.exe",wDir="C:\\Program Files\\CopyPasta\\",icon="C:\\Program Files\\CopyPasta\\copypasta\\static\\favicon.ico") - if __name__ == "__main__": diff --git a/production.md b/production.md index 2b62909..43678b7 100644 --- a/production.md +++ b/production.md @@ -4,7 +4,7 @@ - copypasta icon - nuitka : - - python -m nuitka .\copypasta.py --follow-imports --plugin-enable=multiprocessing --onefile --windows-disable-console + ```python -m nuitka .\copypasta.py --follow-imports --standalone --windows-disable-console``` ## compile copypasta with pyinstaller : diff --git a/templates/index.html b/templates/index.html index d2b443b..c871006 100644 --- a/templates/index.html +++ b/templates/index.html @@ -275,11 +275,10 @@

Last files/data sent :

var n_elements = 0; - function fill_history_tab(init,json){ - console.log(json); + var json = json.history; var elements = []; diff --git a/test.py b/test.py index e69de29..6f78455 100644 --- a/test.py +++ b/test.py @@ -0,0 +1,33 @@ +from socket import * +from requests import get +from json import loads + +PORT = 21988 + +#print(get("http://127.0.0.1:21987/api/ping").text) + +HTTP_REQ = b"GET /api/ping HTTP/1.1\r\nHost:www.example.com\r\n\r\n" + +def t1(): + s = socket(AF_INET,SOCK_DGRAM) + + s.bind(('',0)) + s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) + s.sendto(HTTP_REQ,('',PORT)) + print("packet sent") + print(s.recv(10)) + +def t2(): + with socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP) as sock: + sock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) + sock.sendto(b"ping", ("255.255.255.255", PORT)) + + resp = sock.recv(1024).decode("utf-8") + + if resp.startswith("[PONG FLAG]"): + resp = loads(resp.replace("[PONG_FLAG]","")) + print(resp["ip_addr"]) + + + +t2() \ No newline at end of file diff --git a/util.py b/util.py index f4585bd..8013b5c 100644 --- a/util.py +++ b/util.py @@ -374,4 +374,6 @@ def change_accepting_uploads_state(): config["accepting_uploads"] = not config["accepting_uploads"] - f.write(dumps(config)) \ No newline at end of file + f.write(dumps(config)) + + diff --git a/version b/version index abb1658..ee672d8 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.9.0 \ No newline at end of file +1.9.1 \ No newline at end of file