forked from tblobaum/pixel-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·201 lines (175 loc) · 5.68 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
let fs = require('fs')
, crypto = require('crypto')
, qs = require('querystring')
, url = require('url')
, pixel = fs.readFileSync(__dirname + '/pixel.gif')
, fns = []
, conf = {
disable_cookies: false, maxAge: 2592000000, cookieName: '_tracker', ip: false,
noRefreshPath: ''
}
function use(fn) {
if (typeof fn === 'function') {
fns.push(fn)
}
return module.exports
}
function configure(obj) {
if (typeof obj === 'object') {
extend(conf, obj)
}
return module.exports
}
function middleware(req, res, next) {
if (!conf.disable_cookies && (!req.cookies || !req.cookies[conf.cookieName])
&& req.path !== conf.noRefreshPath) {
_getUserToken(function (e, token) {
req.cookies[conf.cookieName] = token
res.cookie(conf.cookieName, req.cookies[conf.cookieName], {
maxAge: conf.maxAge
, httpOnly: true
})
res.end(pixel, 'binary')
})
}
else {
res.end(pixel, 'binary')
}
var response = {
cookies: req.cookies || req.headers.cookies || {}
, host: req.headers.host
, path: req.path
, cache: qs.parse(req.headers['cache-control']) || {}
, referer: (req.headers.referer || req.headers.referrer || 'direct')
, params: (req.params || {})
}
if (conf.ip === true) {
response.ip = req.ip
}
req.query = req.query || {}
_getDecay(req.query.decay, function (e, decay) {
response.decay = decay
_getAgent(req.headers['user-agent'], 2, function (e, useragent) {
response.useragent = useragent
_getLanguage(function (e, language) {
response.language = language
_getRemoteAddress(function (remoteAddress) {
response.geo = {ip: remoteAddress}
_fixHref(req.headers.host, function (e, href) {
response.domain = url.parse(href).hostname
_flush(extend(response, req.query))
})
})
})
})
})
function _flush(res) {
each(fns, function (fn, i, done) {
fn(null, res)
})
}
function _getLanguage(callback) {
let lang = req.headers['accept-language'] || ''
lang = lang.split(';') || ''
, format = lang[0].split(',')
format.push(qs.parse(lang[1]))
callback(null, format)
}
function _getRemoteAddress(callback) {
let rc = req.connection
if (req.socket && req.socket.remoteAddress)
callback(req.socket.remoteAddress)
else if (rc)
if (rc.remoteAddress)
callback(rc.remoteAddress)
else if (rc.socket && rc.socket.remoteAddress)
callback(rc.socket.remoteAddress)
else {
console.log('ERR: no remoteAddress')
callback(null)
}
}
function _getUserToken(callback) {
let md5sum = crypto.createHash('md5')
let val = String(Math.random())
md5sum.update(val)
callback(null, md5sum.digest('hex'))
}
function _getDecay(decay, callback) {
if (typeof decay === 'undefined') {
callback(null, +new Date() + (1000 * 60 * 5))
}
else {
callback(null, decay)
}
}
function _getAgent(userAgent, elements, callback) {
// If userAgent is undefined return browser: false
if (typeof userAgent === 'undefined')
return callback({browser: false, version: ''})
let regexps = {
'Chrome': [/Chrome\/(\S+)/],
'Firefox': [/Firefox\/(\S+)/],
'MSIE': [/MSIE (\S+);/],
'Opera': [
/Opera\/.*?Version\/(\S+)/, /* Opera 10 */
/Opera\/(\S+)/ /* Opera 9 and older */
],
'Safari': [/Version\/(\S+).*?Safari\//]
}
, re
, m
, browser
, version
if (typeof elements === 'undefined') {
elements = 2
}
else if (elements === 0) {
elements = 1337
}
for (browser in regexps) {
while (re = regexps[browser].shift()) {
if (m = userAgent.match(re)) {
version = (m[1].match(new RegExp('[^.]+(?:\.[^.]+){0,' + --elements + '}')))[0];
callback(null, {browser: browser, version: version})
}
}
}
}
function _fixHref(str, callback) {
if (!str) {
callback(new Error('fixHref requires a string as the first parameter'))
}
else {
if (str.match(/^http:\/\//)) str = str.substring(7)
if (str.match(/^https:\/\//)) str = str.substring(8)
if (str.match(/^www\./)) str = str.substring(4)
if (!/http:/.test(str) && !/https:/.test(str)) str = 'http://' + str
callback(false, str)
}
}
}
function extend(d) {
let callback = Array.prototype.slice.call(arguments).pop()
, b = arguments, c, i = 1
for (let len = b.length; i < len; i++) for (var e in c = b[i]) d[e] = c[e]
if (typeof callback == 'function') callback(null, d)
else return d
}
// each(array, function (item, i, done) { }, function () { })
function each(array, iterator, cb) {
let len = array.length
array.forEach(function (item, i) {
iterator.call(null, item, i, done)
})
function done() {
if (!--len) {
return cb && cb()
}
}
}
module.exports = {
use: use
, configure: configure
, middleware: middleware
}