move sql out of core/Rank
finish own whois library
This commit is contained in:
parent
a5f8874dc8
commit
931ffcc296
|
@ -2,8 +2,7 @@
|
||||||
* timers and cron for account related actions
|
* timers and cron for account related actions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Sequelize from 'sequelize';
|
import { populateRanking } from '../data/sql/RegUser';
|
||||||
import RegUser from '../data/sql/RegUser';
|
|
||||||
import {
|
import {
|
||||||
getRanks,
|
getRanks,
|
||||||
resetDailyRanks,
|
resetDailyRanks,
|
||||||
|
@ -42,44 +41,6 @@ class Ranks {
|
||||||
DailyCron.hook(this.resetDailyRanking);
|
DailyCron.hook(this.resetDailyRanking);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* take array of {useId: score} and resolve
|
|
||||||
* user informations
|
|
||||||
*/
|
|
||||||
static async populateRanking(rawRanks) {
|
|
||||||
if (!rawRanks.length) {
|
|
||||||
return rawRanks;
|
|
||||||
}
|
|
||||||
const uids = rawRanks.map((r) => r.id);
|
|
||||||
const userData = await RegUser.findAll({
|
|
||||||
attributes: [
|
|
||||||
'id',
|
|
||||||
'name',
|
|
||||||
[
|
|
||||||
Sequelize.fn(
|
|
||||||
'DATEDIFF',
|
|
||||||
Sequelize.literal('CURRENT_TIMESTAMP'),
|
|
||||||
Sequelize.col('createdAt'),
|
|
||||||
),
|
|
||||||
'age',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
where: {
|
|
||||||
id: uids,
|
|
||||||
},
|
|
||||||
raw: true,
|
|
||||||
});
|
|
||||||
for (let i = 0; i < userData.length; i += 1) {
|
|
||||||
const { id, name, age } = userData[i];
|
|
||||||
const dat = rawRanks.find((r) => r.id === id);
|
|
||||||
if (dat) {
|
|
||||||
dat.name = name;
|
|
||||||
dat.age = age;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return rawRanks;
|
|
||||||
}
|
|
||||||
|
|
||||||
static async updateRanking() {
|
static async updateRanking() {
|
||||||
/*
|
/*
|
||||||
* only main shard updates and sends it to others
|
* only main shard updates and sends it to others
|
||||||
|
@ -87,13 +48,13 @@ class Ranks {
|
||||||
if (!socketEvents.amIImportant()) {
|
if (!socketEvents.amIImportant()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const ranking = await Ranks.populateRanking(
|
const ranking = await populateRanking(
|
||||||
await getRanks(
|
await getRanks(
|
||||||
false,
|
false,
|
||||||
1,
|
1,
|
||||||
100,
|
100,
|
||||||
));
|
));
|
||||||
const dailyRanking = await Ranks.populateRanking(
|
const dailyRanking = await populateRanking(
|
||||||
await getRanks(
|
await getRanks(
|
||||||
true,
|
true,
|
||||||
1,
|
1,
|
||||||
|
|
189
src/core/draw.js
189
src/core/draw.js
|
@ -6,13 +6,10 @@ import {
|
||||||
getPixelFromChunkOffset,
|
getPixelFromChunkOffset,
|
||||||
} from './utils';
|
} from './utils';
|
||||||
import logger, { pixelLogger } from './logger';
|
import logger, { pixelLogger } from './logger';
|
||||||
import RedisCanvas from '../data/redis/RedisCanvas';
|
|
||||||
import allowPlace from '../data/redis/cooldown';
|
import allowPlace from '../data/redis/cooldown';
|
||||||
import socketEvents from '../socket/socketEvents';
|
import socketEvents from '../socket/socketEvents';
|
||||||
import {
|
import { setPixelByOffset } from './setPixel';
|
||||||
setPixelByOffset,
|
import isIPAllowed from './isAllowed';
|
||||||
setPixelByCoords,
|
|
||||||
} from './setPixel';
|
|
||||||
import rankings from './Ranks';
|
import rankings from './Ranks';
|
||||||
import canvases from './canvases';
|
import canvases from './canvases';
|
||||||
|
|
||||||
|
@ -58,7 +55,7 @@ setInterval(() => {
|
||||||
* Offset is the offset of the pixel within the chunk
|
* Offset is the offset of the pixel within the chunk
|
||||||
* @return Promise<Object>
|
* @return Promise<Object>
|
||||||
*/
|
*/
|
||||||
export async function drawByOffsets(
|
export default async function drawByOffsets(
|
||||||
user,
|
user,
|
||||||
canvasId,
|
canvasId,
|
||||||
i,
|
i,
|
||||||
|
@ -70,7 +67,6 @@ export async function drawByOffsets(
|
||||||
let retCode = 0;
|
let retCode = 0;
|
||||||
let pxlCnt = 0;
|
let pxlCnt = 0;
|
||||||
let rankedPxlCnt = 0;
|
let rankedPxlCnt = 0;
|
||||||
let needProxycheck = 0;
|
|
||||||
const { ipSub: ip } = user;
|
const { ipSub: ip } = user;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -189,6 +185,7 @@ export async function drawByOffsets(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let needProxycheck;
|
||||||
[retCode, pxlCnt, wait, coolDown, needProxycheck] = await allowPlace(
|
[retCode, pxlCnt, wait, coolDown, needProxycheck] = await allowPlace(
|
||||||
ip,
|
ip,
|
||||||
user.id,
|
user.id,
|
||||||
|
@ -202,6 +199,10 @@ export async function drawByOffsets(
|
||||||
pxlOffsets,
|
pxlOffsets,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (needProxycheck) {
|
||||||
|
await isIPAllowed(ip, true);
|
||||||
|
}
|
||||||
|
|
||||||
for (let u = 0; u < pxlCnt; u += 1) {
|
for (let u = 0; u < pxlCnt; u += 1) {
|
||||||
const [offset, color] = pixels[u];
|
const [offset, color] = pixels[u];
|
||||||
setPixelByOffset(canvasId, color, i, j, offset);
|
setPixelByOffset(canvasId, color, i, j, offset);
|
||||||
|
@ -235,179 +236,5 @@ export async function drawByOffsets(
|
||||||
pxlCnt,
|
pxlCnt,
|
||||||
rankedPxlCnt,
|
rankedPxlCnt,
|
||||||
retCode,
|
retCode,
|
||||||
needProxycheck,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Old version of draw that returns explicit error messages
|
|
||||||
* used for http json api/pixel, used with coordinates
|
|
||||||
* Is not used anywhere currently, but we keep it around.
|
|
||||||
* @param user
|
|
||||||
* @param canvasId
|
|
||||||
* @param x
|
|
||||||
* @param y
|
|
||||||
* @param color
|
|
||||||
* @returns {Promise.<Object>}
|
|
||||||
*/
|
|
||||||
export async function drawByCoords(
|
|
||||||
user,
|
|
||||||
canvasId,
|
|
||||||
color,
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
z = null,
|
|
||||||
) {
|
|
||||||
const canvas = canvases[canvasId];
|
|
||||||
|
|
||||||
if (!canvas) {
|
|
||||||
return {
|
|
||||||
error: 'This canvas does not exist',
|
|
||||||
success: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const canvasMaxXY = canvas.size / 2;
|
|
||||||
const canvasMinXY = -canvasMaxXY;
|
|
||||||
if (x < canvasMinXY || x >= canvasMaxXY) {
|
|
||||||
return {
|
|
||||||
error: 'x Coordinate not within canvas',
|
|
||||||
success: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const clrIgnore = canvas.cli || 0;
|
|
||||||
|
|
||||||
if (canvas.v) {
|
|
||||||
if (z < canvasMinXY || z >= canvasMaxXY) {
|
|
||||||
return {
|
|
||||||
error: 'z Coordinate not within canvas',
|
|
||||||
success: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (y >= THREE_CANVAS_HEIGHT) {
|
|
||||||
return {
|
|
||||||
error: 'You reached build limit. Can\'t place higher than 128 blocks.',
|
|
||||||
success: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (y < 0) {
|
|
||||||
return {
|
|
||||||
error: 'Can\'t place on y < 0',
|
|
||||||
success: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (z === null) {
|
|
||||||
return {
|
|
||||||
error: 'This is a 3D canvas. z is required.',
|
|
||||||
success: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (y < canvasMinXY || y >= canvasMaxXY) {
|
|
||||||
return {
|
|
||||||
error: 'y Coordinate not within canvas',
|
|
||||||
success: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (color < clrIgnore) {
|
|
||||||
return {
|
|
||||||
error: 'Invalid color selected',
|
|
||||||
success: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (z !== null) {
|
|
||||||
if (!canvas.v) {
|
|
||||||
return {
|
|
||||||
error: 'This is not a 3D canvas',
|
|
||||||
success: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (color < 0 || color >= canvas.colors.length) {
|
|
||||||
return {
|
|
||||||
error: 'Invalid color selected',
|
|
||||||
success: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (canvas.req !== -1) {
|
|
||||||
if (user.id === null) {
|
|
||||||
return {
|
|
||||||
errorTitle: 'Not Logged In',
|
|
||||||
error: 'You need to be logged in to use this canvas.',
|
|
||||||
success: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
// if the canvas has a requirement of totalPixels that the user
|
|
||||||
// has to have set
|
|
||||||
const totalPixels = await user.getTotalPixels();
|
|
||||||
if (totalPixels < canvas.req) {
|
|
||||||
return {
|
|
||||||
errorTitle: 'Not Yet :(',
|
|
||||||
// eslint-disable-next-line max-len
|
|
||||||
error: `You need to set ${canvas.req} pixels on another canvas first, before you can use this one.`,
|
|
||||||
success: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const isAdmin = (user.userlvl === 1);
|
|
||||||
const setColor = await RedisCanvas.getPixel(canvasId, canvas.size, x, y, z);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* bitwise operation to get rid of protection
|
|
||||||
*/
|
|
||||||
let coolDown = ((setColor & 0x3F) >= clrIgnore && canvas.pcd)
|
|
||||||
? canvas.pcd : canvas.bcd;
|
|
||||||
if (isAdmin) {
|
|
||||||
coolDown = 0.0;
|
|
||||||
} else {
|
|
||||||
coolDown *= coolDownFactor;
|
|
||||||
}
|
|
||||||
|
|
||||||
const now = Date.now();
|
|
||||||
let wait = await user.getWait(canvasId);
|
|
||||||
if (!wait) wait = now;
|
|
||||||
wait += coolDown;
|
|
||||||
const waitLeft = wait - now;
|
|
||||||
if (waitLeft > canvas.cds) {
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
waitSeconds: (waitLeft - coolDown) / 1000,
|
|
||||||
coolDownSeconds: (canvas.cds - waitLeft) / 1000,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (setColor & 0x80
|
|
||||||
|| (canvas.v
|
|
||||||
&& x >= 96 && x <= 128 && z >= 35 && z <= 100
|
|
||||||
&& !isAdmin)
|
|
||||||
) {
|
|
||||||
logger.info(`${user.ip} tried to set on protected pixel (${x}, ${y})`);
|
|
||||||
return {
|
|
||||||
errorTitle: 'Pixel Protection',
|
|
||||||
error: 'This pixel is protected',
|
|
||||||
success: false,
|
|
||||||
waitSeconds: (waitLeft - coolDown) / 1000,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
setPixelByCoords(canvasId, color, x, y, z);
|
|
||||||
|
|
||||||
user.setWait(waitLeft, canvasId);
|
|
||||||
/* hardcode to not count pixels in antarctica */
|
|
||||||
// eslint-disable-next-line eqeqeq
|
|
||||||
if (canvas.ranked && (canvasId != 0 || y < 14450)) {
|
|
||||||
user.incrementPixelcount();
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
success: true,
|
|
||||||
waitSeconds: waitLeft / 1000,
|
|
||||||
coolDownSeconds: coolDown / 1000,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,13 +44,13 @@ async function saveIPInfo(ip, whoisRet, allowed, info) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* execute proxycheck without caring about cache
|
* execute proxycheck and blacklist whitelist check
|
||||||
* @param f function for checking if proxy
|
* @param f proxycheck function
|
||||||
* @param ip IP to check
|
* @param ip full ip
|
||||||
* @return true if proxy or blacklisted, false if not or whitelisted
|
* @param ipKey
|
||||||
|
* @return [ allowed, status, pcheck ]
|
||||||
*/
|
*/
|
||||||
async function withoutCache(f, ip) {
|
async function checkPCAndLists(f, ip, ipKey) {
|
||||||
const ipKey = getIPv6Subnet(ip);
|
|
||||||
let allowed = true;
|
let allowed = true;
|
||||||
let status = -2;
|
let status = -2;
|
||||||
let pcheck = null;
|
let pcheck = null;
|
||||||
|
@ -70,16 +70,33 @@ async function withoutCache(f, ip) {
|
||||||
allowed = res.allowed;
|
allowed = res.allowed;
|
||||||
pcheck = res.pcheck;
|
pcheck = res.pcheck;
|
||||||
}
|
}
|
||||||
} finally {
|
} catch (err) {
|
||||||
let whoisRet = null;
|
logger.error(`Error checkAllowed for ${ip}: ${err.message}`);
|
||||||
try {
|
|
||||||
whoisRet = await whois(ip);
|
|
||||||
} catch (err) {
|
|
||||||
logger.error(`Error whois for ${ip}: ${err.message}`);
|
|
||||||
}
|
|
||||||
await cacheAllowed(ipKey, status);
|
|
||||||
await saveIPInfo(ipKey, whoisRet || {}, status, pcheck);
|
|
||||||
}
|
}
|
||||||
|
return [allowed, status, pcheck];
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* execute proxycheck and whois and save result into cache
|
||||||
|
* @param f function for checking if proxy
|
||||||
|
* @param ip IP to check
|
||||||
|
* @return checkifAllowed return
|
||||||
|
*/
|
||||||
|
async function withoutCache(f, ip) {
|
||||||
|
const ipKey = getIPv6Subnet(ip);
|
||||||
|
|
||||||
|
const [
|
||||||
|
[allowed, status, pcheck],
|
||||||
|
whoisRet,
|
||||||
|
] = await Promise.all([
|
||||||
|
checkPCAndLists(f, ip, ipKey),
|
||||||
|
whois(ip),
|
||||||
|
]);
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
cacheAllowed(ipKey, status),
|
||||||
|
saveIPInfo(ipKey, whoisRet, status, pcheck),
|
||||||
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
allowed,
|
allowed,
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { DataTypes, QueryTypes } from 'sequelize';
|
import Sequelize, { DataTypes, QueryTypes } from 'sequelize';
|
||||||
|
|
||||||
import sequelize from './sequelize';
|
import sequelize from './sequelize';
|
||||||
import { generateHash } from '../../utils/hash';
|
import { generateHash } from '../../utils/hash';
|
||||||
|
@ -181,4 +181,42 @@ export async function getNamesToIds(ids) {
|
||||||
return idToNameMap;
|
return idToNameMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* take array of {id: useId, ...} object and resolve
|
||||||
|
* user informations
|
||||||
|
*/
|
||||||
|
export async function populateRanking(rawRanks) {
|
||||||
|
if (!rawRanks.length) {
|
||||||
|
return rawRanks;
|
||||||
|
}
|
||||||
|
const uids = rawRanks.map((r) => r.id);
|
||||||
|
const userData = await RegUser.findAll({
|
||||||
|
attributes: [
|
||||||
|
'id',
|
||||||
|
'name',
|
||||||
|
[
|
||||||
|
Sequelize.fn(
|
||||||
|
'DATEDIFF',
|
||||||
|
Sequelize.literal('CURRENT_TIMESTAMP'),
|
||||||
|
Sequelize.col('createdAt'),
|
||||||
|
),
|
||||||
|
'age',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
where: {
|
||||||
|
id: uids,
|
||||||
|
},
|
||||||
|
raw: true,
|
||||||
|
});
|
||||||
|
for (let i = 0; i < userData.length; i += 1) {
|
||||||
|
const { id, name, age } = userData[i];
|
||||||
|
const dat = rawRanks.find((r) => r.id === id);
|
||||||
|
if (dat) {
|
||||||
|
dat.name = name;
|
||||||
|
dat.age = age;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rawRanks;
|
||||||
|
}
|
||||||
|
|
||||||
export default RegUser;
|
export default RegUser;
|
||||||
|
|
|
@ -4,26 +4,12 @@
|
||||||
|
|
||||||
|
|
||||||
import getMe from '../../core/me';
|
import getMe from '../../core/me';
|
||||||
import {
|
|
||||||
USE_PROXYCHECK,
|
|
||||||
} from '../../core/config';
|
|
||||||
import checkIPAllowed from '../../core/isAllowed';
|
|
||||||
|
|
||||||
|
|
||||||
export default async (req, res, next) => {
|
export default async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const { user, lang } = req;
|
const { user, lang } = req;
|
||||||
const userdata = await getMe(user, lang);
|
const userdata = await getMe(user, lang);
|
||||||
user.updateLogInTimestamp();
|
user.updateLogInTimestamp();
|
||||||
|
|
||||||
const { trueIp: ip } = req;
|
|
||||||
if (USE_PROXYCHECK) {
|
|
||||||
// pre-fire ip check to give it time to get a real result
|
|
||||||
// once api_pixel needs it
|
|
||||||
checkIPAllowed(ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://stackoverflow.com/questions/49547/how-to-control-web-page-caching-across-all-browsers
|
|
||||||
res.json(userdata);
|
res.json(userdata);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
|
|
|
@ -22,7 +22,7 @@ import OnlineCounter from './packets/OnlineCounter';
|
||||||
import socketEvents from './socketEvents';
|
import socketEvents from './socketEvents';
|
||||||
import chatProvider, { ChatProvider } from '../core/ChatProvider';
|
import chatProvider, { ChatProvider } from '../core/ChatProvider';
|
||||||
import authenticateClient from './authenticateClient';
|
import authenticateClient from './authenticateClient';
|
||||||
import { drawByOffsets } from '../core/draw';
|
import drawByOffsets from '../core/draw';
|
||||||
import isIPAllowed from '../core/isAllowed';
|
import isIPAllowed from '../core/isAllowed';
|
||||||
|
|
||||||
|
|
||||||
|
@ -87,7 +87,6 @@ class SocketServer {
|
||||||
ws.chunkCnt = 0;
|
ws.chunkCnt = 0;
|
||||||
|
|
||||||
const { ip } = user;
|
const { ip } = user;
|
||||||
isIPAllowed(ip);
|
|
||||||
|
|
||||||
ws.send(OnlineCounter.dehydrate(socketEvents.onlineCounter));
|
ws.send(OnlineCounter.dehydrate(socketEvents.onlineCounter));
|
||||||
|
|
||||||
|
@ -174,6 +173,7 @@ class SocketServer {
|
||||||
const { headers } = request;
|
const { headers } = request;
|
||||||
// Limiting socket connections per ip
|
// Limiting socket connections per ip
|
||||||
const ip = getIPFromRequest(request);
|
const ip = getIPFromRequest(request);
|
||||||
|
isIPAllowed(ip);
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const limiter = rateLimit.get(ip);
|
const limiter = rateLimit.get(ip);
|
||||||
if (limiter && limiter[1]) {
|
if (limiter && limiter[1]) {
|
||||||
|
@ -506,7 +506,6 @@ class SocketServer {
|
||||||
pxlCnt,
|
pxlCnt,
|
||||||
rankedPxlCnt,
|
rankedPxlCnt,
|
||||||
retCode,
|
retCode,
|
||||||
needProxycheck,
|
|
||||||
} = await drawByOffsets(
|
} = await drawByOffsets(
|
||||||
ws.user,
|
ws.user,
|
||||||
canvasId,
|
canvasId,
|
||||||
|
@ -514,10 +513,6 @@ class SocketServer {
|
||||||
pixels,
|
pixels,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (needProxycheck) {
|
|
||||||
isIPAllowed(ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (retCode > 9 && retCode !== 13) {
|
if (retCode > 9 && retCode !== 13) {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (limiter && limiter[0] > now) {
|
if (limiter && limiter[0] > now) {
|
||||||
|
|
|
@ -245,7 +245,7 @@ class ProxyCheck {
|
||||||
if (!key) {
|
if (!key) {
|
||||||
setTimeout(
|
setTimeout(
|
||||||
() => reject(new Error('No pc key available')),
|
() => reject(new Error('No pc key available')),
|
||||||
5000,
|
2000,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -318,7 +318,7 @@ class ProxyCheck {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
req.setTimeout(60000, () => {
|
req.setTimeout(30000, () => {
|
||||||
req.destroy(new Error('Connection TIMEOUT'));
|
req.destroy(new Error('Connection TIMEOUT'));
|
||||||
});
|
});
|
||||||
req.on('error', (err) => {
|
req.on('error', (err) => {
|
||||||
|
|
|
@ -11,124 +11,146 @@ const WHOIS_PORT = 43;
|
||||||
const QUERY_SUFFIX = '\r\n';
|
const QUERY_SUFFIX = '\r\n';
|
||||||
const WHOIS_TIMEOUT = 30000;
|
const WHOIS_TIMEOUT = 30000;
|
||||||
|
|
||||||
function splitStringBy(string, by) {
|
/*
|
||||||
return [string.slice(0, by), string.slice(by + 1)];
|
* parse whois return into fields
|
||||||
}
|
*/
|
||||||
|
|
||||||
function parseSimpleWhois(whois) {
|
function parseSimpleWhois(whois) {
|
||||||
const data = {};
|
let data = {
|
||||||
const text = [];
|
groups: {},
|
||||||
|
|
||||||
const renameLabels = {
|
|
||||||
NetRange: 'range',
|
|
||||||
inetnum: 'range',
|
|
||||||
CIDR: 'route',
|
|
||||||
origin: 'asn',
|
|
||||||
OriginAS: 'asn',
|
|
||||||
};
|
|
||||||
const lineToGroup = {
|
|
||||||
contact: 'contact',
|
|
||||||
OrgName: 'organisation',
|
|
||||||
organisation: 'organisation',
|
|
||||||
OrgAbuseHandle: 'contactAbuse',
|
|
||||||
irt: 'contactAbuse',
|
|
||||||
RAbuseHandle: 'contactAbuse',
|
|
||||||
OrgTechHandle: 'contactTechnical',
|
|
||||||
RTechHandle: 'contactTechnical',
|
|
||||||
OrgNOCHandle: 'contactNoc',
|
|
||||||
RNOCHandle: 'contactNoc',
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (whois.includes('returned 0 objects') || whois.includes('No match found')) {
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
let resultNum = 0;
|
|
||||||
const groups = [{}];
|
const groups = [{}];
|
||||||
|
const text = [];
|
||||||
|
const lines = whois.split('\n');
|
||||||
let lastLabel;
|
let lastLabel;
|
||||||
|
|
||||||
whois.split('\n').forEach((line) => {
|
for (let i = 0; i < lines.length; i += 1) {
|
||||||
// catch comment lines
|
const line = lines[i].trim();
|
||||||
if (line.startsWith('%') || line.startsWith('#')) {
|
if (line.startsWith('%') || line.startsWith('#')) {
|
||||||
// detect if an ASN or IP has multiple WHOIS results
|
/*
|
||||||
if (line.includes('# start')) {
|
* detect if an ASN or IP has multiple WHOIS results,
|
||||||
// nothing
|
* and only care about first one
|
||||||
} else if (line.includes('# end')) {
|
*/
|
||||||
resultNum++;
|
if (line.includes('# end')) {
|
||||||
} else {
|
break;
|
||||||
|
} else if (!lines.includes('# start')) {
|
||||||
text.push(line);
|
text.push(line);
|
||||||
}
|
}
|
||||||
} else if (resultNum === 0) {
|
continue;
|
||||||
// for the moment, parse only first WHOIS result
|
}
|
||||||
|
if (line) {
|
||||||
if (line) {
|
const sep = line.indexOf(':');
|
||||||
if (line.includes(':')) {
|
if (~sep) {
|
||||||
const [label, value] = splitStringBy(line, line.indexOf(':')).map((info) => info.trim());
|
const label = line.slice(0, sep).toLowerCase();
|
||||||
lastLabel = label;
|
lastLabel = label;
|
||||||
|
const value = line.slice(sep + 1).trim();
|
||||||
// 1) Filter out unnecessary info, 2) then detect if the label is already added to group
|
// 1) Filter out unnecessary info, 2) then detect if the label is already added to group
|
||||||
if (value.includes('---')) {
|
if (value.includes('---')) {
|
||||||
// do nothing with useless data
|
// do nothing with useless data
|
||||||
} else if (groups[groups.length - 1][label]) {
|
} else if (groups[groups.length - 1][label]) {
|
||||||
groups[groups.length - 1][label] += `\n${value}`;
|
groups[groups.length - 1][label] += `\n${value}`;
|
||||||
} else {
|
|
||||||
groups[groups.length - 1][label] = value;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
groups[groups.length - 1][lastLabel] += `\n${line.trim()}`;
|
groups[groups.length - 1][label] = value;
|
||||||
}
|
}
|
||||||
} else if (Object.keys(groups[groups.length - 1]).length) {
|
} else {
|
||||||
// if empty line, means another info group starts
|
groups[groups.length - 1][lastLabel] += `\n${line}`;
|
||||||
groups.push({});
|
|
||||||
}
|
}
|
||||||
|
} else if (Object.keys(groups[groups.length - 1]).length) {
|
||||||
|
// if empty line, means another info group starts
|
||||||
|
groups.push({});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
groups.forEach((group) => {
|
||||||
|
if (group.role) {
|
||||||
|
const role = group.role.replaceAll(' ', '-').toLowerCase();
|
||||||
|
delete group.role;
|
||||||
|
data.groups[role] = group;
|
||||||
|
} else {
|
||||||
|
data = {
|
||||||
|
...group,
|
||||||
|
...data,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
groups
|
data.text = text.join('\n');
|
||||||
.filter((group) => Object.keys(group).length)
|
|
||||||
.forEach((group) => {
|
|
||||||
const groupLabels = Object.keys(group);
|
|
||||||
let isGroup = false;
|
|
||||||
|
|
||||||
// check if a label is marked as group
|
|
||||||
groupLabels.forEach((groupLabel) => {
|
|
||||||
if (!isGroup && Object.keys(lineToGroup).includes(groupLabel)) {
|
|
||||||
isGroup = lineToGroup[groupLabel];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// check if a info group is a Contact in APNIC result
|
|
||||||
// @Link https://www.apnic.net/manage-ip/using-whois/guide/role/
|
|
||||||
if (!isGroup && groupLabels.includes('role')) {
|
|
||||||
isGroup = `Contact ${group.role.split(' ')[1]}`;
|
|
||||||
} else if (!isGroup && groupLabels.includes('person')) {
|
|
||||||
isGroup = `Contact ${group['nic-hdl']}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isGroup === 'contact') {
|
|
||||||
data.contacts = data.contacts || {};
|
|
||||||
data.contacts[group.contact] = group;
|
|
||||||
} else if (isGroup) {
|
|
||||||
data[isGroup] = group;
|
|
||||||
} else {
|
|
||||||
groupLabels.forEach((key) => {
|
|
||||||
const label = renameLabels[key] || key;
|
|
||||||
data[label] = group[key];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Append the WHOIS comments
|
|
||||||
data.text = text;
|
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
function whoisQuery(
|
/*
|
||||||
host = null,
|
* parse whois return
|
||||||
query = '',
|
* @param ip ip string
|
||||||
|
* @param whois whois return
|
||||||
|
* @return object with whois data
|
||||||
|
*/
|
||||||
|
function parseWhois(ip, whoisReturn) {
|
||||||
|
const whoisData = parseSimpleWhois(whoisReturn);
|
||||||
|
|
||||||
|
let cidr;
|
||||||
|
if (isIPv6(ip)) {
|
||||||
|
const range = whoisData.inet6num || whoisData.netrange || whoisData.inetnum
|
||||||
|
|| whoisData.route || whoisData.cidr;
|
||||||
|
cidr = range && !range.includes('-') && range;
|
||||||
|
} else {
|
||||||
|
const range = whoisData.inetnum || whoisData.netrange
|
||||||
|
|| whoisData.route || whoisData.cidr;
|
||||||
|
if (range) {
|
||||||
|
if (range.includes('/') && !range.includes('-')) {
|
||||||
|
cidr = range;
|
||||||
|
} else {
|
||||||
|
cidr = ip4InRangeToCIDR(ip, range);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let org = whoisData['org-name']
|
||||||
|
|| whoisData.organization
|
||||||
|
|| whoisData.orgname
|
||||||
|
|| whoisData.descr
|
||||||
|
|| whoisData['mnt-by'];
|
||||||
|
if (!org) {
|
||||||
|
const contactGroup = Object.keys(whoisData.groups).find(
|
||||||
|
(g) => whoisData.groups[g].address,
|
||||||
|
);
|
||||||
|
if (contactGroup) {
|
||||||
|
[org] = whoisData.groups[contactGroup].address.split('\n');
|
||||||
|
} else {
|
||||||
|
org = whoisData.owner || whoisData['mnt-by'] || 'N/A';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const descr = whoisData.netname || whoisData.descr || 'N/A';
|
||||||
|
const asn = whoisData.asn
|
||||||
|
|| whoisData.origin
|
||||||
|
|| whoisData.originas
|
||||||
|
|| whoisData['aut-num'] || 'N/A';
|
||||||
|
let country = whoisData.country
|
||||||
|
|| (whoisData.organisation && whoisData.organisation.Country)
|
||||||
|
|| 'xx';
|
||||||
|
if (country.length > 2) {
|
||||||
|
country = country.slice(0, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
ip,
|
||||||
|
cidr: cidr || 'N/A',
|
||||||
|
org,
|
||||||
|
country,
|
||||||
|
asn,
|
||||||
|
descr,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* send a raw whois query to server
|
||||||
|
* @param query
|
||||||
|
* @param host
|
||||||
|
*/
|
||||||
|
function singleWhoisQuery(
|
||||||
|
query,
|
||||||
|
host,
|
||||||
) {
|
) {
|
||||||
console.log('whois with query', query, 'for host', host);
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let data = '';
|
let data = '';
|
||||||
const socket = net.createConnection({
|
const socket = net.createConnection({
|
||||||
|
@ -144,126 +166,76 @@ function whoisQuery(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function whoisIp(query) {
|
|
||||||
query = String(query);
|
|
||||||
|
|
||||||
// find WHOIS server for IP
|
|
||||||
let whoisResult = await whoisQuery('whois.iana.org', query);
|
|
||||||
whoisResult = parseSimpleWhois(whoisResult);
|
|
||||||
const host = whoisResult.whois;
|
|
||||||
|
|
||||||
if (!host) {
|
|
||||||
throw new Error(`No WHOIS server for "${query}"`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// hardcoded custom queries..
|
|
||||||
console.log('HOST', host);
|
|
||||||
if (host === 'whois.arin.net') {
|
|
||||||
query = `+ n ${query}`;
|
|
||||||
} else if (host === 'whois.ripe.net') {
|
|
||||||
/*
|
|
||||||
* flag to not return personal informations, otherwise
|
|
||||||
* RIPE is gonne rate limit and ban
|
|
||||||
*/
|
|
||||||
query = `-r ${query}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const rawResult = await whoisQuery(host, query);
|
|
||||||
console.log(rawResult);
|
|
||||||
const data = parseSimpleWhois(rawResult);
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* get CIDR of ip from whois return
|
* check if whois result is refering us to
|
||||||
* @param ip ip string
|
* a different whois server
|
||||||
* @param whois whois return
|
|
||||||
* @return cidr string
|
|
||||||
*/
|
*/
|
||||||
function cIDRofWhois(ip, whoisData) {
|
const referralKeys = [
|
||||||
if (isIPv6(ip)) {
|
'whois:',
|
||||||
return whoisData.inet6num
|
'refer:',
|
||||||
|| (whoisData.range && !whoisData.range.includes('-') && whoisData.range)
|
'ReferralServer:',
|
||||||
|| whoisData.route
|
];
|
||||||
|| null;
|
function checkForReferral(
|
||||||
}
|
whoisResult,
|
||||||
const { range } = whoisData;
|
) {
|
||||||
if (range && range.includes('/') && !range.includes('-')) {
|
for (let u = 0; u < referralKeys.length; u += 1) {
|
||||||
return range;
|
const key = referralKeys[u];
|
||||||
}
|
const pos = whoisResult.indexOf(key);
|
||||||
return ip4InRangeToCIDR(ip, range) || null;
|
if (~pos) {
|
||||||
}
|
const line = whoisResult.slice(
|
||||||
|
whoisResult.lastIndexOf('\n', pos) + 1,
|
||||||
/*
|
whoisResult.indexOf('\n', pos),
|
||||||
* get organisation from whois return
|
).trim();
|
||||||
* @param whois whois return
|
if (!line.startsWith(key)) {
|
||||||
* @return organisation string
|
continue;
|
||||||
*/
|
|
||||||
function orgFromWhois(whoisData) {
|
|
||||||
return (whoisData.organisation && whoisData.organisation['org-name'])
|
|
||||||
|| (whoisData.organisation && whoisData.organisation.OrgName)
|
|
||||||
|| (whoisData['Contact Master']
|
|
||||||
&& whoisData['Contact Master'].address.split('\n')[0])
|
|
||||||
|| (whoisData['Contact undefined']
|
|
||||||
&& whoisData['Contact undefined'].person)
|
|
||||||
|| whoisData.netname
|
|
||||||
|| whoisData.owner
|
|
||||||
|| 'N/A';
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* get counry from whois return
|
|
||||||
* @param whois whois return
|
|
||||||
* @return organisation string
|
|
||||||
*/
|
|
||||||
function countryFromWhois(whoisData) {
|
|
||||||
return whoisData.country
|
|
||||||
|| (whoisData.organisation && whoisData.organisation.Country)
|
|
||||||
|| 'xx';
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* parse whois return
|
|
||||||
* @param ip ip string
|
|
||||||
* @param whois whois return
|
|
||||||
* @return object with whois data
|
|
||||||
*/
|
|
||||||
function parseWhois(ip, whoisData) {
|
|
||||||
return {
|
|
||||||
ip,
|
|
||||||
country: countryFromWhois(whoisData),
|
|
||||||
cidr: cIDRofWhois(ip, whoisData) || 'N/A',
|
|
||||||
org: orgFromWhois(whoisData),
|
|
||||||
descr: whoisData.descr || 'N/A',
|
|
||||||
asn: whoisData.asn || whoisData['aut-num'] || 'N/A',
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export default async function whoiser(ip) {
|
|
||||||
const whoisData = await whoisIp(ip);
|
|
||||||
if (whoisData.ReferralServer) {
|
|
||||||
let referral = whoisData.ReferralServer;
|
|
||||||
const prot = referral.indexOf('://');
|
|
||||||
if (prot !== -1) {
|
|
||||||
referral = referral.slice(prot + 3);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
/*
|
|
||||||
* if referral whois server produces any error
|
|
||||||
* fallback to initial one
|
|
||||||
*/
|
|
||||||
const refWhoisData = await whoisIp(ip, {
|
|
||||||
host: referral,
|
|
||||||
});
|
|
||||||
const refParsedData = parseWhois(ip, refWhoisData);
|
|
||||||
if (refParsedData.cidr !== 'N/A') {
|
|
||||||
return refParsedData;
|
|
||||||
}
|
}
|
||||||
} catch {
|
let value = line.slice(line.indexOf(':') + 1).trim();
|
||||||
// nothing
|
const prot = value.indexOf('://');
|
||||||
|
if (~prot) {
|
||||||
|
value = value.slice(prot + 3);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(whoisData);
|
return null;
|
||||||
return parseWhois(ip, whoisData);
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* whois ip
|
||||||
|
*/
|
||||||
|
export default async function whoisIp(
|
||||||
|
ip,
|
||||||
|
host = null,
|
||||||
|
) {
|
||||||
|
let useHost = host || 'whois.iana.org';
|
||||||
|
let whoisResult = '';
|
||||||
|
let refCnt = 0;
|
||||||
|
while (refCnt < 5) {
|
||||||
|
let queryPrefix = '';
|
||||||
|
if (useHost === 'whois.arin.net') {
|
||||||
|
queryPrefix = '+ n';
|
||||||
|
} else if (useHost === 'whois.ripe.net') {
|
||||||
|
/*
|
||||||
|
* flag to not return personal informations, otherwise
|
||||||
|
* RIPE is gonne rate limit and ban
|
||||||
|
*/
|
||||||
|
// queryPrefix = '-r';
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// eslint-disable-next-line no-await-in-loop
|
||||||
|
whoisResult = await singleWhoisQuery(`${queryPrefix} ${ip}`, useHost);
|
||||||
|
const ref = checkForReferral(whoisResult);
|
||||||
|
if (!ref) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
useHost = ref;
|
||||||
|
} catch (err) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.error(`Error on WHOIS ${ip} ${useHost}: ${err.message}`);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
refCnt += 1;
|
||||||
|
}
|
||||||
|
return parseWhois(ip, whoisResult);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user