-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path11-redis-storage.php
More file actions
228 lines (178 loc) · 7.27 KB
/
Copy path11-redis-storage.php
File metadata and controls
228 lines (178 loc) · 7.27 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* Example 11: Redis Storage Backend
*
* This example demonstrates how to use Redis as the storage backend for
* Phirewall counters and bans. Redis is recommended for production deployments
* with multiple application servers.
*
* Features shown:
* - Redis cache backend setup
* - Key prefixing for isolation
* - Automatic TTL management
* - Multi-server rate limiting
*
* Required dependency: predis/predis
* composer require predis/predis
*
* Run: php examples/11-redis-storage.php
*
* Environment:
* REDIS_URL=redis://localhost:6379/0 (optional, defaults to localhost)
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use Flowd\Phirewall\Config;
use Flowd\Phirewall\Middleware;
use Flowd\Phirewall\Store\RedisCache;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response;
use Nyholm\Psr7\ServerRequest;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
echo "=== Redis Storage Backend Example ===\n\n";
// =============================================================================
// PREDIS CHECK
// =============================================================================
if (!class_exists(\Predis\Client::class)) {
echo "Predis is not installed.\n";
echo "Install with: composer require predis/predis\n\n";
echo "This example requires Redis. Exiting.\n";
exit(0);
}
// =============================================================================
// REDIS CONNECTION
// =============================================================================
$redisUrl = getenv('REDIS_URL') ?: 'redis://localhost:6379/0';
echo sprintf('Connecting to Redis: %s%s', $redisUrl, PHP_EOL);
try {
$redisClient = new \Predis\Client($redisUrl);
// Verify connection
$pong = (string) $redisClient->ping();
if (stripos($pong, 'PONG') === false) {
throw new RuntimeException("Redis did not respond with PONG");
}
echo "Redis connection successful\n\n";
} catch (\Throwable $throwable) {
echo "Failed to connect to Redis: " . $throwable->getMessage() . "\n";
echo "Make sure Redis is running and REDIS_URL is set correctly.\n";
exit(1);
}
// =============================================================================
// CACHE SETUP
// =============================================================================
// Use a unique prefix for this example to avoid conflicts
$keyPrefix = 'phirewall:example:' . bin2hex(random_bytes(4)) . ':';
$cache = new RedisCache($redisClient, $keyPrefix);
echo "Redis cache configured with prefix: {$keyPrefix}\n\n";
// =============================================================================
// CONFIGURATION
// =============================================================================
$config = new Config($cache);
$config->enableRateLimitHeaders();
$config->enableResponseHeaders();
// Strict throttle to demonstrate Redis storage
$config->throttles->add(
name: 'ip-limit',
limit: 3, // Only 3 requests
period: 30 // Per 30 seconds
);
echo "Throttle rule: 3 requests per 30 seconds per IP\n";
// Fail2Ban for demonstration
$config->fail2ban->add(
name: 'abuse',
threshold: 2, // 2 blocked requests
period: 60, // In 1 minute
ban: 300, // 5 minute ban
filter: fn($req): bool => $req->getHeaderLine('X-Abuse') === '1'
);
echo "Fail2Ban rule: 2 abuse markers = 5 minute ban\n\n";
// =============================================================================
// MIDDLEWARE & HANDLER
// =============================================================================
$middleware = new Middleware($config, new Psr17Factory());
$handler = new class implements RequestHandlerInterface {
public function handle(ServerRequestInterface $serverRequest): ResponseInterface
{
return new Response(200, ['Content-Type' => 'application/json'], '{"status":"ok"}');
}
};
// Helper function
$testRequest = function (string $desc, string $ip, array $headers = []) use ($middleware, $handler): void {
$request = new ServerRequest('GET', '/api/data', $headers, null, '1.1', ['REMOTE_ADDR' => $ip]);
$response = $middleware->process($request, $handler);
$status = $response->getStatusCode();
$remaining = $response->getHeaderLine('X-RateLimit-Remaining');
$phirewall = $response->getHeaderLine('X-Phirewall');
echo sprintf(" %-40s => %d", $desc, $status);
if ($remaining !== '') {
echo sprintf(' [remaining: %s]', $remaining);
}
if ($phirewall === 'blocked') {
$rule = $response->getHeaderLine('X-Phirewall-Matched');
echo sprintf(' [blocked: %s]', $rule);
}
echo "\n";
};
// =============================================================================
// SIMULATION
// =============================================================================
echo "=== Request Simulation ===\n\n";
echo "Test 1: Rate limiting with Redis storage\n";
$testIp = '192.168.1.100';
for ($i = 1; $i <= 5; ++$i) {
$testRequest(sprintf('Request %d from %s', $i, $testIp), $testIp);
}
echo "\n";
echo "Test 2: Different IP has separate quota\n";
$testIp2 = '192.168.1.200';
for ($i = 1; $i <= 3; ++$i) {
$testRequest(sprintf('Request %d from %s', $i, $testIp2), $testIp2);
}
echo "\n";
echo "Test 3: Fail2Ban with Redis storage\n";
$abuserIp = '10.0.0.50';
for ($i = 1; $i <= 4; ++$i) {
$testRequest(sprintf('Abuse attempt %d from %s', $i, $abuserIp), $abuserIp, ['X-Abuse' => '1']);
}
echo "\n";
// =============================================================================
// VERIFY REDIS KEYS
// =============================================================================
echo "=== Redis Keys Created ===\n\n";
$keys = $redisClient->keys($keyPrefix . '*');
echo "Keys stored in Redis:\n";
foreach ($keys as $key) {
$ttl = $redisClient->ttl($key);
$type = $redisClient->type($key);
echo sprintf(" %s (type: %s, TTL: %ds)\n", $key, $type, $ttl);
}
echo "\n";
// =============================================================================
// CLEANUP
// =============================================================================
echo "=== Cleanup ===\n\n";
// Delete all keys created by this example
foreach ($keys as $key) {
$redisClient->del($key);
}
echo "Deleted " . count($keys) . " keys from Redis\n";
// =============================================================================
// PRODUCTION TIPS
// =============================================================================
echo "\n=== Production Tips ===\n\n";
echo "1. Key Prefix Strategy:\n";
echo " - Use environment-specific prefixes: phirewall:prod:, phirewall:staging:\n";
echo " - Include application name for multi-app Redis: myapp:phirewall:\n\n";
echo "2. Redis Configuration:\n";
echo " - Use Redis Cluster for high availability\n";
echo " - Set appropriate maxmemory-policy (allkeys-lru recommended)\n";
echo " - Monitor memory usage as keys accumulate\n\n";
echo "3. Connection Pooling:\n";
echo " - Reuse the Predis client across requests\n";
echo " - Consider using persistent connections\n\n";
echo "4. Failover Strategy:\n";
echo " - Wrap RedisCache in try/catch for graceful degradation\n";
echo " - Fall back to InMemoryCache if Redis is unavailable\n\n";
echo "=== Example Complete ===\n";