dotfiles/.local/bin/scripts/walli

29 lines
1.4 KiB
Plaintext
Raw Normal View History

2022-01-17 12:28:24 +00:00
#!/bin/sh -e
2021-07-29 14:02:20 +00:00
# Blocks internet at night using iptables and enables it only upon request
# REPLACED by netkeeper with nftables
2021-07-29 14:02:20 +00:00
# Auto-elevate
2022-01-17 12:28:24 +00:00
test ${EUID:-0} -eq 0 || exec sudo "$0" "$@"
2021-04-02 18:57:58 +00:00
# https://askubuntu.com/a/124512 and https://blog.sleeplessbeastie.eu/2018/06/21/how-to-create-iptables-firewall-using-custom-chains/
2021-04-02 18:57:58 +00:00
iptables --new-chain chain-times 2>/dev/null || iptables --flush chain-times
# Always allow local connections - https://serverfault.com/a/550278
2021-04-02 18:57:58 +00:00
iptables -A chain-times -m owner --uid-owner janek -d 192.168.1.0/24 -j ACCEPT
iptables -A chain-times -m owner --uid-owner janek -d 127.0.0.0/8 -j ACCEPT
2021-03-26 18:02:21 +00:00
2022-03-29 11:51:18 +00:00
time9=$(date -u -d "$(date -d 9:00)" +%k)
2021-04-02 18:57:58 +00:00
iptables -A chain-times -m owner --uid-owner janek -j DROP -m time \
2022-03-29 11:51:18 +00:00
--timestart $(date -u -d "$(date -d "${1:-15 min}")" +%k:%M) --timestop $(expr $time9):00
2021-05-20 10:15:04 +00:00
2021-04-02 18:57:58 +00:00
# Only allow periodical internet access in the morning
#iptables -A chain-times -m owner --uid-owner janek -j DROP -m time --weekdays 1-5 \
# --timestart $time9:20 --timestop $(expr $time9 + 1):00
#iptables -A chain-times -m owner --uid-owner janek -j DROP -m time --weekdays 1-5 \
# --timestart $(expr $time9 + 1):20 --timestop $(expr $time9 + 2):00
2021-03-26 18:02:21 +00:00
2021-04-02 18:57:58 +00:00
iptables -L OUTPUT | grep -q "^chain-times" || iptables -A OUTPUT -j chain-times
2021-02-01 03:15:53 +00:00
if test "$2" = "--save"; then
2021-04-02 18:57:58 +00:00
iptables-save | tee /etc/iptables/walli.rules
echo "@reboot root $(which iptables-restore) < /etc/iptables/walli.rules" | tee /etc/cron.d/iptables-times
2021-02-01 03:15:53 +00:00
fi