Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add firewall rules for iptables on linux #2078

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions alvr/xtask/firewall/alvr_fw_config.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ firewalld_cfg() {
if [ "${1}" == 'add' ]; then
# If running or permanent alvr service is missing, add it
if ! firewall-cmd --zone="${zone}" --list-services | grep 'alvr' >/dev/null 2>&1; then
firewall-cmd --zone="${zone}" --add-service='alvr'
firewall-cmd --zone="${zone}" --add-service='alvr'
fi
if ! firewall-cmd --zone="${zone}" --list-services --permanent | grep 'alvr' >/dev/null 2>&1; then
firewall-cmd --zone="${zone}" --add-service='alvr' --permanent
firewall-cmd --zone="${zone}" --add-service='alvr' --permanent
fi
elif [ "${1}" == 'remove' ]; then
# If running or persistent alvr service exists, remove it
if firewall-cmd --zone="${zone}" --list-services | grep 'alvr' >/dev/null 2>&1; then
firewall-cmd --zone="${zone}" --remove-service='alvr'
firewall-cmd --zone="${zone}" --remove-service='alvr'
fi
if firewall-cmd --zone="${zone}" --list-services --permanent | grep 'alvr' >/dev/null 2>&1; then
firewall-cmd --zone="${zone}" --remove-service='alvr' --permanent
firewall-cmd --zone="${zone}" --remove-service='alvr' --permanent
fi
else
exit 2
Expand Down Expand Up @@ -55,6 +55,38 @@ ufw_cfg() {
fi
}

iptables_cfg() {
first_port_match_count=$(iptables -S | grep -c '9943')
second_port_match_count=$(iptables -S | grep -c '9944')
if [ "${1}" == 'add' ]; then
if [ "$first_port_match_count" == "0" ] || [ "$second_port_match_count" == "0" ]; then
iptables -I OUTPUT -p tcp --sport 9943 -j ACCEPT
iptables -I INPUT -p tcp --dport 9943 -j ACCEPT
iptables -I OUTPUT -p udp --sport 9943 -j ACCEPT
iptables -I INPUT -p udp --dport 9943 -j ACCEPT
iptables -I OUTPUT -p tcp --sport 9944 -j ACCEPT
iptables -I INPUT -p tcp --dport 9944 -j ACCEPT
iptables -I OUTPUT -p udp --sport 9944 -j ACCEPT
iptables -I INPUT -p udp --dport 9944 -j ACCEPT
iptables-save >/etc/iptables/rules.v4
fi
elif [ "${1}" == 'remove' ]; then
if [ "$first_port_match_count" == "4" ] || [ "$second_port_match_count" == "4" ]; then
iptables -D OUTPUT -p tcp --sport 9943 -j ACCEPT
iptables -D INPUT -p tcp --dport 9943 -j ACCEPT
iptables -D OUTPUT -p udp --sport 9943 -j ACCEPT
iptables -D INPUT -p udp --dport 9943 -j ACCEPT
iptables -D OUTPUT -p tcp --sport 9944 -j ACCEPT
iptables -D INPUT -p tcp --dport 9944 -j ACCEPT
iptables -D OUTPUT -p udp --sport 9944 -j ACCEPT
iptables -D INPUT -p udp --dport 9944 -j ACCEPT
iptables-save >/etc/iptables/rules.v4
fi
else
exit 2
fi
}

main() {
# If we're not root use pkexec for GUI prompt
if [ "${USER}" == 'root' ]; then
Expand All @@ -64,6 +96,8 @@ main() {
# Check if ufw exists and is running
elif which ufw >/dev/null 2>&1 && ! ufw status | grep 'Status: inactive' >/dev/null 2>&1; then
ufw_cfg "${1,,}"
elif which iptables >/dev/null 2>&1; then
iptables_cfg "${1,,}"
else
exit 99
fi
Expand Down
Loading