1
0
Fork 0
mirror of https://github.com/Ysurac/openmptcprouter-feeds.git synced 2025-03-09 15:40:03 +00:00

Add quota support and small others changes

This commit is contained in:
Ycarus 2018-03-06 15:09:20 +01:00
parent 6fe07f493b
commit b549503be7
23 changed files with 397 additions and 59 deletions

39
omr-quota/Makefile Normal file
View file

@ -0,0 +1,39 @@
#
# Copyright (C) 2018 Ycarus (Yannick Chabanois) <ycarus@zugaina.org>
#
# This is free software, licensed under the GNU General Public License v3.
# See /LICENSE for more information.
#
include $(TOPDIR)/rules.mk
PKG_NAME:=omr-quota
PKG_VERSION:=1.0
PKG_RELEASE:=1
include $(INCLUDE_DIR)/package.mk
define Package/$(PKG_NAME)
SECTION:=net
CATEGORY:=Network
TITLE:=OpenMPTCProuter quota
PKGARCH:=all
DEPENDS:=+vnstat
endef
define Package/$(PKG_NAME)/description
A module to enforce quota limit
endef
define Build/Compile
endef
define Package/$(PKG_NAME)/conffiles
/etc/config/$(PKG_NAME)
endef
define Package/$(PKG_NAME)/install
$(CP) ./files/* $(1)/
endef
$(eval $(call BuildPackage,$(PKG_NAME)))

24
omr-quota/files/bin/omr-quota Executable file
View file

@ -0,0 +1,24 @@
#!/bin/sh
[ -n "$1" ] || exit
. /lib/functions.sh
# retrieve args
OMR_QUOTA_INTERFACE="$1"
shift
# main loop
while true; do
rx=`vnstat -i $OMR_QUOTA_INTERFACE --json | jsonfilter -e '@.interfaces[0].traffic.months[-1].rx' | tr -d "\n"`
tx=`vnstat -i $OMR_QUOTA_INTERFACE --json | jsonfilter -e '@.interfaces[0].traffic.months[-1].tx' | tr -d "\n"`
tt=$((rx + tx))
[ -n "$OMR_QUOTA_RX" ] && [ "$OMR_QUOTA_RX" -gt 0 ] && [ -n "$rx" ] && [ "$OMR_QUOTA_RX" -ge "$rx" ] && ifdown $OMR_QUOTA_INTERFACE \
|| \
[ -n "$OMR_QUOTA_TX" ] && [ "$OMR_QUOTA_TX" -gt 0 ] && [ -n "$tx" ] && [ "$OMR_QUOTA_TX" -ge "$tx" ] && ifdown $OMR_QUOTA_INTERFACE \
|| \
[ -n "$OMR_QUOTA_TT" ] && [ "$OMR_QUOTA_TT" -gt 0 ] && [ -n "$tt" ] && [ "$OMR_QUOTA_TT" -ge "$tt" ] && ifdown $OMR_QUOTA_INTERFACE \
|| \
ifup $OMR_QUOTA_INTERFACE
sleep "$OMR_QUOTA_INTERVAL"
done

View file

@ -0,0 +1,15 @@
config interface 'wan1'
option enabled '0'
option txquota '100000'
option rxquota '400000'
option ttquota '500000'
option interval '10'
option interface 'wan1'
config interface 'wan2'
option enabled '0'
option txquota '100000'
option rxquota '400000'
option ttquota '500000'
option interval '10'
option interface 'wan2'

View file

@ -0,0 +1,54 @@
#!/bin/sh /etc/rc.common
{
START=90
STOP=10
USE_PROCD=1
}
_validate_section() {
uci_validate_section omr-quota "$1" "$2" \
'txquota:uinteger' \
'rxquota:uinteger' \
'ttquota:uinteger' \
'interval:uinteger' \
'interface:string' \
'enabled:bool:0'
}
_launch_quota() {
local txquota rxquota ttquota interval enabled interface
_validate_section "interface" "$1"
[ -z "$txquota" ] && [ -z "$rxquota" ] && [ -z "$ttquota" ] && return
[ $enabled = 0 ] && return
[ "$(uci get vnstat.@vnstat[-1].interface | grep $interface)" = "" ] && {
uci -q batch <<-EOF
add_list vnstat.@vnstat[-1].interface=$interface
EOF
}
procd_open_instance
procd_set_param command /bin/omr-quota "$interface"
procd_append_param env "OMR_QUOTA_TX=$txquota"
procd_append_param env "OMR_QUOTA_RX=$rxquota"
procd_append_param env "OMR_QUOTA_TT=$ttquota"
procd_append_param env "OMR_QUOTA_INTERVAL=$interval"
procd_set_param respawn 0 10 0
procd_set_param stderr 1
procd_close_instance
}
start_service() {
config_load omr-quota
config_foreach _launch_quota interface
}
service_triggers() {
procd_add_reload_trigger omr-quota network
}
reload_service() {
stop
start
}

View file

@ -0,0 +1,8 @@
#!/bin/sh
uci -q batch <<-EOF
delete ucitrack.@omr-quota[-1]
add ucitrack omr-quota
set ucitrack.@omr-quota[-1].init="omr-quota"
add_list ucitrack.@network[-1].affects="omr-quota"
commit ucitrack
EOF