"""仅检查远程传输层,不登录、不发信、不读写业务数据库。""" from datetime import datetime, timezone import hashlib import http.client import json from pathlib import Path import smtplib import socket import ssl import subprocess HOST = 'mail.solit.cn' CONTEXT = ssl.create_default_context() def request(port, path='/', host=HOST, tls=True): raw = socket.create_connection(('127.0.0.1', port), timeout=10) stream = CONTEXT.wrap_socket(raw, server_hostname=host) if tls else raw try: stream.sendall(f'GET {path} HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\n\r\n'.encode()) response = http.client.HTTPResponse(stream) response.begin() body = response.read() return response.status, dict(response.getheaders()), body finally: stream.close() def check_redirects(results): expected = 'https://' + HOST + '/acceptance?x=1' failures = [] for port in (80, 8001): status, headers, _ = request(port, '/acceptance?x=1', tls=False) location = headers.get('Location') results['checks'].append({'port': port, 'redirect': location, 'status': status}) if status != 308 or location != expected: failures.append({'port': port, 'check': 'public_redirect', 'expectedStatus': 308, 'actualStatus': status, 'expectedLocation': expected, 'actualLocation': location}) return failures def main(): results = {'time': datetime.now(timezone.utc).isoformat(), 'checks': []} leaf = subprocess.run(['openssl', 'x509', '-in', '/opt/smail/ssl/custom.crt', '-outform', 'DER'], check=True, capture_output=True).stdout expected = hashlib.sha256(leaf).hexdigest() for port, host in [(443, HOST), (8002, HOST), (465, 'smtp.solit.cn'), (993, 'imap.solit.cn'), (995, 'pop.solit.cn')]: with socket.create_connection(('127.0.0.1', port), timeout=10) as raw: with CONTEXT.wrap_socket(raw, server_hostname=host) as stream: actual = hashlib.sha256(stream.getpeercert(binary_form=True)).hexdigest() assert actual == expected, (port, 'certificate mismatch') results['checks'].append({'port': port, 'hostname': host, 'tls': stream.version(), 'systemTrust': True, 'sameCertificate': True}) for port in (25, 587): with smtplib.SMTP(timeout=10) as client: assert client.connect('127.0.0.1', port)[0] == 220 client._host = 'smtp.solit.cn' assert client.ehlo('localhost')[0] == 250 assert not client.has_extn('auth'), 'plaintext AUTH advertised' assert client.has_extn('starttls') assert client.starttls(context=CONTEXT)[0] == 220 assert hashlib.sha256(client.sock.getpeercert(binary_form=True)).hexdigest() == expected assert client.ehlo('localhost')[0] == 250 results['checks'].append({'port': port, 'systemTrust': True, 'starttls': True, 'plaintextAuthHidden': True}) for port in (443, 8002): status, headers, body = request(port) assert status == 200 and b'