Skip to content

Commit

Permalink
prod patch and preparing instances scan
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaaoBlues committed Feb 20, 2023
1 parent 1aa9320 commit 7bc3be1
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 8 deletions.
14 changes: 14 additions & 0 deletions copypasta.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from re import findall



# socket io for real time speeeeeed
from flask_socketio import SocketIO
# necessary to compile -__(°-°)__-
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
69 changes: 69 additions & 0 deletions discovery_server.py
Original file line number Diff line number Diff line change
@@ -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








8 changes: 1 addition & 7 deletions launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__":

Expand Down
33 changes: 33 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -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,('<broadcast>',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()
4 changes: 3 additions & 1 deletion util.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,6 @@ def change_accepting_uploads_state():

config["accepting_uploads"] = not config["accepting_uploads"]

f.write(dumps(config))
f.write(dumps(config))


0 comments on commit 7bc3be1

Please sign in to comment.