-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathconfig.py
More file actions
373 lines (350 loc) · 13.2 KB
/
config.py
File metadata and controls
373 lines (350 loc) · 13.2 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import logging
import os
import pathlib
import secrets
import sys
import tempfile
import zipfile
from contextlib import suppress
from seleniumbase.config import settings
from typing import Union, List, Optional
__all__ = [
"Config",
"find_chrome_executable",
"temp_profile_dir",
"is_root",
"is_posix",
"PathLike",
]
logger = logging.getLogger(__name__)
is_posix = sys.platform.startswith(("darwin", "cygwin", "linux", "linux2"))
PathLike = Union[str, pathlib.Path]
AUTO = None
class Config:
"""Config object"""
def __init__(
self,
user_data_dir: Optional[PathLike] = AUTO,
headless: Optional[bool] = False,
incognito: Optional[bool] = False,
guest: Optional[bool] = False,
browser_executable_path: Optional[PathLike] = AUTO,
browser_args: Optional[List[str]] = AUTO,
sandbox: Optional[bool] = True,
lang: Optional[str] = "en-US",
host: str = AUTO,
port: int = AUTO,
expert: bool = AUTO,
proxy: Optional[str] = None,
extension_dir: Optional[str] = None,
**kwargs: dict,
):
"""
Creates a config object.
Can be called without any arguments to generate a best-practice config,
which is recommended.
Calling the object, eg: myconfig(), returns the list of arguments which
are provided to the browser.
Additional args can be added using the :py:obj:`~add_argument method`.
Instances of this class are usually not instantiated by end users.
:param user_data_dir: the data directory to use
:param headless: set to True for headless mode
:param browser_executable_path:
Specify browser executable, instead of using autodetect.
:param browser_args: Forwarded to browser executable.
Eg: ["--some-chromeparam=somevalue", "some-other-param=someval"]
:param sandbox: disables sandbox
:param autodiscover_targets: use autodiscovery of targets
:param lang:
Language string to use other than the default "en-US,en;q=0.9"
:param expert: When set to True, "expert" mode is enabled.
This adds: --disable-web-security --disable-site-isolation-trials,
as well as some scripts and patching useful for debugging.
(For example, ensuring shadow-root is always in "open" mode.)
:param kwargs:
:type user_data_dir: PathLike
:type headless: bool
:type browser_executable_path: PathLike
:type browser_args: list[str]
:type sandbox: bool
:type lang: str
:type kwargs: dict
"""
if not browser_args:
browser_args = []
if not user_data_dir:
self._user_data_dir = temp_profile_dir()
self._custom_data_dir = False
else:
self.user_data_dir = user_data_dir
profile = os.path.join(self.user_data_dir, "Default")
preferences_file = os.path.join(profile, "Preferences")
preferences = get_default_preferences()
if not os.path.exists(profile):
with suppress(Exception):
os.makedirs(profile)
with open(preferences_file, "w") as f:
f.write(preferences)
if not browser_executable_path:
browser_executable_path = find_chrome_executable()
self._browser_args = browser_args
self.browser_executable_path = browser_executable_path
self.headless = headless
self.incognito = incognito
self.guest = guest
self.sandbox = sandbox
self.host = host
self.port = port
self.expert = expert
self.proxy = proxy
self.extension_dir = extension_dir
self._extensions = []
# When using posix-ish operating system and running as root,
# you must use no_sandbox=True
if is_posix and is_root() and sandbox:
logger.info("Detected root usage, auto-disabling sandbox mode.")
self.sandbox = False
self.autodiscover_targets = True
self.lang = lang
# Other keyword args will be accessible by attribute
self.__dict__.update(kwargs)
super().__init__()
start_width = settings.CHROME_START_WIDTH
start_height = settings.CHROME_START_HEIGHT
start_x = settings.WINDOW_START_X
start_y = settings.WINDOW_START_Y
self._default_browser_args = [
"--window-size=%s,%s" % (start_width, start_height),
"--window-position=%s,%s" % (start_x, start_y),
"--remote-allow-origins=*",
"--no-first-run",
"--no-service-autorun",
"--disable-auto-reload",
"--no-default-browser-check",
"--homepage=about:blank",
"--no-pings",
"--wm-window-animations-disabled",
"--animation-duration-scale=0",
"--enable-privacy-sandbox-ads-apis",
"--safebrowsing-disable-download-protection",
'--simulate-outdated-no-au="Tue, 31 Dec 2099 23:59:59 GMT"',
"--password-store=basic",
"--deny-permission-prompts",
"--disable-infobars",
"--disable-breakpad",
"--disable-prompt-on-repost",
"--disable-password-generation",
"--disable-ipc-flooding-protection",
"--disable-background-timer-throttling",
"--disable-search-engine-choice-screen",
"--disable-backgrounding-occluded-windows",
"--disable-client-side-phishing-detection",
"--disable-top-sites",
"--disable-translate",
"--disable-renderer-backgrounding",
"--disable-background-networking",
"--disable-dev-shm-usage",
]
@property
def browser_args(self):
return sorted(self._default_browser_args + self._browser_args)
@property
def user_data_dir(self):
return self._user_data_dir
@user_data_dir.setter
def user_data_dir(self, path: PathLike):
self._user_data_dir = str(path)
self._custom_data_dir = True
@property
def uses_custom_data_dir(self) -> bool:
return self._custom_data_dir
def add_extension(self, extension_path: PathLike):
"""
Adds an extension to load. You can set the extension_path to a
folder (containing the manifest), or an extension zip file (.crx)
:param extension_path:
"""
path = pathlib.Path(extension_path)
if not path.exists():
raise FileNotFoundError(
"Could not find anything here: %s" % str(path)
)
if path.is_file():
tf = tempfile.mkdtemp(
prefix="extension_", suffix=secrets.token_hex(4)
)
with zipfile.ZipFile(path, "r") as z:
z.extractall(tf)
self._extensions.append(tf)
elif path.is_dir():
for item in path.rglob("manifest.*"):
path = item.parent
self._extensions.append(path)
def __call__(self):
# The host and port will be added when starting the browser.
# By the time it starts, the port is probably already taken.
args = self._default_browser_args.copy()
args += ["--user-data-dir=%s" % self.user_data_dir]
args += [
"--disable-features=IsolateOrigins,site-per-process,Translate,"
"InsecureDownloadWarnings,DownloadBubble,DownloadBubbleV2,"
"OptimizationTargetPrediction,OptimizationGuideModelDownloading,"
"SidePanelPinning,UserAgentClientHint,PrivacySandboxSettings4,"
"DisableLoadExtensionCommandLineSwitch"
]
if self.proxy:
args += ["--test-type"]
args += ["--disable-session-crashed-bubble"]
if self.expert:
args += [
"--disable-web-security",
"--disable-site-isolation-trials",
]
if self.proxy:
args.append("--proxy-server=%s" % self.proxy.split("@")[-1])
args.append("--ignore-certificate-errors")
args.append("--ignore-ssl-errors=yes")
if self.extension_dir:
args.append("--load-extension=%s" % self.extension_dir)
if self._browser_args:
args.extend([arg for arg in self._browser_args if arg not in args])
if self.headless:
args.append("--headless=new")
if self.incognito:
args.append("--incognito")
if self.guest:
args.append("--guest")
if not self.sandbox:
args.append("--no-sandbox")
if self.host:
args.append("--remote-debugging-host=%s" % self.host)
if self.port:
args.append("--remote-debugging-port=%s" % self.port)
return args
def add_argument(self, arg: str):
if any(
x in arg.lower()
for x in [
"headless",
"data-dir",
"data_dir",
"no-sandbox",
"no_sandbox",
"lang",
]
):
raise ValueError(
'"%s" is not allowed. Please use one of the '
'attributes of the Config object to set it.'
% arg
)
self._browser_args.append(arg)
def __repr__(self):
s = f"{self.__class__.__name__}"
for k, v in ({**self.__dict__, **self.__class__.__dict__}).items():
if k[0] == "_":
continue
if not v:
continue
if isinstance(v, property):
v = getattr(self, k)
if callable(v):
continue
s += f"\n\t{k} = {v}"
return s
def is_root():
"""
Helper function to determine if the user is trying to launch chrome
under linux as root, which needs some alternative handling.
"""
import ctypes
import os
try:
return os.getuid() == 0
except AttributeError:
return ctypes.windll.shell32.IsUserAnAdmin() != 0
def get_default_preferences():
return (
"""{"credentials_enable_service": false,
"password_manager_enabled": false,
"password_manager_leak_detection": false}"""
)
def temp_profile_dir():
"""Generate a temp dir (path)"""
path = os.path.normpath(tempfile.mkdtemp(prefix="uc_"))
profile = os.path.join(path, "Default")
preferences_file = os.path.join(profile, "Preferences")
preferences = get_default_preferences()
if not os.path.exists(profile):
with suppress(Exception):
os.makedirs(profile)
with open(preferences_file, "w") as f:
f.write(preferences)
return path
def find_chrome_executable(return_all=False):
"""
Finds the chrome, beta, canary, chromium executable
and returns the disk path.
"""
candidates = []
if is_posix:
for item in os.environ.get("PATH").split(os.pathsep):
for subitem in (
"google-chrome",
"chromium",
"chromium-browser",
"chrome",
"google-chrome-stable",
):
candidates.append(os.sep.join((item, subitem)))
if "darwin" in sys.platform:
candidates += [
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"/Applications/Chromium.app/Contents/MacOS/Chromium",
]
else:
for item in map(
os.environ.get,
(
"PROGRAMFILES",
"PROGRAMFILES(X86)",
"LOCALAPPDATA",
"PROGRAMW6432",
),
):
if item is not None:
for subitem in (
"Google/Chrome/Application",
"Google/Chrome Beta/Application",
"Google/Chrome Canary/Application",
):
candidates.append(
os.sep.join((item, subitem, "chrome.exe"))
)
rv = []
for candidate in candidates:
if os.path.exists(candidate) and os.access(candidate, os.X_OK):
logger.debug("%s is a valid candidate... " % candidate)
rv.append(candidate)
else:
logger.debug(
"%s is not a valid candidate because it doesn't exist "
"or isn't an executable."
% candidate
)
winner = None
if return_all and rv:
return rv
if rv and len(rv) > 1:
# Assuming the shortest path wins
winner = min(rv, key=lambda x: len(x))
elif len(rv) == 1:
winner = rv[0]
if winner:
return os.path.normpath(winner)
raise FileNotFoundError(
"Could not find a valid chrome browser binary. "
"Please make sure Chrome is installed. "
"Or use the keyword argument: "
"'browser_executable_path=/path/to/your/browser'."
)