Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
grasscutters
GitHub Repository: grasscutters/grasscutter
Path: blob/development/scripts/install/install.sh
3156 views
1
#!/usr/bin/env bash
2
3
# Grasscutter install script for GNU/Linux
4
# Made by TurtleIdiot
5
6
# Stops the installer if any command has a non-zero exit status
7
set -e
8
9
# Checks for root
10
if [ $EUID != 0 ]; then
11
echo "Please run the installer as root!"
12
exit
13
fi
14
15
is_command() {
16
# Checks if a given command is available
17
local check_command="$1"
18
command -v "${check_command}" > /dev/null 2>&1
19
}
20
21
# IP validation
22
valid_ip() {
23
local ip=$1
24
local stat=1
25
26
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
27
OIFS=$IFS
28
IFS="."
29
ip=($ip)
30
IFS=$OIFS
31
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
32
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
33
stat=$?
34
fi
35
return $stat
36
}
37
38
# Checks for supported installer(s) (only apt-get and pacman right now, might add more in the future)
39
if is_command apt-get ; then
40
echo -e "Supported package manager found (apt-get)\n"
41
42
GC_DEPS="mongodb openjdk-17-jre"
43
INSTALLER_DEPS="wget openssl unzip git"
44
SYSTEM="deb" # Debian-based (debian, ubuntu)
45
elif is_command pacman ; then
46
echo -e "supported package manager found (pacman)\n"
47
48
GC_DEPS="jre17-openjdk"
49
INSTALLER_DEPS="curl wget openssl unzip git base-devel" # curl is still a dependency here in order to successfully build mongodb
50
SYSTEM="arch" # Arch for the elitists :P
51
else
52
echo "No supported package manager found"
53
exit
54
fi
55
56
BRANCH="stable" # Stable by default
57
# Allows choice between stable and dev branch
58
echo "Please select the branch you wish to install"
59
echo -e "!!NOTE!!: stable is the recommended branch.\nDo *NOT* use development unless you have a reason to and know what you're doing"
60
select branch in "stable" "development" ; do
61
case $branch in
62
stable )
63
BRANCH="stable"
64
break;;
65
development )
66
BRANCH="development"
67
break;;
68
esac
69
done
70
71
echo "The following packages will have to be installed in order to INSTALL grasscutter:"
72
echo -e "$INSTALLER_DEPS \n"
73
echo "The following packages will have to be installed to RUN grasscutter:"
74
echo -e "$GC_DEPS \n"
75
76
echo "Do you wish to proceed and install grasscutter?"
77
select yn in "Yes" "No" ; do
78
case $yn in
79
Yes ) break;;
80
No ) exit;;
81
esac
82
done
83
84
echo "Updating package cache..."
85
case $SYSTEM in # More concise than if
86
deb ) apt-get update -qq;;
87
arch ) pacman -Syy;;
88
esac
89
90
# Starts installing dependencies
91
echo "Installing setup dependencies..."
92
case $SYSTEM in # These are one-liners anyways
93
deb ) apt-get -qq install $INSTALLER_DEPS -y;;
94
arch ) pacman -Sq --noconfirm --needed $INSTALLER_DEPS > /dev/null;;
95
esac
96
echo "Done"
97
98
echo "Installing grasscutter dependencies..."
99
case $SYSTEM in
100
deb) apt-get -qq install $GC_DEPS -y > /dev/null;;
101
arch ) pacman -Sq --noconfirm --needed $GC_DEPS > /dev/null;;
102
esac
103
# *sighs* here we go...
104
INST_ARCH_MONGO="no"
105
if [ $SYSTEM = "arch" ]; then
106
echo -e "-=-=-=-=-=--- !! IMPORTANT !! ---=-=-=-=-=-\n"
107
echo -e " Due to licensing issues with mongodb,\n it is no longer available on the official arch repositiries."
108
echo -e " In order to install mongodb,\n it needs to be fetched from the Arch User Repository.\n"
109
echo -e " As this script is running as root,\n a temporary user will need to be created to run makepkg."
110
echo -e " The temporary user will be deleted once\n makepkg has finished.\n"
111
echo -e " This will be handled automatically.\n"
112
echo -e "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n"
113
echo -e "!!NOTE!!: Only select \"Skip\" if mongodb is already installed on this system"
114
echo "Do you want to continue?"
115
select yn in "Yes" "Skip" "No" ; do
116
case $yn in
117
Yes )
118
INST_ARCH_MONGO="yes"
119
break;;
120
No ) exit;;
121
Skip )
122
INST_ARCH_MONGO="no"
123
break;;
124
esac
125
done
126
fi
127
128
if [ $INST_ARCH_MONGO = "yes" ]; then
129
DIR=$(pwd)
130
# Make temp user
131
echo "Creating temporary user..."
132
TEMPUSER="gctempuser"
133
TEMPHOME="/home/$TEMPUSER"
134
useradd -m $TEMPUSER
135
cd $TEMPHOME
136
137
# Do the actual makepkg shenanigans
138
echo "Building mongodb... (this will take a moment)"
139
su $TEMPUSER<<EOF
140
mkdir temp
141
cd temp
142
git clone https://aur.archlinux.org/mongodb-bin.git -q
143
cd mongodb-bin
144
makepkg -s > /dev/null
145
exit
146
EOF
147
mv "$(find -name "mongodb-bin*.pkg.tar.zst" -type f)" ./mongodb-bin.pkg.tar.zst
148
cd $DIR
149
150
# Snatch the file to current working directory
151
mv "$TEMPHOME/mongodb-bin.pkg.tar.zst" ./mongodb-bin.pkg.tar.zst
152
chown root ./mongodb-bin.pkg.tar.zst
153
chgrp root ./mongodb-bin.pkg.tar.zst
154
chmod 775 ./mongodb-bin.pkg.tar.zst
155
156
echo "Installing mongodb..."
157
pacman -U mongodb-bin.pkg.tar.zst --noconfirm > /dev/null
158
rm mongodb-bin.pkg.tar.zst
159
160
echo "Starting mongodb..."
161
systemctl enable mongodb
162
systemctl start mongodb
163
164
echo "Removing temporary account..."
165
userdel -r $TEMPUSER
166
fi
167
echo "Done"
168
169
echo "Getting grasscutter..."
170
171
# Download and rename jar
172
wget -q --show-progress "https://nightly.link/Grasscutters/Grasscutter/workflows/build/$BRANCH/Grasscutter.zip"
173
echo "unzipping"
174
unzip -qq Grasscutter.zip
175
mv $(find -name "grasscutter*.jar" -type f) grasscutter.jar
176
177
# Download resources
178
echo "Downloading resources... (this will take a moment)"
179
wget -q --show-progress https://github.com/Koko-boya/Grasscutter_Resources/archive/refs/heads/main.zip -O resources.zip
180
echo "Extracting..."
181
unzip -qq resources.zip
182
mv ./Grasscutter_Resources-main/Resources ./resources
183
184
# Here we do a sparse checkout to only pull /data and /keys
185
echo "Downloading keys and data..."
186
mkdir repo
187
cd repo
188
git init -q
189
git remote add origin https://github.com/Grasscutters/Grasscutter.git
190
git fetch -q
191
git config core.sparseCheckout true
192
echo "data/" >> .git/info/sparse-checkout
193
echo "keys/" >> .git/info/sparse-checkout
194
git pull origin stable -q
195
cd ../
196
mv ./repo/data ./data
197
mv ./repo/keys ./keys
198
199
# Generate handbook/config
200
echo "Please enter language when *NEXT* prompted (press enter/return to continue to language select)"
201
read
202
java -jar grasscutter.jar -handbook
203
204
# Prompt IP address for config.json and for generating new keystore.p12 file
205
echo "Please enter the IP address that will be used to connect to the server"
206
echo "This can be a local or a public IP address"
207
echo "This IP address will be used to generate SSL certificates so it is important it is correct"
208
209
while : ; do
210
read -p "Enter IP: " SERVER_IP
211
if valid_ip $SERVER_IP; then
212
break;
213
else
214
echo "Invalid IP address. Try again."
215
fi
216
done
217
218
# Replaces "127.0.0.1" with given IP
219
sed -i "s/127.0.0.1/$SERVER_IP/g" config.json
220
221
# Generates new keystore.p12 with the server's IP address
222
# This is done to prevent a "Connection Timed Out" error from appearing
223
# after clicking to enter the door in the main menu/title screen
224
# This issue only exists when connecting to a server *other* than localhost
225
# since the default keystore.p12 has only been made for localhost
226
227
mkdir certs
228
cd certs
229
echo "Generating CA key and certificate pair..."
230
openssl req -x509 -nodes -days 25202 -newkey rsa:2048 -subj "/C=GB/ST=Essex/L=London/O=Grasscutters/OU=Grasscutters/CN=$SERVER_IP" -keyout CAkey.key -out CAcert.crt
231
echo "Generating SSL key and certificate pair..."
232
233
openssl genpkey -out ssl.key -algorithm rsa
234
235
# Creates a conf file in order to generate a csr
236
cat > csr.conf <<EOF
237
[ req ]
238
default_bits = 2048
239
prompt = no
240
default_md = sha256
241
req_extensions = req_ext
242
distinguished_name = dn
243
244
[ dn ]
245
C = GB
246
ST = Essex
247
L = London
248
O = Grasscutters
249
OU = Grasscutters
250
CN = $SERVER_IP
251
252
[ req_ext ]
253
subjectAltName = @alt_names
254
255
[ alt_names ]
256
IP.1 = $SERVER_IP
257
EOF
258
259
# Creates csr using key and conf
260
openssl req -new -key ssl.key -out ssl.csr -config csr.conf
261
262
# Creates conf to finalise creation of certificate
263
cat > cert.conf <<EOF
264
265
authorityKeyIdentifier=keyid,issuer
266
basicConstraints=CA:FALSE
267
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, keyAgreement, dataEncipherment
268
subjectAltName = @alt_names
269
270
[alt_names]
271
IP.1 = $SERVER_IP
272
273
EOF
274
275
# Creates ssl cert
276
openssl x509 -req -in ssl.csr -CA CAcert.crt -CAkey CAkey.key -CAcreateserial -out ssl.crt -days 25202 -sha256 -extfile cert.conf
277
278
echo "Generating keystore.p12 from key and certificate..."
279
openssl pkcs12 -export -out keystore.p12 -inkey ssl.key -in ssl.crt -certfile CAcert.crt -passout pass:123456
280
281
cd ../
282
mv ./certs/keystore.p12 ./keystore.p12
283
echo "Done"
284
285
echo -e "Asking Noelle to clean up...\n"
286
rm -rf Grasscutter.zip resources.zip ./certs ./Grasscutter_Resources-main ./repo
287
echo -e "All done!\n"
288
echo -e "You can now uninstall the following packages if you wish:\n$INSTALLER_DEPS"
289
echo -e "-=-=-=-=-=--- !! IMPORTANT !! ---=-=-=-=-=-\n"
290
echo "Please make sure that ports 443 and 22102 are OPEN (both tcp and udp)"
291
echo -e "In order to run the server, run the following command:\nsudo java -jar grasscutter.jar"
292
echo "You must run it using sudo as port 443 is a privileged port"
293
echo "To play, use the IP you provided earlier ($SERVER_IP) via GrassClipper or Fiddler"
294
295
exit
296
297