class MacSetting(object):
def __init__(self, args):
= None
= None
= None
self.mode_name = None
for option in :
protos = [ for x in ]
if or 'ssl' in protos or 'secure' in protos:
continue
if 'socks5' in protos:
= ['setsocksfirewallproxy']
self.mode_name = 'socks5'
= option
break
if 'http' in protos:
= ['setwebproxy', 'setsecurewebproxy']
self.mode_name = 'http'
= option
break
if is None:
print('No server listen on localhost by http/socks5')
ret = subprocess.check_output(['/usr/sbin/networksetup', '-listnetworkserviceorder']).decode()
en0 = next(filter(lambda x: 'Device: en0' in x, ('\n\n')), None)
if en0 is None:
print('Cannot find en0 device name!\n\nInfo:\n\n'+ret)
return
line = next(filter(lambda x: ('('), ('\n')), None)
if line is None:
print('Cannot find en0 device name!\n\nInfo:\n\n'+ret)
return
= line[3:].strip()
for mode in :
subprocess.check_call(['/usr/sbin/networksetup', mode, , 'localhost', str(), 'off'])
print(f'System proxy setting -> {self.mode_name} localhost:{}')
def clear(self):
if is None:
return
for mode in :
subprocess.check_call(['/usr/sbin/networksetup', mode+'state', , 'off'])
print('System proxy setting -> off')
这段代码定义了一个名为 MacSetting 的类,其结构如下:
- 有一个构造函数(init)来初始化类属性,以及一种清除属性(clear)的方法。
- 在类的构造函数中,根据输入参数来确定设备、监听、模式和模式名称的状态。
- 如果没有找到监听,则输出一个警告信息。
- 通过调用 macOS 的命令行工具 networksetup,将设备上的代理设置为指定的模式和本地主机的端口。
- 如果设备属性值为 None,则表示清除代理设置。与设置代理类似,也是通过运行 networksetup 命令行工具来完成。
总的来说,这个类的作用是设置或清除 macOS 操作系统上的代理服务器。在构造函数中,根据监听协议来确定代理模式(socks5 或 http),接着使用网络设置工具设置代理规则。在 clear 方法中,则使用相同的网络设置工具来将代理服务器禁用。