Update unraid-config.sh

This commit is contained in:
HHF Technology 2024-12-08 17:15:39 +05:30
parent 029e88bf67
commit 927c37ec2d

View file

@ -15,12 +15,6 @@ WAN_INTERFACE="br0"
VPS_IP="YOUR_VPS_IP" # Replace with your Dallas VPS public IP
GAME_SUBNET="172.17.0.0/16" # docker0 network
# Script paths
SCRIPT_DIR="/boot/config/plugins/game_routing"
ROUTING_SCRIPT="$SCRIPT_DIR/routing.sh"
IPTABLES_SCRIPT="$SCRIPT_DIR/iptables-restore.sh"
IPTABLES_RULES="$SCRIPT_DIR/rules.v4"
# Function to check if script is run as root
check_root() {
if [ "$EUID" -ne 0 ]; then
@ -37,54 +31,44 @@ check_tailscale() {
fi
}
# Function to create script directory
create_directories() {
echo -e "${YELLOW}Creating script directories...${NC}"
mkdir -p $SCRIPT_DIR
chmod 755 $SCRIPT_DIR
}
# Function to fix routing
fix_routing() {
echo -e "${YELLOW}Fixing routing table...${NC}"
# Delete any conflicting default routes
ip route del default 2>/dev/null
ip route del 0.0.0.0/0 2>/dev/null
ip route del 0.0.0.0/0 dev tailscale1 2>/dev/null
ip route del 0.0.0.0/0 dev wg0 2>/dev/null
# Get default gateway from br0
local DEFAULT_GATEWAY=$(ip route | grep br0 | grep default | awk '{print $3}')
local TAILSCALE_NET=$(ip route | grep tailscale1 | grep -v default | head -n1 | awk '{print $1}')
# Set up main routing
ip route add default via $DEFAULT_GATEWAY
ip route add default via $DEFAULT_GATEWAY dev br0
# Add specific route only for game traffic to VPS through Tailscale
# Only add if we have both VPS_IP and TAILSCALE_NET
if [[ ! -z "$VPS_IP" && ! -z "$TAILSCALE_NET" ]]; then
ip route add $VPS_IP via $(echo $TAILSCALE_NET | cut -d/ -f1)
fi
ip route add $VPS_IP via $(ip route | grep tailscale1 | awk '{print $1}') dev tailscale1
# Ensure WireGuard routes are preserved
if ip link show wg0 >/dev/null 2>&1; then
# Get WireGuard IP and add its routes back
local WG_IP=$(ip -4 addr show wg0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
if [ ! -z "$WG_IP" ]; then
ip route add $WG_IP/32 dev wg0 scope link
ip route add $WG_IP dev wg0 scope link
fi
fi
# Save the routing configuration with proper permissions
echo "#!/bin/bash" > $ROUTING_SCRIPT
echo "ip route del default 2>/dev/null" >> $ROUTING_SCRIPT
echo "ip route del 0.0.0.0/0 2>/dev/null" >> $ROUTING_SCRIPT
echo "ip route add default via $DEFAULT_GATEWAY" >> $ROUTING_SCRIPT
if [[ ! -z "$VPS_IP" && ! -z "$TAILSCALE_NET" ]]; then
echo "ip route add $VPS_IP via $(echo $TAILSCALE_NET | cut -d/ -f1)" >> $ROUTING_SCRIPT
fi
# Save the routing configuration
echo "#!/bin/bash" > /boot/config/routing.sh
echo "ip route del default 2>/dev/null" >> /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 del 0.0.0.0/0 dev wg0 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 \$(ip route | grep tailscale1 | awk '{print \$1}') dev tailscale1" >> /boot/config/routing.sh
if [ ! -z "$WG_IP" ]; then
echo "ip route add $WG_IP/32 dev wg0 scope link" >> $ROUTING_SCRIPT
echo "ip route add $WG_IP dev wg0 scope link" >> /boot/config/routing.sh
fi
chmod 755 $ROUTING_SCRIPT
chmod +x /boot/config/routing.sh
echo -e "${GREEN}Routing table fixed successfully${NC}"
}
@ -129,25 +113,22 @@ make_persistent() {
echo -e "${YELLOW}Making settings persistent...${NC}"
# Save iptables rules
iptables-save > $IPTABLES_RULES
chmod 644 $IPTABLES_RULES
mkdir -p /boot/config/iptables
iptables-save > /boot/config/iptables/rules.v4
# Create restore script
echo "#!/bin/bash" > $IPTABLES_SCRIPT
echo "iptables-restore < $IPTABLES_RULES" >> $IPTABLES_SCRIPT
chmod 755 $IPTABLES_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
# Update go script if needed
if ! grep -q "$ROUTING_SCRIPT" /boot/config/go; then
echo "$ROUTING_SCRIPT" >> /boot/config/go
# 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 "$IPTABLES_SCRIPT" /boot/config/go; then
echo "$IPTABLES_SCRIPT" >> /boot/config/go
if ! grep -q "/boot/config/iptables/restore.sh" /boot/config/go; then
echo "/boot/config/iptables/restore.sh" >> /boot/config/go
fi
# Ensure go script is executable
chmod 755 /boot/config/go
echo -e "${GREEN}Settings made persistent${NC}"
}
@ -157,7 +138,6 @@ main() {
check_root
check_tailscale
create_directories
fix_routing
configure_iptables
make_persistent