refactor: Convert alert_tanner to use aiohttp
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
16
main.py
16
main.py
@@ -19,10 +19,12 @@ except FileNotFoundError:
|
|||||||
logging.error("hosts.json not found. Please copy hosts.json.example to hosts.json and configure it.")
|
logging.error("hosts.json not found. Please copy hosts.json.example to hosts.json and configure it.")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
def alert_tanner(message):
|
async def alert_tanner(message):
|
||||||
try:
|
try:
|
||||||
params = {'ssl-monitor': message}
|
params = {'ssl-monitor': message}
|
||||||
requests.get('https://tbot.tannercollin.com/message', params=params, timeout=4)
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get('https://tbot.tannercollin.com/message', params=params, timeout=4) as response:
|
||||||
|
response.raise_for_status()
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
logging.error('Problem alerting Tanner: ' + str(e))
|
logging.error('Problem alerting Tanner: ' + str(e))
|
||||||
|
|
||||||
@@ -43,7 +45,7 @@ async def check_host_cert(host, port, seen_serials):
|
|||||||
# this case should be rare if handshake succeeded
|
# this case should be rare if handshake succeeded
|
||||||
msg = f"Could not get certificate for {host}:{port}"
|
msg = f"Could not get certificate for {host}:{port}"
|
||||||
logging.warning(msg)
|
logging.warning(msg)
|
||||||
alert_tanner(msg)
|
await alert_tanner(msg)
|
||||||
return
|
return
|
||||||
|
|
||||||
serial_number = cert.get('serialNumber')
|
serial_number = cert.get('serialNumber')
|
||||||
@@ -61,18 +63,18 @@ async def check_host_cert(host, port, seen_serials):
|
|||||||
if time_left < timedelta(days=7):
|
if time_left < timedelta(days=7):
|
||||||
msg = f"Certificate for {host}:{port} expires in less than a week: {expiry_date}"
|
msg = f"Certificate for {host}:{port} expires in less than a week: {expiry_date}"
|
||||||
logging.warning(msg)
|
logging.warning(msg)
|
||||||
alert_tanner(msg)
|
await alert_tanner(msg)
|
||||||
else:
|
else:
|
||||||
logging.info(f"Certificate for {host}:{port} is valid until {expiry_date} ({time_left.days} days left)")
|
logging.info(f"Certificate for {host}:{port} is valid until {expiry_date} ({time_left.days} days left)")
|
||||||
|
|
||||||
except ssl.SSLCertVerificationError as e:
|
except ssl.SSLCertVerificationError as e:
|
||||||
msg = f"Certificate verification error for {host}:{port}: {e.reason}"
|
msg = f"Certificate verification error for {host}:{port}: {e.reason}"
|
||||||
logging.error(msg)
|
logging.error(msg)
|
||||||
alert_tanner(msg)
|
await alert_tanner(msg)
|
||||||
except ssl.SSLError as e:
|
except ssl.SSLError as e:
|
||||||
msg = f"SSL error for {host}:{port}: {e}"
|
msg = f"SSL error for {host}:{port}: {e}"
|
||||||
logging.error(msg)
|
logging.error(msg)
|
||||||
alert_tanner(msg)
|
await alert_tanner(msg)
|
||||||
except (asyncio.TimeoutError, OSError) as e:
|
except (asyncio.TimeoutError, OSError) as e:
|
||||||
# Per instructions: log and move on for connection errors
|
# Per instructions: log and move on for connection errors
|
||||||
logging.error(f"Connection error for {host}:{port}: {e}")
|
logging.error(f"Connection error for {host}:{port}: {e}")
|
||||||
@@ -80,7 +82,7 @@ async def check_host_cert(host, port, seen_serials):
|
|||||||
# Catchall for other things
|
# Catchall for other things
|
||||||
msg = f"An unexpected error occurred for {host}:{port}: {e}"
|
msg = f"An unexpected error occurred for {host}:{port}: {e}"
|
||||||
logging.error(msg)
|
logging.error(msg)
|
||||||
alert_tanner(msg)
|
await alert_tanner(msg)
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
|||||||
Reference in New Issue
Block a user