Added new cronreg argument for conditional reregistration

This commit is contained in:
Steven Burnham 2023-11-09 20:53:10 -05:00
parent e34a1e2b87
commit e38d9fcda2
No known key found for this signature in database
GPG key ID: D765679712A2FC3D

23
demo.py
View file

@ -221,6 +221,28 @@ async def main(args: argparse.Namespace):
logging.info("Reregistered!")
if args.cronreg:
reregister_within = 60 # Minutes, time where if expiration time is less than, rereg.
for user in users:
if "P:" in str(user.user_id):
logging.info(f'The user is: {user}')
cert = x509.load_pem_x509_certificate(user.id_cert.encode('utf-8'))
expiration = cert.not_valid_after
logging.info(f'Certificate expires on: {expiration}')
reregister_time = expiration - datetime.timedelta(minutes=reregister_within)
reregister_time = reregister_time.astimezone(datetime.timezone.utc)
logging.info(f'Reregistration will occur at: {reregister_time}')
reregister_delta = (reregister_time - datetime.datetime.now(datetime.timezone.utc)).total_seconds()
logging.info(f'The time between now and reregistration time is: {reregister_delta / 3600} hours or {reregister_delta / 86400} days')
if reregister_delta > 3600:
logging.info('Certificates expiration is greater than 60 minutes, quiting')
else:
logging.info('Certificate expires soon, reregistering now')
expiration = await reregister(conn, users)
expiration = expiration.replace(tzinfo=datetime.timezone.utc)
logging.info('Reregistered')
logging.info(expire_msg(expiration))
if args.reregister:
await reregister(conn, users)
@ -255,6 +277,7 @@ if __name__ == "__main__":
parser.add_argument("--phone", type=str, help="Override the phone IP")
parser.add_argument("--gateway", type=str, help="Override the gateway phone number")
parser.add_argument("--daemon", action="store_true", help="Continuously reregister 5 minutes before the certificate expires")
parser.add_argument("--cronreg", action="store_true", help="Reregister if less than 60 minutes from expiration")
args = parser.parse_args()