add cookie. add city database

pull/1644/head
Sergei Meza 2022-10-18 16:17:20 +09:00
parent 0336f41e12
commit 55dfea1aba
5 changed files with 38 additions and 5 deletions

View File

@ -1,5 +1,5 @@
import { v4, v5 } from 'uuid'; import { v4, v5 } from 'uuid';
import { startOfMonth } from 'date-fns'; import { startOfYear } from 'date-fns';
import { hash } from 'next-basics'; import { hash } from 'next-basics';
export function secret() { export function secret() {
@ -7,7 +7,7 @@ export function secret() {
} }
export function salt() { export function salt() {
const ROTATING_SALT = hash(startOfMonth(new Date()).toUTCString()); const ROTATING_SALT = hash(startOfYear(new Date()).toUTCString());
return hash([secret(), ROTATING_SALT]); return hash([secret(), ROTATING_SALT]);
} }

View File

@ -21,6 +21,8 @@ export const useSession = createMiddleware(async (req, res, next) => {
return badRequest(res); return badRequest(res);
} }
res.setHeader('Set-Cookie', `lemon_session_uuid=${session.session_uuid}`);
req.session = session; req.session = session;
next(); next();
}); });

View File

@ -68,7 +68,8 @@ export async function getCountry(req, ip) {
// Database lookup // Database lookup
if (!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); const result = lookup.get(ip);

View File

@ -50,7 +50,7 @@ export async function getSession(req) {
let session_uuid = uuid(websiteId, hostname, ip, userAgent); let session_uuid = uuid(websiteId, hostname, ip, userAgent);
if (process.env.CROSSDOMAIN_TRACKING) { if (process.env.CROSSDOMAIN_TRACKING) {
session_uuid = uuid(websiteId, ip, userAgent); session_uuid = uuid(websiteId, ip);
} }
let sessionCreated = false; let sessionCreated = false;

View File

@ -1,3 +1,4 @@
/* eslint-disable no-console */
require('dotenv').config(); require('dotenv').config();
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
@ -8,6 +9,9 @@ const tar = require('tar');
let url = let url =
'https://raw.githubusercontent.com/GitSquared/node-geolite2-redist/master/redist/GeoLite2-Country.tar.gz'; '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) { if (process.env.MAXMIND_LICENSE_KEY) {
url = url =
`https://download.maxmind.com/app/geoip_download` + `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 dest = path.resolve(__dirname, '../node_modules/.geo');
const citiesDest = path.resolve(__dirname, '../node_modules/.cities-geo');
if (!fs.existsSync(dest)) { if (!fs.existsSync(dest)) {
fs.mkdirSync(dest); fs.mkdirSync(dest);
} }
if (!fs.existsSync(citiesDest)) {
fs.mkdirSync(citiesDest);
}
const download = url => const download = url =>
new Promise(resolve => { new Promise(resolve => {
https.get(url, res => { https.get(url, res => {
@ -35,7 +45,27 @@ download(url).then(
const filename = path.join(dest, path.basename(entry.path)); const filename = path.join(dest, path.basename(entry.path));
entry.pipe(fs.createWriteStream(filename)); 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);
} }
}); });