class MacSetting(object):def __init__(self, args):self.device = Noneself.listen = Noneself.modes = Noneself.mode_name =
class MacSetting(object):def __init__(self, args):self.device = Noneself.listen = Noneself.modes = Noneself.mode_name = Nonefor option in args.listen:protos = [x.name for x in option.protos]if option.unix or 'ssl' in protos or 'secure' in protos:continueif 'socks5' in protos:self.modes = ['setsocksfirewallproxy']self.mode_name = 'socks5'self.listen = optionbreakif 'http' in protos:self.modes = ['setwebproxy', 'setsecurewebproxy']self.mode_name = 'http'self.listen = optionbreakif self.listen 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, ret.split('\n\n')), None)if en0 is None:print('Cannot find en0 device name!\n\nInfo:\n\n'+ret)returnline = next(filter(lambda x: x.startswith('('), en0.split('\n')), None)if line is None:print('Cannot find en0 device name!\n\nInfo:\n\n'+ret)returnself.device = line[3:].strip()for mode in self.modes:subprocess.check_call(['/usr/sbin/networksetup', mode, self.device, 'localhost', str(self.listen.port), 'off'])print(f'System proxy setting -> {self.mode_name} localhost:{self.listen.port}')def clear(self):if self.device is None:returnfor mode in self.modes:subprocess.check_call(['/usr/sbin/networksetup', mode+'state', self.device, 'off'])print('System proxy setting -> off')
这段代码定义了一个名为 MacSetting 的类,其结构如下:
总的来说,这个类的作用是设置或清除 macOS 操作系统上的代理服务器。在构造函数中,根据监听协议来确定代理模式(socks5 或 Http),接着使用网络设置工具设置代理规则。在 clear 方法中,则使用相同的网络设置工具来将代理服务器禁用。
来源地址:https://blog.csdn.net/weixin_36829761/article/details/129318769
--结束END--
本文标题: 设置或清除 macOS 系统的代理服务器
本文链接: https://lsjlt.com/news/394774.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0