Setting time using HTTP without NTP server nor service

Post new topic   Reply to topic    DD-WRT Forum Index -> Contributions Upload
Author Message
mwchang
DD-WRT Guru


Joined: 26 Mar 2013
Posts: 1851
Location: Hung Hom, Hong Kong

PostPosted: Thu Dec 03, 2020 9:18    Post subject: Setting time using HTTP without NTP server nor service Reply with quote
Here is a command-line that uses curl to get the current time from the HTTP response header:

* Get current time in Epoch/Unix format by curl-ing Google, using sed and grep to filter out irrelevant information:

Code:
curl -I 'https://www.google.com/'  2>/dev/null | grep -i '^date:' | sed 's/^[Dd]ate: //g'

Result: Fri, 09 Feb 2018 08:58:49 GMT

* Convert the Unix time string into YYYYMMDDHHMM.SS using awk

Code:
curl -I 'https://www.google.com/'  2>/dev/null | grep -i '^date:' | sed 's/^[Dd]ate: //g' | awk 'BEGIN{months="JanFebMarAprMayJunJulAugSepOctNovDec"}{printf("%s%02d%s%s", $4, index(months,$3)/3+1,$2,$5)}' | sed 's/://' | sed 's/:/./'

Result: 201802090858.49

* Set system date by calling "date --utc -s" with "`curl .....`" as parameter:

Code:
date --utc -s `curl -I 'https://www.google.com/'  2>/dev/null | grep -i '^date:' | sed 's/^[Dd]ate: //g' | awk 'BEGIN{months="JanFebMarAprMayJunJulAugSepOctNovDec"}{printf("%s%02d%s%s", $4, index(months,$3)/3+1,$2,$5)}' | sed 's/://' | sed 's/:/./'`


FYI, you could just paste this command-line into Administration -> Command Shell -> Commands and hit "Save Startup"! If DNS server is not guaranteed, you can use IP address "8.8.8.8" instead of the domain name (FQDN) "www.google.com".

If you don't trust Google for time, maybe you could try http://www.usno.navy.mil/USNO/time. I believe it's the master clock for everything in USA. Well, I don't know the details though. USNO has a Javascript webpage that just displays current time: http://www.usno.navy.mil/USNO/time/display-clocks/simpletime. USNO also has a static webpage that just display the current time at http://tycho.usno.navy.mil/timer.pl.

BTW, if you don't wanna use any DNS server, you should use IP addresses instead of names above. Above method might work better with DNSSEC, DNSSEC over HTTP and DNS over TLS, aka using unbound service. Not sure how those would impact my curl calls, not to mention the move to HTTPS! Smile

And DD-WRT sets initial system time using a daemon called process_monitor, with hard-coded default NTP server IP address "212.18.3.19" (which is pool.ntp.org) when Basic Setup->Time Settings->Server IP/Name is blank!

I just found that there is a new time server "time.cloudflare.com" for both NTP and NTS:
https://blog.cloudflare.com/secure-time/

In addition, Cloudfare's webpage shows a new method called Roughtime:
https://developers.cloudflare.com/time-services/roughtime

(I heard that iOS hard-coded a NTP server IP address as well, again for time setting during startup! Most of these encryption and certification stuff relies on accurate time.)




_________________
Router: Asus RT-N18U (rev. A1)

Drink, Blink, Stretch! Live long and prosper! May the Force and farces be with you!

Facebook: https://www.facebook.com/changmanwai
Website: https://sites.google.com/site/changmw
SETI@Home profile: http://setiathome.berkeley.edu/view_profile.php?userid=211832
GitHub: https://github.com/changmw/changmw


Last edited by mwchang on Mon Aug 28, 2023 15:29; edited 2 times in total
Sponsor
Wildlion
DD-WRT Guru


Joined: 24 May 2016
Posts: 1405

PostPosted: Thu Dec 03, 2020 23:42    Post subject: Reply with quote
as I responded to you hard code in the other thread:

poor form and actually could be a flaw to fall back to a default hard coded address. Everyone could have the error (or not even know) and then DDOS that one IP address or even what happens when that IP address has to go down for maintenance or is compromised? Oh and what if you are on IPv6 only or not connected to the regular internet but an isolated network?


ntp provides other options and slews the clock for a continuous time, and not having discontinuous jumps. If your clock drifted future in time and then you jumped backwards according to this method, now all of the logs are confusing and not making sense, not to mention time stamps and other things.

This algorithm also has no measurement of the time it took to get the packet and the time, so it could be a minute old or 47 seconds, or 0.5 milliseconds, that is unknown.
mwchang
DD-WRT Guru


Joined: 26 Mar 2013
Posts: 1851
Location: Hung Hom, Hong Kong

PostPosted: Mon Dec 07, 2020 16:25    Post subject: Reply with quote
Wildlion wrote:
...

ntp provides other options and slews the clock for a continuous time, and not having discontinuous jumps.....

This algorithm also has no measurement of the time it took to get the packet and the time, so it could be a minute old or 47 seconds, or 0.5 milliseconds, that is unknown.

Understood the precision of NTP.... Smile


_________________
Router: Asus RT-N18U (rev. A1)

Drink, Blink, Stretch! Live long and prosper! May the Force and farces be with you!

Facebook: https://www.facebook.com/changmanwai
Website: https://sites.google.com/site/changmw
SETI@Home profile: http://setiathome.berkeley.edu/view_profile.php?userid=211832
GitHub: https://github.com/changmw/changmw
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Index -> Contributions Upload All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum