From d13d786d6cc2eeb72249745bbdb448f029179297 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 28 May 2023 16:25:42 -0600 Subject: [PATCH] Validate config file --- ishare2 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/ishare2 b/ishare2 index e3231f5..2051a6a 100755 --- a/ishare2 +++ b/ishare2 @@ -93,6 +93,61 @@ function create_config() { echo -e "${RED} [!] Failed to create configuration file. Please try again.${NO_COLOR}" exit 1 fi + remove_duplicates +} + +remove_duplicates() { + local config_file="$ISHARE2_DIR/ishare2.conf" + + # Check if the configuration file exists + if [ ! -f "$config_file" ]; then + echo -e "${RED} [!] Configuration file does not exist.${NO_COLOR}" + return + fi + + # Remove duplicate lines from the configuration file + awk '!seen[$0]++' "$config_file" >"$config_file.tmp" && mv "$config_file.tmp" "$config_file" + + echo -e "${GREEN} [+] Duplicates removed from the configuration file.${NO_COLOR}" +} + +validate_conf_file() { + local config_file="$ISHARE2_DIR/ishare2.conf" + + # Check if the configuration file exists + if [ ! -f "$config_file" ]; then + echo -e "${RED} [!] Configuration file does not exist.${NO_COLOR}" + return 1 + fi + + # Array of allowed variables + local allowed_vars=("USE_ARIA2C" "CHANNEL") + + # Read the configuration file line by line and filter out invalid variables + local filtered_content="" + while IFS= read -r line; do + # Skip empty lines and comments + if [[ -z "$line" || "$line" == \#* ]]; then + continue + fi + + # Extract variable name + local var_name="${line%%=*}" + + # Check if the variable is allowed + if [[ " ${allowed_vars[@]} " =~ " $var_name " ]]; then + filtered_content+="$line"$'\n' + else + echo -e "${RED} [!] Removing invalid variable from the configuration file: $line.${NO_COLOR}" + fi + done <"$config_file" + + # Write the filtered content back to the configuration file + echo "$filtered_content" >"$config_file" + + echo -e "${GREEN} [+] Configuration file validated and updated.${NO_COLOR}" >$ISHARE2_DIR/logs.txt + source "$config_file" + return 0 } function install_aria2c() {