From fcc43983936e8d3278060eb2bb13f98b6e1a22da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Korde=C5=BE?= Date: Sat, 9 Dec 2023 22:49:11 +0100 Subject: [PATCH] Add readme, add arg parse, fix proxy --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ client.py | 36 +++++++++++++++++++++++++++++++++++- flex_proxy.py | 2 +- 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..86435af --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +# FlexRadio Discovery Pakcet Proxy + +This project was created to allow connecting to a FlexRadio over a VPN as an +alternative to using SmartLink. + +## Why? + +FlexRadio does not want to provide a way to connect to the radio by IP address. +The only way to connect to the radio is by recieving a discovery packet from the radio. +This is a problem if you want to connect to the radio from a different network when broadcast packets are not forwarded. + +## How? + +The proxy consists of two Python 3 scripts. + +`flex_proxy.py` is the main script that listens for discovery packets and forwards them to the client. +It listens for UDP discovery packets on port 4992 and forwards the payload to all clients connected to the TCP socket on port 4996. + +`client.py` connects to the proxy via the TCP socket on port 4996. When a message is recieved, it re-broadcasts it as a UDP packet on port 4992. + +## Usage + +None of the two scripts require any external libraries. +All libraries used are part of the Python 3 standard library: + +- `argparse` +- `re` +- `select` +- `socket` +- `sys` +- `time` + +### Server + +``` +python3 flex_proxy.py +``` + +The TCP socket port can be changed by changing the `PORT` variable in `flex_proxy.py`. + +### Client + +``` +python3 client.py [ip address] +``` + +The default IP address is `localhost`. +It can be changed by changing the `HOST` variable in `client.py` or by passing the IP address as an argument. diff --git a/client.py b/client.py index 1192e80..859f7c1 100644 --- a/client.py +++ b/client.py @@ -22,16 +22,50 @@ from socket import ( ) from select import select import sys +from argparse import ArgumentParser +import re SERVER = "localhost" PORT = 4996 + +# Argument parsing +serverRegex = r"^(?P[^:]+)(:(?P\d+))?$" + +parser = ArgumentParser( + prog="FlexRadio discovery proxy client", + description="Connect to a FlexRadio discovery proxy and re-broadcast all received packets", + epilog="Author: Jakob Kordež, S52KJ", +) +parser.add_argument( + "server", + metavar="SERVER", + type=str, + nargs="?", + default=SERVER, + help="Server address (default: %(default)s)", +) + +args = parser.parse_args() +sp = re.fullmatch(serverRegex, args.server) +if not sp: + print("Invalid server address") + sys.exit(1) + +SERVER = sp.group("host") +if sp.group("port"): + PORT = int(sp.group("port")) + + +# Create broadcast socket broadcastSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) broadcastSocket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) + +# Connect to TCP server with socket(AF_INET, SOCK_STREAM) as s: s.settimeout(5) - print("Connecting to server...") + print("Connecting to server...", (SERVER, PORT)) # Try to connect to the server for 5 seconds try: diff --git a/flex_proxy.py b/flex_proxy.py index d31d0a0..1216c05 100644 --- a/flex_proxy.py +++ b/flex_proxy.py @@ -67,7 +67,7 @@ try: except: print("Client disconnected") clients.remove(s) -except KeyboardInterrupt: +except: print("\nExiting...") for s in clients: s.close()