secubox-openwrt/package/secubox/luci-app-bandwidth-manager/root/usr/libexec/rpcd/luci.bandwidth-manager
CyberMind-FR 31a87c5d7a feat(structure): reorganize luci-app packages into package/secubox/ + appstore migration
Major structural reorganization and feature additions:

## Folder Reorganization
- Move 17 luci-app-* packages to package/secubox/ (except luci-app-secubox core hub)
- Update all tooling to support new structure:
  - secubox-tools/quick-deploy.sh: search both locations
  - secubox-tools/validate-modules.sh: validate both directories
  - secubox-tools/fix-permissions.sh: fix permissions in both locations
  - .github/workflows/test-validate.yml: build from both paths
- Update README.md links to new package/secubox/ paths

## AppStore Migration (Complete)
- Add catalog entries for all remaining luci-app packages:
  - network-tweaks.json: Network optimization tools
  - secubox-bonus.json: Documentation & demos hub
- Total: 24 apps in AppStore catalog (22 existing + 2 new)
- New category: 'documentation' for docs/demos/tutorials

## VHost Manager v2.0 Enhancements
- Add profile activation system for Internal Services and Redirects
- Implement createVHost() API wrapper for template-based deployment
- Fix Virtual Hosts view rendering with proper LuCI patterns
- Fix RPCD backend shell script errors (remove invalid local declarations)
- Extend backend validation for nginx return directives (redirect support)
- Add section_id parameter for named VHost profiles
- Add Remove button to Redirects page for feature parity
- Update README to v2.0 with comprehensive feature documentation

## Network Tweaks Dashboard
- Close button added to component details modal

Files changed: 340+ (336 renames with preserved git history)
Packages affected: 19 luci-app, 2 secubox-app, 1 theme, 4 tools

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-01 14:59:38 +01:00

706 lines
17 KiB
Bash
Executable File

