-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon_functions.sh
executable file
·129 lines (110 loc) · 3.55 KB
/
common_functions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to detect the platform (Linux, WSL, or Windows)
detect_platform() {
if grep -qEi "(Microsoft|WSL)" /proc/version &> /dev/null; then
echo "wsl"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo "linux"
elif [[ "$OSTYPE" == "cygwin" || "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
echo "windows"
else
echo "unknown"
fi
}
# Function to check if an IP address is valid and not in restricted ranges
is_valid_ip() {
local ip="$1"
# Check if IP is empty
if [ -z "$ip" ]; then
return 1
fi
# Check if IP is in restricted ranges
if [[ $ip =~ ^127\. ]] || [[ $ip =~ ^172\. ]] || [[ $ip == "0.0.0.0" ]] || [[ $ip == "localhost" ]]; then
return 1
fi
# Basic IP format validation (xxx.xxx.xxx.xxx where xxx is 1-3 digits)
if [[ ! $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
return 1
fi
# Validate each octet
local IFS='.'
read -ra ADDR <<< "$ip"
for i in "${ADDR[@]}"; do
if [ $i -lt 0 ] || [ $i -gt 255 ]; then
return 1
fi
done
return 0
}
# Function to save IP to file
save_ip() {
local ip="$1"
echo "$ip" > local_ip.txt
return $?
}
# Function to retrieve saved IP from file
retrieve_saved_ip() {
if [ ! -f local_ip.txt ]; then
return 1
fi
local ip=$(cat local_ip.txt)
if [ -z "$ip" ] || ! is_valid_ip "$ip"; then
return 1
fi
echo "$ip"
}
# Function to get the local IP address (now prompts user for manual input)
get_local_ip() {
manual_ip_prompt
}
# Function to prompt the user for manual IP input and retry until valid
manual_ip_prompt() {
echo -e "${GREEN}Local IP Configuration:${NC}"
echo -e "${GREEN}Valid IP Format Examples:${NC}"
echo -e "${GREEN}1. 192.168.1.xxx${NC}"
echo -e "${GREEN}2. 10.0.0.xxx${NC}"
echo -e "${GREEN}Note: IP cannot be from ranges 127.x.x.x, 172.x.x.x, 0.0.0.0, or localhost${NC}"
while true; do
read -p "$(echo -e "${YELLOW}Please enter your machine's IP address: ${NC}")" manual_ip
if is_valid_ip "$manual_ip"; then
save_ip "$manual_ip"
break
else
echo -e "${RED}Error: Invalid IP entered. Please check the format examples above and try again.${NC}"
fi
done
}
# Function to get and store Real-Debrid API key
get_real_debrid_api_key() {
local api_key_file="./riven/real_debrid_api_key.txt"
# First try to read from existing files
if [ -f "$api_key_file" ]; then
local api_key=$(cat "$api_key_file")
if [ ! -z "$api_key" ]; then
echo "$api_key"
return 0
fi
fi
# If not found or empty, prompt for it
echo -e "${GREEN}Real-Debrid API Key Configuration:${NC}"
echo -e "${GREEN}A Real-Debrid API key is required for downloading content${NC}"
echo -e "${GREEN}You can find your API key at: https://real-debrid.com/apitoken${NC}"
local api_key=""
while [[ -z "$api_key" ]]; do
read -p "$(echo -e "${YELLOW}Enter your Real-Debrid API Key: ${NC}")" api_key
if [[ -z "$api_key" ]]; then
echo -e "${RED}Error: Real-Debrid API Key cannot be empty.${NC}"
fi
done
# Store the API key
if ! echo "$api_key" > "$api_key_file"; then
echo -e "${YELLOW}Warning: Failed to store API key for future use.${NC}" >&2
fi
echo "$api_key"
return 0
}