Repository navigation
Expand file tree
/
Copy pathsysproxy.py
More file actions
114 lines (107 loc) · 4.65 KB
/
Copy pathsysproxy.py
File metadata and controls
114 lines (107 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""Platform-specific system proxy integration."""
import struct
import subprocess
import sys
from .errors import require
class MacSetting: # pylint: disable=too-few-public-methods
"""Manage the selected macOS system proxy setting."""
def __init__(self, args):
"""Select a supported listener and enable its macOS proxy modes."""
self.device = None
self.listen = None
self.modes = None
self.mode_name = None
for option in args.listen:
protos = [x.name for x in option.protos]
if option.unix or 'ssl' in protos or 'secure' in protos:
continue
if 'socks5' in protos:
self.modes = ['setsocksfirewallproxy']
self.mode_name = 'socks5'
self.listen = option
break
if 'http' in protos:
self.modes = ['setwebproxy', 'setsecurewebproxy']
self.mode_name = 'http'
self.listen = option
break
if 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)
return
line = 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)
return
self.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):
"""Disable the proxy modes previously enabled by this instance."""
if self.device is None:
return
for mode in self.modes:
subprocess.check_call(['/usr/sbin/networksetup', mode+'state', self.device, 'off'])
print('System proxy setting -> off')
class WindowsSetting: # pylint: disable=too-few-public-methods
"""Manage the selected Windows system proxy registry value."""
KEY = r'Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections'
SUBKEY = 'DefaultConnectionSettings'
def __init__(self, args):
"""Select a supported listener and update Windows proxy settings."""
self.listen = None
for option in args.listen:
protos = [x.name for x in option.protos]
if option.unix or 'ssl' in protos or 'secure' in protos:
continue
if 'http' in protos:
self.listen = option
break
if self.listen is None:
print('No server listen on localhost by http')
import winreg # pylint: disable=import-error,import-outside-toplevel
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, self.KEY, 0, winreg.KEY_ALL_ACCESS)
value, regtype = winreg.QueryValueEx(key, self.SUBKEY)
require(regtype == winreg.REG_BINARY)
server = f'localhost:{self.listen.port}'.encode()
bypass = '<local>'.encode()
counter = int.from_bytes(value[4:8], 'little') + 1
value = (
value[:4]
+ struct.pack('<III', counter, 3, len(server))
+ server
+ struct.pack('<I', len(bypass))
+ bypass
+ b'\x00' * 36
)
winreg.SetValueEx(key, self.SUBKEY, None, regtype, value)
winreg.CloseKey(key)
def clear(self):
"""Restore the Windows proxy registry value to its disabled state."""
if self.listen is None:
return
import winreg # pylint: disable=import-error,import-outside-toplevel
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, self.KEY, 0, winreg.KEY_ALL_ACCESS)
value, regtype = winreg.QueryValueEx(key, self.SUBKEY)
require(regtype == winreg.REG_BINARY)
counter = int.from_bytes(value[4:8], 'little') + 1
value = value[:4] + struct.pack('<II', counter, 1) + b'\x00'*44
winreg.SetValueEx(key, self.SUBKEY, None, regtype, value)
winreg.CloseKey(key)
def setup(args):
"""Apply platform-specific system proxy integration when supported."""
if sys.platform == 'darwin':
return MacSetting(args)
if sys.platform == 'win32':
return WindowsSetting(args)
print(f'System proxy setting: platform "{sys.platform}" not supported')
return None