mc-paper/script.sh
2024-07-20 11:32:48 +02:00

244 lines
6.3 KiB
Bash
Executable File

#!/bin/bash
printf "Better to run as root, because of permisions handeling.\n"
sleep 2
printf "Do you want to set up firewall rules by iptables? [Y/n]: \n"
read ansFirewallSetup
if [[ "$ansFirewallSetup" =~ ("N"|"n") ]]; then
printf "+ Thanks, working on next bit\n"
elif [[ "$ansFirewallSetup" =~ ("Y"|"y") || -z $ansFirewallSetup ]]; then
printf "Do you want to block non-LAN ssh connections? [Y/n/castom]: "
read sshBlockRange
if [[ $sshBlockRange =~ ("n"|"N") ]]; then
printf "+ OK, working on next bit\n"
ipList=""
elif [[ $sshBlockRange =~ ("y"|"Y") || -z $sshBlockRange ]]; then
#Pipes all informations about ip interfaces
ipList=$(ip -o -4 a show scope global)
#Founds data in pattern
ipList=$(echo $ipList | grep -Eo "([0-9]*\.){3}[0-9]*/[0-9]*")
#substitues space for coma
ipList=$(echo $ipList | tr " " ",")
printf "+ Accepting SSH connections only on these ip's: $ipList\n"
else
printf "\nHope you know what you are doing, I am not checking these :]"
printf "\nSubmit your IP range to acces SSH in format 192.168.1.1/24
\nYour IP range: "
read ipList
printf "\n+ Allowing SSH on this range: $ipList \n"
fi
fi
### RAM ###
printf "How much of RAM are you willing to allocate in GB (Max) [4G/?G]: \n"
read maxRam
printf "How little you want to let RAM go down to in GB (Min) [1G/?G]: \n"
read minRam
if [[ "$minRam" > "$maxRam" ]]; then
pritf "\n You allocated too low max ram or too much min ram. Manual intervention at the end will be needed\n"
xmx=$(echo "-Xmx?G")
$maxRam="Error"
elif [[ "$maxRam" =~ ("G"|"M") ]]; then
printf "\n+ Allocating $maxRam as RAM maximum."
xmx=$(echo "-Xmx$maxRam")
elif [[ -z $maxRam ]]; then
$maxRam=4G
printf "\n+ Allocating $MaxRam as RAM maximum"
xmx=$(echo "-Xmx$maxRam")
elif [[ ! "$maxRam" =~ ("G"|"MB") ]]; then
printf "\n+ Allocating ${maxRam}G as RAM maximum"
xmx=$(echo "-Xmx${maxRam}G")
else
pritnf "\n \n!!! Sorry I have no idea what are you trying to do :/ Manual intervention will be needed at the end \n"
$maxRam="Error"
xmx=$(echo "-Xmx?G")
fi
if [[ "$minRam" > "$maxRam" ]]; then
$xms=$(echo "-Xms?G")
elif [[ "$minRam" =~ ("G"|"M") ]]; then
printf "\n+ Allocating $minRam as RAM minimum."
xms=$(echo "-Xms$minRam")
elif [[ -z $minRam ]]; then
$minRam=1G
printf "\n+ Allocating $minRam as RAM minimum"
xms=$(echo "-Xms$minRam")
elif [[ ! "$minRam" =~ ("G"|"MB") ]]; then
printf "Allocating ${minRam}G as RAM maximum"
xms=$(echo "-Xms${minRam}G")
else
pritnf "\n!!! Sorry I have no idea what are you trying to do :/ Manual intervention will be needed at the end \n"
$minRam="Error"
$xms=$(echo "-Xms?G")
fi
echo "java ${xmx} ${xms} -jar paper-1.20.6-148.jar --nogui" >> data/run.sh
sleep 2
# Just to be shure
printf "\n+ Starting to upgrade base system\n\n"
apt update
apt upgrade -y
# Installing depandencies
printf "\n+ Installing depandencies \n\n"
apt install openjdk-21-jre-headless screen -y
if [[ "$ansFirewallSetup" =~ ("y"|"Y") || -z $ansFirewallSetup ]]; then
apt install iptables -y
fi
# User and groupe managment
printf "\n+ Creating user minecraft and basic file structure"
groupadd minecraft
useradd --system --shell /usr/sbin/nologin --home /opt/minecraft -g minecraft minecraft
# File strukture and basic install
mkdir /opt/minecraft
mv data/paper-1.20.6-148.jar /opt/minecraft/
cd /opt/minecraft/
# Installing server
printf "+ Installing Paper 1.20.6 \n \n"
java -Xms512M -Xmx2048M -jar paper-1.20.6-148.jar --nogui
rm eula.txt server.properties
cd -
cd data/
mv eula.txt server-icon.png server.properties run.sh /opt/minecraft/
cd ..
### Addons support???
# mkdir /opt/minecraft/mods
# printf "\n+ Unpacking mods \n \n"
# cd data
# 7z x Mods.zip -o/opt/minecraft/mods/
# cd ..
# Permision handeling
printf "\n+ Permision handeling\n"
chown -R minecraft:minecraft /opt/minecraft
# SystemD service
printf "+ Preparign systemD service\n"
mv data/mc-paper.service /etc/systemd/system/
systemctl daemon-reload
# integration of Maxopoly's instructions on firewall
if [[ "$ansFirewallSetup" =~ ("Y"|"y") || -z "$ansFirewallSetup" ]]; then
printf "+ Executing firewall setup"
iptables -P INPUT ACCEPT #Clears existing rules
iptables -A INPUT -i lo -j ACCEPT #Allow loopback
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #Allow continuing connections
iptables -A INPUT -p tcp --dport 22 --source $ipList -j ACCEPT #Allow ssh from set network
iptables -A INPUT -p tcp --dport 25565 -j ACCEPT #Allow MC port
iptables -L ### Good place to paste a manual check to not lock yourself out
printf "\n Does it looks right? [Y/n]: "
read ansFirewallOK
if [[ "$ansFirewallOK" =~ ("Y"|"y") || -z "$ansFirewallOK" ]]; then
iptables -P INPUT DROP #Disallow everythink else
iptables -P FORWARD DROP #Block all forwarding
iptables -P OUTPUT ACCEPT #Allow all outgouing
apt install iptables-persistent -y
else
printf "\nOK it is time for manual configuration"
printf "\nAfter you are done ramamber to run 'apt install iptables-persistent' to save your config. System will automaticly remove your work after restart \n \n"
fi
fi
### Error handeling ###
if [[ "$minRam" = "Error" || "$maxRam" = "Error" ]]; then
printf "\n Encountred some eror in Ram settings. Manual configuration needed"
printf "\n Open /opt/minecraft/run.sh in your favorite editor. File contains more instructions"
printf "\n After manual interventing all erorrs start server with: systemctl start mc-paper"
printf "\n Automatic start of server you can enable by: systemctl enable mc-paper"
echo "# In place of ? write number of RAM you want to allocate" >> /opt/minecraft/run.sh
echo "# You can always change this number. Remamber to restart server after it" >> /opt/minecraft/run.sh
else
printf "Do you want to start server right now? [Y/n]: \n"
read startServer
if [[ "$startServer" =~ ("y"|"Y") || -z $startServer ]]; then
systemctl start mc-paper.service
fi
systemctl enable mc-paper
fi
printf "\n+ Done, thanks for your time. It is goode idea to chack if everythink is alright by running:"
printf "\nsystemctl status mc-paper"
printf "\nsu minecraft -s /bin/bash"
printf "\n+ it is good idea to have some fun with screen before trying to open minecraft conesole 'screen -r' while loged wia minecraft user"
### Permision ERROR