forked from InstaPy/InstaPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_extension.py
78 lines (69 loc) · 2.03 KB
/
proxy_extension.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
import zipfile
import os
def create_proxy_extension(proxy):
"""takes proxy looks like login:password@ip:port"""
ip = proxy.split("@")[1].split(":")[0]
port = int(proxy.split(":")[-1])
login = proxy.split(":")[0]
password = proxy.split("@")[0].split(":")[1]
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
"""
background_js = """
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "%s",
port: parseInt(%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']
);
""" % (
ip,
port,
login,
password,
)
dir_path = "assets/chrome_extensions"
if not os.path.exists(dir_path):
os.makedirs(dir_path)
pluginfile = "%s/proxy_auth_%s:%s.zip" % (dir_path, ip, port)
with zipfile.ZipFile(pluginfile, "w") as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
return pluginfile