47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
import requests
|
|
import ssl
|
|
import socket
|
|
from datetime import datetime
|
|
|
|
def get_ssl_info(hostname, port=443):
|
|
try:
|
|
ctx = ssl.create_default_context()
|
|
with socket.create_connection((hostname, port), timeout=10) as sock:
|
|
with ctx.wrap_socket(sock, server_hostname=hostname) as ssock:
|
|
cert = ssock.getpeercert()
|
|
issuer = dict(x[0] for x in cert['issuer'])
|
|
not_after = datetime.strptime(cert['notAfter'], "%b %d %H:%M:%S %Y %Z")
|
|
return {
|
|
"issuer": issuer.get("organizationName", "Unknown"),
|
|
"expiry": not_after.strftime("%Y-%m-%d %H:%M:%S"),
|
|
"valid": not_after > datetime.utcnow()
|
|
}
|
|
except Exception as e:
|
|
return {"error": str(e)}
|
|
|
|
def lambda_handler(event, context):
|
|
"""
|
|
Expects event = {"domains": ["rossmanith.family", "vaishnavi.house"]}
|
|
"""
|
|
domains = event.get("domains", [])
|
|
results = {}
|
|
|
|
for domain in domains:
|
|
url = f"https://{domain}"
|
|
domain_result = {}
|
|
try:
|
|
resp = requests.get(url, timeout=10)
|
|
domain_result["status_code"] = resp.status_code
|
|
domain_result["reachable"] = resp.status_code == 200
|
|
except requests.exceptions.RequestException as e:
|
|
domain_result["error"] = str(e)
|
|
domain_result["reachable"] = False
|
|
|
|
# SSL info
|
|
ssl_info = get_ssl_info(domain)
|
|
domain_result["ssl"] = ssl_info
|
|
|
|
results[domain] = domain_result
|
|
|
|
return results
|