-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (50 loc) · 1.79 KB
/
index.js
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
const express = require('express');
const bodyParser = require('body-parser');
const crypto = require('crypto');
const fs = require('fs');
const geoip = require('geoip-lite');
const app = express();
const port = 3000;
app.use(bodyParser.json());
let flips = [];
let idCounter = 1;
app.get('/coinflip', (req, res) => {
const result = Math.random() > 0.5 ? 'heads' : 'tails';
const timestampStart = Date.now();
const timestampEnd = timestampStart + 2000; // Adding 2 seconds for simulation
const hash = crypto.createHash('sha256').update(result + timestampStart + timestampEnd).digest('hex');
// Extract the user's IP address (excluding the IPv6 prefix if present)
const ip = req.ip.replace(/^::ffff:/, '');
const geo = geoip.lookup(ip); // Get geolocation data based on IP
const flip = {
id: idCounter++,
result,
timestampStart,
timestampEnd,
hash,
userAgent: req.get('User-Agent'),
ip: ip,
geolocation: geo,
protocol: req.protocol,
method: req.method,
originalUrl: req.originalUrl,
secure: req.secure,
encrypted: req.connection.encrypted ? true : false,
referer: req.get('Referer'),
acceptLanguage: req.get('Accept-Language'),
origin: req.get('Origin')
};
flips.push(flip);
fs.writeFileSync('./data.json', JSON.stringify(flips, null, 2)); // Pretty print JSON
res.json({ result: flip.result, hash: flip.hash });
});
app.get('/verify/:hashid', (req, res) => {
const flip = flips.find(f => f.hash === req.params.hashid);
if (!flip) {
return res.status(404).json({ error: 'Hash not found' });
}
res.json(flip);
});
app.listen(port, () => {
console.log(`Coinflip server listening at http://localhost:${port}`);
});