import subprocess import time import os import json import yaml # 1. 启动 mihomo 并绑定到具体网卡 (en0) local_port = 10850 cfg = """ mode: global mixed-port: 10850 bind-address: '127.0.0.1' dns: enable: true nameserver: - https://223.5.5.5/dns-query - https://1.1.1.1/dns-query proxies: - name: "test-direct" type: direct interface-name: en0 """ with open("test_b.yaml", "w") as f: f.write(cfg) # 找到 mihomo mihomo = "./.venv/bin/mihomo" if not os.path.exists(mihomo): mihomo = "mihomo" proc = subprocess.Popen([mihomo, "-f", "test_b.yaml"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) time.sleep(2) try: from curl_cffi import requests resp = requests.get("http://httpbin.org/ip", proxies={"http": "socks5://127.0.0.1:10850", "https": "socks5://127.0.0.1:10850"}, impersonate="chrome") print("Mihomo (direct bound to en0) IP:", resp.json()["origin"]) resp_direct = requests.get("http://httpbin.org/ip", impersonate="chrome") print("System (Surge default) IP:", resp_direct.json()["origin"]) finally: proc.kill() os.remove("test_b.yaml")