Skip to content

Commit

Permalink
add icons
Browse files Browse the repository at this point in the history
  • Loading branch information
leozhang2018 committed Apr 25, 2018
1 parent af12fc7 commit 0f31a57
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 65 deletions.
Binary file added icons/content.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/dns.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/header.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/server.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/ssl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/tcp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/total.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
241 changes: 176 additions & 65 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
'use strict';
const util = require('util');
const isUrl = require('is-url');
const alfy = require('alfy');
const http = require('http');
const https = require('https');
const parse = require('url').parse;
const dns = require('dns');

if (isUrl(alfy.input)) {
let url = parse(alfy.input);
let protocol = url.protocol === 'https:' ? https : http;
function getDnslookup(host) {
return new Promise((resolve, reject) => {
dns.lookup(host, (err, address, family) => {
resolve(address);
if (err) {
reject(err);
}
});
});
}

function httpOutput(url, ip) {
let begin = Date.now();
let onLookup = begin; // diff begin - dns resolve
let onConnect = begin; // diff dns resolve - connect
let onSecureConnect = begin; // diff connect - secureConnect
let onTransfer = begin; // diff connect - transfer
let onTotal = begin; // diff begin - end
let body = '';
let ip = '';

dns.lookup(url.host, (err, address, family) => {
ip = address;
});
const req = protocol.request(url, res => {
const req = http.request(url, res => {
res.once('readable', () => {
onTransfer = Date.now();
});
Expand All @@ -31,65 +36,140 @@ if (isUrl(alfy.input)) {
res.on('end', () => {
onTotal = Date.now();
res.body = body;
if (url.protocol === 'https:') {
alfy.output([
{
title: 'DNS Lookup',
subtitle: onLookup - begin + 'ms' + ` IP: ${ip}`
},
{
title: 'TCP Connection',
subtitle: onConnect - onLookup + 'ms'
},
{
title: 'SSL Handshake',
subtitle: onSecureConnect - onConnect + 'ms'
},
{
title: 'Server Processing',
subtitle: onTransfer - onSecureConnect + 'ms'
},
{
title: 'Content Transfer',
subtitle: onTotal - onTransfer + 'ms'
},
{
title: 'Total',
subtitle: onTotal - begin + 'ms'
},
{
title: 'Headers',
subtitle: `HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage} ${res.headers.server} ${res.headers.date}`
alfy.output([
{
title: 'DNS Lookup',
subtitle: onLookup - begin + 'ms' + ` IP: ${ip}`,
icon: {
path: 'icons/dns.png'
}
]);
} else if (url.protocol === 'http:') {
alfy.output([
{
title: 'DNS Lookup',
subtitle: onLookup - begin + 'ms' + ` IP: ${ip}`
},
{
title: 'TCP Connection',
subtitle: onConnect - onLookup + 'ms'
},
{
title: 'Server Processing',
subtitle: onTransfer - onConnect + 'ms'
},
{
title: 'Content Transfer',
subtitle: onTotal - onTransfer + 'ms'
},
{
title: 'Total',
subtitle: onTotal - begin + 'ms'
},
{
title: 'Headers',
subtitle: `HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage} ${res.headers.server} ${res.headers.date}`
},
{
title: 'TCP Connection',
subtitle: onConnect - onLookup + 'ms',
icon: {
path: 'icons/tcp.png'
}
]);
},
{
title: 'Server Processing',
subtitle: onTransfer - onConnect + 'ms',
icon: {
path: 'icons/server.png'
}
},
{
title: 'Content Transfer',
subtitle: onTotal - onTransfer + 'ms',
icon: {
path: 'icons/content.png'
}
},
{
title: 'Total',
subtitle: onTotal - begin + 'ms',
icon: {
path: 'icons/total.png'
}
},
{
title: 'Headers',
subtitle: `HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage} ${res.headers.server} ${res.headers.date}`,
icon: {
path: 'icons/header.png'
}
}
]);
});
});
req.on('socket', socket => {
socket.on('lookup', () => {
onLookup = Date.now();
});
socket.on('connect', () => {
onConnect = Date.now();
});
});
req.on('error', reject => {
alfy.output([
{
title: 'Resolve Error',
subtitle: 'Please Check the URL'
}
]);
});
req.end();
}

function httpsOutput(url, ip) {
let begin = Date.now();
let onLookup = begin; // diff begin - dns resolve
let onConnect = begin; // diff dns resolve - connect
let onSecureConnect = begin; // diff connect - secureConnect
let onTransfer = begin; // diff connect - transfer
let onTotal = begin; // diff begin - end
let body = '';
const req = https.request(url, res => {
res.once('readable', () => {
onTransfer = Date.now();
});
res.on('data', chunk => {
body += chunk;
});
res.on('end', () => {
onTotal = Date.now();
res.body = body;
alfy.output([
{
title: 'DNS Lookup',
subtitle: onLookup - begin + 'ms' + ` IP: ${ip}`,
icon: {
path: 'icons/dns.png'
}
},
{
title: 'TCP Connection',
subtitle: onConnect - onLookup + 'ms',
icon: {
path: 'icons/tcp.png'
}
},
{
title: 'SSL Handshake',
subtitle: onSecureConnect - onConnect + 'ms',
icon: {
path: 'icons/ssl.png'
}
},
{
title: 'Server Processing',
subtitle: onTransfer - onSecureConnect + 'ms',
icon: {
path: 'icons/server.png'
}
},
{
title: 'Content Transfer',
subtitle: onTotal - onTransfer + 'ms',
icon: {
path: 'icons/content.png'
}
},
{
title: 'Total',
subtitle: onTotal - begin + 'ms',
icon: {
path: 'icons/total.png'
}
},
{
title: 'Headers',
subtitle: `HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage} ${res.headers.server} ${res.headers.date}`,
icon: {
path: 'icons/header.png'
}
}
]);
});
});
req.on('socket', socket => {
Expand All @@ -113,6 +193,37 @@ if (isUrl(alfy.input)) {
]);
});
req.end();
}

if (isUrl(alfy.input)) {
let url = parse(alfy.input);
if (url.protocol === 'http:') {
getDnslookup(url.host)
.then(ip => {
httpOutput(url, ip);
})
.catch(() => {
alfy.output([
{
title: 'Parse URL Formate Error',
subtitle: 'Please check the URL formate'
}
]);
});
} else if (url.protocol === 'https:') {
getDnslookup(url.host)
.then(ip => {
httpsOutput(url, ip);
})
.catch(() => {
alfy.output([
{
title: 'Parse URL Formate Error',
subtitle: 'Please check the URL formate'
}
]);
});
}
} else {
alfy.output([
{
Expand Down

0 comments on commit 0f31a57

Please sign in to comment.