summaryrefslogtreecommitdiff
path: root/scripts/pihole-dns-toggle
blob: 74770b12162224d4e7d251f2898439edd9ee10fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
# Toggle Pi-hole DNS on/off for active network connection

set -e

# Pi-hole DNS servers on the Raspberry Pi pair, with the router as last-resort fallback.
PIHOLE_DNS="192.168.1.127 192.168.1.128 192.168.1.101 192.168.1.1"

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# Get active Ethernet/Wi-Fi connection names.
get_active_connections() {
    nmcli -t -f NAME,DEVICE,TYPE connection show --active | \
        awk -F: '$2 != "lo" && ($3 == "802-3-ethernet" || $3 == "802-11-wireless") { print $1 }'
}

# Check if Pi-hole DNS is currently enabled
is_pihole_enabled() {
    local connection="$1"
    local ignore_auto_dns
    local configured_dns

    ignore_auto_dns=$(nmcli -g ipv4.ignore-auto-dns connection show "$connection")
    configured_dns=$(nmcli -g ipv4.dns connection show "$connection" | tr ',' ' ')

    [[ "$ignore_auto_dns" == "yes" && "$configured_dns" == "$PIHOLE_DNS" ]]
}

# Enable Pi-hole DNS
enable_pihole() {
    local connection

    for connection in "$@"; do
        echo -e "${YELLOW}Enabling Pi-hole DNS for connection: $connection${NC}"
        nmcli con mod "$connection" ipv4.dns "$PIHOLE_DNS"
        nmcli con mod "$connection" ipv4.ignore-auto-dns yes
        nmcli con up "$connection" > /dev/null 2>&1
    done

    echo -e "${GREEN}✓ Pi-hole DNS enabled${NC}"
    echo "DNS servers: $PIHOLE_DNS"
}

# Disable Pi-hole DNS (use DHCP-provided DNS)
disable_pihole() {
    local connection

    for connection in "$@"; do
        echo -e "${YELLOW}Disabling Pi-hole DNS for connection: $connection${NC}"
        nmcli con mod "$connection" ipv4.dns ""
        nmcli con mod "$connection" ipv4.ignore-auto-dns no
        nmcli con up "$connection" > /dev/null 2>&1
    done

    echo -e "${GREEN}✓ Pi-hole DNS disabled (using DHCP DNS)${NC}"
}

all_connections_pihole_enabled() {
    local connection

    for connection in "$@"; do
        if ! is_pihole_enabled "$connection"; then
            return 1
        fi
    done

    return 0
}

# Show current DNS status
show_status() {
    echo -e "\n${YELLOW}Current DNS configuration:${NC}"
    nmcli dev show | awk -F: '
        function trim(value) {
            sub(/^[[:space:]]+/, "", value)
            sub(/[[:space:]]+$/, "", value)
            return value
        }
        /^GENERAL.DEVICE:/ { device=trim($2) }
        /^IP4.DNS/ { print device ": " trim($2) }
    '
}

# Check if running on Fedora
check_os() {
    if [[ ! -f /etc/fedora-release ]]; then
        echo -e "${RED}Error: This script is designed for Fedora Linux only${NC}"
        if [[ -f /etc/os-release ]]; then
            source /etc/os-release
            echo "Detected OS: $NAME"
        fi
        exit 1
    fi
}

# Main logic
main() {
    # Check OS compatibility
    check_os

    # Check if running as root
    if [[ $EUID -eq 0 ]]; then
        echo -e "${RED}Error: Do not run this script as root${NC}"
        exit 1
    fi

    # Get active connections
    mapfile -t CONNECTIONS < <(get_active_connections)
    
    if [[ "${#CONNECTIONS[@]}" -eq 0 ]]; then
        echo -e "${RED}Error: No active network connection found${NC}"
        exit 1
    fi

    echo "Active connections: ${CONNECTIONS[*]}"

    # Handle command-line arguments
    case "${1:-toggle}" in
        on|enable)
            enable_pihole "${CONNECTIONS[@]}"
            show_status
            ;;
        off|disable)
            disable_pihole "${CONNECTIONS[@]}"
            show_status
            ;;
        status)
            if all_connections_pihole_enabled "${CONNECTIONS[@]}"; then
                echo -e "${GREEN}Pi-hole DNS is currently ENABLED${NC}"
            else
                echo -e "${YELLOW}Pi-hole DNS is currently DISABLED${NC}"
            fi
            show_status
            ;;
        toggle|*)
            if all_connections_pihole_enabled "${CONNECTIONS[@]}"; then
                disable_pihole "${CONNECTIONS[@]}"
            else
                enable_pihole "${CONNECTIONS[@]}"
            fi
            show_status
            ;;
    esac
}

main "$@"