blocklist management work

This commit is contained in:
Nicole O'Connor 2024-11-04 20:52:37 -08:00
parent 1d23029591
commit 93b46a9c48
4 changed files with 98 additions and 5 deletions

View File

@ -13,10 +13,13 @@
# limitations under the License. # limitations under the License.
import atexit import atexit
import fnmatch
import logging import logging
import os import os
import pathlib import pathlib
import shutil
import time import time
import zipfile
import requests import requests
@ -26,7 +29,7 @@ import screen
log = logging.getLogger("blocklists") log = logging.getLogger("blocklists")
running = True 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_data_src_path = "dnsmasq"
blocklist_lib_path = "/var/lib/starcontrol" 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 = pathlib.Path(blocklist_lib_path)
pth_blocklist_lib.mkdir(exist_ok=True, parents=True) pth_blocklist_lib.mkdir(exist_ok=True, parents=True)
deploy_pending = False
def blocklist_check(): def blocklist_check():
log.debug("checking blocklist mtimes and updating if needed") log.debug("checking blocklist mtimes and updating if needed")
try: try:
@ -47,6 +52,8 @@ def blocklist_check():
update_blocklists() update_blocklists()
def update_blocklists(): def update_blocklists():
global deploy_pending
log.info("blocklist update in progress") log.info("blocklist update in progress")
screen.download_state = 1 screen.download_state = 1
@ -59,11 +66,46 @@ def update_blocklists():
hnd_download.close() hnd_download.close()
fd_listpayload.close() fd_listpayload.close()
screen.download_state = 0 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(): def blocklist_thread():
sz_blocklist_check_timer = 0
while running: while running:
if sz_blocklist_check_timer >= 3600: # 1 hour
blocklist_check() blocklist_check()
time.sleep(3600) # 1 hour sz_blocklist_check_timer = 0
else:
sz_blocklist_check_timer += 1
if deploy_pending:
deploy_blocklists()
time.sleep(1)
@atexit.register @atexit.register
def stop_blocklist(): def stop_blocklist():

View File

@ -1,2 +1,44 @@
[internal] [internal]
cache_root=/run/starcontrol 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

View File

@ -18,5 +18,5 @@ import logging
log = logging.getLogger("main") log = logging.getLogger("main")
log.debug("reading configuration file") log.debug("reading configuration file")
cparse = configparser.ConfigParser() cparse = configparser.ConfigParser(allow_no_value=True)
cparse.read("config.ini") cparse.read("config.ini")

View File

@ -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))) 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) 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_blocklists = threading.Thread(target=blocklists.blocklist_thread)
thr_screen = threading.Thread(target=screen.screen_thread) thr_screen = threading.Thread(target=screen.screen_thread)
thr_blocklists.start() thr_blocklists.start()