Skip to content

Commit b650064

Browse files
LoST202MHSanaei
authored andcommitted
Firewall improvements (#2630)
* The menu has been completed *Added firewall shutdown *Improved port removal process (optional)
1 parent 9fb9d72 commit b650064

File tree

1 file changed

+80
-40
lines changed

1 file changed

+80
-40
lines changed

x-ui.sh

+80-40
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,13 @@ show_xray_status() {
683683
}
684684

685685
firewall_menu() {
686-
echo -e "${green}\t1.${plain} Install Firewall"
687-
echo -e "${green}\t2.${plain} Port List"
688-
echo -e "${green}\t3.${plain} Open Ports"
689-
echo -e "${green}\t4.${plain} Delete Ports from List"
690-
echo -e "${green}\t5.${plain} Disable Firewall"
691-
echo -e "${green}\t6.${plain} Firewall Status"
686+
echo -e "${green}\t1.${plain} ${green}Install${plain} Firewall"
687+
echo -e "${green}\t2.${plain} Port List [numbered]"
688+
echo -e "${green}\t3.${plain} ${green}Open${plain} Ports"
689+
echo -e "${green}\t4.${plain} ${red}Delete${plain} Ports from List"
690+
echo -e "${green}\t5.${plain} ${green}Enable${plain} Firewall"
691+
echo -e "${green}\t6.${plain} ${red}Disable${plain} Firewall"
692+
echo -e "${green}\t7.${plain} Firewall Status"
692693
echo -e "${green}\t0.${plain} Back to Main Menu"
693694
read -p "Choose an option: " choice
694695
case "$choice" in
@@ -712,10 +713,14 @@ firewall_menu() {
712713
firewall_menu
713714
;;
714715
5)
715-
ufw disable
716+
ufw enable
716717
firewall_menu
717718
;;
718719
6)
720+
ufw disable
721+
firewall_menu
722+
;;
723+
7)
719724
ufw status verbose
720725
firewall_menu
721726
;;
@@ -794,46 +799,81 @@ open_ports() {
794799
}
795800

796801
delete_ports() {
797-
# Prompt the user to enter the ports they want to delete
798-
read -p "Enter the ports you want to delete (e.g. 80,443,2053 or range 400-500): " ports
802+
# Display current rules with numbers
803+
echo "Current UFW rules:"
804+
ufw status numbered
805+
806+
# Ask the user how they want to delete rules
807+
echo "Do you want to delete rules by:"
808+
echo "1) Rule numbers"
809+
echo "2) Ports"
810+
read -p "Enter your choice (1 or 2): " choice
811+
812+
if [[ $choice -eq 1 ]]; then
813+
# Deleting by rule numbers
814+
read -p "Enter the rule numbers you want to delete (1, 2, etc.): " rule_numbers
815+
816+
# Validate the input
817+
if ! [[ $rule_numbers =~ ^([0-9]+)(,[0-9]+)*$ ]]; then
818+
echo "Error: Invalid input. Please enter a comma-separated list of rule numbers." >&2
819+
exit 1
820+
fi
799821

800-
# Check if the input is valid
801-
if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
802-
echo "Error: Invalid input. Please enter a comma-separated list of ports or a range of ports (e.g. 80,443,2053 or 400-500)." >&2
803-
exit 1
804-
fi
822+
# Split numbers into an array
823+
IFS=',' read -ra RULE_NUMBERS <<<"$rule_numbers"
824+
for rule_number in "${RULE_NUMBERS[@]}"; do
825+
# Delete the rule by number
826+
ufw delete "$rule_number" || echo "Failed to delete rule number $rule_number"
827+
done
805828

806-
# Delete the specified ports using ufw
807-
IFS=',' read -ra PORT_LIST <<<"$ports"
808-
for port in "${PORT_LIST[@]}"; do
809-
if [[ $port == *-* ]]; then
810-
# Split the range into start and end ports
811-
start_port=$(echo $port | cut -d'-' -f1)
812-
end_port=$(echo $port | cut -d'-' -f2)
813-
# Delete the port range
814-
ufw delete allow $start_port:$end_port/tcp
815-
ufw delete allow $start_port:$end_port/udp
816-
else
817-
ufw delete allow "$port"
818-
fi
819-
done
829+
echo "Selected rules have been deleted."
820830

821-
# Confirm that the ports are deleted
831+
elif [[ $choice -eq 2 ]]; then
832+
# Deleting by ports
833+
read -p "Enter the ports you want to delete (e.g. 80,443,2053 or range 400-500): " ports
822834

823-
echo "Deleted the specified ports:"
824-
for port in "${PORT_LIST[@]}"; do
825-
if [[ $port == *-* ]]; then
826-
start_port=$(echo $port | cut -d'-' -f1)
827-
end_port=$(echo $port | cut -d'-' -f2)
828-
# Check if the port range has been successfully deleted
829-
(ufw status | grep -q "$start_port:$end_port") || echo "$start_port-$end_port"
830-
else
831-
# Check if the individual port has been successfully deleted
832-
(ufw status | grep -q "$port") || echo "$port"
835+
# Validate the input
836+
if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
837+
echo "Error: Invalid input. Please enter a comma-separated list of ports or a range of ports (e.g. 80,443,2053 or 400-500)." >&2
838+
exit 1
833839
fi
834-
done
840+
841+
# Split ports into an array
842+
IFS=',' read -ra PORT_LIST <<<"$ports"
843+
for port in "${PORT_LIST[@]}"; do
844+
if [[ $port == *-* ]]; then
845+
# Split the port range
846+
start_port=$(echo $port | cut -d'-' -f1)
847+
end_port=$(echo $port | cut -d'-' -f2)
848+
# Delete the port range
849+
ufw delete allow $start_port:$end_port/tcp
850+
ufw delete allow $start_port:$end_port/udp
851+
else
852+
# Delete a single port
853+
ufw delete allow "$port"
854+
fi
855+
done
856+
857+
# Confirmation of deletion
858+
echo "Deleted the specified ports:"
859+
for port in "${PORT_LIST[@]}"; do
860+
if [[ $port == *-* ]]; then
861+
start_port=$(echo $port | cut -d'-' -f1)
862+
end_port=$(echo $port | cut -d'-' -f2)
863+
# Check if the port range has been deleted
864+
(ufw status | grep -q "$start_port:$end_port") || echo "$start_port-$end_port"
865+
else
866+
# Check if the individual port has been deleted
867+
(ufw status | grep -q "$port") || echo "$port"
868+
fi
869+
done
870+
else
871+
echo "${red}Error:${plain} Invalid choice. Please enter 1 or 2." >&2
872+
exit 1
873+
fi
835874
}
836875

876+
837877
update_geo() {
838878
echo -e "${green}\t1.${plain} Loyalsoldier (geoip.dat, geosite.dat)"
839879
echo -e "${green}\t2.${plain} chocolate4u (geoip_IR.dat, geosite_IR.dat)"

0 commit comments

Comments
 (0)