From 55dfea1aba6dbafeb2fe0437e7fccbe8aaf77ffe Mon Sep 17 00:00:00 2001 From: Sergei Meza Date: Tue, 18 Oct 2022 16:17:20 +0900 Subject: [PATCH] add cookie. add city database --- lib/crypto.js | 4 ++-- lib/middleware.js | 2 ++ lib/request.js | 3 ++- lib/session.js | 2 +- scripts/build-geo.js | 32 +++++++++++++++++++++++++++++++- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/crypto.js b/lib/crypto.js index 74470549..7a9a5862 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -1,5 +1,5 @@ import { v4, v5 } from 'uuid'; -import { startOfMonth } from 'date-fns'; +import { startOfYear } from 'date-fns'; import { hash } from 'next-basics'; export function secret() { @@ -7,7 +7,7 @@ export function secret() { } export function salt() { - const ROTATING_SALT = hash(startOfMonth(new Date()).toUTCString()); + const ROTATING_SALT = hash(startOfYear(new Date()).toUTCString()); return hash([secret(), ROTATING_SALT]); } diff --git a/lib/middleware.js b/lib/middleware.js index f7289335..e446a0a6 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -21,6 +21,8 @@ export const useSession = createMiddleware(async (req, res, next) => { return badRequest(res); } + res.setHeader('Set-Cookie', `lemon_session_uuid=${session.session_uuid}`); + req.session = session; next(); }); diff --git a/lib/request.js b/lib/request.js index 3b3b5580..bbc7e4a0 100644 --- a/lib/request.js +++ b/lib/request.js @@ -68,7 +68,8 @@ export async function getCountry(req, ip) { // Database lookup if (!lookup) { - lookup = await maxmind.open(path.resolve('node_modules/.geo/GeoLite2-Country.mmdb')); + lookup = await maxmind.open(path.resolve('node_modules/.geo/GeoLite2-City.mmdb')); + // lookup = await maxmind.open(path.resolve('node_modules/.geo/GeoLite2-Country.mmdb')); } const result = lookup.get(ip); diff --git a/lib/session.js b/lib/session.js index 82766517..81964676 100644 --- a/lib/session.js +++ b/lib/session.js @@ -50,7 +50,7 @@ export async function getSession(req) { let session_uuid = uuid(websiteId, hostname, ip, userAgent); if (process.env.CROSSDOMAIN_TRACKING) { - session_uuid = uuid(websiteId, ip, userAgent); + session_uuid = uuid(websiteId, ip); } let sessionCreated = false; diff --git a/scripts/build-geo.js b/scripts/build-geo.js index a4554739..b61255b4 100644 --- a/scripts/build-geo.js +++ b/scripts/build-geo.js @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ require('dotenv').config(); const fs = require('fs'); const path = require('path'); @@ -8,6 +9,9 @@ const tar = require('tar'); let url = 'https://raw.githubusercontent.com/GitSquared/node-geolite2-redist/master/redist/GeoLite2-Country.tar.gz'; +let citiesUrl = + 'https://raw.githubusercontent.com/GitSquared/node-geolite2-redist/master/redist/GeoLite2-City.tar.gz'; + if (process.env.MAXMIND_LICENSE_KEY) { url = `https://download.maxmind.com/app/geoip_download` + @@ -16,10 +20,16 @@ if (process.env.MAXMIND_LICENSE_KEY) { const dest = path.resolve(__dirname, '../node_modules/.geo'); +const citiesDest = path.resolve(__dirname, '../node_modules/.cities-geo'); + if (!fs.existsSync(dest)) { fs.mkdirSync(dest); } +if (!fs.existsSync(citiesDest)) { + fs.mkdirSync(citiesDest); +} + const download = url => new Promise(resolve => { https.get(url, res => { @@ -35,7 +45,27 @@ download(url).then( const filename = path.join(dest, path.basename(entry.path)); entry.pipe(fs.createWriteStream(filename)); - console.log('Saved geo database:', filename); + console.log('Saved countries geo database:', filename); + } + }); + + res.on('error', e => { + reject(e); + }); + res.on('finish', () => { + resolve(); + }); + }), +); +download(citiesUrl).then( + res => + new Promise((resolve, reject) => { + res.on('entry', entry => { + if (entry.path.endsWith('.mmdb')) { + const filename = path.join(citiesDest, path.basename(entry.path)); + entry.pipe(fs.createWriteStream(filename)); + + console.log('Saved cities geo database:', filename); } });