Update ip.sh

"ensure_config_env" ,"Add" ,"Edit" functionalities
This commit is contained in:
Whispering Wind
2024-12-07 23:13:10 +03:30
committed by GitHub
parent 526fe293fc
commit 56beded51a

View File

@ -1,6 +1,14 @@
#!/bin/bash
source /etc/hysteria/core/scripts/path.sh
ensure_config_env() {
if [ ! -f "$CONFIG_ENV" ]; then
echo ".configs.env not found. Creating it with default SNI=bts.com."
echo "SNI=bts.com" > "$CONFIG_ENV"
fi
}
add_ips() {
ipv4_address=""
ipv6_address=""
@ -20,9 +28,43 @@ for interface in $interfaces; do
fi
done
{
sed -i '/^IP4=/d' "$CONFIG_ENV" 2>/dev/null
sed -i '/^IP6=/d' "$CONFIG_ENV" 2>/dev/null
echo -e "\nIP4=${ipv4_address:-}" >> "$CONFIG_ENV"
echo "IP6=${ipv6_address:-}" >> "$CONFIG_ENV"
echo "IPs have been added to $CONFIG_ENV:"
echo "IP4=${ipv4_address:-Not Found}"
echo "IP6=${ipv6_address:-Not Found}"
}
edit_ip() {
local type=$1
local new_ip=$2
if [[ $type == "-4" ]]; then
sed -i '/^IP4=/d' "$CONFIG_ENV" 2>/dev/null
echo "IP4=$new_ip" >> "$CONFIG_ENV"
echo "IP4 has been updated to $new_ip."
elif [[ $type == "-6" ]]; then
sed -i '/^IP6=/d' "$CONFIG_ENV" 2>/dev/null
echo "IP6=$new_ip" >> "$CONFIG_ENV"
echo "IP6 has been updated to $new_ip."
else
echo "Invalid option. Use -4 for IPv4 or -6 for IPv6."
fi
}
ensure_config_env
case "$1" in
add)
add_ips
;;
edit)
edit_ip "$2" "$3"
;;
*)
echo "Usage: $0 {add|edit -4|-6 <new_ip>}"
exit 1
;;
esac