add prefecture and city info
parent
911eae897f
commit
75f49d3042
|
|
@ -0,0 +1,3 @@
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `session` ADD COLUMN `city` VARCHAR(100) NULL,
|
||||||
|
ADD COLUMN `prefecture` VARCHAR(2) NULL;
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import requestIp from 'request-ip';
|
import requestIp from 'request-ip';
|
||||||
import { browserName, detectOS } from 'detect-browser';
|
import { browserName, detectOS } from 'detect-browser';
|
||||||
import isLocalhost from 'is-localhost-ip';
|
|
||||||
import maxmind from 'maxmind';
|
import maxmind from 'maxmind';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
|
@ -56,17 +55,7 @@ export function getDevice(screen, browser, os) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCountry(req, ip) {
|
export async function getGeoInfo(ip) {
|
||||||
// Cloudflare
|
|
||||||
if (req.headers['cf-ipcountry']) {
|
|
||||||
return req.headers['cf-ipcountry'];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignore local ips
|
|
||||||
if (await isLocalhost(ip)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Database lookup
|
// Database lookup
|
||||||
if (!lookup) {
|
if (!lookup) {
|
||||||
lookup = await maxmind.open(path.resolve('node_modules/.geo/GeoLite2-City.mmdb'));
|
lookup = await maxmind.open(path.resolve('node_modules/.geo/GeoLite2-City.mmdb'));
|
||||||
|
|
@ -74,20 +63,32 @@ export async function getCountry(req, ip) {
|
||||||
|
|
||||||
const result = lookup.get(ip);
|
const result = lookup.get(ip);
|
||||||
|
|
||||||
console.log(JSON.stringify(result));
|
if (!result) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
return result?.country?.iso_code;
|
console.log(result.subdivisions);
|
||||||
|
|
||||||
|
const city = result.city?.names.en;
|
||||||
|
const country = result.country?.iso_code;
|
||||||
|
const prefecture = result.subdivisions[0]?.iso_code;
|
||||||
|
|
||||||
|
return {
|
||||||
|
city,
|
||||||
|
country,
|
||||||
|
prefecture,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getClientInfo(req, { screen }) {
|
export async function getClientInfo(req, { screen }) {
|
||||||
const userAgent = req.headers['user-agent'];
|
const userAgent = req.headers['user-agent'];
|
||||||
const ip = getIpAddress(req);
|
const ip = getIpAddress(req);
|
||||||
const country = await getCountry(req, ip);
|
const { country, prefecture, city } = await getGeoInfo(ip);
|
||||||
const browser = browserName(userAgent);
|
const browser = browserName(userAgent);
|
||||||
const os = detectOS(userAgent);
|
const os = detectOS(userAgent);
|
||||||
const device = getDevice(screen, browser, os);
|
const device = getDevice(screen, browser, os);
|
||||||
|
|
||||||
return { userAgent, browser, os, ip, country, device };
|
return { userAgent, browser, os, ip, country, prefecture, city, device };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getJsonBody(req) {
|
export function getJsonBody(req) {
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,10 @@ export async function getSession(req) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
// const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
||||||
const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload);
|
const { userAgent, browser, os, ip, country, prefecture, city, device } = await getClientInfo(
|
||||||
|
req,
|
||||||
|
payload,
|
||||||
|
);
|
||||||
|
|
||||||
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) {
|
||||||
|
|
@ -80,6 +83,8 @@ export async function getSession(req) {
|
||||||
screen,
|
screen,
|
||||||
language,
|
language,
|
||||||
country,
|
country,
|
||||||
|
prefecture,
|
||||||
|
city,
|
||||||
device,
|
device,
|
||||||
ip,
|
ip,
|
||||||
user_agent: userAgent,
|
user_agent: userAgent,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue