From 93b46a9c481a795827c15b6a309476baaca73995 Mon Sep 17 00:00:00 2001 From: Nicole O'Connor Date: Mon, 4 Nov 2024 20:52:37 -0800 Subject: [PATCH] blocklist management work --- blocklists.py | 48 +++++++++++++++++++++++++++++++++++++++++++++--- config.ini | 44 +++++++++++++++++++++++++++++++++++++++++++- config.py | 2 +- mainloop.py | 9 +++++++++ 4 files changed, 98 insertions(+), 5 deletions(-) diff --git a/blocklists.py b/blocklists.py index e345ff3..f1804a7 100644 --- a/blocklists.py +++ b/blocklists.py @@ -13,10 +13,13 @@ # limitations under the License. import atexit +import fnmatch import logging import os import pathlib +import shutil import time +import zipfile import requests @@ -26,7 +29,7 @@ import screen log = logging.getLogger("blocklists") running = True -blocklist_data_src = "https://github.com/hagezi/dns-blocklists/archive/refs/heads/main.zip" +blocklist_data_src = "https://github.com/hagezi/dns-blocklists/archive/refs/tags/2024.310.16385.zip" blocklist_data_src_path = "dnsmasq" blocklist_lib_path = "/var/lib/starcontrol" @@ -36,6 +39,8 @@ pth_cache_root.mkdir(exist_ok=True, parents=True) pth_blocklist_lib = pathlib.Path(blocklist_lib_path) pth_blocklist_lib.mkdir(exist_ok=True, parents=True) +deploy_pending = False + def blocklist_check(): log.debug("checking blocklist mtimes and updating if needed") try: @@ -47,6 +52,8 @@ def blocklist_check(): update_blocklists() def update_blocklists(): + global deploy_pending + log.info("blocklist update in progress") screen.download_state = 1 @@ -59,11 +66,46 @@ def update_blocklists(): hnd_download.close() fd_listpayload.close() screen.download_state = 0 + deploy_pending = True + +def build_dnsmasq_file(): + with open(pth_cache_root / "dnsmasq.conf", "w") as cfg_dnsmasq: + for server in list(config.cparse["upstreams"].keys()): + cfg_dnsmasq.write(f"server={server}\n") + + for blocklist in list(config.cparse["enabled_lists"].keys()): + listpath = pth_cache_root / f"lists/{blocklist}.txt" + if listpath.exists(): + cfg_dnsmasq.write(f"conf-file={listpath.as_posix()}\n") + +def deploy_blocklists(): + global deploy_pending + + with zipfile.ZipFile(pth_blocklist_lib / "dns-blocklists-main.zip") as zip_blocklists: + for item in fnmatch.filter(zip_blocklists.namelist(), "dns-blocklists-main/dnsmasq/*"): + basename = os.path.basename(item) + if not basename: + continue # skips raw directories + + fd_source = zip_blocklists.open(item) + fd_destination = open(pth_cache_root / f"lists/{basename}", "wb") + with fd_source, fd_destination: + shutil.copyfileobj(fd_source, fd_destination) + + deploy_pending = False def blocklist_thread(): + sz_blocklist_check_timer = 0 while running: - blocklist_check() - time.sleep(3600) # 1 hour + if sz_blocklist_check_timer >= 3600: # 1 hour + blocklist_check() + sz_blocklist_check_timer = 0 + else: + sz_blocklist_check_timer += 1 + + if deploy_pending: + deploy_blocklists() + time.sleep(1) @atexit.register def stop_blocklist(): diff --git a/config.ini b/config.ini index d6df357..17580b3 100644 --- a/config.ini +++ b/config.ini @@ -1,2 +1,44 @@ [internal] -cache_root=/run/starcontrol \ No newline at end of file +cache_root=/run/starcontrol + +[enabled_lists] +; anti.piracy = +; doh-vpn-proxy-bypass = +; doh = +; dyndns = +; fake = +; gambling.medium = +; gambling.mini = +; gambling = +; hoster = +; light = +; multi = +; native.amazon = +; native.apple = +; native.huawei = +; native.lgwebos = +; native.oppo-realme = +; native.samsung = +; native.tiktok.extend = +; native.tiktok = +; native.vivo = +; native.winoffice = +; native.xiaomi = +; nosafesearch = +; popupads = +; pro.mini = +; pro.plus.mini = +; pro.plus = +pro = +; tif-ips = +; tif.medium = +; tif.mini = +tif = +; ultimate.mini = +; ultimate = + +[upstreams] # one per line +1.1.1.1 +1.0.0.1 +8.8.8.8 +8.8.4.4 \ No newline at end of file diff --git a/config.py b/config.py index b9a0918..1718eb8 100644 --- a/config.py +++ b/config.py @@ -18,5 +18,5 @@ import logging log = logging.getLogger("main") log.debug("reading configuration file") -cparse = configparser.ConfigParser() +cparse = configparser.ConfigParser(allow_no_value=True) cparse.read("config.ini") \ No newline at end of file diff --git a/mainloop.py b/mainloop.py index 1463f95..a6056cc 100755 --- a/mainloop.py +++ b/mainloop.py @@ -37,6 +37,15 @@ if __name__ == "__main__": img_canvas.paste(Image.open("res/image/hourglass.png"),(math.floor(135/2) - math.ceil(49/2),math.floor(240/2) - math.ceil(90/2))) screen.scr.image(img_canvas) + if not (blocklists.pth_blocklist_lib / "dns-blocklists-main.zip").exists(): + blocklists.update_blocklists() + + if not (blocklists.pth_cache_root / "lists").exists(): + (blocklists.pth_cache_root / "lists").mkdir() + blocklists.deploy_blocklists() + + blocklists.build_dnsmasq_file() + thr_blocklists = threading.Thread(target=blocklists.blocklist_thread) thr_screen = threading.Thread(target=screen.screen_thread) thr_blocklists.start()