-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
61 lines (53 loc) · 2.15 KB
/
index.coffee
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
app = require('express')()
request = require 'request'
cloudflareIp = require 'cloudflare-ip'
app.set 'trust proxy', (ip) ->
if ip.startsWith '::ffff:'
ip = ip.substr 7
ip == '127.0.0.1' || cloudflareIp(ip)
app.use require('helmet')()
app.all '/', (req, res) ->
res.sendStatus(200)
app.all '/favicon.ico', (req, res) ->
res.sendStatus(404)
apicache = {}
ipreport = (ip) ->
return new Promise (res, rej) ->
if not apicache[ip]?
# If not cached, we have to fetch the text for the thing to display, using our API
request.get 'http://ip-api.com/json/' + ip, (api_err, api_res, api_data) ->
try
api_data = JSON.parse api_data
catch json_err
api_err = "JSON failed to parse: " + json_err
if api_err or api_res.statusCode != 200 then apicache[ip] = ip + ', api error (status code ' + api_res.statusCode + '): ' + api_err
else if api_data?.status != 'success' then apicache[ip] = ip + ', lookup error: ' + api_data?.message + '.'
else apicache[ip] = ip + ',' +
' ISP ' + api_data?.isp +
(if api_data?.org != api_data?.isp then ' ORG ' + api_data?.org else '') +
' FROM ' + api_data?.city + ', ' + api_data?.regionName + ', ' + api_data?.countryCode + '.'
res(apicache[ip])
else
res(apicache[ip])
outbuf = {}
app.all '/:id', (req, res) ->
# From https://github.com/tblobaum/pixel-tracker
# Cross-confirmed with http://proger.i-forge.net/%D0%9A%D0%BE%D0%BC%D0%BF%D1%8C%D1%8E%D1%82%D0%B5%D1%80/[20121112]%20The%20smallest%20transparent%20pixel.html
res.sendFile "pixel.gif", {root: __dirname}
# Display it after a delay of 10 seconds to wait for any multi requests
key = req.ip + '/' + req.params.id
if not outbuf[key]?
outbuf[key] = {n: 0, timeout: null}
if outbuf[key].timeout?
clearTimeout outbuf[key].timeout
outbuf[key].n++
outbuf[key].timeout = setTimeout () ->
n = outbuf[key].n
delete outbuf[key]
ipreport(req.ip).then (text) ->
console.log req.params.id + "*" + n + " from " + text
, 10000
app.all '*', (req, res) ->
res.sendStatus(404)
app.listen(process.env.PORT || 3040)
console.log("Now serving pixels.")