-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.py
134 lines (110 loc) · 3.09 KB
/
proxy.py
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
from pathlib import Path
try:
from selenium.webdriver import ChromeOptions
except ImportError:
import sys
packages_path = Path.cwd() / "env" / "Lib" / "site-packages"
sys.path.insert(0, f"{packages_path}")
from selenium.webdriver import ChromeOptions
from config_reader import config
from logger import logger
def get_proxies() -> list[str]:
"""Get proxies from file
:rtype: list
:returns: List of proxies
"""
filepath = Path(config.paths.proxy_file)
if not filepath.exists():
raise SystemExit(f"Couldn't find proxy file: {filepath}")
with open(filepath, encoding="utf-8") as proxyfile:
proxies = [
proxy.strip().replace("'", "").replace('"', "")
for proxy in proxyfile.read().splitlines()
]
return proxies
def install_plugin(
chrome_options: ChromeOptions,
proxy_host: str,
proxy_port: int,
username: str,
password: str,
plugin_folder_name: str,
) -> None:
"""Install plugin on the fly for proxy authentication
:type chrome_options: ChromeOptions
:param chrome_options: ChromeOptions instance to add plugin
:type proxy_host: str
:param proxy_host: Proxy host
:type proxy_port: int
:param proxy_port: Proxy port
:type username: str
:param username: Proxy username
:type password: str
:param password: Proxy password
:type plugin_folder_name: str
:param plugin_folder_name: Plugin folder name for proxy
"""
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 3,
"name": "Chrome Proxy Authentication",
"background": {
"service_worker": "background.js"
},
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"webRequest",
"webRequestAuthProvider"
],
"host_permissions": [
"<all_urls>"
],
"minimum_chrome_version": "108"
}
"""
background_js = """
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "%s",
port: %s
},
bypassList: ["localhost"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "%s",
password: "%s"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{ urls: ["<all_urls>"] },
['blocking']
);
""" % (
proxy_host,
proxy_port,
username,
password,
)
plugins_folder = Path.cwd() / "proxy_auth_plugin"
plugins_folder.mkdir(exist_ok=True)
plugin_folder = plugins_folder / plugin_folder_name
logger.debug(f"Creating '{plugin_folder}' folder...")
plugin_folder.mkdir(exist_ok=True)
with open(plugin_folder / "manifest.json", "w") as manifest_file:
manifest_file.write(manifest_json)
with open(plugin_folder / "background.js", "w") as background_js_file:
background_js_file.write(background_js)
chrome_options.add_argument(f"--load-extension={plugin_folder}")