Compare commits

..

2 Commits

Author SHA1 Message Date
6e8391545b fix: do not return negative quotas 2026-07-11 21:13:48 +02:00
e5c985fe00 feat: remove time. Improve README. 2026-07-11 21:12:36 +02:00
3 changed files with 24 additions and 17 deletions

View File

@@ -18,7 +18,15 @@ scp -rO www root@ROUTER:/
``` ```
6) Run `chmod +x /www/cgi-bin/limitator.json` 6) Run `chmod +x /www/cgi-bin/limitator.json`
7) Run `opkg update && opkg install iptables-mod-extra coreutils-date` 7) Run `opkg update && opkg install iptables-mod-extra coreutils-date`
8) Run `mkdir ~/limitator` 8) Run `opkg install iptables-mod-extra`
9) Run `crontab -e` and copy&paste contents in `crontab` file. 9) Run `opkg install coreutils-date`
10) Run `/etc/init.d/limitator enable` 10) Run `mkdir ~/limitator`
11) Reboot. 11) Run `crontab -e` and copy&paste contents in `crontab` file.
12) Run `/etc/init.d/limitator enable`
13) Reboot.
### Additional steps for Backfire 10.03
1) Change packages source in /etc/opkg.conf putting http://web.archive.org/web/20260213065024id_/ before the URL. For example: http://web.archive.org/web/20260213065024id_/https://archive.openwrt.org/backfire/10.03.1/brcm-2.4/packages
2) Install iptables-mod-ipopt (NO ESTOY SEGURO, SIN PONERLO FUNCIONA Y EN EL WRT54G DABA ERROR EL COMANDO IPTABLES CON EL MODULO QUOTA Y LO TENÍA INSTALADO)
3) Delete /bin/date link
4) Cannot scp files, you need to paste but vi messes with idents. Use `:set noai` command in vi. Other option is using wget from other routers (not an option for limitator.json).

View File

@@ -5,8 +5,8 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
*/10 7-23 * * * /etc/init.d/limitator save & */10 7-23 * * * /etc/init.d/limitator save &
# Restart at midnight # Restart at midnight
1 0 1-12,14-31 * * /etc/init.d/limitator restart & 1 0 1-12,14-31 * * /etc/init.d/limitator restart &
# Reset the quota monthly # Reset the quota monthly (MUST BE START DAY!)
1 0 13 * * /etc/init.d/limitator reset & 1 0 1 * * /etc/init.d/limitator reset &
# Reboot at 4:30am every day # Reboot at 4:30am every day
# Note: To avoid infinite reboot loop, wait 70 seconds # Note: To avoid infinite reboot loop, wait 70 seconds

View File

@@ -13,9 +13,7 @@ EOF
config_load "limitator" config_load "limitator"
config_get MONTHLY_QUOTA limitator quota 120 # Monthly quota in GB config_get MONTHLY_QUOTA limitator quota 120 # Monthly quota in GB
config_get WAN_DEV limitator wan_dev 'wlan0' # Interface name for WAN config_get WAN_DEV limitator wan_dev 'wlan0' # Interface name for WAN
config_get FIRST_DAY limitator first_day 1 # Day of month when internet plan starts accounting.. config_get FIRST_DAY limitator first_day 1 # Day of month when internet plan starts accounting.
config_get START_TIME limitator start_time '00:00' # Starting time of internet plan limited hours.
config_get END_TIME limitator end_time '23:59' # Ending time of internet plan limited hours.
config_get INTRANET limitator intranet '10.0.0.0/8' # Traffic in Intranet does not count for quota. config_get INTRANET limitator intranet '10.0.0.0/8' # Traffic in Intranet does not count for quota.
config_get EXCLUDE_NETWORK limitator exclude_network # Traffic originated in specified network does not count for quota. config_get EXCLUDE_NETWORK limitator exclude_network # Traffic originated in specified network does not count for quota.
config_get EXCLUDE_NETWORK2 limitator exclude_network2 # Traffic originated in specified network does not count for quota. config_get EXCLUDE_NETWORK2 limitator exclude_network2 # Traffic originated in specified network does not count for quota.
@@ -89,7 +87,12 @@ get_total_quota_until_today(){
} }
get_remaining_quota(){ get_remaining_quota(){
echo -n $(($(get_total_quota_until_today) - $(get_current_used_bytes))) local remaining_quota=$(($(get_total_quota_until_today) - $(get_current_used_bytes)))
if [ $remaining_quota -lt 0 ]; then
echo 0
else
echo -n $remaining_quota
fi
} }
show_remaining_quota(){ show_remaining_quota(){
@@ -137,15 +140,11 @@ set_iptables_rule(){
iptables --wait -I $IPTABLES_CHAIN $(get_iptables_quota_rule_num) -c 0 $(get_saved_used_bytes) -m quota --quota $(get_remaining_quota) -j ACCEPT 2>> $LOG_FILE iptables --wait -I $IPTABLES_CHAIN $(get_iptables_quota_rule_num) -c 0 $(get_saved_used_bytes) -m quota --quota $(get_remaining_quota) -j ACCEPT 2>> $LOG_FILE
iptables --wait -I $IPTABLES_CHAIN $(($(get_iptables_quota_rule_num) + 1)) -j REJECT 2>> $LOG_FILE iptables --wait -I $IPTABLES_CHAIN $(($(get_iptables_quota_rule_num) + 1)) -j REJECT 2>> $LOG_FILE
# Iptables requires UTC time
local start_time_utc=$(date -u +%H:%M --date=@$(date -d "today $START_TIME" +%s))
local end_time_utc=$(date -u +%H:%M --date=@$(date -d "today $END_TIME" +%s))
iptables --wait -I $IPTABLES_FORWARD 1 -d $INTRANET -s $INTRANET -j ACCEPT iptables --wait -I $IPTABLES_FORWARD 1 -d $INTRANET -s $INTRANET -j ACCEPT
iptables -I $IPTABLES_FORWARD 2 -m time --timestart $start_time_utc --timestop $end_time_utc -j $IPTABLES_CHAIN iptables -I $IPTABLES_FORWARD 2 -j $IPTABLES_CHAIN
iptables -I $IPTABLES_INPUT -i $WAN_DEV ! -s $INTRANET -m time --timestart $start_time_utc --timestop $end_time_utc -j $IPTABLES_CHAIN iptables -I $IPTABLES_INPUT -i $WAN_DEV ! -s $INTRANET -j $IPTABLES_CHAIN
iptables -I $IPTABLES_OUTPUT -o $WAN_DEV ! -d $INTRANET -m time --timestart $start_time_utc --timestop $end_time_utc -j $IPTABLES_CHAIN iptables -I $IPTABLES_OUTPUT -o $WAN_DEV ! -d $INTRANET -j $IPTABLES_CHAIN
} }
start() { start() {