#!/bin/sh
# Bandwidth Manager RPCD Backend
# Provides QoS rules, client quotas, and traffic statistics
. /lib/functions.sh
. /usr/share/libubox/jshn.sh
# Configuration paths
CONFIG_FILE="/etc/config/bandwidth"
USAGE_DB="/tmp/bandwidth_usage.db"
IPTABLES_CHAIN="BW_TRACKING"
# Initialize usage database
init_usage_db() {
if [ ! -f "$USAGE_DB" ]; then
cat > "$USAGE_DB" << 'EOF'
# MAC|Timestamp|RX_Bytes|TX_Bytes
EOF
fi
}
# Get system status and global stats
status() {
json_init
local enabled interface sqm_enabled
config_load bandwidth
config_get enabled global enabled "0"
config_get interface global interface "br-lan"
config_get sqm_enabled global sqm_enabled "0"
json_add_boolean "enabled" "$enabled"
json_add_string "interface" "$interface"
json_add_boolean "sqm_enabled" "$sqm_enabled"
# Check QoS status
local qos_active=0
tc qdisc show dev "$interface" 2>/dev/null | grep -qE "(cake|htb|fq_codel)" && qos_active=1
json_add_boolean "qos_active" "$qos_active"
# Get interface stats
if [ -d "/sys/class/net/$interface" ]; then
local rx_bytes=$(cat /sys/class/net/$interface/statistics/rx_bytes 2>/dev/null || echo 0)
local tx_bytes=$(cat /sys/class/net/$interface/statistics/tx_bytes 2>/dev/null || echo 0)
local rx_packets=$(cat /sys/class/net/$interface/statistics/rx_packets 2>/dev/null || echo 0)
local tx_packets=$(cat /sys/class/net/$interface/statistics/tx_packets 2>/dev/null || echo 0)
json_add_object "stats"
json_add_int "rx_bytes" "$rx_bytes"
json_add_int "tx_bytes" "$tx_bytes"
json_add_int "rx_packets" "$rx_packets"
json_add_int "tx_packets" "$tx_packets"
json_close_object
fi
# Count rules and quotas
local rule_count=0
local quota_count=0
config_foreach count_section rule && rule_count=$?
config_foreach count_section quota && quota_count=$?
json_add_int "rule_count" "$rule_count"
json_add_int "quota_count" "$quota_count"
json_dump
}
count_section() {
return $(( $? + 1 ))
}
# List all QoS rules
list_rules() {
config_load bandwidth
json_init
json_add_array "rules"
_add_rule() {
local name type target limit_down limit_up priority enabled schedule
config_get name "$1" name ""
config_get type "$1" type "application"
config_get target "$1" target ""
config_get limit_down "$1" limit_down "0"
config_get limit_up "$1" limit_up "0"
config_get priority "$1" priority "5"
config_get enabled "$1" enabled "1"
config_get schedule "$1" schedule ""
json_add_object ""
json_add_string "id" "$1"
json_add_string "name" "$name"
json_add_string "type" "$type"
json_add_string "target" "$target"
json_add_int "limit_down" "$limit_down"
json_add_int "limit_up" "$limit_up"
json_add_int "priority" "$priority"
json_add_boolean "enabled" "$enabled"
json_add_string "schedule" "$schedule"
json_close_object
}
config_foreach _add_rule rule
json_close_array
json_dump
}
# Add new QoS rule
add_rule() {
read -r input
json_load "$input"
local name type target limit_down limit_up priority
json_get_var name name
json_get_var type type "application"
json_get_var target target
json_get_var limit_down limit_down "0"
json_get_var limit_up limit_up "0"
json_get_var priority priority "5"
json_cleanup
if [ -z "$name" ] || [ -z "$target" ]; then
json_init
json_add_boolean "success" 0
json_add_string "message" "Name and target are required"
json_dump
return 1
fi
# Generate unique ID
local rule_id="rule_$(date +%s)"
# Add to UCI config
uci -q batch << EOF
set bandwidth.$rule_id=rule
set bandwidth.$rule_id.name='$name'
set bandwidth.$rule_id.type='$type'
set bandwidth.$rule_id.target='$target'
set bandwidth.$rule_id.limit_down='$limit_down'
set bandwidth.$rule_id.limit_up='$limit_up'
set bandwidth.$rule_id.priority='$priority'
set bandwidth.$rule_id.enabled='1'
commit bandwidth
EOF
json_init
json_add_boolean "success" 1
json_add_string "rule_id" "$rule_id"
json_add_string "message" "Rule created successfully"
json_dump
}
# Delete QoS rule
delete_rule() {
read -r input
json_load "$input"
local rule_id
json_get_var rule_id rule_id
json_cleanup
if [ -z "$rule_id" ]; then
json_init
json_add_boolean "success" 0
json_add_string "message" "Rule ID is required"
json_dump
return 1
fi
# Check if rule exists
if ! uci -q get bandwidth.$rule_id >/dev/null 2>&1; then
json_init
json_add_boolean "success" 0
json_add_string "message" "Rule not found"
json_dump
return 1
fi
uci -q delete bandwidth.$rule_id
uci -q commit bandwidth
json_init
json_add_boolean "success" 1
json_add_string "message" "Rule deleted successfully"
json_dump
}
# List all client quotas
list_quotas() {
config_load bandwidth
json_init
json_add_array "quotas"
_add_quota() {
local mac name limit_mb used_mb action reset_day enabled
config_get mac "$1" mac ""
config_get name "$1" name ""
config_get limit_mb "$1" limit_mb "0"
config_get action "$1" action "throttle"
config_get reset_day "$1" reset_day "1"
config_get enabled "$1" enabled "1"
# Get current usage
used_mb=$(get_mac_usage "$mac")
local percent=0
if [ "$limit_mb" -gt 0 ]; then
percent=$(( (used_mb * 100) / limit_mb ))
fi
json_add_object ""
json_add_string "id" "$1"
json_add_string "mac" "$mac"
json_add_string "name" "$name"
json_add_int "limit_mb" "$limit_mb"
json_add_int "used_mb" "$used_mb"
json_add_int "percent" "$percent"
json_add_string "action" "$action"
json_add_int "reset_day" "$reset_day"
json_add_boolean "enabled" "$enabled"
json_close_object
}
config_foreach _add_quota quota
json_close_array
json_dump
}
# Get usage for a specific MAC address
get_mac_usage() {
local mac="$1"
local total_bytes=0
# Get from iptables counters
if iptables -L $IPTABLES_CHAIN -n -v -x 2>/dev/null | grep -qi "$mac"; then
local bytes=$(iptables -L $IPTABLES_CHAIN -n -v -x 2>/dev/null | grep -i "$mac" | awk '{sum+=$2} END {print sum}')
total_bytes=${bytes:-0}
fi
# Convert to MB
echo $(( total_bytes / 1024 / 1024 ))
}
# Get quota details for specific client
get_quota() {
read -r input
json_load "$input"
local mac
json_get_var mac mac
json_cleanup
if [ -z "$mac" ]; then
json_init
json_add_boolean "success" 0
json_add_string "message" "MAC address is required"
json_dump
return 1
fi
config_load bandwidth
local found=0
local quota_id name limit_mb action reset_day
_find_quota() {
local this_mac
config_get this_mac "$1" mac ""
if [ "$this_mac" = "$mac" ]; then
quota_id="$1"
config_get name "$1" name ""
config_get limit_mb "$1" limit_mb "0"
config_get action "$1" action "throttle"
config_get reset_day "$1" reset_day "1"
found=1
fi
}
config_foreach _find_quota quota
if [ "$found" -eq 0 ]; then
json_init
json_add_boolean "success" 0
json_add_string "message" "Quota not found for this MAC"
json_dump
return 1
fi
local used_mb=$(get_mac_usage "$mac")
local remaining_mb=$(( limit_mb - used_mb ))
[ "$remaining_mb" -lt 0 ] && remaining_mb=0
local percent=0
if [ "$limit_mb" -gt 0 ]; then
percent=$(( (used_mb * 100) / limit_mb ))
fi
json_init
json_add_boolean "success" 1
json_add_string "quota_id" "$quota_id"
json_add_string "mac" "$mac"
json_add_string "name" "$name"
json_add_int "limit_mb" "$limit_mb"
json_add_int "used_mb" "$used_mb"
json_add_int "remaining_mb" "$remaining_mb"
json_add_int "percent" "$percent"
json_add_string "action" "$action"
json_add_int "reset_day" "$reset_day"
json_dump
}
# Set or update quota
set_quota() {
read -r input
json_load "$input"
local mac name limit_mb action reset_day
json_get_var mac mac
json_get_var name name ""
json_get_var limit_mb limit_mb "0"
json_get_var action action "throttle"
json_get_var reset_day reset_day "1"
json_cleanup
if [ -z "$mac" ]; then
json_init
json_add_boolean "success" 0
json_add_string "message" "MAC address is required"
json_dump
return 1
fi
# Check if quota exists for this MAC
config_load bandwidth
local quota_id=""
_find_existing() {
local this_mac
config_get this_mac "$1" mac ""
if [ "$this_mac" = "$mac" ]; then
quota_id="$1"
fi
}
config_foreach _find_existing quota
if [ -z "$quota_id" ]; then
# Create new quota
quota_id="quota_$(date +%s)"
uci -q batch << EOF
set bandwidth.$quota_id=quota
set bandwidth.$quota_id.mac='$mac'
set bandwidth.$quota_id.name='$name'
set bandwidth.$quota_id.limit_mb='$limit_mb'
set bandwidth.$quota_id.action='$action'
set bandwidth.$quota_id.reset_day='$reset_day'
set bandwidth.$quota_id.enabled='1'
commit bandwidth
EOF
local msg="Quota created successfully"
else
# Update existing quota
uci -q batch << EOF
set bandwidth.$quota_id.name='$name'
set bandwidth.$quota_id.limit_mb='$limit_mb'
set bandwidth.$quota_id.action='$action'
set bandwidth.$quota_id.reset_day='$reset_day'
commit bandwidth
EOF
local msg="Quota updated successfully"
fi
json_init
json_add_boolean "success" 1
json_add_string "quota_id" "$quota_id"
json_add_string "message" "$msg"
json_dump
}
# Reset quota counter for a client
reset_quota() {
read -r input
json_load "$input"
local mac
json_get_var mac mac
json_cleanup
if [ -z "$mac" ]; then
json_init
json_add_boolean "success" 0
json_add_string "message" "MAC address is required"
json_dump
return 1
fi
# Reset iptables counters for this MAC
iptables -Z $IPTABLES_CHAIN 2>/dev/null
# Remove from usage DB
if [ -f "$USAGE_DB" ]; then
sed -i "/^${mac}|/d" "$USAGE_DB"
fi
json_init
json_add_boolean "success" 1
json_add_string "message" "Quota counter reset for $mac"
json_dump
}
# Get real-time usage for all clients
get_usage_realtime() {
json_init
json_add_array "clients"
# Parse DHCP leases for active clients
if [ -f /tmp/dhcp.leases ]; then
while read -r expires mac ip hostname clientid; do
local rx_bytes=0 tx_bytes=0
# Get current bytes from iptables
if iptables -L $IPTABLES_CHAIN -n -v -x 2>/dev/null | grep -qi "$mac"; then
rx_bytes=$(iptables -L $IPTABLES_CHAIN -n -v -x 2>/dev/null | grep -i "$mac" | awk '{print $2}' | head -1)
tx_bytes=$(iptables -L $IPTABLES_CHAIN -n -v -x 2>/dev/null | grep -i "$mac" | awk '{print $2}' | tail -1)
fi
# Get quota info if exists
config_load bandwidth
local has_quota=0 limit_mb=0 used_mb=0
_check_quota() {
local this_mac
config_get this_mac "$1" mac ""
if [ "$this_mac" = "$mac" ]; then
has_quota=1
config_get limit_mb "$1" limit_mb "0"
used_mb=$(get_mac_usage "$mac")
fi
}
config_foreach _check_quota quota
json_add_object ""
json_add_string "mac" "$mac"
json_add_string "ip" "$ip"
json_add_string "hostname" "${hostname:-unknown}"
json_add_int "rx_bytes" "${rx_bytes:-0}"
json_add_int "tx_bytes" "${tx_bytes:-0}"
json_add_boolean "has_quota" "$has_quota"
if [ "$has_quota" -eq 1 ]; then
json_add_int "limit_mb" "$limit_mb"
json_add_int "used_mb" "$used_mb"
fi
json_close_object
done < /tmp/dhcp.leases
fi
json_close_array
json_dump
}
# Get usage history
get_usage_history() {
read -r input
json_load "$input"
local timeframe mac
json_get_var timeframe timeframe "24h"
json_get_var mac mac ""
json_cleanup
init_usage_db
json_init
json_add_array "history"
# Calculate time threshold
local now=$(date +%s)
local threshold=0
case "$timeframe" in
"1h") threshold=$(( now - 3600 )) ;;
"6h") threshold=$(( now - 21600 )) ;;
"24h") threshold=$(( now - 86400 )) ;;
"7d") threshold=$(( now - 604800 )) ;;
"30d") threshold=$(( now - 2592000 )) ;;
*) threshold=$(( now - 86400 )) ;;
esac
# Read usage database
if [ -f "$USAGE_DB" ]; then
while IFS='|' read -r db_mac timestamp rx_bytes tx_bytes; do
# Skip header line
[ "$db_mac" = "# MAC" ] && continue
# Filter by MAC if specified
if [ -n "$mac" ] && [ "$db_mac" != "$mac" ]; then
continue
fi
# Filter by timeframe
if [ "$timestamp" -lt "$threshold" ]; then
continue
fi
json_add_object ""
json_add_string "mac" "$db_mac"
json_add_int "timestamp" "$timestamp"
json_add_int "rx_bytes" "$rx_bytes"
json_add_int "tx_bytes" "$tx_bytes"
json_close_object
done < "$USAGE_DB"
fi
json_close_array
json_dump
}
# Get media traffic types for classification
get_media() {
json_init
json_add_array "media"
# VoIP
json_add_object ""
json_add_string "id" "voip"
json_add_string "name" "VoIP"
json_add_string "class" "1 (Highest)"
json_add_string "description" "Voice over IP (SIP, RTP, VoLTE)"
json_add_string "ports" "5060-5061,5004,16384-32767"
json_close_object
# Gaming
json_add_object ""
json_add_string "id" "gaming"
json_add_string "name" "Gaming"
json_add_string "class" "2 (High)"
json_add_string "description" "Online gaming traffic"
json_add_string "ports" "3074,27015-27030,3478-3479"
json_close_object
# Streaming
json_add_object ""
json_add_string "id" "streaming"
json_add_string "name" "Video Streaming"
json_add_string "class" "4 (Normal)"
json_add_string "description" "YouTube, Netflix, Twitch"
json_add_string "ports" "443,80"
json_add_string "domains" "youtube.com,netflix.com,twitch.tv"
json_close_object
# Downloads
json_add_object ""
json_add_string "id" "download"
json_add_string "name" "Downloads"
json_add_string "class" "7 (Low)"
json_add_string "description" "HTTP/HTTPS downloads, torrents"
json_add_string "ports" "6881-6889,51413"
json_close_object
# Social Media
json_add_object ""
json_add_string "id" "social"
json_add_string "name" "Social Media"
json_add_string "class" "5 (Normal)"
json_add_string "description" "Facebook, Twitter, Instagram"
json_add_string "ports" "443,80"
json_close_object
# Work/Business
json_add_object ""
json_add_string "id" "work"
json_add_string "name" "Work / Business"
json_add_string "class" "3"
json_add_string "description" "Email, VPN, remote desktop"
json_add_string "ports" "22,3389,1194,1723"
json_close_object
json_close_array
json_dump
}
# Get QoS priority classes
get_classes() {
json_init
json_add_array "classes"
# Class 1 - Highest Priority
json_add_object ""
json_add_int "priority" 1
json_add_string "name" "Real-time (VoIP, Gaming)"
json_add_string "description" "Latency-sensitive applications requiring immediate delivery"
json_add_int "rate" 30
json_add_int "ceil" 100
json_close_object
# Class 2 - High Priority
json_add_object ""
json_add_int "priority" 2
json_add_string "name" "Interactive (SSH, RDP)"
json_add_string "description" "Interactive sessions and remote administration"
json_add_int "rate" 20
json_add_int "ceil" 90
json_close_object
# Class 3 - Medium-High Priority
json_add_object ""
json_add_int "priority" 3
json_add_string "name" "Business Critical"
json_add_string "description" "Email, VPN, business applications"
json_add_int "rate" 15
json_add_int "ceil" 80
json_close_object
# Class 4 - Normal Priority
json_add_object ""
json_add_int "priority" 4
json_add_string "name" "Video Streaming"
json_add_string "description" "YouTube, Netflix, video conferencing"
json_add_int "rate" 10
json_add_int "ceil" 70
json_close_object
# Class 5 - Normal Priority
json_add_object ""
json_add_int "priority" 5
json_add_string "name" "Web Browsing"
json_add_string "description" "HTTP/HTTPS web traffic, social media"
json_add_int "rate" 10
json_add_int "ceil" 60
json_close_object
# Class 6 - Low Priority
json_add_object ""
json_add_int "priority" 6
json_add_string "name" "Bulk Transfer"
json_add_string "description" "File transfers, cloud sync"
json_add_int "rate" 5
json_add_int "ceil" 50
json_close_object
# Class 7 - Lower Priority
json_add_object ""
json_add_int "priority" 7
json_add_string "name" "Downloads (P2P)"
json_add_string "description" "Torrents, large downloads"
json_add_int "rate" 5
json_add_int "ceil" 40
json_close_object
# Class 8 - Lowest Priority
json_add_object ""
json_add_int "priority" 8
json_add_string "name" "Background / Scavenger"
json_add_string "description" "System updates, backups, lowest priority traffic"
json_add_int "rate" 5
json_add_int "ceil" 30
json_close_object
json_close_array
json_dump
}
# Main dispatcher
case "$1" in
list)
cat << 'EOF'
{
"status": {},
"list_rules": {},
"add_rule": { "name": "string", "type": "string", "target": "string", "limit_down": 0, "limit_up": 0, "priority": 5 },
"delete_rule": { "rule_id": "string" },
"list_quotas": {},
"get_quota": { "mac": "string" },
"set_quota": { "mac": "string", "name": "string", "limit_mb": 0, "action": "string", "reset_day": 1 },
"reset_quota": { "mac": "string" },
"get_usage_realtime": {},
"get_usage_history": { "timeframe": "24h", "mac": "" },
"get_media": {},
"get_classes": {}
}
EOF
;;
call)
case "$2" in
status) status ;;
list_rules) list_rules ;;
add_rule) add_rule ;;
delete_rule) delete_rule ;;
list_quotas) list_quotas ;;
get_quota) get_quota ;;
set_quota) set_quota ;;
reset_quota) reset_quota ;;
get_usage_realtime) get_usage_realtime ;;
get_usage_history) get_usage_history ;;
get_media) get_media ;;
get_classes) get_classes ;;
*)
json_init
json_add_boolean "success" 0
json_add_string "error" "Unknown method: $2"
json_dump
;;
esac
;;
esac