Created
April 24, 2022 23:59
-
-
Save simonmysun/a23ad1bb201a316c21b25e722a6f304b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const http = require('http'); | |
const https = require('https'); | |
let lastTimestamp = undefined; | |
let newTimestamp = undefined; | |
let diffTimestamp = undefined; | |
const hrtlog = (msg) => { | |
newTimestamp = process.hrtime.bigint(); | |
if(lastTimestamp == undefined) { | |
diffTimestamp = 0; | |
} else { | |
diffTimestamp = newTimestamp - lastTimestamp; | |
} | |
lastTimestamp = newTimestamp; | |
console.log(`${newTimestamp}+${diffTimestamp}: ${msg}`); | |
}; | |
const testUrl = (protocol, url, port, path) => { | |
return new Promise((resolve, reject) => { | |
hrtlog('[HTTP] Request starts'); | |
const req = protocol.request({ | |
hostname: url, | |
port: port, | |
path: path, | |
method: 'GET', | |
headers: { | |
'DNT': '1', | |
'sec-ch-ua': '"Chromium";v="94", "Google Chrome";v="94", ";Not A Brand";v="99"', | |
'sec-ch-ua-mobile': '?0', | |
'sec-ch-ua-platform': '"Linux"', | |
'Upgrade-Insecure-Requests': '1', | |
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36' | |
}, | |
timeout: 3000 | |
}, (res) => { | |
hrtlog('[HTTP] Status code ' + res.statusCode); | |
res.once('readable', () => { | |
hrtlog('[HTTP] First byte'); | |
}); | |
const chunks = []; | |
res.on('data', (chunk) => { | |
chunks.push(chunk); | |
}); | |
res.on('end', () => { | |
hrtlog('[HTTP] Response ends'); | |
const data = Buffer.concat(chunks).toString(); | |
resolve(); | |
}); | |
}); | |
req.on('continue', e => { | |
hrtlog('[HTTP] 101 Continue'); | |
}); | |
req.on('information', e => { | |
hrtlog('[HTTP] 1xx Information'); | |
}); | |
req.on('timeout', e => { | |
hrtlog('[HTTP] timeout'); | |
req.destroy(); | |
reject('HTTP timeout'); | |
}); | |
req.on('error', e => { | |
hrtlog('[HTTP] Error: ' + e.toString()); | |
reject('HTTP error'); | |
}); | |
req.on('socket', (socket) => { | |
hrtlog('[Socket] Socket init'); | |
socket.on('lookup', () => { | |
hrtlog('[Socket] DNS lookup finished'); | |
}); | |
socket.on('connect', () => { | |
hrtlog('[Socket] TCP handshake'); | |
}); | |
socket.on('ready', () => { | |
hrtlog('[Socket] Socket ready'); | |
}); | |
socket.on('secureConnect', () => { | |
hrtlog('[Socket] TLS handshake'); | |
}); | |
socket.on('data', () => { | |
hrtlog('[Socket] Data inbound'); | |
}); | |
socket.on('timeout', () => { | |
hrtlog('[Socket] timeout'); | |
reject('Socket timeout'); | |
}); | |
socket.on('close', () => { | |
hrtlog('[Socket] Socket closed'); | |
reject('Socket closed'); | |
}); | |
}); | |
req.end(); | |
}); | |
}; | |
const probeHostname = 'maoyin.eu'; | |
const probePort = 444; | |
const probePath = '/'; | |
testUrl(https, probeHostname, probePort, probePath).then(metrics => { | |
console.log('Succ'); | |
}).catch(err => { | |
console.error(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment