mirror of
https://gitlab.vegova.si/rkv/flex-discovery-proxy.git
synced 2025-05-30 23:50:28 +00:00
Initial commit
This commit is contained in:
commit
455a409e0b
60
client.py
Normal file
60
client.py
Normal file
@ -0,0 +1,60 @@
|
||||
######################################################
|
||||
# FlexRadio discovery Proxy CLIENT
|
||||
######################################################
|
||||
#
|
||||
# This script connects to the FlexRadio discovery
|
||||
# proxy and re-broadcasts all received packets.
|
||||
#
|
||||
# Author: Jakob Kordež, S52KJ
|
||||
#
|
||||
######################################################
|
||||
|
||||
from socket import (
|
||||
socket,
|
||||
AF_INET,
|
||||
SOCK_STREAM,
|
||||
SOCK_DGRAM,
|
||||
IPPROTO_UDP,
|
||||
SOL_SOCKET,
|
||||
SO_BROADCAST,
|
||||
)
|
||||
from select import select
|
||||
import sys
|
||||
|
||||
SERVER = "localhost"
|
||||
PORT = 4996
|
||||
|
||||
broadcastSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
|
||||
broadcastSocket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
|
||||
|
||||
with socket(AF_INET, SOCK_STREAM) as s:
|
||||
s.settimeout(5)
|
||||
print("Connecting to server...")
|
||||
|
||||
# Try to connect to the server for 5 seconds
|
||||
try:
|
||||
s.connect((SERVER, PORT))
|
||||
print("Connected to server")
|
||||
print("\nPress Ctrl+C to exit...")
|
||||
except (ConnectionRefusedError, TimeoutError):
|
||||
print("Connection timed out")
|
||||
input("\nPress enter to exit...")
|
||||
sys.exit(1)
|
||||
|
||||
s.setblocking(False)
|
||||
|
||||
try:
|
||||
while True:
|
||||
r = select([s], [], [], 1)[0]
|
||||
if len(r) == 0:
|
||||
continue
|
||||
|
||||
payload = s.recv(2048)
|
||||
if not payload:
|
||||
print("Server disconnected")
|
||||
break
|
||||
|
||||
broadcastSocket.sendto(payload, ("255.255.255.255", 4992))
|
||||
except KeyboardInterrupt:
|
||||
print("\nExiting...")
|
||||
sys.exit(0)
|
71
flex_proxy.py
Normal file
71
flex_proxy.py
Normal file
@ -0,0 +1,71 @@
|
||||
######################################################
|
||||
# FlexRadio discovery Proxy
|
||||
######################################################
|
||||
#
|
||||
# This script listens for FlexRadio discovery
|
||||
# packets on UDP port 4992 and forwards them
|
||||
# over a TCP connection to all connected clients.
|
||||
#
|
||||
# Author: Jakob Kordež, S52KJ
|
||||
#
|
||||
######################################################
|
||||
|
||||
from socket import socket, AF_INET, SOCK_STREAM, SOCK_DGRAM
|
||||
from select import select
|
||||
from time import time
|
||||
import sys
|
||||
|
||||
TCP_PORT = 4996
|
||||
UDP_PORT = 4992
|
||||
|
||||
# Listen for clients
|
||||
tcp_socket = socket(AF_INET, SOCK_STREAM)
|
||||
tcp_socket.bind(("", TCP_PORT))
|
||||
tcp_socket.listen(5)
|
||||
tcp_socket.setblocking(False)
|
||||
|
||||
# Listen for Discovery packets
|
||||
udp_socket = socket(AF_INET, SOCK_DGRAM)
|
||||
udp_socket.bind(("", UDP_PORT))
|
||||
udp_socket.setblocking(False)
|
||||
|
||||
clients: list[socket] = []
|
||||
|
||||
print("\nPress Ctrl+C to exit...")
|
||||
|
||||
try:
|
||||
while True:
|
||||
r = select([tcp_socket, udp_socket, *clients], [], [], 1)[0]
|
||||
s: socket
|
||||
for s in r:
|
||||
if s is tcp_socket:
|
||||
# A client has connected
|
||||
connection, client_address = tcp_socket.accept()
|
||||
connection.setblocking(False)
|
||||
clients.append(connection)
|
||||
print("New connection from:", client_address)
|
||||
elif s is udp_socket:
|
||||
# Discovery packet received
|
||||
data, client_address = udp_socket.recvfrom(2048)
|
||||
print(int(time()), "Received discovery")
|
||||
|
||||
# Send discovery packet to all clients
|
||||
for c in clients:
|
||||
c.send(data)
|
||||
else:
|
||||
try:
|
||||
data = s.recv(1024)
|
||||
except:
|
||||
data = None
|
||||
if not data:
|
||||
# A client socket has been disconnected
|
||||
print("Client disconnected:", s.getpeername())
|
||||
clients.remove(s)
|
||||
s.close()
|
||||
except KeyboardInterrupt:
|
||||
print("\nExiting...")
|
||||
for s in clients:
|
||||
s.close()
|
||||
tcp_socket.close()
|
||||
udp_socket.close()
|
||||
sys.exit(0)
|
Loading…
x
Reference in New Issue
Block a user