-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (59 loc) · 1.72 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
61
62
63
64
65
66
var http = require('http')
var urls = require('./urls.json')
var crypto = require('crypto')
function getHashedIP(req) {
var timeBlock = Math.round(Date.now() / Math.pow(10, ((1000 * 60 * 60 * 4)+'').length))
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
var hash = crypto.createHash('sha256')
hash.update(String(process.env.PRIVACY_SEED))
hash.update(String(timeBlock))
hash.update(ip)
return hash.digest('hex').substr(0,24)
}
function redirect(res, dest) {
if (!dest) {
// res.statusCode = 404
// return res.end('not found') // todo: friendlier not found page
res.statusCode = 301
res.setHeader('location', 'https://www.civicquarterly.com')
res.setHeader('Cache-Control', 'public; max-age=900')
return res.end()
}
res.statusCode = 301
res.setHeader('location', dest)
res.setHeader('Cache-Control', 'public; max-age=900')
return res.end()
}
http
.createServer(function (req, res) {
var slug = req.url.toLowerCase()
if (slug[slug.length-1] === '/') {
slug = slug.substr(0, slug.length - 1)
}
/*
if (!slug) {
// ultimately we might want some helpful
// disambiguation page, since short urls
// are possibly typed by hand and prone to error.
// for now, just redirect to default
redirect(res)
}
*/
redirect(res, urls[slug])
console.log(JSON.stringify({
ipHash: getHashedIP(req),
time: Date.now(),
slug: slug,
status: res.statusCode
}))
})
.listen(process.env.PORT || 8888, function (e) {
if (e) {
console.error(e)
process.exit(1)
}
console.log(JSON.stringify({
msg: 'serving ' + Object.keys(urls).length + ' shorturls',
time: Date.now()
}))
})