48 lines
1.8 KiB
Python
48 lines
1.8 KiB
Python
import platform
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
def get_architecture():
|
|
"""检测系统架构"""
|
|
machine = platform.machine().lower()
|
|
if 'arm' in machine or 'aarch64' in machine:
|
|
return 'arm64'
|
|
return 'amd64'
|
|
|
|
def get_v2ray_binary_path():
|
|
"""获取v2ray二进制路径"""
|
|
arch = get_architecture()
|
|
base_dir = Path(__file__).parent.parent
|
|
v2ray_dir = base_dir / "v2ray"
|
|
|
|
if arch == 'arm64':
|
|
return v2ray_dir / "v2ray-macos-arm64"
|
|
return v2ray_dir / "v2ray-macos-64"
|
|
|
|
def set_system_proxy(enable, host="127.0.0.1", port=1081):
|
|
"""配置macOS系统代理"""
|
|
# 获取当前网络服务
|
|
service_cmd = "networksetup -listnetworkserviceorder | grep $(route -n get default | grep 'interface' | awk '{print $2}') -A1 | grep -o '[^ ]*$'"
|
|
service = subprocess.check_output(service_cmd, shell=True).decode().strip()
|
|
|
|
if enable:
|
|
cmd = f"""
|
|
osascript -e 'do shell script "networksetup -setwebproxy \\"{service}\\" {host} {port}" with administrator privileges'
|
|
osascript -e 'do shell script "networksetup -setsecurewebproxy \\"{service}\\" {host} {port}" with administrator privileges'
|
|
"""
|
|
else:
|
|
cmd = f"""
|
|
osascript -e 'do shell script "networksetup -setwebproxystate \\"{service}\\" off" with administrator privileges'
|
|
osascript -e 'do shell script "networksetup -setsecurewebproxystate \\"{service}\\" off" with administrator privileges'
|
|
"""
|
|
|
|
try:
|
|
subprocess.run(cmd, shell=True, check=True)
|
|
return True
|
|
except subprocess.CalledProcessError:
|
|
return False
|
|
|
|
def get_temp_dir():
|
|
"""获取安全的临时目录"""
|
|
import tempfile
|
|
return Path(tempfile.gettempdir()) / f"ezproxy_update_{os.getpid()}" |