better logging

This commit is contained in:
HF 2020-01-04 14:08:56 +01:00
parent b484ff17b6
commit 1e76ced03a
4 changed files with 14 additions and 14 deletions

View File

@ -19,7 +19,7 @@ import logger from './logger';
async function getIPIntel(ip: string): Promise<boolean> { async function getIPIntel(ip: string): Promise<boolean> {
const email = `${Math.random().toString(36).substring(8)}-${Math.random().toString(36).substring(4)}@gmail.com`; const email = `${Math.random().toString(36).substring(8)}-${Math.random().toString(36).substring(4)}@gmail.com`;
const url = `http://check.getipintel.net/check.php?ip=${ip}&contact=${email}&flags=m`; const url = `http://check.getipintel.net/check.php?ip=${ip}&contact=${email}&flags=m`;
logger.info('fetching getipintel', url); logger.info(`PROXYCHECK fetching getipintel ${url}`);
const response = await fetch(url, { const response = await fetch(url, {
headers: { headers: {
Accept: '*/*', Accept: '*/*',
@ -29,13 +29,13 @@ async function getIPIntel(ip: string): Promise<boolean> {
}, },
}); });
// TODO log response code // TODO log response code
logger.debug('getipintel?', ip); logger.debug('PROXYCHECK getipintel? %s', ip);
if (!response.ok) { if (!response.ok) {
const text = await response.text(); const text = await response.text();
throw new Error(`getipintel not ok ${response.status}/${text}`); throw new Error(`PROXYCHECK getipintel not ok ${response.status}/${text}`);
} }
const body = await response.text(); const body = await response.text();
logger.info('fetch getipintel is proxy?', ip, body); logger.info('PROXYCHECK fetch getipintel is proxy? %s : %s', ip, body);
// returns tru iff we found 1 in the response and was ok (http code = 200) // returns tru iff we found 1 in the response and was ok (http code = 200)
const value = parseFloat(body); const value = parseFloat(body);
return value > 0.995; return value > 0.995;
@ -49,7 +49,7 @@ async function getIPIntel(ip: string): Promise<boolean> {
*/ */
async function getProxyCheck(ip: string): Promise<boolean> { async function getProxyCheck(ip: string): Promise<boolean> {
const url = `http://proxycheck.io/v2/${ip}?risk=1&vpn=1&asn=1`; const url = `http://proxycheck.io/v2/${ip}?risk=1&vpn=1&asn=1`;
logger.info('fetching proxycheck', url); logger.info('PROXYCHECK fetching proxycheck %s', url);
const response = await fetch(url, { const response = await fetch(url, {
headers: { headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
@ -60,7 +60,7 @@ async function getProxyCheck(ip: string): Promise<boolean> {
throw new Error(`proxycheck not ok ${response.status}/${text}`); throw new Error(`proxycheck not ok ${response.status}/${text}`);
} }
const data = await response.json(); const data = await response.json();
logger.info('proxycheck is proxy?', ip, data); logger.info('PROXYCHECK proxycheck is proxy?', data);
return data.status == 'ok' && data[ip].proxy === 'yes'; return data.status == 'ok' && data[ip].proxy === 'yes';
} }
@ -72,7 +72,7 @@ async function getProxyCheck(ip: string): Promise<boolean> {
* @return true if proxy, false if not * @return true if proxy, false if not
*/ */
async function getShroomey(ip: string): Promise<boolean> { async function getShroomey(ip: string): Promise<boolean> {
logger.info('fetching shroomey', ip); logger.info('PROXYCHECK fetching shroomey %s', ip);
const response = await fetch(`http://www.shroomery.org/ythan/proxycheck.php?ip=${ip}`, { const response = await fetch(`http://www.shroomery.org/ythan/proxycheck.php?ip=${ip}`, {
headers: { headers: {
Accept: '*/*', Accept: '*/*',
@ -83,7 +83,7 @@ async function getShroomey(ip: string): Promise<boolean> {
}); });
if (!response.ok) throw new Error('shroomery.org not ok'); if (!response.ok) throw new Error('shroomery.org not ok');
const body = await response.text(); const body = await response.text();
logger.info('fetch shroomey is proxy?', ip, body); logger.info('PROXYCHECK fetch shroomey is proxy? %s %s', ip, body);
return body === 'Y'; return body === 'Y';
} }
@ -168,10 +168,10 @@ async function withCache(f, ip) {
const cache = await redis.getAsync(key); const cache = await redis.getAsync(key);
if (cache) { if (cache) {
const str = cache.toString('utf8'); const str = cache.toString('utf8');
logger.debug('fetch isproxy from cache', key, cache, typeof cache, str, typeof str); logger.debug('PROXYCHECK fetch isproxy from cache %s %s %s %s %s', key, cache, typeof cache, str, typeof str);
return str === 'y'; return str === 'y';
} }
logger.debug('fetch isproxy not from cache', key); logger.debug('PROXYCHECK fetch isproxy not from cache %s', key);
// else make asynchronous ipcheck and assume no proxy in the meantime // else make asynchronous ipcheck and assume no proxy in the meantime
// use lock to just check three at a time // use lock to just check three at a time
@ -188,7 +188,7 @@ async function withCache(f, ip) {
lock += 1; lock += 1;
}) })
.catch((error) => { .catch((error) => {
logger.error('withCache', error.message || error); logger.error('PROXYCHECK withCache %s', error.message || error);
const pos = checking.indexOf(ip); const pos = checking.indexOf(ip);
if (~pos) checking.splice(pos, 1); if (~pos) checking.splice(pos, 1);
lock += 1; lock += 1;

View File

@ -7,10 +7,10 @@
import { createLogger, format, transports } from 'winston'; import { createLogger, format, transports } from 'winston';
const logger = createLogger({ const logger = createLogger({
level: 'info', level: 'info',
format: format.combine( format: format.combine(
format.splat(),
format.simple(), format.simple(),
), ),
transports: [ transports: [

View File

@ -35,7 +35,7 @@ async function verifyClient(info, done) {
// Limiting socket connections per ip // Limiting socket connections per ip
const ip = await getIPFromRequest(req); const ip = await getIPFromRequest(req);
logger.info('Got ws request from', ip); logger.info(`Got ws request from ${ip}`);
if (ipCounter.get(ip) > 50) { if (ipCounter.get(ip) > 50) {
logger.info(`Client ${ip} has more than 50 connections open.`); logger.info(`Client ${ip} has more than 50 connections open.`);
return done(false); return done(false);

View File

@ -13,7 +13,7 @@ import logger from '../core/logger';
function randomProxyURL() { function randomProxyURL() {
const rand = proxylist[Math.floor(Math.random() * proxylist.length)]; const rand = proxylist[Math.floor(Math.random() * proxylist.length)];
logger.info('choosesn fetch proxy', rand); logger.info(`choosesn fetch proxy ${rand}`);
return rand; return rand;
} }