Path: blob/development/scripts/install/install.sh
3156 views
#!/usr/bin/env bash12# Grasscutter install script for GNU/Linux3# Made by TurtleIdiot45# Stops the installer if any command has a non-zero exit status6set -e78# Checks for root9if [ $EUID != 0 ]; then10echo "Please run the installer as root!"11exit12fi1314is_command() {15# Checks if a given command is available16local check_command="$1"17command -v "${check_command}" > /dev/null 2>&118}1920# IP validation21valid_ip() {22local ip=$123local stat=12425if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then26OIFS=$IFS27IFS="."28ip=($ip)29IFS=$OIFS30[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \31&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]32stat=$?33fi34return $stat35}3637# Checks for supported installer(s) (only apt-get and pacman right now, might add more in the future)38if is_command apt-get ; then39echo -e "Supported package manager found (apt-get)\n"4041GC_DEPS="mongodb openjdk-17-jre"42INSTALLER_DEPS="wget openssl unzip git"43SYSTEM="deb" # Debian-based (debian, ubuntu)44elif is_command pacman ; then45echo -e "supported package manager found (pacman)\n"4647GC_DEPS="jre17-openjdk"48INSTALLER_DEPS="curl wget openssl unzip git base-devel" # curl is still a dependency here in order to successfully build mongodb49SYSTEM="arch" # Arch for the elitists :P50else51echo "No supported package manager found"52exit53fi5455BRANCH="stable" # Stable by default56# Allows choice between stable and dev branch57echo "Please select the branch you wish to install"58echo -e "!!NOTE!!: stable is the recommended branch.\nDo *NOT* use development unless you have a reason to and know what you're doing"59select branch in "stable" "development" ; do60case $branch in61stable )62BRANCH="stable"63break;;64development )65BRANCH="development"66break;;67esac68done6970echo "The following packages will have to be installed in order to INSTALL grasscutter:"71echo -e "$INSTALLER_DEPS \n"72echo "The following packages will have to be installed to RUN grasscutter:"73echo -e "$GC_DEPS \n"7475echo "Do you wish to proceed and install grasscutter?"76select yn in "Yes" "No" ; do77case $yn in78Yes ) break;;79No ) exit;;80esac81done8283echo "Updating package cache..."84case $SYSTEM in # More concise than if85deb ) apt-get update -qq;;86arch ) pacman -Syy;;87esac8889# Starts installing dependencies90echo "Installing setup dependencies..."91case $SYSTEM in # These are one-liners anyways92deb ) apt-get -qq install $INSTALLER_DEPS -y;;93arch ) pacman -Sq --noconfirm --needed $INSTALLER_DEPS > /dev/null;;94esac95echo "Done"9697echo "Installing grasscutter dependencies..."98case $SYSTEM in99deb) apt-get -qq install $GC_DEPS -y > /dev/null;;100arch ) pacman -Sq --noconfirm --needed $GC_DEPS > /dev/null;;101esac102# *sighs* here we go...103INST_ARCH_MONGO="no"104if [ $SYSTEM = "arch" ]; then105echo -e "-=-=-=-=-=--- !! IMPORTANT !! ---=-=-=-=-=-\n"106echo -e " Due to licensing issues with mongodb,\n it is no longer available on the official arch repositiries."107echo -e " In order to install mongodb,\n it needs to be fetched from the Arch User Repository.\n"108echo -e " As this script is running as root,\n a temporary user will need to be created to run makepkg."109echo -e " The temporary user will be deleted once\n makepkg has finished.\n"110echo -e " This will be handled automatically.\n"111echo -e "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n"112echo -e "!!NOTE!!: Only select \"Skip\" if mongodb is already installed on this system"113echo "Do you want to continue?"114select yn in "Yes" "Skip" "No" ; do115case $yn in116Yes )117INST_ARCH_MONGO="yes"118break;;119No ) exit;;120Skip )121INST_ARCH_MONGO="no"122break;;123esac124done125fi126127if [ $INST_ARCH_MONGO = "yes" ]; then128DIR=$(pwd)129# Make temp user130echo "Creating temporary user..."131TEMPUSER="gctempuser"132TEMPHOME="/home/$TEMPUSER"133useradd -m $TEMPUSER134cd $TEMPHOME135136# Do the actual makepkg shenanigans137echo "Building mongodb... (this will take a moment)"138su $TEMPUSER<<EOF139mkdir temp140cd temp141git clone https://aur.archlinux.org/mongodb-bin.git -q142cd mongodb-bin143makepkg -s > /dev/null144exit145EOF146mv "$(find -name "mongodb-bin*.pkg.tar.zst" -type f)" ./mongodb-bin.pkg.tar.zst147cd $DIR148149# Snatch the file to current working directory150mv "$TEMPHOME/mongodb-bin.pkg.tar.zst" ./mongodb-bin.pkg.tar.zst151chown root ./mongodb-bin.pkg.tar.zst152chgrp root ./mongodb-bin.pkg.tar.zst153chmod 775 ./mongodb-bin.pkg.tar.zst154155echo "Installing mongodb..."156pacman -U mongodb-bin.pkg.tar.zst --noconfirm > /dev/null157rm mongodb-bin.pkg.tar.zst158159echo "Starting mongodb..."160systemctl enable mongodb161systemctl start mongodb162163echo "Removing temporary account..."164userdel -r $TEMPUSER165fi166echo "Done"167168echo "Getting grasscutter..."169170# Download and rename jar171wget -q --show-progress "https://nightly.link/Grasscutters/Grasscutter/workflows/build/$BRANCH/Grasscutter.zip"172echo "unzipping"173unzip -qq Grasscutter.zip174mv $(find -name "grasscutter*.jar" -type f) grasscutter.jar175176# Download resources177echo "Downloading resources... (this will take a moment)"178wget -q --show-progress https://github.com/Koko-boya/Grasscutter_Resources/archive/refs/heads/main.zip -O resources.zip179echo "Extracting..."180unzip -qq resources.zip181mv ./Grasscutter_Resources-main/Resources ./resources182183# Here we do a sparse checkout to only pull /data and /keys184echo "Downloading keys and data..."185mkdir repo186cd repo187git init -q188git remote add origin https://github.com/Grasscutters/Grasscutter.git189git fetch -q190git config core.sparseCheckout true191echo "data/" >> .git/info/sparse-checkout192echo "keys/" >> .git/info/sparse-checkout193git pull origin stable -q194cd ../195mv ./repo/data ./data196mv ./repo/keys ./keys197198# Generate handbook/config199echo "Please enter language when *NEXT* prompted (press enter/return to continue to language select)"200read201java -jar grasscutter.jar -handbook202203# Prompt IP address for config.json and for generating new keystore.p12 file204echo "Please enter the IP address that will be used to connect to the server"205echo "This can be a local or a public IP address"206echo "This IP address will be used to generate SSL certificates so it is important it is correct"207208while : ; do209read -p "Enter IP: " SERVER_IP210if valid_ip $SERVER_IP; then211break;212else213echo "Invalid IP address. Try again."214fi215done216217# Replaces "127.0.0.1" with given IP218sed -i "s/127.0.0.1/$SERVER_IP/g" config.json219220# Generates new keystore.p12 with the server's IP address221# This is done to prevent a "Connection Timed Out" error from appearing222# after clicking to enter the door in the main menu/title screen223# This issue only exists when connecting to a server *other* than localhost224# since the default keystore.p12 has only been made for localhost225226mkdir certs227cd certs228echo "Generating CA key and certificate pair..."229openssl 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.crt230echo "Generating SSL key and certificate pair..."231232openssl genpkey -out ssl.key -algorithm rsa233234# Creates a conf file in order to generate a csr235cat > csr.conf <<EOF236[ req ]237default_bits = 2048238prompt = no239default_md = sha256240req_extensions = req_ext241distinguished_name = dn242243[ dn ]244C = GB245ST = Essex246L = London247O = Grasscutters248OU = Grasscutters249CN = $SERVER_IP250251[ req_ext ]252subjectAltName = @alt_names253254[ alt_names ]255IP.1 = $SERVER_IP256EOF257258# Creates csr using key and conf259openssl req -new -key ssl.key -out ssl.csr -config csr.conf260261# Creates conf to finalise creation of certificate262cat > cert.conf <<EOF263264authorityKeyIdentifier=keyid,issuer265basicConstraints=CA:FALSE266keyUsage = digitalSignature, nonRepudiation, keyEncipherment, keyAgreement, dataEncipherment267subjectAltName = @alt_names268269[alt_names]270IP.1 = $SERVER_IP271272EOF273274# Creates ssl cert275openssl x509 -req -in ssl.csr -CA CAcert.crt -CAkey CAkey.key -CAcreateserial -out ssl.crt -days 25202 -sha256 -extfile cert.conf276277echo "Generating keystore.p12 from key and certificate..."278openssl pkcs12 -export -out keystore.p12 -inkey ssl.key -in ssl.crt -certfile CAcert.crt -passout pass:123456279280cd ../281mv ./certs/keystore.p12 ./keystore.p12282echo "Done"283284echo -e "Asking Noelle to clean up...\n"285rm -rf Grasscutter.zip resources.zip ./certs ./Grasscutter_Resources-main ./repo286echo -e "All done!\n"287echo -e "You can now uninstall the following packages if you wish:\n$INSTALLER_DEPS"288echo -e "-=-=-=-=-=--- !! IMPORTANT !! ---=-=-=-=-=-\n"289echo "Please make sure that ports 443 and 22102 are OPEN (both tcp and udp)"290echo -e "In order to run the server, run the following command:\nsudo java -jar grasscutter.jar"291echo "You must run it using sudo as port 443 is a privileged port"292echo "To play, use the IP you provided earlier ($SERVER_IP) via GrassClipper or Fiddler"293294exit295296297