-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from ThaaoBlues/main
V1.9.1
- Loading branch information
Showing
8 changed files
with
129 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.9.0 | ||
1.9.1 |