Add readme, add arg parse, fix proxy

This commit is contained in:
Jakob Kordež 2023-12-09 22:49:11 +01:00
parent 544d5dd265
commit fcc4398393
3 changed files with 84 additions and 2 deletions

48
README.md Normal file
View File

@ -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.

View File

@ -22,16 +22,50 @@ from socket import (
) )
from select import select from select import select
import sys import sys
from argparse import ArgumentParser
import re
SERVER = "localhost" SERVER = "localhost"
PORT = 4996 PORT = 4996
# Argument parsing
serverRegex = r"^(?P<host>[^:]+)(:(?P<port>\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 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
broadcastSocket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) broadcastSocket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
# Connect to TCP server
with socket(AF_INET, SOCK_STREAM) as s: with socket(AF_INET, SOCK_STREAM) as s:
s.settimeout(5) s.settimeout(5)
print("Connecting to server...") print("Connecting to server...", (SERVER, PORT))
# Try to connect to the server for 5 seconds # Try to connect to the server for 5 seconds
try: try:

View File

@ -67,7 +67,7 @@ try:
except: except:
print("Client disconnected") print("Client disconnected")
clients.remove(s) clients.remove(s)
except KeyboardInterrupt: except:
print("\nExiting...") print("\nExiting...")
for s in clients: for s in clients:
s.close() s.close()