Skip to content

Commit 7210c0e

Browse files
committed
Add 'secret reset-traffic <label|all>' — manually reset user traffic counters
Clears cumulative traffic, snapshots, and quota alert tracking for a single user or all users at once. Closes #34
1 parent d046b1a commit 7210c0e

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

mtproxymax.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,6 +2175,35 @@ secret_reenable() {
21752175
log_success "Secret '${label}' re-enabled"
21762176
}
21772177

2178+
# Reset traffic counters for a secret (or all)
2179+
secret_reset_traffic() {
2180+
local label="${1:-}"
2181+
[ -z "$label" ] && { log_error "Usage: mtproxymax secret reset-traffic <label|all>"; return 1; }
2182+
2183+
local _ut="${STATS_DIR}/user_traffic"
2184+
local _snap="${STATS_DIR}/user_traffic_snapshot"
2185+
local _qa="${STATS_DIR}/.quota_alerts_sent"
2186+
2187+
if [ "$label" = "all" ]; then
2188+
: > "$_ut" 2>/dev/null || true
2189+
: > "$_snap" 2>/dev/null || true
2190+
: > "$_qa" 2>/dev/null || true
2191+
log_success "Traffic counters reset for all users"
2192+
else
2193+
# Verify label exists
2194+
local found=false i
2195+
for i in "${!SECRETS_LABELS[@]}"; do
2196+
[ "${SECRETS_LABELS[$i]}" = "$label" ] && { found=true; break; }
2197+
done
2198+
[ "$found" = "false" ] && { log_error "Secret '${label}' not found"; return 1; }
2199+
2200+
for f in "$_ut" "$_snap" "$_qa"; do
2201+
[ -f "$f" ] && { grep -v "^${label}|" "$f" > "${f}.tmp" 2>/dev/null || true; mv "${f}.tmp" "$f" 2>/dev/null || true; }
2202+
done
2203+
log_success "Traffic counters reset for '${label}'"
2204+
fi
2205+
}
2206+
21782207
# Show limits for a secret
21792208
secret_show_limits() {
21802209
local label="${1:-}"
@@ -4918,6 +4947,7 @@ show_cli_help() {
49184947
echo -e " ${GREEN}secret limits${NC} [label] Show user limits"
49194948
echo -e " ${GREEN}secret setlimit${NC} <label> conns|ips|quota|expires <value> [--no-restart]"
49204949
echo -e " ${GREEN}secret setlimits${NC} <label> <conns> <ips> <quota> [expires] [--no-restart]"
4950+
echo -e " ${GREEN}secret reset-traffic${NC} <label|all> Reset traffic counters"
49214951
echo -e " ${DIM}Tip: add/remove support --no-restart flag for scripting${NC}"
49224952
echo ""
49234953
echo -e " ${BOLD}Upstream Routing:${NC}"
@@ -5331,6 +5361,11 @@ cli_main() {
53315361
[ -z "$1" ] && { log_error "Usage: mtproxymax secret reenable <label>"; return 1; }
53325362
secret_reenable "$1"
53335363
;;
5364+
reset-traffic)
5365+
check_root
5366+
[ -z "$1" ] && { log_error "Usage: mtproxymax secret reset-traffic <label|all>"; return 1; }
5367+
secret_reset_traffic "$1"
5368+
;;
53345369
note)
53355370
local label="$1"; shift 2>/dev/null || true
53365371
local note_text="$*"

0 commit comments

Comments
 (0)