-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (42 loc) · 1.53 KB
/
server.js
File metadata and controls
52 lines (42 loc) · 1.53 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
const express = require('express');
const nodeFetch = require('node-fetch');
const path = require('path');
const app = express();
app.use(express.urlencoded({ extended: true }));
// Serve turnstile-demo.html at the root URL
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'turnstile-demo.html'));
});
app.use(express.static(path.join(__dirname)));
const SECRET_KEY = '0x4AAAAAAB5xKFIL9N0vcQW6m7XQbiC3UqA';
async function validateTurnstile(token, remoteip) {
const formData = new URLSearchParams();
formData.append('secret', SECRET_KEY);
formData.append('response', token);
if (remoteip) formData.append('remoteip', remoteip);
try {
const response = await nodeFetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {
method: 'POST',
body: formData
});
const result = await response.json();
return result;
} catch (error) {
console.error('Turnstile validation error:', error);
return { success: false, 'error-codes': ['internal-error'] };
}
}
app.post('/submit', async (req, res) => {
const token = req.body['cf-turnstile-response'];
const ip = req.headers['cf-connecting-ip'] || req.headers['x-forwarded-for'] || req.ip;
const validation = await validateTurnstile(token, ip);
if (validation.success) {
res.send('Form submission successful!');
} else {
res.status(400).send({
message: 'Invalid verification',
errors: validation['error-codes']
});
}
});
app.listen(3000, () => console.log('Express server running on http://localhost:3000'));