#!/bin/bash # OpenVPN client-disconnect script # Called when a client disconnects # Environment variables provided by OpenVPN: # - common_name: Client certificate CN # - trusted_ip / untrusted_ip: Client's real IP # - ifconfig_pool_remote_ip: Assigned VPN IP # - bytes_received: Total bytes received from client # - bytes_sent: Total bytes sent to client # - time_duration: Connection duration in seconds # Log disconnection (optional - log file might not be writable) echo "$(date '+%Y-%m-%d %H:%M:%S') DISCONNECT: CN=$common_name Duration=${time_duration}s RX=$bytes_received TX=$bytes_sent" >> /var/log/openvpn/clients.log 2>/dev/null || true # Notify API about disconnection (optional) if [ -n "$API_URL" ]; then curl -s -X POST "$API_URL/vpn-servers/${VPN_SERVER_ID:-1}/client-disconnected" \ -H "Content-Type: application/json" \ -d "{\"common_name\": \"$common_name\", \"bytes_received\": $bytes_received, \"bytes_sent\": $bytes_sent, \"duration\": $time_duration}" \ 2>/dev/null || true fi exit 0