v2.5.0: add shared 443 and per-user traffic

This commit is contained in:
Виталий Литвинов
2026-04-25 14:07:47 +03:00
parent c1b5ffc5a7
commit 63b564f70f
12 changed files with 990 additions and 34 deletions

View File

@@ -12,9 +12,11 @@ NC='\033[0m' # No Color
STATS_DIR="/run/gotelegram"
HISTORY_FILE="/opt/gotelegram/stats_history.csv"
USER_HISTORY_FILE="/opt/gotelegram/user_stats_history.csv"
SNAPSHOTS_DIR="$STATS_DIR/snapshots"
CURRENT_SNAPSHOT="$STATS_DIR/stats_current.json"
CONFIG_FILE="/opt/gotelegram/config.json"
TELEMT_CONFIG_FILE="/etc/telemt/config.toml"
# Initialize stats infrastructure
stats_init() {
@@ -51,6 +53,9 @@ stats_init() {
if [[ ! -f "$HISTORY_FILE" ]]; then
echo "epoch,proxy_bytes,site_bytes" > "$HISTORY_FILE" 2>/dev/null
fi
if [[ ! -f "$USER_HISTORY_FILE" ]]; then
echo "epoch,user,total_octets,current_connections,active_unique_ips,recent_unique_ips" > "$USER_HISTORY_FILE" 2>/dev/null
fi
# Write initial snapshot
stats_collect
@@ -124,9 +129,63 @@ EOF
fi
fi
stats_collect_users "$ts"
rm -f "$temp_file" 2>/dev/null
}
# Print active telemt usernames from [access.users]. Usernames are restricted by
# goTelegram to A-Z/a-z/0-9/_.- so they are safe in URLs and CSV fields.
stats_active_users() {
[[ -f "$TELEMT_CONFIG_FILE" ]] || return 0
awk '
/^\[access\.users\]$/ { in_users=1; next }
in_users && /^\[/ { exit }
in_users && /^[[:space:]]*#/ { next }
in_users && /=/ {
key=$1
gsub(/^[[:space:]]+|[[:space:]]+$/, "", key)
gsub(/^"|"$/, "", key)
if (key ~ /^[A-Za-z0-9_.-]{1,48}$/) print key
}
' "$TELEMT_CONFIG_FILE" 2>/dev/null
}
stats_collect_users() {
local ts="${1:-$(date +%s)}"
local current_minute=$((ts - (ts % 60)))
mkdir -p "$(dirname "$USER_HISTORY_FILE")" 2>/dev/null
if [[ ! -f "$USER_HISTORY_FILE" ]]; then
echo "epoch,user,total_octets,current_connections,active_unique_ips,recent_unique_ips" > "$USER_HISTORY_FILE" 2>/dev/null
fi
command -v curl &>/dev/null || return 0
command -v jq &>/dev/null || return 0
local user payload total conns active_ips recent_ips
while IFS= read -r user; do
[[ -n "$user" ]] || continue
if awk -F, -v ts="$current_minute" -v user="$user" '$1 == ts && $2 == user { found=1 } END { exit found ? 0 : 1 }' "$USER_HISTORY_FILE" 2>/dev/null; then
continue
fi
payload=$(curl -sS --max-time 2 "http://127.0.0.1:9091/v1/users/${user}" 2>/dev/null || true)
[[ -n "$payload" ]] || continue
total=$(echo "$payload" | jq -r '.data.total_octets // .total_octets // 0' 2>/dev/null)
conns=$(echo "$payload" | jq -r '.data.current_connections // .current_connections // 0' 2>/dev/null)
active_ips=$(echo "$payload" | jq -r '.data.active_unique_ips // .active_unique_ips // 0' 2>/dev/null)
recent_ips=$(echo "$payload" | jq -r '.data.recent_unique_ips // .recent_unique_ips // 0' 2>/dev/null)
[[ "$total" =~ ^[0-9]+$ ]] || total=0
[[ "$conns" =~ ^[0-9]+$ ]] || conns=0
[[ "$active_ips" =~ ^[0-9]+$ ]] || active_ips=0
[[ "$recent_ips" =~ ^[0-9]+$ ]] || recent_ips=0
echo "$current_minute,$user,$total,$conns,$active_ips,$recent_ips" >> "$USER_HISTORY_FILE" 2>/dev/null
done < <(stats_active_users)
stats_cleanup_user_history
}
# Read current snapshot as JSON
stats_read_current() {
if [[ -f "$CURRENT_SNAPSHOT" ]]; then
@@ -308,6 +367,23 @@ stats_cleanup_history() {
mv "$temp_file" "$HISTORY_FILE" 2>/dev/null
}
stats_cleanup_user_history() {
if [[ ! -f "$USER_HISTORY_FILE" ]]; then
return
fi
local now=$(date +%s)
local ts_365d=$((now - 31536000))
local temp_file=$(mktemp)
{
head -1 "$USER_HISTORY_FILE"
awk -F, -v ts="$ts_365d" '$1 >= ts' "$USER_HISTORY_FILE" | tail -n +2
} > "$temp_file" 2>/dev/null
mv "$temp_file" "$USER_HISTORY_FILE" 2>/dev/null
}
# Toggle stats collection on/off
toggle_stats() {
local current_state="false"
@@ -432,13 +508,13 @@ remove_stats_collector() {
# Clean up directories and files
rm -rf "$STATS_DIR" 2>/dev/null
rm -f "$HISTORY_FILE" 2>/dev/null
rm -f "$HISTORY_FILE" "$USER_HISTORY_FILE" 2>/dev/null
echo "Сервис статистики удалён" >&2
}
# Export functions for external use
export -f stats_init stats_collect stats_read_current stats_calculate_rates
export -f stats_init stats_collect stats_collect_users stats_active_users stats_read_current stats_calculate_rates
export -f show_traffic_stats format_bytes format_rate toggle_stats
export -f stats_cleanup_history install_stats_collector remove_stats_collector
export -f stats_cleanup_history stats_cleanup_user_history install_stats_collector remove_stats_collector
export -f json_get