👽 Revert geoip api to ipinfo.now.sh

and optimize number of requests
This commit is contained in:
Gabriel SAILLARD (GitSquared) 2019-07-15 18:09:37 +02:00
parent cb597e3b76
commit 0bbced5ca1
No known key found for this signature in database
GPG Key ID: 4F615B5FE436611A
2 changed files with 20 additions and 20 deletions

View File

@ -82,14 +82,14 @@ class LocationGlobe {
// Connections // Connections
this.conns = []; this.conns = [];
this.addConn = ip => { this.addConn = ip => {
require("https").get({host: "freegeoip.app", port: 443, path: "/json/"+ip, localAddress: window.mods.netstat.internalIPv4, agent: false}, res => { require("https").get({host: "ipinfo.now.sh", port: 443, path: "/"+ip, localAddress: window.mods.netstat.internalIPv4, agent: false}, res => {
let rawData = ""; let rawData = "";
res.on("data", chunk => { res.on("data", chunk => {
rawData += chunk; rawData += chunk;
}); });
res.on("end", () => { res.on("end", () => {
this.parseResponse(rawData, ip).catch(e => { this.parseResponse(rawData, ip).catch(e => {
window.mods.netstat.failedAttempts[e] = (window.mods.netstat.failedAttemps[e] || 0) + 1; window.mods.netstat.failedAttempts[e] = (window.mods.netstat.failedAttempts[e] || 0) + 1;
if (window.mods.netstat.failedAttempts[e] > 2) return false; if (window.mods.netstat.failedAttempts[e] > 2) return false;
let electron = require("electron"); let electron = require("electron");
electron.ipcRenderer.send("log", "note", "LocationGlobe: Error parsing data from ipinfo.now.sh"); electron.ipcRenderer.send("log", "note", "LocationGlobe: Error parsing data from ipinfo.now.sh");
@ -137,9 +137,9 @@ class LocationGlobe {
async parseResponse(rawData, ip) { async parseResponse(rawData, ip) {
const json = JSON.parse(rawData); const json = JSON.parse(rawData);
if (json.latitude && json.longitude) { if (json.geo !== null && json.geo.latitude && json.geo.longitude) {
const lat = Number(json.latitude); const lat = Number(json.geo.latitude);
const lon = Number(json.longitude); const lon = Number(json.geo.longitude);
window.mods.globe.conns.push({ window.mods.globe.conns.push({
ip, ip,
@ -155,7 +155,7 @@ class LocationGlobe {
this.globe.addMarker(randomLat - 20, randomLong + 150, '', true); this.globe.addMarker(randomLat - 20, randomLong + 150, '', true);
} }
addTemporaryConnectedMarker(ip) { addTemporaryConnectedMarker(ip) {
require("https").get({host: "freegeoip.app", port: 443, path: "/json/"+ip, localAddress: window.mods.netstat.internalIPv4, agent: false}, res => { require("https").get({host: "ipinfo.now.sh", port: 443, path: "/"+ip, localAddress: window.mods.netstat.internalIPv4, agent: false}, res => {
let rawData = ""; let rawData = "";
res.on("data", chunk => { res.on("data", chunk => {
rawData += chunk; rawData += chunk;
@ -167,9 +167,9 @@ class LocationGlobe {
} catch(e) { } catch(e) {
return; return;
} }
if (json.latitude && json.longitude) { if (json.geo.latitude && json.geo.longitude) {
const lat = Number(json.latitude); const lat = Number(json.geo.latitude);
const lon = Number(json.longitude); const lon = Number(json.geo.longitude);
window.mods.globe.conns.push({ window.mods.globe.conns.push({
ip, ip,

View File

@ -28,6 +28,7 @@ class Netstat {
this.lastconn = {finished: true}; this.lastconn = {finished: true};
this.iface = null; this.iface = null;
this.failedAttempts = {}; this.failedAttempts = {};
this.runsBeforeGeoIPUpdate = 0;
this._httpsAgent = new require("https").Agent({ this._httpsAgent = new require("https").Agent({
keepAlive: false, keepAlive: false,
@ -79,6 +80,8 @@ class Netstat {
} }
} }
if (net.ip4 !== this.internalIPv4) this.runsBeforeGeoIPUpdate = 0;
this.iface = net.iface; this.iface = net.iface;
this.internalIPv4 = net.ip4; this.internalIPv4 = net.ip4;
document.getElementById("mod_netstat_iname").innerText = "Interface: "+net.iface; document.getElementById("mod_netstat_iname").innerText = "Interface: "+net.iface;
@ -86,8 +89,8 @@ class Netstat {
if (net.ip4 === "127.0.0.1") { if (net.ip4 === "127.0.0.1") {
offline = true; offline = true;
} else { } else {
if (this.lastconn.finished) { if (this.runsBeforeGeoIPUpdate === 0 && this.lastconn.finished) {
this.lastconn = require("https").get({host: "freegeoip.app", port: 443, path: "/json/", localAddress: net.ip4, agent: this._httpsAgent}, res => { this.lastconn = require("https").get({host: "ipinfo.now.sh", port: 443, path: "/", localAddress: net.ip4, agent: this._httpsAgent}, res => {
let rawData = ""; let rawData = "";
res.on("data", chunk => { res.on("data", chunk => {
rawData += chunk; rawData += chunk;
@ -97,20 +100,15 @@ class Netstat {
let data = JSON.parse(rawData); let data = JSON.parse(rawData);
this.ipinfo = { this.ipinfo = {
ip: data.ip, ip: data.ip,
geo: { geo: data.geo
latitude: data.latitude,
longitude: data.longitude,
metro_code: data.zip_code,
time_zone: data.time_zone
}
}; };
// if (!this.ipinfo.api_version.startsWith("3")) console.warn("Warning: ipinfo API version might not be compatible"); if (!data.api_version.startsWith("4")) console.warn("Warning: ipinfo API version might not be compatible");
// delete this.ipinfo.api_version;
// delete this.ipinfo.time;
let ip = this.ipinfo.ip; let ip = this.ipinfo.ip;
document.querySelector("#mod_netstat_innercontainer > div:nth-child(2) > h2").innerHTML = window._escapeHtml(ip); document.querySelector("#mod_netstat_innercontainer > div:nth-child(2) > h2").innerHTML = window._escapeHtml(ip);
this.runsBeforeGeoIPUpdate = 10;
} catch(e) { } catch(e) {
this.failedAttempts[e] = (this.failedAttempts[e] || 0) + 1; this.failedAttempts[e] = (this.failedAttempts[e] || 0) + 1;
if (this.failedAttempts[e] > 2) return false; if (this.failedAttempts[e] > 2) return false;
@ -124,6 +122,8 @@ class Netstat {
}).on("error", e => { }).on("error", e => {
// Drop it // Drop it
}); });
} else if (this.runsBeforeGeoIPUpdate !== 0) {
this.runsBeforeGeoIPUpdate = this.runsBeforeGeoIPUpdate - 1;
} }
let p = await this.ping(window.settings.pingAddr || "1.1.1.1", 80, net.ip4).catch(() => { offline = true }); let p = await this.ping(window.settings.pingAddr || "1.1.1.1", 80, net.ip4).catch(() => { offline = true });