Skip to content

Commit 0859d23

Browse files
authored
Firewall management: improved (#2614)
* fix permissions * Update install func + add/edit func open/close ports + status firewall * hotfix * subport
1 parent 02998c5 commit 0859d23

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

x-ui.sh

+40-17
Original file line numberDiff line numberDiff line change
@@ -682,40 +682,50 @@ show_xray_status() {
682682
}
683683

684684
firewall_menu() {
685-
echo -e "${green}\t1.${plain} Install Firewall & open ports"
686-
echo -e "${green}\t2.${plain} Allowed List"
687-
echo -e "${green}\t3.${plain} Delete Ports from List"
688-
echo -e "${green}\t4.${plain} Disable Firewall"
685+
echo -e "${green}\t1.${plain} Install Firewall"
686+
echo -e "${green}\t2.${plain} Port List"
687+
echo -e "${green}\t3.${plain} Open Ports"
688+
echo -e "${green}\t4.${plain} Delete Ports from List"
689+
echo -e "${green}\t5.${plain} Disable Firewall"
690+
echo -e "${green}\t6.${plain} Firewall Status"
689691
echo -e "${green}\t0.${plain} Back to Main Menu"
690692
read -p "Choose an option: " choice
691693
case "$choice" in
692694
0)
693695
show_menu
694696
;;
695697
1)
696-
open_ports
698+
install_firewall
697699
firewall_menu
698700
;;
699701
2)
700-
sudo ufw status
702+
sudo ufw status numbered
701703
firewall_menu
702704
;;
703705
3)
704-
delete_ports
706+
sudo open_ports
705707
firewall_menu
706708
;;
707709
4)
710+
sudo delete_ports
711+
firewall_menu
712+
;;
713+
5)
708714
sudo ufw disable
709715
firewall_menu
710716
;;
717+
6)
718+
sudo ufw status verbose
719+
firewall_menu
720+
;;
711721
*)
712722
echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
713723
firewall_menu
714724
;;
715725
esac
716726
}
717727

718-
open_ports() {
728+
install_firewall() {
719729
if ! command -v ufw &>/dev/null; then
720730
echo "ufw firewall is not installed. Installing now..."
721731
apt-get update
@@ -733,13 +743,17 @@ open_ports() {
733743
ufw allow ssh
734744
ufw allow http
735745
ufw allow https
736-
ufw allow 2053/tcp
746+
ufw allow 2053/tcp #webPort
747+
ufw allow 2096/tcp #subport
737748

738749
# Enable the firewall
739750
ufw --force enable
740-
fi
751+
fi
752+
done
753+
}
741754

742-
# Prompt the user to enter a list of ports
755+
open_ports() {
756+
# Prompt the user to enter the ports they want to open
743757
read -p "Enter the ports you want to open (e.g. 80,443,2053 or range 400-500): " ports
744758

745759
# Check if the input is valid
@@ -755,19 +769,28 @@ open_ports() {
755769
# Split the range into start and end ports
756770
start_port=$(echo $port | cut -d'-' -f1)
757771
end_port=$(echo $port | cut -d'-' -f2)
772+
# Open the port range
758773
ufw allow $start_port:$end_port/tcp
759774
ufw allow $start_port:$end_port/udp
760775
else
776+
# Open the single port
761777
ufw allow "$port"
762778
fi
763779
done
764780

765-
# Confirm that the ports are open
766-
echo "The following ports are now open:"
767-
ufw status | grep "ALLOW" | grep -Eo "[0-9]+(/[a-z]+)?"
768-
769-
echo "Firewall status:"
770-
ufw status verbose
781+
# Confirm that the ports are opened
782+
echo "Opened the specified ports:"
783+
for port in "${PORT_LIST[@]}"; do
784+
if [[ $port == *-* ]]; then
785+
start_port=$(echo $port | cut -d'-' -f1)
786+
end_port=$(echo $port | cut -d'-' -f2)
787+
# Check if the port range has been successfully opened
788+
(ufw status | grep -q "$start_port:$end_port") && echo "$start_port-$end_port"
789+
else
790+
# Check if the individual port has been successfully opened
791+
(ufw status | grep -q "$port") && echo "$port"
792+
fi
793+
done
771794
}
772795

773796
delete_ports() {

0 commit comments

Comments
 (0)