Update unraid-config.sh

This commit is contained in:
HHF Technology 2024-12-08 15:27:40 +05:30
parent b84fcb015e
commit 0b57a4ada6

View file

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# Unraid Network Configuration Script # Unraid Network Configuration Script
# This script configures the game server routing through Tailscale # This script configures game server routing through Tailscale
# Color codes for output # Color codes for output
RED='\033[0;31m' RED='\033[0;31m'
@ -10,10 +10,10 @@ YELLOW='\033[1;33m'
NC='\033[0m' NC='\033[0m'
# Configuration variables # Configuration variables
TAILSCALE_INTERFACE="tailscale0" TAILSCALE_INTERFACE="tailscale1"
WAN_INTERFACE="br0" # Typical Unraid bridge interface WAN_INTERFACE="br0"
VPS_IP="YOUR_VPS_IP" # Replace with your Dallas VPS IP VPS_IP="YOUR_VPS_IP" # Replace with your Dallas VPS public IP
GAME_SUBNET="172.16.0.0/24" # Replace with your game servers' subnet GAME_SUBNET="172.17.0.0/16" # docker0 network
# Function to check if script is run as root # Function to check if script is run as root
check_root() { check_root() {
@ -22,24 +22,6 @@ check_root() {
exit 1 exit 1
fi fi
} }
# Function to fix routing
fix_routing() {
echo -e "${YELLOW}Fixing routing table...${NC}"
# Delete Tailscale's default route if it exists
ip route del 0.0.0.0/0 dev tailscale0 2>/dev/null
# Ensure br0 is the default route
ip route add default via $(ip route | grep br0 | grep default | awk '{print $3}') dev br0
# Add specific route for VPS through Tailscale
ip route add $VPS_IP via $(ip route | grep tailscale0 | awk '{print $1}') dev tailscale0
# Save current routing config
echo "ip route del 0.0.0.0/0 dev tailscale0 2>/dev/null" > /boot/config/routing.sh
echo "ip route add default via $(ip route | grep br0 | grep default | awk '{print $3}') dev br0" >> /boot/config/routing.sh
chmod +x /boot/config/routing.sh
}
# Function to check if Tailscale is installed # Function to check if Tailscale is installed
check_tailscale() { check_tailscale() {
@ -49,6 +31,31 @@ check_tailscale() {
fi fi
} }
# Function to fix routing
fix_routing() {
echo -e "${YELLOW}Fixing routing table...${NC}"
# Delete Tailscale's default route if it exists
ip route del 0.0.0.0/0 dev tailscale1 2>/dev/null
# Ensure br0 is the default route
local DEFAULT_GATEWAY=$(ip route | grep br0 | grep default | awk '{print $3}')
ip route add default via $DEFAULT_GATEWAY dev br0
# Add specific route for VPS through Tailscale
local TAILSCALE_NET=$(ip route | grep tailscale1 | awk '{print $1}')
ip route add $VPS_IP via $TAILSCALE_NET dev tailscale1
# Save current routing config
echo "#!/bin/bash" > /boot/config/routing.sh
echo "ip route del 0.0.0.0/0 dev tailscale1 2>/dev/null" >> /boot/config/routing.sh
echo "ip route add default via $DEFAULT_GATEWAY dev br0" >> /boot/config/routing.sh
echo "ip route add $VPS_IP via $TAILSCALE_NET dev tailscale1" >> /boot/config/routing.sh
chmod +x /boot/config/routing.sh
echo -e "${GREEN}Routing table fixed successfully${NC}"
}
# Function to configure iptables # Function to configure iptables
configure_iptables() { configure_iptables() {
echo -e "${YELLOW}Configuring iptables rules...${NC}" echo -e "${YELLOW}Configuring iptables rules...${NC}"
@ -65,53 +72,47 @@ configure_iptables() {
# Allow established connections # Allow established connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# UDP Ports # UDP Ports for game servers
for port in 8766 8767 16261 19132; do for port in 8766 8767 16261 19132; do
iptables -A INPUT -p udp --dport $port -j ACCEPT iptables -A INPUT -p udp --dport $port -j ACCEPT
iptables -A FORWARD -p udp --dport $port -j ACCEPT iptables -A FORWARD -p udp --dport $port -j ACCEPT
done done
# TCP Port Ranges # TCP Port ranges for game servers
iptables -A INPUT -p tcp --match multiport --dports 16262,27015:27050,25500:25600 -j ACCEPT iptables -A INPUT -p tcp -m multiport --dports 16262,27015:27050,25500:25600 -j ACCEPT
iptables -A FORWARD -p tcp --match multiport --dports 16262,27015:27050,25500:25600 -j ACCEPT iptables -A FORWARD -p tcp -m multiport --dports 16262,27015:27050,25500:25600 -j ACCEPT
# Route game traffic through Tailscale # Route game subnet traffic through Tailscale
iptables -t nat -A POSTROUTING -o $TAILSCALE_INTERFACE -j MASQUERADE iptables -t nat -A POSTROUTING -s $GAME_SUBNET -d $VPS_IP -o $TAILSCALE_INTERFACE -j MASQUERADE
# Allow other Docker traffic through br0
iptables -t nat -A POSTROUTING -o $WAN_INTERFACE -j MASQUERADE
echo -e "${GREEN}iptables rules configured successfully${NC}" echo -e "${GREEN}iptables rules configured successfully${NC}"
} }
# Function to configure routing
configure_routing() {
echo -e "${YELLOW}Configuring routing...${NC}"
# Enable IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
# Add route for game servers through Tailscale
ip route add $VPS_IP via $(ip route | grep $TAILSCALE_INTERFACE | awk '{print $1}')
echo -e "${GREEN}Routing configured successfully${NC}"
}
# Function to make settings persistent # Function to make settings persistent
make_persistent() { make_persistent() {
echo -e "${YELLOW}Making settings persistent...${NC}" echo -e "${YELLOW}Making settings persistent...${NC}"
# Save iptables rules # Save iptables rules
if [ -d "/boot/config/iptables" ]; then
iptables-save > /boot/config/iptables/rules.v4
echo -e "${GREEN}iptables rules saved to /boot/config/iptables/rules.v4${NC}"
else
mkdir -p /boot/config/iptables mkdir -p /boot/config/iptables
iptables-save > /boot/config/iptables/rules.v4 iptables-save > /boot/config/iptables/rules.v4
echo -e "${GREEN}Created iptables directory and saved rules${NC}"
# Create restore script
echo "#!/bin/bash" > /boot/config/iptables/restore.sh
echo "iptables-restore < /boot/config/iptables/rules.v4" >> /boot/config/iptables/restore.sh
chmod +x /boot/config/iptables/restore.sh
# Add to go script if not already present
if ! grep -q "/boot/config/routing.sh" /boot/config/go; then
echo "/boot/config/routing.sh" >> /boot/config/go
fi
if ! grep -q "/boot/config/iptables/restore.sh" /boot/config/go; then
echo "/boot/config/iptables/restore.sh" >> /boot/config/go
fi fi
# Ensure IP forwarding is enabled on boot echo -e "${GREEN}Settings made persistent${NC}"
if ! grep -q "net.ipv4.ip_forward=1" /etc/sysctl.conf; then
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
fi
} }
# Main execution # Main execution
@ -120,12 +121,12 @@ main() {
check_root check_root
check_tailscale check_tailscale
fix_routing
configure_iptables configure_iptables
configure_routing
make_persistent make_persistent
echo -e "${GREEN}Unraid network configuration completed successfully${NC}" echo -e "${GREEN}Network configuration completed successfully${NC}"
echo -e "${YELLOW}Please test your game server connectivity${NC}" echo -e "${YELLOW}Please test your docker containers and game server connectivity${NC}"
} }
# Run main function # Run main function