mirror of https://github.com/bobwen-dev/htpdate
1.0.2
Bug fixed: Using keep-alive to Get more accurate calculations; --user-agent default to none; Adjust time using fixed UTC format in shell commands; Code error in the version display; New: http2 protocol; Option --verbose make the operation more talkative;
This commit is contained in:
parent
bb3696ed2a
commit
f7e756a710
|
@ -110,4 +110,5 @@ dists/
|
|||
|
||||
# host files
|
||||
.github
|
||||
.gitee
|
||||
.gitee
|
||||
/yarn.lock
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
util = require 'util'
|
||||
{ spawn } = require 'child_process'
|
||||
dayjs = require 'dayjs'
|
||||
dayjs.extend require 'dayjs/plugin/utc'
|
||||
dayjs.extend require './dayjs_format_ms'
|
||||
platform = require('os').platform()
|
||||
|
||||
|
@ -25,14 +26,14 @@ adjust_time = (delta) ->
|
|||
await wait_data() if platform in ['win32']
|
||||
cb p...
|
||||
await wait_data() if platform in ['win32']
|
||||
cmd = dayjs().add(delta, 'ms').format adjust_time.command
|
||||
cmd = dayjs().add(delta, 'ms').utc().format adjust_time.command
|
||||
await input_line cmd
|
||||
await input_line 'exit'
|
||||
|
||||
|
||||
COMMANDS = {
|
||||
win32: '[time ]HH:mm:ss.SS[ && date ]YYYY-MM-DD'
|
||||
linux: '[date -s ]YYYY-MM-DDTHH:mm:ss.SSSZ'
|
||||
win32: '[wmic OS Set localdatetime=]YYYYMMDDmmss.SSS[000][+000]'
|
||||
linux: '[date --utc -set=]YYYY-MM-DDTHH:mm:ss.SSS'
|
||||
}
|
||||
adjust_time.command = COMMANDS[platform] or COMMANDS.linux
|
||||
|
||||
|
|
11
argv.coffee
11
argv.coffee
|
@ -17,7 +17,7 @@ DEFAULT_OPTIONS = {
|
|||
version: {
|
||||
describe: "display the version of #{info.name} and exit"
|
||||
type: 'boolean'
|
||||
alias: 'v'
|
||||
alias: 'V'
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ DEFAULT_OPTIONS = {
|
|||
print_version = ->
|
||||
console.log """
|
||||
#{info.name} #{info.version}, #{info.description}
|
||||
|
||||
License: AGPL-3.0
|
||||
Copyright (C) 2021 Bob Wen. All rights reserved.
|
||||
Homepage: #{info.homepage}
|
||||
|
@ -40,14 +41,16 @@ print_version = ->
|
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Affero General Public License for more details.
|
||||
|
||||
|
||||
Libraries:
|
||||
|
||||
"""
|
||||
for lib_name, version of info.dependencies
|
||||
lib_info = require "./node_modules/#{lib_name}/package.json"
|
||||
console.log """
|
||||
#{lib_info.name} #{lib_info.version}
|
||||
License: #{lib_info.license}
|
||||
Homepage: #{lib_info.homepage.split('#')[0]}
|
||||
License: #{lib_info.license or ''}
|
||||
Homepage: #{lib_info.homepage?.split('#')[0] or ''}
|
||||
|
||||
"""
|
||||
|
||||
|
@ -123,4 +126,6 @@ module.exports = (opt, exam) ->
|
|||
argv.count = 1
|
||||
if argv.retry < 0
|
||||
argv.retry = 0
|
||||
argv['user-agent'] = argv['user-agent'].trim() if argv['user-agent']
|
||||
argv['user-agent'] = undefined if argv['user-agent'] == ''
|
||||
argv
|
||||
|
|
63
index.coffee
63
index.coffee
|
@ -2,6 +2,8 @@
|
|||
|
||||
util = require 'util'
|
||||
got = require 'got'
|
||||
Agent = require 'agentkeepalive'
|
||||
{ HttpProxyAgent, HttpsProxyAgent } = require('hpagent')
|
||||
dayjs = require 'dayjs'
|
||||
info = require './package.json'
|
||||
median = require './median'
|
||||
|
@ -33,6 +35,11 @@ argv = require('./argv') {
|
|||
default: 'HEAD'
|
||||
type: 'string'
|
||||
}
|
||||
http2: {
|
||||
describe: 'Try to choose either HTTP/1.1 or HTTP/2 depending on the ALPN protocol'
|
||||
default: false
|
||||
type: 'boolean'
|
||||
}
|
||||
count: {
|
||||
describe: 'The number of requests for each URL'
|
||||
alias: 'c'
|
||||
|
@ -62,7 +69,7 @@ argv = require('./argv') {
|
|||
type: 'boolean'
|
||||
}
|
||||
command: {
|
||||
describe: 'Command to adjust system time, in https://day.js.org/ display format'
|
||||
describe: 'Command to adjust system time, in https://day.js.org/ UTC format'
|
||||
alias: 'C'
|
||||
default: adjust_time.command
|
||||
type: 'string'
|
||||
|
@ -75,12 +82,38 @@ argv = require('./argv') {
|
|||
}
|
||||
'user-agent': {
|
||||
alias: 'u'
|
||||
default: "#{info.name}/#{info.version}"
|
||||
type: 'string'
|
||||
}
|
||||
verbose: {
|
||||
describe: 'Make the operation more talkative'
|
||||
alias: 'v'
|
||||
default: false
|
||||
type: 'boolean'
|
||||
}
|
||||
}
|
||||
adjust_time.command = argv.command
|
||||
|
||||
agent_opt = {
|
||||
keepAlive: true,
|
||||
keepAliveMsecs: 60000,
|
||||
maxSockets: 1,
|
||||
}
|
||||
proxy = process.env.http_proxy || process.env.https_proxy
|
||||
agent = if proxy not in [undefined, '']
|
||||
agent_opt = {
|
||||
agent_opt...
|
||||
freeSocketTimeout: 30000
|
||||
proxy
|
||||
}
|
||||
{
|
||||
http: new HttpProxyAgent agent_opt
|
||||
https: new HttpsProxyAgent agent_opt
|
||||
}
|
||||
else
|
||||
{
|
||||
http: new Agent agent_opt
|
||||
https: new Agent.HttpsAgent agent_opt
|
||||
}
|
||||
|
||||
req_opt = {
|
||||
method: argv.method.trim().toUpperCase()
|
||||
|
@ -89,18 +122,20 @@ req_opt = {
|
|||
retry: 0
|
||||
dnsCache: true
|
||||
cache: false
|
||||
agent
|
||||
headers: {
|
||||
'user-agent': argv['user-agent']
|
||||
}
|
||||
https: {
|
||||
rejectUnauthorized: not argv.insecure
|
||||
}
|
||||
http2: argv.http2
|
||||
}
|
||||
|
||||
|
||||
client = got.extend {}
|
||||
get_server_time = (url) ->
|
||||
try
|
||||
return await got url, req_opt
|
||||
return await client url, req_opt
|
||||
catch e
|
||||
return e.response if e.response?.timings?
|
||||
throw e
|
||||
|
@ -123,9 +158,22 @@ get_time_delta = (url) ->
|
|||
if r?.timings? and server_moment?
|
||||
break
|
||||
continue if not server_moment?
|
||||
duration = r.timings.response - r.timings.upload
|
||||
delta = Math.round(server_moment - r.timings.end - duration / 2 + 500)
|
||||
console.log "#{step}" + "#{if delta > 0 then '+' else ''}#{delta} ms".padStart 10
|
||||
if not r.timings.secureConnect?
|
||||
upload_at = r.timings.connect
|
||||
else
|
||||
upload_at = r.timings.secureConnect
|
||||
duration = r.timings.response - upload_at
|
||||
delta = Math.round(server_moment - r.timings.response - duration / 2) + 500
|
||||
delta_text = "#{if delta > 0 then '+' else ''}#{delta} ms".padStart 10
|
||||
if not argv.verbose
|
||||
console.log "#{step}#{delta_text}"
|
||||
else
|
||||
details = " DNS:" + "#{r.timings.phases.dns}".padStart 5
|
||||
details += " TCP:" + "#{r.timings.phases.tcp}".padStart 5
|
||||
details += " TSL:" + "#{if r.timings.phases.tls? then r.timings.phases.tls else ''}".padStart 5
|
||||
details += " Send:" + "#{r.timings.upload - upload_at}".padStart 5
|
||||
details += " Recv:" + "#{r.timings.response - r.timings.upload}".padStart 5
|
||||
console.log "#{step}#{delta_text}#{details}"
|
||||
delta
|
||||
|
||||
|
||||
|
@ -135,7 +183,6 @@ delay = util.promisify (ms, cb) ->
|
|||
|
||||
|
||||
do ->
|
||||
proxy = process.env.http_proxy or process.env.https_proxy
|
||||
if proxy not in [undefined, '']
|
||||
console.debug "Using explicit proxy server #{proxy}"
|
||||
values = []
|
||||
|
|
18
package.json
18
package.json
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "htpdate",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.2",
|
||||
"description": "a tool to synchronize system time from web servers",
|
||||
"keywords": [
|
||||
"htp",
|
||||
|
@ -26,17 +26,17 @@
|
|||
"compile": "npx coffee -bc --no-header ./",
|
||||
"clean": "rm *.js",
|
||||
"b": "npx nexe -i index.js -r node_modules/**/package.json",
|
||||
"b-win-x86": "npm run b -- -t windows-x86-14.15.3 -o dists/htpdate-x86.exe",
|
||||
"b-win-x64": "npm run b -- -t windows-x64-14.15.4 -o dists/htpdate-x64.exe",
|
||||
"b-win-x86": "npm run b -- -t windows-x86-14.15.3 -o dists/htpdate-windows-x86.exe",
|
||||
"b-win-x64": "npm run b -- -t windows-x64-14.17.0 -o dists/htpdate-windows-x64.exe",
|
||||
"b-linux-x64": "npm run b -- -t linux-x64-14.15.3 -o dists/htpdate-linux-x64",
|
||||
"b-linux-arm64": "npm run b -- -t linux-arm64-14.15.4 -o dists/htpdate-linux-arm64",
|
||||
"b-mac-x64": "npm run b -- -t mac-x64-14.15.3 -o dists/htpdate-mac-x64",
|
||||
"b-all": "npm run b-win-x86 && npm run b-win-x64 && npm run b-linux-x64 && npm run b-linux-arm64 && npm run b-mac-x64",
|
||||
"c-win-x86": "cd dists && cp htpdate-x86.exe htpdate.exe && 7z a htpdate-windows-x86.7z htpdate.exe && rm htpdate.exe",
|
||||
"c-win-x64": "cd dists && cp htpdate-x64.exe htpdate.exe && 7z a htpdate-windows-x64.7z htpdate.exe && rm htpdate.exe",
|
||||
"c-linux-x64": "cd dists && cp htpdate-linux-x64 htpdate && 7z a htpdate-linux-x86.7z htpdate && rm htpdate",
|
||||
"c-linux-arm64": "cd dists && cp htpdate-linux-arm64 htpdate && 7z a htpdate-linux-arm64.7z htpdate && rm htpdate",
|
||||
"c-mac-x64": "cd dists && cp htpdate-mac-x64 htpdate && 7z a htpdate-mac-x64.7z htpdate && rm htpdate",
|
||||
"c-win-x86": "cd dists && cp htpdate-windows-x86.exe htpdate.exe && 7z a htpdate-windows-x86.7z htpdate.exe && rm -rf htpdate.exe",
|
||||
"c-win-x64": "cd dists && cp htpdate-windows-x64.exe htpdate.exe && 7z a htpdate-windows-x64.7z htpdate.exe && rm -rf htpdate.exe",
|
||||
"c-linux-x64": "cd dists && cp htpdate-linux-x64 htpdate && 7z a htpdate-linux-x86.7z htpdate && rm -rf htpdate",
|
||||
"c-linux-arm64": "cd dists && cp htpdate-linux-arm64 htpdate && 7z a htpdate-linux-arm64.7z htpdate && rm -rf htpdate",
|
||||
"c-mac-x64": "cd dists && cp htpdate-mac-x64 htpdate && 7z a htpdate-mac-x64.7z htpdate && rm -rf htpdate",
|
||||
"compress": "npm run c-win-x86 && npm run c-win-x64 && npm run c-linux-x64 && npm run c-linux-arm64 && npm run c-mac-x64",
|
||||
"build": "npm run compile && npm run b-all && npm run clean",
|
||||
"test": "npx coffee index.coffee www.pool.ntp.org"
|
||||
|
@ -46,8 +46,10 @@
|
|||
"node": ">=10.19.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"agentkeepalive": "^4.1.4",
|
||||
"dayjs": "^1.10.3",
|
||||
"got": "^11.8.0",
|
||||
"hpagent": "^0.1.2",
|
||||
"minimist": "^1.2.5",
|
||||
"minimist-options": "^4.1.0",
|
||||
"natural-orderby": "^2.0.3"
|
||||
|
|
Loading…
Reference in New Issue