-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
67 lines (59 loc) · 1.81 KB
/
example.js
File metadata and controls
67 lines (59 loc) · 1.81 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
const express = require('express');
const cloudflareRealIp = require('../src/index');
const app = express();
const PORT = process.env.PORT || 3000;
// Load Cloudflare IP ranges
console.log('Loading Cloudflare IP ranges...');
cloudflareRealIp.load()
.then(() => {
console.log('Cloudflare IP ranges loaded successfully');
})
.catch(err => {
console.error('Failed to load ranges:', err);
});
// Apply Cloudflare Real IP middleware
app.use(cloudflareRealIp.express());
// Example routes
app.get('/', (req, res) => {
res.json({
message: 'Cloudflare Real IP Example',
realIp: req.realIp,
socketRemoteAddress: req.socket.remoteAddress,
headers: {
'cf-connecting-ip': req.headers['cf-connecting-ip'],
'true-client-ip': req.headers['true-client-ip'],
'x-forwarded-for': req.headers['x-forwarded-for']
}
});
});
app.get('/check', (req, res) => {
const isCloudflare = cloudflareRealIp.check(req);
const realIp = cloudflareRealIp.get(req);
res.json({
isFromCloudflare: isCloudflare,
realIp: realIp,
socketIp: req.socket.remoteAddress
});
});
app.get('/update-ranges', async (req, res) => {
try {
const ranges = await cloudflareRealIp.updateFromCloudflare();
res.json({
message: 'Cloudflare IP ranges updated',
ipv4Count: ranges.v4.length,
ipv6Count: ranges.v6.length
});
} catch (err) {
res.status(500).json({
error: 'Failed to update ranges',
message: err.message
});
}
});
app.listen(PORT, () => {
console.log(`\nServer running on http://localhost:${PORT}`);
console.log(`\nAvailable endpoints:`);
console.log(` GET / - Show your real IP and headers`);
console.log(` GET /check - Check if request is from Cloudflare`);
console.log(` GET /update-ranges - Update Cloudflare IP ranges\n`);
});