-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlisteners.php
99 lines (78 loc) · 2.76 KB
/
listeners.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
require_once 'config.php';
$redis = getRedisInstance();
// Function to get active listeners count
function getActiveListenersCount($redis)
{
$now = time();
$allListeners = $redis->sMembers('active_listeners');
$activeCount = 0;
foreach ($allListeners as $uuid) {
$listenerKey = "listener:$uuid";
$lastSeen = $redis->hGet($listenerKey, 'last_seen');
if ($lastSeen !== false && $now - $lastSeen <= 60) {
$activeCount++;
}
}
return $activeCount;
}
// Check if the stats parameter is set
if (isset($_GET['stats'])) {
// Get the active listener count without modifying the database
$activeCount = getActiveListenersCount($redis);
// Return the active listener count
header('Content-Type: application/json');
echo json_encode(['listeners' => $activeCount]);
exit;
}
// If not returning count, get client IP and user agent
$clientIP = $_SERVER['REMOTE_ADDR'];
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';
// Get name and UUID from POST data
$name = isset($_POST['name']) ? $_POST['name'] : null;
$uuid = isset($_POST['uuid']) ? $_POST['uuid'] : null;
// Validate UUID
if (!$uuid || !preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/', $uuid)) {
http_response_code(400); // Bad Request
exit('Invalid UUID');
}
// Sanitize name function
function sanitizeName($name)
{
// Remove all HTML tags
$name = strip_tags($name);
// Remove all non-printable characters
$name = preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $name);
// Remove potential JavaScript events
$name = preg_replace('/on\w+\s*=\s*(?:(?:"|\')[^"\']*(?:"|\')|[^\s>])+/i', '', $name);
// Remove excessive whitespace
$name = preg_replace('/\s+/', ' ', $name);
// Trim whitespace from the beginning and end
$name = trim($name);
// Convert special characters to HTML entities
$name = htmlspecialchars($name, ENT_QUOTES | ENT_HTML5, 'UTF-8');
// Limit the length of the name
$name = substr($name, 0, 50);
return $name;
}
// Sanitize the name
$sanitizedName = $name ? sanitizeName($name) : null;
// Update listener information in Redis
$listenerKey = "listener:$uuid";
$listenerData = [
'uuid' => $uuid,
'ip' => $clientIP,
'user_agent' => $userAgent,
'name' => $sanitizedName,
'last_seen' => time(),
];
// Store listener data
$redis->hMSet($listenerKey, $listenerData);
// Add to active listeners set
$redis->sAdd('active_listeners', $uuid);
// Set expiration for both the listener data and the active listeners set
$expirationTime = 60; // 1 minute
$redis->expire($listenerKey, $expirationTime);
$redis->expire('active_listeners', $expirationTime);
// No response needed for tracking updates
http_response_code(204); // No Content