Skip to content

Commit 705cc03

Browse files
committed
fix(script): Enhance script security and usability
- Updated error handling for invalid environment variables - Added required checks for VNYAN_VERSION, SERVER_PASSWORD, and MAX_LOBBY_AMOUNT - Moved and renamed various paths for better organization - Extracted ZIP file in a temporary directory before unpacking it - Changed variable type for SERVER_DIR to use double quotes - Updated find command to exclude 'VNyanNet.cfg' from copying process - Added check to ensure config file exists before starting the server - Applied environment variables to config file before starting/stopping the server - Implemented trap to stop the server upon receiving SIGINT or SIGTERM signals - Monitored config file for changes and restarted the server accordingly
1 parent 02fb0ac commit 705cc03

File tree

1 file changed

+89
-34
lines changed

1 file changed

+89
-34
lines changed

entrypoint.sh

Lines changed: 89 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,118 @@
11
#!/bin/bash
2-
set -e
2+
set -euo pipefail
33

4+
# -------------------------------------------------
5+
# Paths
6+
# -------------------------------------------------
47
SERVER_DIR="/server"
5-
EXE_FILE="$SERVER_DIR/VNyanNet.exe"
6-
ZIP_FILE="$SERVER_DIR/VNyanNetServer.zip"
8+
BIN_DIR="$SERVER_DIR/bin"
9+
CONFIG_DIR="$SERVER_DIR/config"
10+
11+
EXE_FILE="$BIN_DIR/VNyanNet.exe"
12+
CONFIG_FILE="$CONFIG_DIR/VNyanNet.cfg"
713
VERSION_FILE="$SERVER_DIR/VERSION.txt"
8-
CONFIG_FILE="$SERVER_DIR/VNyanNet.cfg"
914

10-
# Determine installed version
15+
ZIP_FILE="$SERVER_DIR/VNyanNetServer.zip"
16+
TMP_DIR="$SERVER_DIR/tmp_extract"
17+
18+
# -------------------------------------------------
19+
# Required ENV checks
20+
# -------------------------------------------------
21+
: "${VNYAN_VERSION:?VNYAN_VERSION is required}"
22+
: "${SERVER_PASSWORD:?SERVER_PASSWORD is required}"
23+
: "${MAX_LOBBY_AMOUNT:?MAX_LOBBY_AMOUNT is required}"
24+
25+
mkdir -p "$BIN_DIR" "$CONFIG_DIR"
26+
27+
# -------------------------------------------------
28+
# Read current version
29+
# -------------------------------------------------
1130
CURRENT_VERSION=""
12-
if [ -f "$VERSION_FILE" ]; then
13-
CURRENT_VERSION=$(cat "$VERSION_FILE")
14-
fi
31+
[[ -f "$VERSION_FILE" ]] && CURRENT_VERSION="$(cat "$VERSION_FILE")"
32+
33+
# -------------------------------------------------
34+
# Update / install
35+
# -------------------------------------------------
36+
if [[ "$CURRENT_VERSION" != "$VNYAN_VERSION" ]] || [[ ! -f "$EXE_FILE" ]]; then
37+
echo "[VNyanNet] Updating server: ${CURRENT_VERSION:-none}$VNYAN_VERSION"
38+
39+
DOWNLOAD_URL="https://drive.nekosunevr.co.uk/public.php/dav/files/p7xjS2TpD6St7wN/VNyanNetServer-${VNYAN_VERSION}.zip"
1540

16-
# Check if we need to update/downgrade
17-
if [ "$CURRENT_VERSION" != "$VNYAN_VERSION" ] || [ ! -f "$EXE_FILE" ]; then
18-
echo "[VNyanNet] Installing/updating server from version '${CURRENT_VERSION:-none}' to '${VNYAN_VERSION}'..."
41+
wget -O "$ZIP_FILE" "$DOWNLOAD_URL"
1942

20-
wget --content-disposition -O "$ZIP_FILE" "https://drive.nekosunevr.co.uk/public.php/dav/files/p7xjS2TpD6St7wN/VNyanNetServer-${VNYAN_VERSION}.zip" \
21-
|| echo "⚠️ Could not download VNyanNetServer-${VNYAN_VERSION}.zip. Please provide manually."
43+
rm -rf "$TMP_DIR"
44+
mkdir -p "$TMP_DIR"
45+
unzip -o "$ZIP_FILE" -d "$TMP_DIR"
2246

