forked from ozten/TLD.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
104 lines (96 loc) · 2.67 KB
/
server.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
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
var express = require('express'),
redis = require("redis"),
https = require('https'),
crypto = require('crypto');
var db = redis.createClient();
var app = express.createServer(
express.logger(),
express.static(__dirname + "/site"),
express.bodyParser()
);
app.post('/signmeup', function(req, res) {
var body = JSON.stringify({
assertion: req.body.assertion,
audience: 'http://' + req.headers.host
});
var vreq = https.request({
host: 'browserid.org',
path: '/verify',
method: 'POST',
headers: {
'Content-Length': body.length,
'Content-Type': 'application/json'
}
}, function (vres) {
var body = "";
vres.on('data', function(chunk) { body += chunk; });
vres.on('end', function() {
try {
body = JSON.parse(body);
if (!body.email) throw "no email";
db.sismember('supporters', body.email, function(err, rez) {
if (err || rez) {
res.json({
success: false,
reason: "You can't sign twice. Great enthusiasm tho!"
});
} else {
db.sadd('supporters', body.email, function(err) {
cache = null;
res.json({success: !err, email: body.email});
});
}
});
} catch(e) {
res.json({success: false, reason: "couldn't validate that email"});
}
});
});
vreq.write(body);
vreq.end();
});
var cache = null;
// function which regens random peeps every 15s or so.
function getPeeps(max, cb) {
if (!cache || (new Date() - cache.when) > 15000) {
var arr = [];
var max = (64 < max ? 64 : max);
console.log(max);
var i = (max * 2).toFixed();
function moar() {
if (!i || arr.length >= max) {
cache = { when: new Date(), data: arr };
cb(arr);
}
else {
i--;
db.srandmember('supporters', function(err, mem) {
if (err || !mem) return cb([]);
mem = mem.toLowerCase();
var hash = crypto.createHash('md5').update(mem).digest("hex")
if (arr.indexOf(hash) === -1) arr.push(hash);
moar();
});
}
}
moar();
} else {
process.nextTick(function() {
cb(cache.data);
});
}
}
app.get('/who', function(req, res) {
db.scard('supporters', function(err, num) {
getPeeps(num, function(data) {
if (req.query.me) {
data = data.slice();
if (data.length >= 64) data.pop();
req.query.me = req.query.me.toLowerCase();
data.unshift(crypto.createHash('md5').update(req.query.me).digest("hex"));
}
res.json({ count: num, some: data });
});
});
});
app.listen(process.env.PORT || 8080);