From 245ba2980d9f9112e3a2b27666971d800acb69ab Mon Sep 17 00:00:00 2001 From: Toasterkitten Date: Sun, 24 May 2026 17:27:25 -0400 Subject: [PATCH] add a hole punch test server --- extras/hole-punch-test-server.py | 16 ++++++++++++++++ main.py | 25 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 extras/hole-punch-test-server.py diff --git a/extras/hole-punch-test-server.py b/extras/hole-punch-test-server.py new file mode 100644 index 0000000..0719c39 --- /dev/null +++ b/extras/hole-punch-test-server.py @@ -0,0 +1,16 @@ +import socket + +SERVER_PORT = 41234 +SERVER_SEND_PORT = 41235 + +sock_recv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +sock_recv.bind(("0.0.0.0", SERVER_PORT)) +sock_send = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +sock_send.bind(("0.0.0.0", SERVER_SEND_PORT)) + +print("Running...") + +while True: + data, addr = sock_recv.recvfrom(1024) + print(f"Recieved test from addr {addr}") + sock_send.sendto(data, addr) diff --git a/main.py b/main.py index 5391d3e..4ebef6c 100644 --- a/main.py +++ b/main.py @@ -15,6 +15,8 @@ import ipaddress import pyperclip import requests +HOLE_PUNCH_TEST = ("0.0.0.0", 41234) + class SetupApp(App): DEFAULT_CSS = """ @@ -446,12 +448,35 @@ class SettingsScreen(Screen): id="instructions", ) yield Button("Add Contact", id="add-contact") + with TabPane("Extras"): + yield Label( + "This button will run a test to see if this application will work correctly." + ) + yield Button("Test hole punching", id="test-hole") + + def test_hole_punch(self): + test_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + test_sock.bind((my_ip, 0)) + test_sock.settimeout(5) + + test_sock.sendto(b"TEST", HOLE_PUNCH_TEST) + + try: + data, addr = test_sock.recvfrom(1024) + except socket.timeout: + self.app.call_from_thread(self.notify, "Test failed", severity="error") + else: + self.app.call_from_thread(self.notify, "Test worked!") + test_sock.close() def on_button_pressed(self, event): if event.button.id == "close-btn": self.dismiss() elif event.button.id == "add-contact": self.app.push_screen(ChoiceScreen(), self.on_screen_done) + elif event.button.id == "test-hole": + self.notify("Test started....", severity="information") + threading.Thread(target=self.test_hole_punch).start() elif event.button.id == "profile-confirm": valid_ip = True valid_port = True