76 lines
3.4 KiB
Python
76 lines
3.4 KiB
Python
import os
|
|
import sys
|
|
import logging
|
|
import subprocess
|
|
from pathlib import Path
|
|
from utils.system_utils import get_architecture
|
|
|
|
class ProxyManager:
|
|
def __init__(self):
|
|
self.is_enabled = False
|
|
self.http_proxy = "127.0.0.1:1081"
|
|
self.socks_proxy = "127.0.0.1:1080"
|
|
|
|
def enable(self):
|
|
"""启用系统代理"""
|
|
if self.is_enabled:
|
|
return True
|
|
|
|
try:
|
|
# 获取当前网络服务
|
|
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, text=True).strip()
|
|
|
|
# 设置HTTP/HTTPS代理
|
|
http_cmd = f"networksetup -setwebproxy '{service}' {self.http_proxy.split(':')[0]} {self.http_proxy.split(':')[1]}"
|
|
https_cmd = f"networksetup -setsecurewebproxy '{service}' {self.http_proxy.split(':')[0]} {self.http_proxy.split(':')[1]}"
|
|
|
|
# 设置SOCKS代理
|
|
socks_cmd = f"networksetup -setsocksfirewallproxy '{service}' {self.socks_proxy.split(':')[0]} {self.socks_proxy.split(':')[1]}"
|
|
|
|
# 使用AppleScript请求管理员权限
|
|
script = f"""
|
|
do shell script "{http_cmd}" with administrator privileges
|
|
do shell script "{https_cmd}" with administrator privileges
|
|
do shell script "{socks_cmd}" with administrator privileges
|
|
do shell script "networksetup -setwebproxystate '{service}' on" with administrator privileges
|
|
do shell script "networksetup -setsecurewebproxystate '{service}' on" with administrator privileges
|
|
do shell script "networksetup -setsocksfirewallproxystate '{service}' on" with administrator privileges
|
|
"""
|
|
|
|
subprocess.run(['osascript', '-e', script], check=True)
|
|
self.is_enabled = True
|
|
logging.info("System proxy enabled successfully")
|
|
return True
|
|
except Exception as e:
|
|
logging.error(f"Failed to enable system proxy: {str(e)}")
|
|
return False
|
|
|
|
def disable(self):
|
|
"""禁用系统代理"""
|
|
if not self.is_enabled:
|
|
return True
|
|
|
|
try:
|
|
# 获取当前网络服务
|
|
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, text=True).strip()
|
|
|
|
# 使用AppleScript请求管理员权限
|
|
script = f"""
|
|
do shell script "networksetup -setwebproxystate '{service}' off" with administrator privileges
|
|
do shell script "networksetup -setsecurewebproxystate '{service}' off" with administrator privileges
|
|
do shell script "networksetup -setsocksfirewallproxystate '{service}' off" with administrator privileges
|
|
"""
|
|
|
|
subprocess.run(['osascript', '-e', script], check=True)
|
|
self.is_enabled = False
|
|
logging.info("System proxy disabled successfully")
|
|
return True
|
|
except Exception as e:
|
|
logging.error(f"Failed to disable system proxy: {str(e)}")
|
|
return False
|
|
|
|
def get_proxy_status(self):
|
|
"""获取代理状态"""
|
|
return self.is_enabled |