diff --git a/copypasta.py b/copypasta.py index e851264..801b230 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 @@ -378,7 +382,11 @@ def change_accepting_uploads(json_data:dict): else: socketio.emit("[NOTIFY_USER]",{"msg":"CopyPasta is now refusing incoming files !"}) +# fill up the list of local instances of copypasta available +@socketio.on("[GET_CP_INSTANCES]") +def get_cp_instances(json_data:dict): + socketio.emit("[NOTIFY_USER]",{"msg":str(d_server.discover_instances())}) #processes @@ -872,7 +880,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/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)) + +