-
Notifications
You must be signed in to change notification settings - Fork 0
/
clockdrift.ts
executable file
·97 lines (82 loc) · 2.47 KB
/
clockdrift.ts
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
#!/usr/bin/env node
/*!
* clockdrift.js by Rich Trott, (c) 2012 Regents of University of California, MIT License
*/
import http from 'node:http'
import https from 'node:https'
let i
const argvLength = process.argv.length
function usage (errorMessage: string): void {
if (errorMessage !== '') {
console.error(`\nError: ${errorMessage}.\n`)
}
console.error('Usage: clockdrift.js tolerance url1 [url2 ...]')
console.error(' tolerance: number of seconds a timestamp can be off without being reported')
console.error(' url1 ...: URLs to inspect')
console.error('\nExample: clockdrift.js 15 http://tycho.usno.navy.mil/ http://www.example.com/')
}
function checkTime (host: string) {
return function (res: http.IncomingMessage) {
if (typeof res.headers.date !== 'string') {
console.error(`No date header returned by ${host}.`)
process.exitCode = 1
return
}
const clockTimestamp = new Date(res.headers.date).getTime()
if (isNaN(clockTimestamp)) {
console.error('Could not convert date header from ' + host + ' to timestamp. (Malformed?)')
process.exitCode = 1
return
}
const diff = Math.round((clockTimestamp - before) / 1000)
if (Math.abs(diff) > tolerance) {
console.log(`Clock at ${host} is ${diff}s off from local clock.`)
}
}
}
function dispatchRequest (targetUrl: string): void {
let reqObj
let req: http.ClientRequest
const options = new URL(targetUrl) as URL & { method: string }
options.method = 'HEAD'
switch (options.protocol) {
case 'http:':
reqObj = http
break
case 'https:':
reqObj = https
break
}
if (reqObj != null) {
req = reqObj.request(options, checkTime(options.host))
req.on('error', function (e) {
console.error(`Error on ${options.host}: ${e.message}`)
process.exitCode = 1
})
req.on('socket', function (socket) {
socket.setTimeout(1000)
socket.on('timeout', function () {
console.error('Socket timeout; hanging up.')
process.exitCode = 1
req.abort()
})
})
req.end()
} else {
console.error('Could not determine protocol: ' + options.protocol)
process.exitCode = 1
}
}
if (argvLength < 4) {
usage('')
process.exit(1)
}
const tolerance = parseInt(process.argv[2], 10)
if (isNaN(tolerance)) {
usage('tolerance must be a number')
process.exit(1)
}
const before = new Date().getTime()
for (i = 3; i < argvLength; i++) {
dispatchRequest(process.argv[i])
}