Rework getting local-Google time offset (#1572)

This commit is contained in:
Ross Scroggs
2022-10-30 05:22:58 -07:00
committed by GitHub
parent dc22b024b8
commit 37e3fd904d

View File

@@ -634,28 +634,28 @@ TIME_OFFSET_UNITS = [('day', 86400), ('hour', 3600), ('minute', 60),
def getLocalGoogleTimeOffset(testLocation='admin.googleapis.com'): def getLocalGoogleTimeOffset(testLocation='admin.googleapis.com'):
localUTC = datetime.datetime.now(datetime.timezone.utc) # Try with http first, if time is close (<MAX_LOCAL_GOOGLE_TIME_OFFSET seconds),
try: # retry with https
# we disable SSL verify so we can still get time even if clock badhttp = transport.create_http()
# is way off. This could be spoofed / MitM but we'll fail for those for prot in ['http', 'https']:
# situations everywhere else but here. localUTC = datetime.datetime.now(datetime.timezone.utc)
badhttp = transport.create_http() try:
badhttp.check_hostname = False googleUTC = dateutil.parser.parse(
badhttp.disable_ssl_certificate_validation = True badhttp.request(f'{prot}://' + testLocation, 'HEAD')[0]['date'])
googleUTC = dateutil.parser.parse( except (httplib2.ServerNotFoundError, RuntimeError, ValueError) as e:
badhttp.request('https://' + testLocation, 'HEAD')[0]['date']) controlflow.system_error_exit(4, str(e))
except (httplib2.ServerNotFoundError, RuntimeError, ValueError) as e: offset = remainder = int(abs((localUTC - googleUTC).total_seconds()))
controlflow.system_error_exit(4, str(e)) if offset < MAX_LOCAL_GOOGLE_TIME_OFFSET and prot == 'http':
offset = remainder = int(abs((localUTC - googleUTC).total_seconds())) continue
timeoff = [] timeoff = []
for tou in TIME_OFFSET_UNITS: for tou in TIME_OFFSET_UNITS:
uval, remainder = divmod(remainder, tou[1]) uval, remainder = divmod(remainder, tou[1])
if uval: if uval:
timeoff.append(f'{uval} {tou[0]}{"s" if uval != 1 else ""}') timeoff.append(f'{uval} {tou[0]}{"s" if uval != 1 else ""}')
if not timeoff: if not timeoff:
timeoff.append('less than 1 second') timeoff.append('less than 1 second')
nicetime = ', '.join(timeoff) nicetime = ', '.join(timeoff)
return (offset, nicetime) return (offset, nicetime)
def doGAMCheckForUpdates(forceCheck=False): def doGAMCheckForUpdates(forceCheck=False):