-
Notifications
You must be signed in to change notification settings - Fork 1
/
pf_geoip.user.js
496 lines (437 loc) · 18.8 KB
/
pf_geoip.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// ==UserScript==
// @name ProgrammersForumGeoIp
// @namespace http://programmersforum.ru/
// @version 1.12
// @description adds country/city info on the page with user IP, as well as current user agent, IP for online user
// @author Alex P
// @include *programmersforum.ru/postings.php?do=getip*
// @include *programmersforum.ru/online.php?*ua=1*
// @include *programmersforum.ru
// @include *programmersforum.ru/
// @include *programmersforum.ru/index.php
// @grant none
// @downloadURL https://github.com/AlexP11223/ProgForumRuUserscripts/raw/master/pf_geoip.user.js
// ==/UserScript==
(function () {
'use strict';
// free API keys
//const IPSTACK_API_KEY = 'b890dc8f7bc14c40deb7af6a2f9be451';
const IPDATA_API_KEY = 'c8648f40fbd7bfcab452647636b8dd6344d430acc54019cacd6b0f34';
const IPGEOLOCATION_API_KEY = '02accd26591949d7adad15565717a3c2';
function getJson(url, success, error) {
$.getJSON(url, function (data) {
success(data);
}).fail(function (request) {
error(`HTTP error ${request.status} ${request.statusText}`);
});
}
function parseUrlQuery(queryStr) {
const dict = {};
const queries = queryStr.replace(/^\?/, '').split('&');
let i;
for (i = 0; i < queries.length; i++) {
const parts = queries[i].split('=');
dict[parts[0]] = parts[1];
}
return dict;
}
// no HTTPS, cannot use on a HTTPS website
// function requestIpApi(ip, success, error) {
// getJson(`http://ip-api.com/json/${ip}`, function (data) {
// if (data.status === 'success') {
// success({country: data.country, countryCode: data.countryCode.toLowerCase(), region: data.regionName, city: data.city, isp: data.isp});
// } else {
// error(`API error ${data.message}`);
// }
// },
// error);
// }
// HTTPS only in the paid tiers
// function requestIpstack(ip, success, error) {
// getJson(`http://api.ipstack.com/${ip}?access_key=${IPSTACK_API_KEY}`, function (data) {
// success({country: data.country_name, countryCode: data.country_code.toLowerCase(), region: data.region_name, city: data.city});
// },
// error);
// }
function requestIpData(ip, success, error) {
getJson(`https://api.ipdata.co/${ip}?api-key=${IPDATA_API_KEY}`, function (data) {
let threat = null;
const threatData = data.threat;
if (threatData) {
const activeThreats = Object.keys(threatData)
.map(function (key) {
return [key, threatData[key]];
})
.filter(function (it) {
return it[1] && ['is_anonymous', 'is_threat'].indexOf(it[0]) < 0;
})
.map(function (it) {
return it[0].replace(/is_/gi, '');
});
threat = activeThreats.join(', ');
}
success({country: data.country_name, countryCode: data.country_code.toLowerCase(), region: data.region, city: data.city, isp: data.organisation, threat: threat});
},
error);
}
// looks the same as ipdata.co
// function requestIpApiCo(ip, success, error) {
// getJson(`https://ipapi.co/${ip}/json/`, function (data) {
// success({country: data.country_name, countryCode: data.country.toLowerCase(), region: data.region, city: data.city, isp: data.org});
// },
// error);
// }
function requestIpInfo(ip, success, error) {
getJson(`https://ipinfo.io/${ip}`, function (data) {
success({country: data.country, countryCode: data.country.toLowerCase(), region: data.region, city: data.city, isp: data.org});
},
error);
}
function requestGeoipDb(ip, success, error) {
getJson(`https://geoip-db.com/json/${ip}`, function (data) {
success({country: data.country_name, countryCode: data.country_code.toLowerCase(), region: data.state, city: data.city});
},
error);
}
function requestIpGeoLocation(ip, success, error) {
getJson(`https://api.ipgeolocation.io/ipgeo?apiKey=${IPGEOLOCATION_API_KEY}&lang=ru&ip=${ip}`, function (data) {
success({country: data.country_name, countryCode: data.country_code2.toLowerCase(), region: data.state_prov, district: data.district, city: data.city, isp: data.isp});
},
error);
}
const GEOIP_PROVIDERS = [
{
name: 'ipdata.co',
request: requestIpData
},
{
name: 'geoip-db.com',
request: requestGeoipDb
},
{
name: 'ipinfo.io',
request: requestIpInfo
},
{
name: 'ipgeolocation.io',
request: requestIpGeoLocation
},
];
function getSecurityToken(html) {
const m = html.match(/var SECURITYTOKEN = "(.+)"/);
return m ? m[1] : null;
}
function getUserAgent(ipUaCell) {
const cellHtml = $(ipUaCell).html();
return cellHtml.substr(cellHtml.indexOf('<br>') + 4).trim();
}
function loadOnlineInfoForUser(userId, success, error) {
$.get('/online.php?s=&sortfield=time&sortorder=desc&who=members&ua=1&pp=50', function (html) {
const doc = $(html);
const userNode = doc.find(`#woltable a[href="member.php?u=${userId}"]`);
if (!userNode.length) {
error('Оффлайн');
return;
}
const row = userNode.closest('tr');
const ipResolveLink = row.find('a[id^="resolveip"]');
const ipUaCell = ipResolveLink.parent();
const ua = getUserAgent(ipUaCell);
const ip = ipResolveLink.text().trim();
const time = row.find('.time').html();
const securityToken = getSecurityToken(html);
if (!securityToken){
success(ua, ip, null, time);
}
$.post(ipResolveLink.attr('href'), {
'securitytoken': securityToken,
'do': 'resolveip',
'ajax': 1,
'ipaddress': ip
}, function(result) {
const host = $(result).text();
success(ua, ip, host, time);
}).fail(function() {
success(ua, ip, null, time);
});
}).fail(function(request) {
error(`HTTP error ${request.status} ${request.statusText}`);
});
}
function loadOnlineInfo(success, error) {
const urlQuery = parseUrlQuery(window.location.search);
if (!urlQuery['p']) {
return;
}
const postId = parseInt(urlQuery['p']);
$.get(`/showpost.php?p=${postId}`, function (html) {
const doc = $(html);
const href = doc.find('.bigusername').attr('href');
const query = parseUrlQuery(href.substr(href.indexOf('?') + 1));
const userId = query['u'];
loadOnlineInfoForUser(userId, success, error);
}).fail(function(request) {
error(`HTTP error ${request.status} ${request.statusText}`);
});
}
function formatGeoipData(data) {
const countryFlagUrl = `https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.2.1/flags/4x3/${data.countryCode}.svg`;
const locationParts = [data.country, data.region, data.city, data.district]
.filter(function (it) { return it; }); // filter nulls
let result = `${getImgHtml(countryFlagUrl, 16, 12)} ${locationParts.join(', ')}`;
if (data.isp) {
result += ` (провайдер ${data.isp})`;
}
if (data.threat) {
result += ` <img src="https://www.programmersforum.ru/images/1070/buttons/report.gif"/> <span style="color: red; font-weight: bold;">${data.threat}</span>`;
}
return result;
}
function formatError(error) {
return `<span style="color: red;">${error}</span>`;
}
function loadScript(url, callback) {
const script = document.createElement('script');
script.onload = callback;
script.setAttribute("type","text/javascript");
script.setAttribute("src", url);
document.getElementsByTagName("head")[0].appendChild(script);
}
function parseUserAgent(userAgent, callback) {
loadScript('https://cdn.jsdelivr.net/npm/ua-parser-js@0/dist/ua-parser.min.js', function () {
callback(UAParser(userAgent));
});
}
// some user-agents are truncated
function isYandexBrowser(ua) {
return ua.split(' ').pop().indexOf('Ya') === 0;
}
function isChrome(ua) {
return ua.split(' ').pop().indexOf('Ch') === 0;
}
function getOsNameVersion(uaData) {
const name = uaData.os.name;
const version = uaData.os.version;
return [name, version].join(' ').trim();
}
function getBrowserNameVersion(uaData) {
let name = uaData.browser.name;
let version = uaData.browser.version;
if (name === 'Chrome' && isYandexBrowser(uaData.ua)) {
name = 'Yandex';
version = '';
}
return [name, version].join(' ').trim();
}
function getOsIcon(uaData) {
if (!uaData.os.name)
return null;
const win8Icon = 'https://i.imgur.com/OC1xkLD.png';
const winIcon = 'https://i.imgur.com/o08ewuG.png';
const linuxIcon = 'https://i.imgur.com/I1JBl7R.png';
const appleIcon = 'https://i.imgur.com/m8Iw72B.png';
const androidIcon = 'https://i.imgur.com/Pmi5K2W.png';
switch (uaData.os.name) {
case 'Windows':
case 'Windows Server':
case 'Windows Phone':
switch (uaData.os.version) {
case '10':
case '8':
case '8.1':
case '2012':
case '2016':
return win8Icon;
default:
return winIcon;
}
case 'Linux':
case 'Unix':
case 'Ubuntu':
case 'Mint':
case 'Arch':
case 'Gentoo':
case 'RedHat':
case 'SUSE':
return linuxIcon;
case 'Mac OS':
case 'iOS':
return appleIcon;
case 'Android':
return androidIcon;
default:
if (uaData.os.name.indexOf('Windows') > -1)
return winIcon;
if (uaData.os.name.indexOf('Linux') > -1 || uaData.os.name.indexOf('BSD') || uaData.os.name.indexOf('GNU'))
return linuxIcon;
return null;
}
}
function getDeviceIcon(uaData) {
switch (uaData.os.name) {
case 'iOS':
case 'Android':
case 'Windows Phone':
case 'Windows Mobile':
case 'IEMobile':
return 'https://i.imgur.com/0bSHkRs.png';
default:
return null;
}
}
function getBrowserIcon(uaData) {
function getIconId() {
if (!uaData.browser.name)
return null;
switch (uaData.browser.name.toLowerCase()) {
case 'firefox':
case 'iceweasel':
case 'icecat':
case 'icedragon':
case 'iceape':
return 'firefox/firefox';
case 'chrome':
case 'chrome webview':
case 'chrome headless':
case 'chromium':
case 'comodo dragon':
if (isYandexBrowser(uaData.ua))
return 'yandex/yandex';
return 'chrome/chrome';
case 'yandex':
return 'yandex/yandex';
case 'safari':
return 'safari/safari';
case 'opera':
case 'opera mini':
case 'opera tablet':
case 'opera mobi':
case 'opera coast':
return 'opera/opera';
case 'vivaldi':
return 'vivaldi/vivaldi';
case '2345explorer':
return 'archive/internet-explorer_6/internet-explorer_6';
case 'iemobile':
case 'ie':
if (!uaData.browser.version)
return 'archive/internet-explorer_6/internet-explorer_6';
if (uaData.browser.version[0] === '7' || uaData.browser.version[0] === '8')
return 'archive/internet-explorer_7-8/internet-explorer_7-8';
return 'archive/internet-explorer_9-11/internet-explorer_9-11';
case 'edge':
return 'edge/edge';
default:
if (isChrome(uaData.ua))
return 'chrome/chrome';
return null;
}
}
const id = getIconId();
return id ? `https://cdnjs.cloudflare.com/ajax/libs/browser-logos/46.1.0/${id}_32x32.png` : null;
}
function getParsedUserAgentLineHtml(uaData) {
return [
`${getImgHtml(getDeviceIcon(uaData), 16, 16)} ${getImgHtml(getOsIcon(uaData), 16, 16)} ${getOsNameVersion(uaData)}`,
`${getImgHtml(getBrowserIcon(uaData), 16, 16)} ${getBrowserNameVersion(uaData)}`]
.map(s => s.trim())
.filter(Boolean)
.join(', ');
}
function getImgHtml(url, width, height) {
if (!url)
return '';
return `<img src="${url}" height="${width}" width="${height}" style="vertical-align: middle;"/>`
}
function elementFromString(html) {
const range = document.createRange();
return range.createContextualFragment(html);
}
function appendLine(parent, name, content) {
parent.appendChild(elementFromString(`<div>${name}: <strong>${content}</strong></div>`));
}
function appendLineSimple(parent, name, content) {
parent.appendChild(elementFromString(`<div>${name}: ${content}</div>`));
}
function handleUserIpPage() {
const ipElement = window.document.querySelector('.panelsurround div.panel div div strong');
if (!ipElement)
return;
const ip = ipElement.innerText;
const container = ipElement.parentNode;
container.appendChild(elementFromString('<div id="postGeo"></div>'));
container.appendChild(elementFromString('<h4 style="margin-top: 20px; margin-bottom: 0; font-weight: normal">Текущие данные<span id="onlineTime"></span>:</h4><div id="onlineUserInfo"></div>'));
const postUserInfoContainer = $('#postGeo')[0];
const onlineUserInfoContainer = $('#onlineUserInfo')[0];
GEOIP_PROVIDERS.forEach(function (provider) {
provider.request(ip, function (data) {
appendLine(postUserInfoContainer, `Месторасположение (${provider.name})`, formatGeoipData(data));
}, function (error) {
appendLine(postUserInfoContainer, `Месторасположение (${provider.name})`, formatError(error));
});
});
loadOnlineInfo(function (userAgent, ip, host, time) {
$('#onlineTime').html(` (<strong>${time}</strong>)`);
appendLine(onlineUserInfoContainer, 'User-Agent', '<span id="parsedUa"></span>' + userAgent);
parseUserAgent(userAgent, function (uaData) {
const parsedUaHtml = getParsedUserAgentLineHtml(uaData);
if (parsedUaHtml) {
const container = $('#parsedUa');
container.html(parsedUaHtml + '<br/>');
}
});
appendLine(onlineUserInfoContainer, 'IP адрес', ip);
if (host) {
appendLine(onlineUserInfoContainer, 'Хост', host);
}
GEOIP_PROVIDERS.forEach(function (provider) {
provider.request(ip, function (data) {
appendLine(onlineUserInfoContainer, `Месторасположение (${provider.name})`, formatGeoipData(data));
}, function (error) {
appendLine(onlineUserInfoContainer, `Месторасположение (${provider.name})`, formatError(error));
});
});
}, function (error) {
onlineUserInfoContainer.appendChild(elementFromString(`<div><strong>${formatError(error)}</strong></div>`));
});
}
function handleUserListPage() {
const ipUaCells = $('#woltable').find('tr a[id^="resolveip"]').parent();
if (!ipUaCells.length)
return;
ipUaCells.each(function (i, ipUaCell) {
const ipEndNode = $(ipUaCell).find('br');
const userAgent = getUserAgent(ipUaCell);
parseUserAgent(userAgent, function (uaData) {
const parsedUaHtml = getParsedUserAgentLineHtml(uaData);
if (parsedUaHtml) {
$(parsedUaHtml + '<br/>').insertAfter(ipEndNode);
}
});
const ip = $(ipUaCell).find('a[id^="resolveip"]').text().trim();
const geoipLink = $(`<a title="Определить месторасположение" href="javascript:void(0)" style="float: right;">${getImgHtml('https://i.imgur.com/hzmANSi.png', 16, 16)}</a>`)
.insertBefore(ipEndNode);
geoipLink.click(function () {
const container = $('<div></div>').replaceAll(geoipLink)[0];
ipEndNode.hide();
GEOIP_PROVIDERS.forEach(function (provider) {
provider.request(ip, function (data) {
appendLineSimple(container, `${provider.name}`, formatGeoipData(data));
}, function (error) {
appendLineSimple(container, `${provider.name}`, formatError(error));
});
});
});
})
}
if (window.location.pathname === '/postings.php' && window.location.search.indexOf('do=getip') > 0) {
handleUserIpPage();
} else if (window.location.pathname === '/online.php') {
handleUserListPage();
} else {
const userListLink = $('a[href$="online.php"]');
if (userListLink.length) {
userListLink.attr('href', userListLink.attr('href') + '?s=&sortfield=time&sortorder=desc&who=all&ua=1&pp=50');
}
}
})();