nuno ревизий этого фрагмента 1 month ago. К ревизии
1 file changed, 70 insertions
gistfile1.txt(файл создан)
| @@ -0,0 +1,70 @@ | |||
| 1 | + | function firewall::block_ip_logged() { | |
| 2 | + | local client_ip="$1" target_ip="$2" | |
| 3 | + | ||
| 4 | + | iptables -I FORWARD 1 -s "$client_ip" -d "$target_ip" -j LOG --log-prefix "wgctl-dropped: " --log-level 4 | |
| 5 | + | iptables -I FORWARD 1 -s "$client_ip" -d "$target_ip" -j DROP | |
| 6 | + | log::wg_block "Blocked ${client_ip} → ${target_ip} (logged)" | |
| 7 | + | } | |
| 8 | + | function firewall::unblock_ip() { | |
| 9 | + | local client_ip="$1" target_ip="$2" | |
| 10 | + | ||
| 11 | + | iptables -D FORWARD -s "$client_ip" -d "$target_ip" -j LOG --log-prefix "wgctl-dropped: " --log-level 4 2>/dev/null || true | |
| 12 | + | iptables -D FORWARD -s "$client_ip" -d "$target_ip" -j DROP 2>/dev/null || true | |
| 13 | + | } | |
| 14 | + | function firewall::block_all() { | |
| 15 | + | local client_ip="$1" client_name="$2" | |
| 16 | + | ||
| 17 | + | iptables -A FORWARD -s "$client_ip" -j LOG --log-prefix "wgctl-dropped: " --log-level 4 | |
| 18 | + | iptables -A FORWARD -s "$client_ip" -j DROP | |
| 19 | + | ||
| 20 | + | log::debug "Blocked all traffic from: ${client_ip}" | |
| 21 | + | } | |
| 22 | + | function firewall::unblock_all() { | |
| 23 | + | local client_ip="$1" | |
| 24 | + | ||
| 25 | + | iptables -D FORWARD -s "$client_ip" -j LOG --log-prefix "wgctl-dropped: " --log-level 4 2>/dev/null || true | |
| 26 | + | iptables -D FORWARD -s "$client_ip" -j DROP 2>/dev/null || true | |
| 27 | + | ||
| 28 | + | monitor::unwatch "$client_ip" | |
| 29 | + | log::debug "Unblocked all traffic from: ${client_ip}" | |
| 30 | + | } | |
| 31 | + | function firewall::apply_guest_dns_redirect() { | |
| 32 | + | if iptables -t nat -C PREROUTING -s "$(config::subnet_for guest).0/24" -p udp --dport 53 -j DNAT --to-destination "$(config::dns):53" 2>/dev/null; then | |
| 33 | + | log::wg "Guest DNS redirect already applied" | |
| 34 | + | return 0 | |
| 35 | + | fi | |
| 36 | + | ||
| 37 | + | local guest_subnet dns | |
| 38 | + | guest_subnet="$(config::subnet_for "guest").0/24" | |
| 39 | + | dns="$(config::dns)" | |
| 40 | + | ||
| 41 | + | # Log DNS bypass attempts (queries not directed at Pi-hole) | |
| 42 | + | iptables -t nat -A PREROUTING -s "$guest_subnet" -p udp --dport 53 \ | |
| 43 | + | ! -d "$dns" \ | |
| 44 | + | -j LOG --log-prefix "wgctl-dns-redirect: " --log-level 4 | |
| 45 | + | iptables -t nat -A PREROUTING -s "$guest_subnet" -p tcp --dport 53 \ | |
| 46 | + | ! -d "$dns" \ | |
| 47 | + | -j LOG --log-prefix "wgctl-dns-redirect: " --log-level 4 | |
| 48 | + | ||
| 49 | + | # Redirect all DNS to Pi-hole | |
| 50 | + | iptables -t nat -A PREROUTING -s "$guest_subnet" -p udp --dport 53 -j DNAT --to-destination "${dns}:53" | |
| 51 | + | iptables -t nat -A PREROUTING -s "$guest_subnet" -p tcp --dport 53 -j DNAT --to-destination "${dns}:53" | |
| 52 | + | ||
| 53 | + | log::wg_block "Guest DNS redirected to Pi-hole (${dns}), bypass attempts will be logged" | |
| 54 | + | } | |
| 55 | + | function firewall::remove_guest_dns_redirect() { | |
| 56 | + | local guest_subnet dns | |
| 57 | + | guest_subnet="$(config::subnet_for "guest").0/24" | |
| 58 | + | dns="$(config::dns)" | |
| 59 | + | ||
| 60 | + | iptables -t nat -D PREROUTING -s "$guest_subnet" -p udp --dport 53 \ | |
| 61 | + | ! -d "$dns" \ | |
| 62 | + | -j LOG --log-prefix "wgctl-dns-redirect: " --log-level 4 2>/dev/null || true | |
| 63 | + | iptables -t nat -D PREROUTING -s "$guest_subnet" -p tcp --dport 53 \ | |
| 64 | + | ! -d "$dns" \ | |
| 65 | + | -j LOG --log-prefix "wgctl-dns-redirect: " --log-level 4 2>/dev/null || true | |
| 66 | + | iptables -t nat -D PREROUTING -s "$guest_subnet" -p udp --dport 53 -j DNAT --to-destination "${dns}:53" 2>/dev/null || true | |
| 67 | + | iptables -t nat -D PREROUTING -s "$guest_subnet" -p tcp --dport 53 -j DNAT --to-destination "${dns}:53" 2>/dev/null || true | |
| 68 | + | ||
| 69 | + | log::debug "Removed guest DNS redirect" | |
| 70 | + | } | |
Новее
Позже