23-
# Extract while preserving VNyanNet.cfg
24-
TMP_DIR="$SERVER_DIR/tmp_extract"
25-
mkdir -p "$TMP_DIR"
26-
unzip -o "$ZIP_FILE" -d "$TMP_DIR"
47+
echo "[VNyanNet] Installing binaries to /bin"
48+
rsync -av \
49+
--exclude='VNyanNet.cfg' \
50+
"$TMP_DIR"/ "$BIN_DIR"/
2751

28-
echo "[VNyanNet] Copying new version files (preserving VNyanNet.cfg)..."
29-
# Copy all files except VNyanNet.cfg
30-
find "$TMP_DIR" -type f ! -name "VNyanNet.cfg" -exec cp -f --parents {} "$SERVER_DIR" \;
52+
# Copy default config ONLY if missing
53+
if [[ ! -f "$CONFIG_FILE" ]] && [[ -f "$TMP_DIR/VNyanNet.cfg" ]]; then
54+
echo "[VNyanNet] Installing default config"
55+
cp "$TMP_DIR/VNyanNet.cfg" "$CONFIG_FILE"
56+
fi
3157

32-
rm -rf "$TMP_DIR" "$ZIP_FILE"
58+
rm -rf "$TMP_DIR" "$ZIP_FILE"
3359

34-
# Update version file (overwrite old version)
35-
echo "$VNYAN_VERSION" > "$VERSION_FILE"
36-
echo "[VNyanNet] Server updated to version $VNYAN_VERSION"
37-
else
38-
echo "[VNyanNet] Server already at version $CURRENT_VERSION — skipping download."
60+
echo "$VNYAN_VERSION" > "$VERSION_FILE"
3961
fi
4062

41-
# --- Start/stop functions ---
63+
# -------------------------------------------------
64+
# Ensure config exists
65+
# -------------------------------------------------
66+
if [[ ! -f "$CONFIG_FILE" ]]; then
67+
echo "❌ VNyanNet.cfg missing and no default provided"
68+
exit 1
69+
fi
70+
71+
# -------------------------------------------------
72+
# Apply ENV → config (ALWAYS)
73+
# -------------------------------------------------
74+
apply_env_to_config() {
75+
echo "[VNyanNet] Applying environment variables to config"
76+
77+
sed -i \
78+
-e "s/^ServerPassword=.*/ServerPassword=${SERVER_PASSWORD}/" \
79+
-e "s/^MaxLobbyAmount=.*/MaxLobbyAmount=${MAX_LOBBY_AMOUNT}/" \
80+
"$CONFIG_FILE"
81+
}
82+
83+
apply_env_to_config
84+
85+
# -------------------------------------------------
86+
# Start / stop
87+
# -------------------------------------------------
88+
SERVER_PID=""
89+
4290
start_server() {
91+
apply_env_to_config
4392
echo "[VNyanNet] Starting server..."
4493
xvfb-run -a wine "$EXE_FILE" &
4594
SERVER_PID=$!
4695
}
4796

4897
stop_server() {
49-
echo "[VNyanNet] Stopping server (PID $SERVER_PID)..."
50-
kill "$SERVER_PID" 2>/dev/null || true
51-
wait "$SERVER_PID" 2>/dev/null || true
98+
if [[ -n "$SERVER_PID" ]] && kill -0 "$SERVER_PID" 2>/dev/null; then
99+
echo "[VNyanNet] Stopping server (PID $SERVER_PID)"
100+
kill "$SERVER_PID" 2>/dev/null || true
101+
wait "$SERVER_PID" 2>/dev/null || true
102+
fi
52103
}
53104

105+
trap stop_server SIGINT SIGTERM
106+
107+
# -------------------------------------------------
108+
# Run
109+
# -------------------------------------------------
54110
start_server
55111

56-
# --- Monitor config file for real-time updates ---
57-
echo "[VNyanNet] Watching VNyanNet.cfg for changes..."
112+
echo "[VNyanNet] Watching config for changes..."
58113
while true; do
59-
inotifywait -e modify,attrib,close_write,move,create,delete "$CONFIG_FILE" >/dev/null 2>&1
60-
echo "[VNyanNet] Config changed — restarting server..."
114+
inotifywait -e modify,close_write,move,create,delete "$CONFIG_FILE" >/dev/null 2>&1
115+
echo "[VNyanNet] Config changed — restarting server"
61116
stop_server
62117
start_server
63118
done

0 commit comments

Comments
 (0)