diff --git a/cert/__init__.py b/cert/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cert/macos.py b/cert/macos.py new file mode 100644 index 0000000..46a3abb --- /dev/null +++ b/cert/macos.py @@ -0,0 +1,9 @@ +import subprocess + + +def is_certificate_installed(cert_name = 'mitmproxy'): + try: + result = subprocess.run(['security', 'find-certificate', '-c', cert_name], capture_output=True, text=True) + return result.returncode == 0 + except FileNotFoundError: + raise NotImplementedError("此系统中未找到 security 命令") diff --git a/cert/win.py b/cert/win.py new file mode 100644 index 0000000..18261c7 --- /dev/null +++ b/cert/win.py @@ -0,0 +1,13 @@ +import wincertstore +import os + + +def is_certificate_installed(cert_name = "mitmproxy"): + stores = ["MY", "ROOT", "CA"] + for storename in stores: + with wincertstore.CertSystemStore(storename) as store: + for cert in store.itercerts(): + name = cert.get_name() + if name == cert_name: + return True + return False diff --git a/utils.py b/utils.py index c3781ee..5741895 100644 --- a/utils.py +++ b/utils.py @@ -1,13 +1,18 @@ import re -import subprocess import urllib.request + import requests +from cert import macos + + +# 检查代理是否正确 def is_proxy_correct(target_proxy_address): proxy = urllib.request.getproxies() response = requests.get('http://mitm.it', proxies=proxy).text - if re.search(r'If you can see this, traffic is not passing through mitmproxy', response) or proxy != target_proxy_address: + if re.search(r'If you can see this, traffic is not passing through mitmproxy', + response) or proxy != target_proxy_address: print("\n检测到系统的网络代理设置不正确") print(f"当前网络代理: {proxy}") print(f"目标网络代理: {target_proxy_address}") @@ -16,50 +21,15 @@ def is_proxy_correct(target_proxy_address): return True -cert_name = 'mitmproxy' - -def is_certificate_installed_macos(): - try: - result = subprocess.run(['security', 'find-certificate', '-c', cert_name], capture_output=True, text=True) - return result.returncode == 0 - except FileNotFoundError: - raise NotImplementedError("此系统中未找到 security 命令") - -def is_certificate_installed_windows(): - import wincertstore - import os - from cryptography import x509 - from cryptography.hazmat.backends import default_backend - import hashlib - - def is_certificate_installed(cert_name, thumbprint=None): - if os.name != 'nt': - raise NotImplementedError("此函数仅适用于 Windows") - - stores = ["MY", "ROOT", "CA"] - for storename in stores: - with wincertstore.CertSystemStore(storename) as store: - for cert in store.itercerts(): - name = cert.get_name() - pem = cert.get_pem().decode("ascii") - if name == cert_name: - if thumbprint is None: - return True - else: - cert_obj = x509.load_pem_x509_certificate(pem.encode('utf-8'), default_backend()) - der = cert_obj.public_bytes(x509.Encoding.DER) - computed_thumbprint = hashlib.sha1(der).hexdigest().upper() - if computed_thumbprint == thumbprint: - return True - return False - -# 检测证书是否安装 +# 检查证书是否安装 def wait_until_certificate_installed(): while True: - if is_certificate_installed_macos(): + if macos.is_certificate_installed('mitmproxy'): break else: - input("系统中未检测到 mitmproxy 的证书,请进行手动安装。\n证书安装教程请参考: https://docs.mitmproxy.org/stable/concepts/certificates/#installing-the-mitmproxy-ca-certificate-manually\n\n证书安装后请按任意键继续") + input( + "系统中未检测到 mitmproxy 的证书,请进行手动安装。\n证书安装教程请参考: https://docs.mitmproxy.org/stable/concepts/certificates/#installing-the-mitmproxy-ca-certificate-manually\n\n证书安装后请按任意键继续") + # 检查系统代理是否设置正确 def wait_until_proxy_setting(proxy_address): @@ -69,7 +39,8 @@ def wait_until_proxy_setting(proxy_address): else: input("\n按任意键继续") -def wait_until_env_configured(proxy_address = None): + +def wait_until_env_configured(proxy_address=None): # 检查证书是否安装 wait_until_certificate_installed()