Skip to content

Commit

Permalink
Unique visitor id, close #7
Browse files Browse the repository at this point in the history
Fill req.cookies.uvid with uuid.v1.
  • Loading branch information
kucingbasah737 committed Nov 30, 2023
1 parent 8c99dcb commit 34a9f22
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/insert-hit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const mysql = require('./mysql');
* @param {string} [userAgent]
* @param {string} [referrer]
* @param {string} [headers]
* @param {string} [uvid=ip]
* @returns {string} new uuid
*/
module.exports = async (xid, targetUuid, ip, userAgent, referrer, headers) => {
module.exports = async (xid, targetUuid, ip, userAgent, referrer, headers, uvid) => {
const query = `
INSERT INTO ??
SET
Expand All @@ -35,6 +36,7 @@ module.exports = async (xid, targetUuid, ip, userAgent, referrer, headers) => {
referrer: referrer || null,
headers: (typeof headers === 'string' ? headers : JSON.stringify(headers))
|| null,
uvid: uvid || ip,
},
];

Expand Down
16 changes: 16 additions & 0 deletions lib/webserver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ const uniqid = require('uniqid');
const { default: RedisStore } = require('connect-redis');
const session = require('express-session');
const redis = require('redis');
const cookieParser = require('cookie-parser');
const dayjs = require('dayjs');
const { IP, IPv4 } = require('ip-toolkit');
const { v1: uuidv1 } = require('uuid');

const logger = require('../logger');
const accessLogger = require('./access-logger');
Expand Down Expand Up @@ -42,6 +44,8 @@ module.exports = async () => {

const app = express();

app.use(cookieParser());

app.locals.appName = process.env.APP_NAME || 'REDIRECTOR';
app.locals.appVersion = global.appVersion;
app.locals.mainPage = '/dashboard';
Expand Down Expand Up @@ -107,6 +111,18 @@ module.exports = async () => {

res.locals.ip = ip;

// unique visitor id
if (!req.cookies.uvid) {
res.locals.uvid = uuidv1();
logger.debug(`${MODULE_NAME} 3CEB38EE: New visitor detected, setting uivd cookie`, {
xid,
ip: req.ip,
uvid: res.locals.uvid,
});

req.cookies.uvid = res.locals.uvid;
}

next();
});

Expand Down
1 change: 1 addition & 0 deletions lib/webserver/routers/dump-request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const pageMain = (req, res) => {
request: {
clientIp: req.ip,
clientIps: req.ips,
uvid: req.cookies.uvid,
method: req.method,
host: req.hostname,
url: req.originalUrl,
Expand Down
12 changes: 11 additions & 1 deletion lib/webserver/target-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,18 @@ module.exports = async (req, res, next) => {
? IPv4.toIPv6Format(ip).mapped
: ip;

const hitUuid = await insertHit(xid, target.uuid, ipv6, req.get('user-agent'), req.get('referer'), req.headers);
const hitUuid = await insertHit(
xid,
target.uuid,
ipv6,
req.get('user-agent'),
req.get('referer'),
req.headers,
req.cookies?.uvid || null,
);

await incrementHit(xid, target.uuid, hitUuid);

await touchGeoIp(xid, ipv6);
} catch (e) {
logger.warn(`${MODULE_NAME} 9E4990CC: Exception`, {
Expand Down
53 changes: 53 additions & 0 deletions migrations/20231130083559-table-hits-field-uvid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20231130083559-table-hits-field-uvid-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20231130083559-table-hits-field-uvid-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports._meta = {
"version": 1
};
5 changes: 5 additions & 0 deletions migrations/sqls/20231130083559-table-hits-field-uvid-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE `hits` DROP IF EXISTS `uvid`;

ALTER TABLE `hits` DROP INDEX IF EXISTS `uvid`;

ALTER TABLE `hits` DROP INDEX IF EXISTS `target_uvid`;
17 changes: 17 additions & 0 deletions migrations/sqls/20231130083559-table-hits-field-uvid-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ALTER TABLE `hits` ADD IF NOT EXISTS `uvid` VARCHAR(64) NULL DEFAULT NULL AFTER `ip`;

UPDATE `hits`
SET uvid = ip
WHERE uvid IS NULL;

ALTER TABLE `hits` ADD INDEX IF NOT EXISTS `uvid` (`uvid`, `created`);

ALTER TABLE `hits`
DROP INDEX IF EXISTS `created_date`,
ADD INDEX `created_date` (`created_date`, `target_uuid`, `uvid`) USING BTREE;

ALTER TABLE `hits`
DROP INDEX IF EXISTS `created_date_hour`,
ADD INDEX `created_date_hour` (`created_date_hour`, `target_uuid`, `uvid`) USING BTREE;

ALTER TABLE `hits` ADD INDEX IF NOT EXISTS `target_uvid` (`target_uuid`, `uvid`);
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"bcrypt": "^5.1.1",
"bootstrap": "^5.3.2",
"connect-redis": "^7.1.0",
"cookie-parser": "^1.4.6",
"dayjs": "^1.11.10",
"dotenv": "^16.3.1",
"express": "^4.18.2",
Expand Down

0 comments on commit 34a9f22

Please sign in to comment.