当前位置:   article > 正文

设置或清除 macOS 系统的代理服务器_mac 关闭代理

mac 关闭代理

  1. class MacSetting(object):
  2. def __init__(self, args):
  3. self.device = None
  4. self.listen = None
  5. self.modes = None
  6. self.mode_name = None
  7. for option in args.listen:
  8. protos = [x.name for x in option.protos]
  9. if option.unix or 'ssl' in protos or 'secure' in protos:
  10. continue
  11. if 'socks5' in protos:
  12. self.modes = ['setsocksfirewallproxy']
  13. self.mode_name = 'socks5'
  14. self.listen = option
  15. break
  16. if 'http' in protos:
  17. self.modes = ['setwebproxy', 'setsecurewebproxy']
  18. self.mode_name = 'http'
  19. self.listen = option
  20. break
  21. if self.listen is None:
  22. print('No server listen on localhost by http/socks5')
  23. ret = subprocess.check_output(['/usr/sbin/networksetup', '-listnetworkserviceorder']).decode()
  24. en0 = next(filter(lambda x: 'Device: en0' in x, ret.split('\n\n')), None)
  25. if en0 is None:
  26. print('Cannot find en0 device name!\n\nInfo:\n\n'+ret)
  27. return
  28. line = next(filter(lambda x: x.startswith('('), en0.split('\n')), None)
  29. if line is None:
  30. print('Cannot find en0 device name!\n\nInfo:\n\n'+ret)
  31. return
  32. self.device = line[3:].strip()
  33. for mode in self.modes:
  34. subprocess.check_call(['/usr/sbin/networksetup', mode, self.device, 'localhost', str(self.listen.port), 'off'])
  35. print(f'System proxy setting -> {self.mode_name} localhost:{self.listen.port}')
  36. def clear(self):
  37. if self.device is None:
  38. return
  39. for mode in self.modes:
  40. subprocess.check_call(['/usr/sbin/networksetup', mode+'state', self.device, 'off'])
  41. print('System proxy setting -> off')

这段代码定义了一个名为 MacSetting 的类,其结构如下:

  • 有一个构造函数(init)来初始化类属性,以及一种清除属性(clear)的方法。
  • 在类的构造函数中,根据输入参数来确定设备、监听、模式和模式名称的状态。
  • 如果没有找到监听,则输出一个警告信息。
  • 通过调用 macOS 的命令行工具 networksetup,将设备上的代理设置为指定的模式和本地主机的端口。
  • 如果设备属性值为 None,则表示清除代理设置。与设置代理类似,也是通过运行 networksetup 命令行工具来完成。

总的来说,这个类的作用是设置或清除 macOS 操作系统上的代理服务器。在构造函数中,根据监听协议来确定代理模式(socks5 或 http),接着使用网络设置工具设置代理规则。在 clear 方法中,则使用相同的网络设置工具来将代理服务器禁用。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/244819
推荐阅读
相关标签
  

闽ICP备14008679号