Skip to content

Commit

Permalink
fix network change + refactor - closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
csabapalfi committed Mar 12, 2017
1 parent d463103 commit a516c23
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 1,048 deletions.
48 changes: 12 additions & 36 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
const timers = require('timers');
const path = require('path');
const dns = require('dns');
const Ping = require('ping-lite');
const { forever, timeout, parallel, waterfall } = require('async');
const { app, dialog, BrowserWindow, Tray, Menu } = require('electron');
const networkStatus = require('./network-status');
const { app, BrowserWindow, Tray, Menu } = require('electron');
const timeoutMs = 2000;

const dnsLatency = (host, callback) => {
const dnsStartMs = +(new Date);
dns.resolve(host, err => {
if (err) return callback(err);
callback(null, +(new Date) - dnsStartMs);
});
}

const pingLatency = (ip, callback) => {
const ping = new Ping(ip);
ping.send((err, latencyMs) => {
if (err) return callback(err);
callback(null, Math.round(latencyMs));
});
}

let mainWindow;
app.on('ready', () => {
mainWindow = new BrowserWindow({show: false});
new BrowserWindow({show: false});
if (app.dock) app.dock.hide();

const tray = new Tray(path.join(app.getAppPath(), 'icon.png'));
Expand All @@ -39,17 +20,12 @@ app.on('ready', () => {
let title = `.../...ms`;
tray.setTitle(title);

const refreshLatency = (check, arg, timeoutMs, titleRegex) =>
() => setInterval(() => {
timeout(check, timeoutMs)(arg, (err, latency) => {
title = title.replace(titleRegex, latency || '...');
tray.setTitle(title);
})
}, timeoutMs + 100);

parallel([
refreshLatency(dnsLatency, 'google.com', 2000, /[\d\.]+(?=\/)/),
refreshLatency(pingLatency, '8.8.8.8', 2000, /[\d\.]+(?=ms)/),
]);

networkStatus({
timeoutMs: 2000,
intervalMs: 2000,
hostname: 'google.com',
address: '8.8.8.8'
}).on('latencies', ({dns, ping}) =>
tray.setTitle(`${dns || '...'}/${ping || '...'}ms`)
);
});
67 changes: 67 additions & 0 deletions network-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const timers = require('timers');
const EventEmitter = require('events');
const Ping = require('ping-lite');
const dns = require('dns-socket');
const resolv = require('resolv');

const dnsLatency = (hostname, timeout) =>
new Promise((resolve, reject) => {
var socket = dns({ timeout });
const nameserver = resolv().nameserver[0];
const query = { questions: [{ type: 'A', name: hostname }] };
const dnsStartMs = +(new Date());
socket.query(query, 53, nameserver, error =>
error ? reject(error) : resolve(+(new Date()) - dnsStartMs));
}
);

const pingLatency = (address) =>
new Promise((resolve, reject) =>
new Ping(address).send((error, latencyMs) =>
error ? reject(error) : resolve(Math.round(latencyMs))
)
);

const timeout = (promise, timeoutMs) => {
let timer = null;
const clearTimeout = () => timers.clearTimeout(timer);
return Promise.race([
promise,
new Promise((resolve, reject) => {
timer = setTimeout(() => reject(new Error('timed out')), timeoutMs);
})
])
.then(value => { clearTimeout(); return value; })
.catch(error => { clearTimeout(); throw error; });
};

const allIgnoreErrors = (promises) => Promise.all(
promises.map(promise => promise.catch(() => null))
);

const checkLatencies = ({ hostname, address, timeoutMs }) =>
allIgnoreErrors([
dnsLatency(hostname, timeoutMs).catch(() => null),
timeout(pingLatency(address), timeoutMs).catch(() => null)
]);

const networkStatus = (options) => {
const latencies = new EventEmitter();
setInterval(() => checkLatencies(options)
.then(([dns, ping]) =>
latencies.emit('latencies', { dns, ping })
)
, options.intervalMs);
return latencies;
};

if (require.main === module) {
networkStatus({
timeoutMs: 2000,
intervalMs: 2000,
hostname: 'google.com',
address: '8.8.8.8'
}).on('latencies', console.log);
}

module.exports = networkStatus;
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "TrayPing",
"version": "3.0.0",
"description": "tray application showing google.com ping times",
"description": "tray application showing DNS/ping latency",
"dependencies": {
"async": "^2.1.4",
"ping-lite": "^1.0.2"
"dns-socket": "^1.6.1",
"ping-lite": "^1.0.2",
"resolv": "^1.0.0"
},
"devDependencies": {
"electron": "^1.4.13",
Expand Down
Loading

0 comments on commit a516c23

Please sign in to comment.