#!/bin/bash


PORT=21116    # UDP heartbeat/ID server



# Check the status of the port in UFW
status=$(sudo ufw status | grep -w "$PORT")

# Determine if the port is allowed or denied, and toggle the rule
if echo "$status" | grep -q "DENY"; then
    echo "Rustdesk was denied through the firewall"
    NEW_STATUS="allow"
    MSG_PIECE_1="Allowing"
    MSG_PIECE_2="allowed"
else
    echo "Rustdesk was allowed through the firewall"
    NEW_STATUS="deny"
    MSG_PIECE_1="Denying"
    MSG_PIECE_2="denied"
fi 

# Define the list of ports to deny/allow
PORTS=(21114/tcp 21115/tcp 21116 21117/tcp 21118/tcp 21119/tcp)

# Loop through each port in the list
for PORT in "${PORTS[@]}"; do
    echo "$MSG_PIECE_1 access to port $PORT..."
    sudo ufw "$NEW_STATUS" "$PORT"
done

# Reload UFW to apply changes
echo "Reloading UFW to apply changes..."
sudo ufw reload

# Display the updated UFW status for the specified ports
echo "Updated UFW status:"
for PORT in "${PORTS[@]}"; do
    sudo ufw status | grep -w "$PORT"
done

echo "All specified ports have been $MSG_PIECE_2."
