forked from kus/cs2-modded-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
319 lines (276 loc) · 11.4 KB
/
start.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
#!/usr/bin/env bash
# This is used for Google Cloud
# It skips downloading the mod and overwriting addons folder
# Useful for testing and modifying files on the server
# As root (sudo su)
# cd / && curl -s -H "Cache-Control: no-cache" -o "start.sh" "https://raw.githubusercontent.com/kus/cs2-modded-server/master/start.sh" && chmod +x start.sh && bash start.sh
METADATA_URL="${METADATA_URL:-http://metadata.google.internal/computeMetadata/v1/instance/attributes}"
get_metadata () {
if [ -z "$1" ]
then
local result=""
else
local result=$(curl -s "$METADATA_URL/$1?alt=text" -H "Metadata-Flavor: Google")
if [[ $result == *"<!DOCTYPE html>"* ]]; then
result=""
fi
fi
echo $result
}
# Get meta data from GCP and set environment variables
META_RCON_PASSWORD=$(get_metadata RCON_PASSWORD)
META_API_KEY=$(get_metadata API_KEY)
META_MOD_BRANCH=$(get_metadata MOD_BRANCH)
META_PORT=$(get_metadata PORT)
META_TICKRATE=$(get_metadata TICKRATE)
META_MAXPLAYERS=$(get_metadata MAXPLAYERS)
META_LAN=$(get_metadata LAN)
META_EXEC=$(get_metadata EXEC)
export RCON_PASSWORD="${META_RCON_PASSWORD:-changeme}"
export API_KEY="${META_API_KEY:-changeme}"
export STEAM_ACCOUNT="${STEAM_ACCOUNT:-$(get_metadata STEAM_ACCOUNT)}"
export MOD_BRANCH="${META_MOD_BRANCH:-master}"
export SERVER_PASSWORD="${SERVER_PASSWORD:-$(get_metadata SERVER_PASSWORD)}"
export PORT="${META_PORT:-27015}"
export TICKRATE="${META_TICKRATE:-128}"
export MAXPLAYERS="${META_MAXPLAYERS:-32}"
export LAN="${META_LAN:-0}"
export EXEC="${META_EXEC:-on_boot.cfg}"
export DUCK_DOMAIN="${DUCK_DOMAIN:-$(get_metadata DUCK_DOMAIN)}"
export DUCK_TOKEN="${DUCK_TOKEN:-$(get_metadata DUCK_TOKEN)}"
export CUSTOM_FOLDER="${CUSTOM_FOLDER:-$(get_metadata CUSTOM_FOLDER)}"
cd /
# Variables
user="steam"
BRANCH="master"
# Check if MOD_BRANCH is set and not empty
if [ -n "$MOD_BRANCH" ]; then
BRANCH="$MOD_BRANCH"
fi
CUSTOM_FILES="${CUSTOM_FOLDER:-custom_files}"
# 32 or 64 bit Operating System
# If BITS environment variable is not set, try determine it
if [ -z "$BITS" ]; then
# Determine the operating system architecture
architecture=$(uname -m)
# Set OS_BITS based on the architecture
if [[ $architecture == *"64"* ]]; then
export BITS=64
elif [[ $architecture == *"i386"* ]] || [[ $architecture == *"i686"* ]]; then
export BITS=32
else
echo "Unknown architecture: $architecture"
exit 1
fi
fi
if [[ -z $IP ]]; then
IP_ARGS=""
else
IP_ARGS="-ip ${IP}"
fi
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
. /etc/os-release
DISTRO_OS=$NAME
DISTRO_VERSION=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
# linuxbase.org
DISTRO_OS=$(lsb_release -si)
DISTRO_VERSION=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
. /etc/lsb-release
DISTRO_OS=$DISTRIB_ID
DISTRO_VERSION=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
DISTRO_OS=Debian
DISTRO_VERSION=$(cat /etc/debian_version)
else
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
DISTRO_OS=$(uname -s)
DISTRO_VERSION=$(uname -r)
fi
echo "Starting on $DISTRO_OS: $DISTRO_VERSION..."
# Get the free space on the root filesystem in GB
FREE_SPACE=$(df / --output=avail -BG | tail -n 1 | tr -d 'G')
echo "With $FREE_SPACE Gb free space..."
# Check distrib
if ! command -v apt-get &> /dev/null; then
echo "ERROR: OS distribution not supported (apt-get not available). $DISTRO_OS: $DISTRO_VERSION"
exit 1
fi
# Check root
if [ "$EUID" -ne 0 ]; then
echo "ERROR: Please run this script as root..."
exit 1
fi
echo "Updating Operating System..."
apt-get update -y -q && apt-get upgrade -y -q >/dev/null
if [ "$?" -ne "0" ]; then
echo "ERROR: Updating Operating System..."
exit 1
fi
dpkg --configure -a >/dev/null
echo "Adding i386 architecture..."
dpkg --add-architecture i386 >/dev/null
if [ "$?" -ne "0" ]; then
echo "ERROR: Cannot add i386 architecture..."
exit 1
fi
echo "Installing required packages for $DISTRO_OS: $DISTRO_VERSION..."
apt-get update -y -q >/dev/null
if [ "${DISTRO_OS}" == "Ubuntu" ]; then
if [ "${DISTRO_VERSION}" == "16.04" ]; then
apt-get install -y -q dnsutils curl wget screen nano file tar bzip2 gzip unzip hostname bsdmainutils python3 util-linux xz-utils ca-certificates binutils bc jq tmux netcat lib32stdc++6 libsdl2-2.0-0:i386 lib32gcc1 steamcmd >/dev/null
elif [ "${DISTRO_VERSION}" == "18.04" ]; then
apt-get install -y -q dnsutils curl wget screen nano file tar bzip2 gzip unzip hostname bsdmainutils python3 util-linux xz-utils ca-certificates binutils bc jq tmux netcat lib32stdc++6 libsdl2-2.0-0:i386 distro-info lib32gcc1 steamcmd >/dev/null
elif [ "${DISTRO_VERSION}" == "20.04" ]; then
apt-get install -y -q dnsutils curl wget screen nano file tar bzip2 gzip unzip hostname bsdmainutils python3 util-linux xz-utils ca-certificates binutils bc jq tmux netcat lib32stdc++6 libsdl2-2.0-0:i386 distro-info lib32gcc1 steamcmd >/dev/null
elif [ "${DISTRO_VERSION}" == "22.04" ]; then
apt-get install -y -q dnsutils curl wget screen nano file tar bzip2 gzip unzip hostname bsdmainutils python3 util-linux xz-utils ca-certificates binutils bc jq tmux netcat lib32stdc++6 libsdl2-2.0-0:i386 distro-info lib32gcc-s1 steamcmd >/dev/null
else
echo "$DISTRO_OS $DISTRO_VERSION not officially supported; using Ubuntu 22.04 config"
apt-get install -y -q dnsutils curl wget screen nano file tar bzip2 gzip unzip hostname bsdmainutils python3 util-linux xz-utils ca-certificates binutils bc jq tmux netcat lib32stdc++6 libsdl2-2.0-0:i386 distro-info lib32gcc-s1 steamcmd >/dev/null
fi
elif [[ $DISTRO_OS == Debian* ]]; then
if [ "${DISTRO_VERSION}" == "10" ]; then
apt-get install -y dnsutils curl wget screen nano file tar bzip2 gzip unzip hostname bsdmainutils python3 util-linux xz-utils ca-certificates binutils bc jq tmux netcat-traditional lib32stdc++6 libsdl2-2.0-0:i386 distro-info lib32gcc1 >/dev/null
elif [ "${DISTRO_VERSION}" == "11" ]; then
apt-get install -y dnsutils curl wget screen nano file tar bzip2 gzip unzip hostname bsdmainutils python3 util-linux xz-utils ca-certificates binutils bc jq tmux netcat-traditional lib32stdc++6 libsdl2-2.0-0:i386 distro-info lib32gcc-s1 >/dev/null
elif [ "${DISTRO_VERSION}" == "12" ]; then
apt-get install -y dnsutils curl wget screen nano file tar bzip2 gzip unzip hostname bsdmainutils python3 util-linux xz-utils ca-certificates binutils bc jq tmux netcat-traditional lib32stdc++6 libsdl2-2.0-0:i386 distro-info lib32gcc-s1 >/dev/null
elif [ "${DISTRO_VERSION}" == "13" ]; then
apt-get install -y dnsutils curl wget screen nano file tar bzip2 gzip unzip hostname bsdmainutils python3 util-linux xz-utils ca-certificates binutils bc jq tmux netcat-traditional lib32stdc++6 libsdl2-2.0-0:i386 distro-info lib32gcc-s1 >/dev/null
else
echo "$DISTRO_OS: $DISTRO_VERSION not officially supported; using Debian 13 config"
apt-get install -y dnsutils curl wget screen nano file tar bzip2 gzip unzip hostname bsdmainutils python3 util-linux xz-utils ca-certificates binutils bc jq tmux netcat-traditional lib32stdc++6 libsdl2-2.0-0:i386 distro-info lib32gcc-s1 >/dev/null
fi
else
echo "ERROR: OS distribution not supported. $DISTRO_OS: $DISTRO_VERSION"
exit 1
fi
# Download latest stop script
curl -s -H "Cache-Control: no-cache" -o "stop.sh" "https://raw.githubusercontent.com/kus/cs2-modded-server/${BRANCH}/stop.sh" && chmod +x stop.sh
PUBLIC_IP=$(curl -4 ifconfig.me)
if [ -z "$PUBLIC_IP" ]; then
echo "ERROR: Cannot retrieve your public IP address..."
exit 1
fi
# Update DuckDNS with our current IP
if [ ! -z "$DUCK_TOKEN" ]; then
echo url="http://www.duckdns.org/update?domains=$DUCK_DOMAIN&token=$DUCK_TOKEN&ip=$PUBLIC_IP" | curl -k -o /duck.log -K -
fi
echo "Checking $user user exists..."
getent passwd ${user} >/dev/null 2&>1
if [ "$?" -ne "0" ]; then
echo "Adding $user user..."
addgroup ${user} && \
adduser --system --home /home/${user} --shell /bin/false --ingroup ${user} ${user} && \
usermod -a -G tty ${user} && \
mkdir -m 777 /home/${user}/cs2 && \
chown -R ${user}:${user} /home/${user}/cs2
if [ "$?" -ne "0" ]; then
echo "ERROR: Cannot add user $user..."
exit 1
fi
fi
echo "Checking steamcmd exists..."
if [ ! -d "/steamcmd" ]; then
mkdir /steamcmd && cd /steamcmd
wget https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz
tar -xvzf steamcmd_linux.tar.gz
mkdir -p /root/.steam/sdk32/
ln -s /steamcmd/linux32/steamclient.so /root/.steam/sdk32/
mkdir -p /root/.steam/sdk64/
ln -s /steamcmd/linux64/steamclient.so /root/.steam/sdk64/
fi
chown -R ${user}:${user} /steamcmd
# /root/.steam/sdk64/steamclient.so
echo "Downloading any updates for CS2..."
# https://developer.valvesoftware.com/wiki/Command_line_options
sudo -u $user /steamcmd/steamcmd.sh \
+api_logging 1 1 \
+@sSteamCmdForcePlatformType linux \
+@sSteamCmdForcePlatformBitness $BITS \
+force_install_dir /home/${user}/cs2 \
+login anonymous \
+app_update 730 \
+quit
cd /home/${user}
mkdir -p /root/.steam/sdk32/
ln -s /steamcmd/linux32/steamclient.so /root/.steam/sdk32/
mkdir -p /root/.steam/sdk64/
ln -s /steamcmd/linux64/steamclient.so /root/.steam/sdk64/
mkdir -p /home/${user}/.steam/sdk32/
ln -s /steamcmd/linux32/steamclient.so /home/${user}/.steam/sdk32/
mkdir -p /home/${user}/.steam/sdk64/
ln -s /steamcmd/linux64/steamclient.so /home/${user}/.steam/sdk64/
if [ "${DISTRO_OS}" == "Ubuntu" ]; then
if [ "${DISTRO_VERSION}" == "22.04" ]; then
# https://forums.alliedmods.net/showthread.php?t=336183
rm /home/${user}/cs2/bin/libgcc_s.so.1
fi
fi
chown -R ${user}:${user} /home/${user}/cs2
cd /home/${user}/cs2
# Define the file name
FILE="game/csgo/gameinfo.gi"
# Define the pattern to search for and the line to add
PATTERN="Game_LowViolence[[:space:]]*csgo_lv // Perfect World content override"
LINE_TO_ADD="\t\t\tGame\tcsgo/addons/metamod"
# Use a regular expression to ignore spaces when checking if the line exists
REGEX_TO_CHECK="^[[:space:]]*Game[[:space:]]*csgo/addons/metamod"
# Check if the line already exists in the file, ignoring spaces
if grep -qE "$REGEX_TO_CHECK" "$FILE"; then
echo "$FILE already patched for Metamod."
else
# If the line isn't there, use awk to add it after the pattern
awk -v pattern="$PATTERN" -v lineToAdd="$LINE_TO_ADD" '{
print $0;
if ($0 ~ pattern) {
print lineToAdd;
}
}' "$FILE" > tmp_file && mv tmp_file "$FILE"
echo "$FILE successfully patched for Metamod."
fi
echo "Starting server on $PUBLIC_IP:$PORT"
# https://developer.valvesoftware.com/wiki/Counter-Strike_2/Dedicated_Servers#Command-Line_Parameters
echo ./game/bin/linuxsteamrt64/cs2 \
-dedicated \
-console \
-usercon \
-autoupdate \
-tickrate $TICKRATE \
$IP_ARGS \
-port $PORT \
+map de_dust2 \
-maxplayers $MAXPLAYERS \
-authkey $API_KEY \
+sv_setsteamaccount $STEAM_ACCOUNT \
+game_type 0 \
+game_mode 0 \
+mapgroup mg_active \
+sv_lan $LAN \
+sv_password $SERVER_PASSWORD \
+rcon_password $RCON_PASSWORD \
+exec $EXEC
sudo -u $user ./game/bin/linuxsteamrt64/cs2 \
-dedicated \
-console \
-usercon \
-autoupdate \
-tickrate $TICKRATE \
$IP_ARGS \
-port $PORT \
+map de_dust2 \
-maxplayers $MAXPLAYERS \
-authkey $API_KEY \
+sv_setsteamaccount $STEAM_ACCOUNT \
+game_type 0 \
+game_mode 0 \
+mapgroup mg_active \
+sv_lan $LAN \
+sv_password $SERVER_PASSWORD \
+rcon_password $RCON_PASSWORD \
+exec $EXEC