Skip to content

Commit 53d0584

Browse files
committed
Add the ability to set a SOCKS5 proxy on the command line
1 parent fcbbb73 commit 53d0584

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,30 @@ def _create_firefox_profile(
340340
"datareporting.policy.dataSubmissionPolicyAccepted", False)
341341
profile.set_preference("toolkit.telemetry.unified", False)
342342
if proxy_string:
343-
proxy_server = proxy_string.split(':')[0]
344-
proxy_port = proxy_string.split(':')[1]
343+
socks_proxy = False
344+
socks_ver = 0
345+
chunks = proxy_string.split(':')
346+
if len(chunks) == 3 and (
347+
chunks[0] == "socks4" or chunks[0] == "socks5"):
348+
socks_proxy = True
349+
socks_ver = int(chunks[0][5])
350+
if chunks[1].startswith("//") and len(chunks[1]) > 2:
351+
chunks[1] = chunks[1][2:]
352+
proxy_server = chunks[1]
353+
proxy_port = chunks[2]
354+
else:
355+
proxy_server = proxy_string.split(':')[0]
356+
proxy_port = proxy_string.split(':')[1]
345357
profile.set_preference("network.proxy.type", 1)
346-
profile.set_preference("network.proxy.http", proxy_server)
347-
profile.set_preference("network.proxy.http_port", int(proxy_port))
348-
profile.set_preference("network.proxy.ssl", proxy_server)
349-
profile.set_preference("network.proxy.ssl_port", int(proxy_port))
358+
if socks_proxy:
359+
profile.set_preference('network.proxy.socks', proxy_server)
360+
profile.set_preference('network.proxy.socks_port', int(proxy_port))
361+
profile.set_preference('network.proxy.socks_version', socks_ver)
362+
else:
363+
profile.set_preference("network.proxy.http", proxy_server)
364+
profile.set_preference("network.proxy.http_port", int(proxy_port))
365+
profile.set_preference("network.proxy.ssl", proxy_server)
366+
profile.set_preference("network.proxy.ssl_port", int(proxy_port))
350367
if user_agent:
351368
profile.set_preference("general.useragent.override", user_agent)
352369
profile.set_preference(
@@ -406,12 +423,25 @@ def validate_proxy_string(proxy_string):
406423
elif proxy_string.startswith('https://'):
407424
proxy_string = proxy_string.split('https://')[1]
408425
elif '://' in proxy_string:
409-
proxy_string = proxy_string.split('://')[1]
426+
if not proxy_string.startswith('socks4://') and not (
427+
proxy_string.startswith('socks5://')):
428+
proxy_string = proxy_string.split('://')[1]
410429
chunks = proxy_string.split(':')
411430
if len(chunks) == 2:
412431
if re.match(r'^\d+$', chunks[1]):
413432
if page_utils.is_valid_url('http://' + proxy_string):
414433
valid = True
434+
elif len(chunks) == 3:
435+
if re.match(r'^\d+$', chunks[2]):
436+
if page_utils.is_valid_url('http:' + ':'.join(chunks[1:])):
437+
if chunks[0] == "http":
438+
valid = True
439+
elif chunks[0] == "https":
440+
valid = True
441+
elif chunks[0] == "socks4":
442+
valid = True
443+
elif chunks[0] == "socks5":
444+
valid = True
415445
else:
416446
proxy_string = val_ip.group()
417447
valid = True

0 commit comments

Comments
 (0)