-
Notifications
You must be signed in to change notification settings - Fork 5
/
install.sh
395 lines (339 loc) · 12.4 KB
/
install.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/bin/bash
# Step 0: Set variables
WIREREST_ARCHIVE_URL="https://github.com/FokiDoki/WireRest/releases/download/0.7-BETA/wirerest-0.7-archive.tar"
JAVA_TARGET="21"
JAVA_DEFAULT_FOLDER="/usr/lib/jvm"
DEFAULT_PORT=8081
HEALTHCHECK_MAX_RETRIES=30
HEALTHCHECK_RETRY_INTERVAL_SEC=2
# Set color variables
RED='\e[31m'
GREEN='\e[32m'
YELLOW='\e[33m'
NC='\e[0m' # No Color
source /etc/os-release
OS="${ID}"
# Step 1: Check if the user is root
if [ "$(id -u)" -ne 0 ]; then
echo -e "${RED}Error: Please run the script as root.${NC}"
exit 1
fi
# Step 2: Welcome message
echo -e "${GREEN}Installing WireRest${NC}"
# Function to check package installation
check_package() {
local PACKAGE="$1"
echo -e -n "Checking that $PACKAGE is installed... "
if command -v "$PACKAGE" &> /dev/null; then
echo -e "${GREEN}OK${NC}"
else
echo -e "${RED}FAILED${NC}"
echo -e "$PACKAGE not found, please install $PACKAGE package"
exit 1
fi
}
function ask_yes() {
while true; do
read -p "$1 (Y/N): " response
case $response in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
* ) echo "Please, enter Y(es) or N(no)";;
esac
done
}
# Step 3: Check for WireGuard package
check_package "/usr/bin/wg"
# Step 4: Check for WireGuard-tools package
check_package "/usr/bin/wg-quick"
# Step 4.1 Check java
find_java_by_version() {
local dir="$1"
local target_version="$2"
for file in "$dir"/*; do
if [ -x "$file/bin/java" ]; then
local java_version=$("$file/bin/java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
if [[ "$java_version" == "$target_version"* ]]; then
echo -e "Java $target_version ${GREEN}found${NC} in $file"
JAVA_DIRECTORY="$file"
break
fi
fi
done
}
is_java_binary() {
local file="$1"
# Check if the file exists
if [ -e "$file" ]; then
# Check if the file is executable
if [ -x "$file" ]; then
# Check if the file is a Java binary using java --version
if "$file" --version &> /dev/null; then
return 0 # The file is the Java binary
else
return 1 # The file is not the Java binary
fi
else
return 1 # The file is not executable
fi
else
return 1 # The file does not exist
fi
}
# Function to display a numbered list of directories
display_numbered_list() {
local list=("$@")
echo -e "Choose a directory with Java ${JAVA_TARGET} by entering its number:"
echo -e "0. There is no Java ${JAVA_TARGET}, install it"
for i in "${!list[@]}"; do
echo -e "$((i+1)). ${list[i]}"
done
}
install_java_21() {
# Update the package manager
echo -e "Downloading JDK.."
if [[ ${OS} == 'ubuntu' ]] || [[ ${OS} == 'debian' ]]; then
wget -q https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.deb
if [[ ${OS} == 'debian' ]] && [[ ${VERSION_ID} -gt 10 ]]; then
if ! grep -rqs "^deb .* buster-backports" /etc/apt/; then
echo "deb http://deb.debian.org/debian buster-backports main" >/etc/apt/sources.list.d/backports.list
apt-get update
fi
else
apt-get update
fi
# Install JDK with error handling
if sudo dpkg -i jdk-21_linux-x64_bin.deb; then
echo -e "JDK installation ${GREEN}successful${NC}"
else
# If an error occurs during JDK installation, fix broken dependencies
echo -e "Error installing JDK. Fixing broken dependencies..."
sudo apt --fix-broken install
# Retry JDK installation
echo -e "Retrying JDK installation..."
sudo dpkg -i jdk-21_linux-x64_bin.deb
# Check the success of the installation
if [ $? -eq 0 ]; then
echo -e "JDK installation ${GREEN}successful${NC} after fixing broken dependencies"
else
echo -e "${RED}Failed${NC} to install JDK even after fixing broken dependencies. Please check the error messages"
exit 1
fi
fi
rm jdk-21_linux-x64_bin.deb
elif [[ ${OS} == 'fedora' ]] || [[ ${OS} == 'centos' ]]; then
wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.rpm
sudo rpm -i jdk-21_linux-x64_bin.rpm
else
echo "Your Linux distribution does not support automatic installation of Java. Please set Java-$JAVA_TARGET and rerun the script"
exit 1
fi
}
# Function to prompt user for the desired directory
prompt_user_for_choice() {
local list=("$@")
read -p "Enter the number of the desired directory: " user_choice
if [[ "$user_choice" == 0 ]]; then
install_java_21
check_directory_for_java_version "${JAVA_DEFAULT_FOLDER}" "$JAVA_TARGET"
elif [[ "$user_choice" =~ ^[1-9][0-9]*$ && "$user_choice" -le "${#list[@]}" ]]; then
JAVA_DIRECTORY="${list[user_choice-1]}"
else
echo -e "${RED}Invalid choice.${NC} Exiting"
exit 1
fi
}
# Check if a directory exists and contains Java with a specified version
check_directory_for_java_version() {
local dir="$1"
local target_version="$2"
if [ -d "$dir" ]; then
find_java_by_version "$dir" "$target_version"
if [ -z "$JAVA_DIRECTORY" ]; then
local sub_directories=("$dir"/*)
if [ ${#sub_directories[@]} -eq 0 ]; then
echo -e "${RED}No directories found${NC} in $dir. Exiting"
exit 1
fi
display_numbered_list "${sub_directories[@]}"
prompt_user_for_choice "${sub_directories[@]}"
fi
else
echo -e "${YELLOW}Directory $dir does not exist.${NC} Looks like Java not found"
# Check user response
if ask_yes "Do you want to install Java 21?"; then
# Call the install_java_21 function
install_java_21
check_directory_for_java_version "${JAVA_DEFAULT_FOLDER}" "$JAVA_TARGET"
else
echo -e "Can't install WireRest without java. Exiting"
exit 0
fi
fi
}
# Check /usr/lib/jvm/ for Java with the specified version
check_directory_for_java_version "${JAVA_DEFAULT_FOLDER}" "$JAVA_TARGET"
# If Java not found, prompt user for a directory
if [ -z "$JAVA_DIRECTORY" ]; then
read -p "Enter the path to the Java home directory: " JAVA_DIRECTORY
if is_java_binary "$JAVA_DIRECTORY/bin/java"; then
echo -e "Java binary ${GREEN}found${NC}!"
else
echo -e "${RED}Java binary not found${NC} in $JAVA_DIRECTORY"
exit 1
fi
fi
echo -e "Selected directory: $JAVA_DIRECTORY"
# Step 5: Choose WireGuard interface
interfaces=$(wg | grep "interface" | awk '{print $2}')
# Check if there are active interfaces
if [ -z "$interfaces" ]; then
echo -e "${RED}Error: No active interfaces found.${NC} Please activate at least one interface (config)"
exit 1
elif [ $(wc -w <<< "$interfaces") -eq 1 ]; then
# If only one interface is available, choose it automatically
INTERFACE="$interfaces"
echo -e "Only one interface found: ${GREEN}$INTERFACE${NC}. Selected ${GREEN}automatically${NC}"
else
# If there are multiple interfaces, let the user choose
PS3="Select an interface: "
select interface in $interfaces; do
if [ -n "$interface" ]; then
echo -e "Selected interface: ${GREEN}$interface${NC}"
INTERFACE="$interface"
break
else
echo -e "${RED}Please choose a valid interface number.${NC}"
fi
done
fi
cleanup() {
rm -rf "${TMP_DIR}"
}
if ask_yes "All checks passed. Start installing?"; then
echo -e "Starting the download..."
else
echo -e "Installing canceled. Exiting"
exit 0
fi
# Step 6: Download WireRest files
echo -e "Downloading WireRest files..."
wget -q -O wirerest.tar "$WIREREST_ARCHIVE_URL"
TMP_DIR="/tmp/wirerest-install"
# Step 7: Extract the archive
mkdir "${TMP_DIR}"
trap cleanup EXIT
trap cleanup INT
tar -xf wirerest.tar -C "${TMP_DIR}"
cd "${TMP_DIR}" || exit 1
# Step 8: Copy .jar file to /usr/local/bin
cp wirerest-*.jar /usr/local/bin/wirerest.jar
# Step 9: Generate and display security token
WIREREST_TOKEN=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 64)
echo -e "Your WireRest ${GREEN}access token${NC}: $WIREREST_TOKEN"
echo -e "This token provides full access to your VPN. ${YELLOW}Share carefully.${NC}"
# Step 10: Ask for API port
read -e -p "Enter the API port (default is $DEFAULT_PORT): " -i "$DEFAULT_PORT" user_input
# Validate and update the API port
while ! [[ "$user_input" =~ ^[0-9]+$ && "$user_input" -ge 1 && "$user_input" -le 65535 ]]; do
echo -e "${RED}Invalid port.${NC} Please enter a valid port number"
read -p "Enter the API port: " -i "$DEFAULT_PORT" user_input
done
WIREREST_PORT=$user_input
# Step 11: Create /etc/default/wirerest-INTERFACE file
cp enviroment "/etc/default/wirerest-$INTERFACE"
sed -i "s/^ACCESS_TOKEN=.*/ACCESS_TOKEN=${WIREREST_TOKEN}/" "/etc/default/wirerest-$INTERFACE"
sed -i "s/^PORT=.*/PORT=${WIREREST_PORT}/" "/etc/default/wirerest-$INTERFACE"
# Step 12: Replace JAVA_HOME_DIR in wirerest.service
sed -i "s|JAVA_HOME_DIR|$JAVA_DIRECTORY|" wirerest.service
# Step 13: Copy wirerest.service to /lib/systemd/system
cp wirerest.service /lib/systemd/system/wirerest@.service
# Step 13.1 CleanUp
rm -rf "${TMP_DIR}"
# Step 14: Reload systemd
systemctl daemon-reload
# Step 15: Enable and start WireRest service for the selected interface
SERVICE_NAME="wirerest@$INTERFACE"
# Check if the service is running
if systemctl is-active --quiet "$SERVICE_NAME"; then
# Service is running
if ask_yes "The service $SERVICE_NAME is currently running. Are you sure you want to recreate it?"; then
# Stop the service
systemctl stop "$SERVICE_NAME"
# Remove from autostart
systemctl disable "$SERVICE_NAME"
# Recreate the service (replace this with your actual command)
systemctl start "$SERVICE_NAME"
# Add back to autostart
systemctl enable "$SERVICE_NAME"
echo -e "Service $SERVICE_NAME recreated and added back to autostart ${GREEN}successfully${NC}"
else
echo "Recreation canceled. Exiting"
exit 0
fi
else
# Service is not running
systemctl enable --now "$SERVICE_NAME"
fi
# Step 16: Checking service running
is_service_running() {
local response_code
response_code=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:$WIREREST_PORT/webjars/swagger-ui/index.html)
if [ "$response_code" -eq 200 ]; then
return 0
else
return 1
fi
}
get_external_ip() {
# Method 1
if command -v dig >/dev/null 2>&1; then
external_ip=$(dig @resolver4.opendns.com myip.opendns.com +short -4)
is_valid_ipv4 "$external_ip" && return
else
echo "Warning: dig is not installed. " >&2
fi
# Method 2
if command -v curl >/dev/null 2>&1; then
external_ip=$(curl -s http://whatismyip.akamai.com/)
is_valid_ipv4 "$external_ip" && return
else
echo "Warning: curl is not installed. Can't detect your external ip." >&2
return
fi
# Method 3
external_ip=$(curl -s http://l2.io/ip/)
is_valid_ipv4 "$external_ip" && return
# Method 4
external_ip=$(curl -s http://icanhazip.com)
is_valid_ipv4 "$external_ip" && return
echo "Warning: Unable to determine external IP address." >&2
}
is_valid_ipv4() {
# Check if the given string is a valid IPv4 address
local ip=$1
[[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]
}
# Function to wait for the service to start
wait_for_service() {
retries=0
until is_service_running || [ $retries -eq $HEALTHCHECK_MAX_RETRIES ]; do
sleep $HEALTHCHECK_RETRY_INTERVAL_SEC
retries=$((retries + 1))
echo -n "."
done
echo
if is_service_running; then
get_external_ip
if is_valid_ipv4 "$external_ip"; then
echo -e "${GREEN}Success!${NC} Service $SERVICE_NAME is running. Now you can visit http://${external_ip}:${WIREREST_PORT}/swagger-ui and see available methods"
else
echo -e "${GREEN}Success!${NC} Service $SERVICE_NAME is running. Now you can visit http://YOUR_SERVER_IP_ADDRESS:${WIREREST_PORT}/swagger-ui and see available methods"
fi
else
echo -e "${RED}Error: Service $SERVICE_NAME did not start within the expected time.${NC}"
exit 1
fi
}
echo -n "Waiting for the service to start."
wait_for_service