mc-perfect/mc-install.sh
2025-09-07 17:36:41 +02:00

325 lines
8.9 KiB
Bash
Executable File

#!/bin/bash
printf "Better to run as root, because of permisions handeling.\n"
sleep 2
### Deb12 > Deb13 ###
printf "Do you want to update from Debian 12 into Debian 13? [Y/n]: \n"
read ansDebUpdate
### LXC SSH configuration ###
printf "Are you using LXC and want to set up full SSH server? [Y/n]: \n"
read ansLxcSshServer
if [[ "$ansLxcSshServer" =~ ("N"|"n") ]]; then
printf "+ Thanks, working on next bit\n"
elif [[ "$ansLxcSshServer" =~ ("Y"|"y") || -z $ansLxcSshServer ]]; then
printf "+ Setting up full ssh server\n"
systemctl disable ssh.socket
systemctl enable ssh
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
else
printf "\nHope you know what you are doing, I am not able to check your answer."
fi
### Firewall ###
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
### Proxy ###
printf "Are you planing to use Velocity proxy to acces this server? [Y/n]: \n"
read ansVeloProxy
if [[ "$ansVeloProxy" =~ ("y"|"Y") || -z $ansVeloProxy ]]; then
printf "Do you wish to set up moder forwarding? [Y/n]: \n"
read ansModernFarward
fi
printf "Do you want to set up SkinRestorer [Y/n]: \n"
read ansSkinRestorer
printf "Do you want to set up PlayerSync [Y/n]: \n"
read ansPlayerSync
sleep 2
### Update ###
printf "\n+ Starting to upgrade base system\n\n"
apt update
apt upgrade -y
apt autoremove --purge -y
if [[ "$ansDebUpdate" =~ ("N"|"n") ]]; then
printf "+ Not upgrading to Debian 13, may cause problems with java versions. Hope you know what you are doing.\n"
elif [[ "$ansDebUpdate" =~ ("Y"|"y") || -z $ansDebUpdate ]]; then
printf "+ Working on updating Debian 12 Bookworm into Debian 13 Trixie"
# changes every bookworm int trixie
sed -i 's/bookworm/trixie/g' /etc/apt/sources.list
apt update
apt full-upgrade -y
else
printf "\nHope you know what you are doing, I am not able to check your answer."
fi
### Installing depandencies ###
#TODO Tailscale tunel?
printf "\n+ Installing depandencies \n\n"
apt install openjdk-21-jre-headless screen p7zip-full -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 ###
mkdir /opt/minecraft
cd /opt/minecraft/
### Download forge server ###
printf "+ Downloading and installing Forge 1.21.1 \n \n"
wget https://maven.minecraftforge.net/net/minecraftforge/forge/1.21.1-52.1.0/forge-1.21.1-52.1.0-installer.jar
mv forge* Forge.jar
### Installing server ###
java -Xms512M -Xmx2048M -jar Forge.jar --installServer
### RAM configuration ###
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"
echo "-Xmx?G # in place of ? put number of RAM you want to set ass maximum"
$maxRam="Error"
elif [[ "$maxRam" =~ ("G"|"M") ]]; then
printf "\n+ Allocating $maxRam as RAM maximum."
echo "-Xmx$maxRam" >> user_jvm_args.txt
elif [[ -z $maxRam ]]; then
$maxRam=4G
printf "\n+ Allocating $MaxRam as RAM maximum"
echo "-Xmx$maxRam" >> user_jvm_args.txt
elif [[ ! "$maxRam" =~ ("G"|"MB") ]]; then
printf "\n+ Allocating ${maxRam}G as RAM maximum"
echo "-Xmx${maxRam}G" >> user_jvm_args.txt
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"
echo "-Xmx?G # in place of ? put number of RAM you want to set ass maximum"
fi
if [[ "$minRam" > "$maxRam" ]]; then
echo "-Xms?G # in place of ? put number of RAM you want to se ass minimum"
elif [[ "$minRam" =~ ("G"|"M") ]]; then
printf "\n+ Allocating $minRam as RAM minimum."
echo "-Xms$minRam" >> user_jvm_args.txt
elif [[ -z $minRam ]]; then
$minRam=1G
printf "\n+ Allocating $minRam as RAM minimum"
echo "-Xms$minRam" >> user_jvm_args.txt
elif [[ ! "$minRam" =~ ("G"|"MB") ]]; then
printf "Allocating ${minRam}G as RAM maximum"
echo "-Xms${minRam}G" >> user_jvm_args.txt
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"
echo "-Xms?G # in place of ? put number of RAM you want to set ass minimum"
fi
### Other configuraton ###
echo eula=true > eula.txt
cd -
cd data/
mv server-icon.png server.properties /opt/minecraft/
cd /opt/minecraft/
### Mods handling ###
mkdir /opt/minecraft/mods
printf "\n+ Unpacking mods \n \n"
#TODO Not like this, what about to downlad them
cd -
cd data
7z x Mods.zip -o/opt/minecraft/mods/
cd /opt/minecraft/
# adds mode to help comunicate with VeloProxy
if [[ "$ansVeloProxy" =~ ["y"|"Y"] || -z $ansVeloProxy ]]; then
wget https://cdn.modrinth.com/data/vDyrHl8l/versions/jfiEc2mQ/proxy-compatible-forge-1.1.7.jar
mv proxy-compatible-forge*.jar /opt/minecraft/mods/
if [[ "$ansModernFarward" =~ ["y"|"Y"] || -z $ansModernFarward ]]; then
mkdir /opt/minecraft/config
mv data/pcf-common.toml /opt/minecraft/config/ #TODO Research this shit
fi
fi
# adds aditional mods
if [[ "$ansSkinRestorer" =~ ["y"|"Y"] || -z $ansSkinRestorer ]]; then
wget https://mediafilez.forgecdn.net/files/6897/64/skinrestorer-2.4.3%2B1.21-forge.jar
mv skinrestorer*.jar /opt/minecraft/mods/
fi
if [[ "$ansPlayerSync" =~ ["y"|"Y"] || -z $ansPlayerSync ]]; then
cd -
# Not using wget because of castom modification to this mod
mv data/playersync-1.21.1.jar /opt/minecraft/mods
cd /opt/minecarft/
# TODO what abou configuration of this?
fi
### Permisions ###
printf "\n+ Permision handeling\n"
chown -R minecraft:minecraft /opt/minecraft
### SystemD service ###
printf "+ Preparign systemD service\n"
mv data/mc-forge.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"
fi
fi
### Error handeling ###
if [[ "$minRam" = "Error" || "$maxRam" = "Error" ]]; then
printf "\n Encountred some eror in Ram settings"
printf "\n Manual configuration needed"
printf "\n Open /opt/minecraft/user_jvm_args.txt in your favorite editor. File contains more instructions"
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-forge.serice
fi
systemctl enable mc-forge
fi
printf "\n+ Done, thanks for your time. It is goode idea to chack if everythink is alright by running:"
printf "\nsystemctl status mc-forge"
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"