-
-
Save Barafu/c485b21cffb13fef28395d7e6b5837a2 to your computer and use it in GitHub Desktop.
A script that converts a list of network addresses into a PAC script for browsers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from jinja2 import Template | |
import time | |
from ipaddress import ip_network | |
from pathlib import Path | |
def main(): | |
# Адрес списка запрещёнки | |
url = "https://antifilter.download/list/allyouneed.lst" | |
rule_list_filename = Path("source.lst") | |
need_update = False | |
if not rule_list_filename.exists(): | |
need_update = True | |
print("Rule list does not exist. Downloading...") | |
else: | |
file_time = rule_list_filename.stat().st_mtime | |
now_time = time.time() | |
if (now_time - file_time) > 6 * 60 * 60: | |
need_update = True | |
print("Rule list is older than 6 hours. Downloading...") | |
if need_update: | |
r = requests.get(url, allow_redirects=True) | |
rule_list_filename.open("wb").write(r.content) | |
rules_list = r.content.decode("utf-8").split("\n") | |
else: | |
rules_list = rule_list_filename.open().readlines() | |
# Sanitize the list | |
rules_list = [ip_network(s.strip()) for s in rules_list if len(s) >= 4] | |
rules_list = [(n.network_address, n.netmask) for n in rules_list] | |
print("Rule list is ready") | |
print(f"Total rules: {len(rules_list)}") | |
tm = Template(pac_template) | |
result = tm.render(blacks=rules_list) | |
output_file = Path("result.txt") | |
with output_file.open("w") as f: | |
f.write(result) | |
pac_template = """ | |
function FindProxyForURL(url, host) { | |
/* Проксю настраивать здесь */ | |
var proxy = "PROXY 192.168.1.77:8118"; | |
/* Normalize the URL for pattern matching */ | |
url = url.toLowerCase(); | |
host = host.toLowerCase(); | |
/* Добавим вручную сайты, которые не в списке, но открывать надо через прокси */ | |
/* На примере жизненно необходимого */ | |
if (dnsDomainIs(host, ".pornhub.com") || | |
(host == "pornhub.com") ) { | |
return proxy; | |
} | |
/* Don't proxy local hostnames */ | |
if (isPlainHostName(host)) { | |
return 'DIRECT'; | |
} | |
/* Don't proxy Windows Update */ | |
if ((host == "download.microsoft.com") || | |
(host == "ntservicepack.microsoft.com") || | |
(host == "cdm.microsoft.com") || | |
(host == "wustat.windows.com") || | |
(host == "windowsupdate.microsoft.com") || | |
(dnsDomainIs(host, ".windowsupdate.microsoft.com")) || | |
(host == "update.microsoft.com") || | |
(dnsDomainIs(host, ".update.microsoft.com")) || | |
(dnsDomainIs(host, ".windowsupdate.com"))) { | |
return 'DIRECT'; | |
} | |
if (isResolvable(host)) { | |
var hostIP = dnsResolve(host); | |
/* Don't proxy non-routable addresses (RFC 3330) */ | |
if (isInNet(hostIP, '0.0.0.0', '255.0.0.0') || | |
isInNet(hostIP, '10.0.0.0', '255.0.0.0') || | |
isInNet(hostIP, '127.0.0.0', '255.0.0.0') || | |
isInNet(hostIP, '169.254.0.0', '255.255.0.0') || | |
isInNet(hostIP, '172.16.0.0', '255.240.0.0') || | |
isInNet(hostIP, '192.0.2.0', '255.255.255.0') || | |
isInNet(hostIP, '192.88.99.0', '255.255.255.0') || | |
isInNet(hostIP, '192.168.0.0', '255.255.0.0') || | |
isInNet(hostIP, '198.18.0.0', '255.254.0.0') || | |
isInNet(hostIP, '224.0.0.0', '240.0.0.0') || | |
isInNet(hostIP, '240.0.0.0', '240.0.0.0')) { | |
return 'DIRECT'; | |
} | |
/* Шаблон Жижы! Не дышать! */ | |
if (false {% for black in blacks %}|| | |
isInNet(hostIP, '{{black[0]}}', '{{black[1]}}') {% endfor %}){ | |
return proxy; | |
} | |
} | |
return 'DIRECT'; | |
} | |
""" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment