jobs - edit referrers and user-agents data and init work on autoconf integration

This commit is contained in:
bunkerity 2021-07-21 14:42:55 +02:00
parent d12369c900
commit 5f845680ff
No known key found for this signature in database
GPG Key ID: 3D80806F12602A7C
11 changed files with 47 additions and 52 deletions

View File

@ -9,9 +9,9 @@ class Config :
self.__swarm = swarm self.__swarm = swarm
self.__api = api self.__api = api
def __jobs(self, type) : def __jobs(self) :
utils.log("[*] Starting jobs (type = " + type + ") ...") utils.log("[*] Starting jobs")
proc = subprocess.run(["/bin/su", "-c", "/opt/bunkerized-nginx/entrypoint/" + type + "-jobs.sh", "nginx"], capture_output=True) proc = subprocess.run(["/bin/su", "-c", "/opt/bunkerized-nginx/entrypoint/jobs.sh", "nginx"], capture_output=True)
stdout = proc.stdout.decode("ascii") stdout = proc.stdout.decode("ascii")
stderr = proc.stderr.decode("ascii") stderr = proc.stderr.decode("ascii")
if len(stdout) > 1 : if len(stdout) > 1 :
@ -71,7 +71,7 @@ class Config :
# We're done # We're done
if proc.returncode == 0 : if proc.returncode == 0 :
if self.__swarm : if self.__swarm :
return self.__jobs("pre") return self.__jobs()
return True return True
utils.log("[!] Error while generating site config for " + env["SERVER_NAME"] + " : return code = " + str(proc.returncode)) utils.log("[!] Error while generating site config for " + env["SERVER_NAME"] + " : return code = " + str(proc.returncode))
@ -80,11 +80,7 @@ class Config :
return False return False
def reload(self, instances) : def reload(self, instances) :
if self.__api_call(instances, "/reload") : return self.__api_call(instances, "/reload")
if self.__swarm :
return self.__jobs("post")
return True
return False
def __ping(self, instances) : def __ping(self, instances) :
return self.__api_call(instances, "/ping") return self.__api_call(instances, "/ping")

View File

@ -1,20 +1,19 @@
FROM alpine FROM alpine
COPY autoconf/dependencies.sh /tmp
RUN chmod +x /tmp/dependencies.sh && \
/tmp/dependencies.sh && \
rm -f /tmp/dependencies.sh && \
mkdir /opt/bunkerized-nginx
COPY gen/ /opt/bunkerized-nginx/gen COPY gen/ /opt/bunkerized-nginx/gen
COPY entrypoint/ /opt/bunkerized-nginx/entrypoint COPY entrypoint/ /opt/bunkerized-nginx/entrypoint
COPY confs/global/ /opt/bunkerized-nginx/confs/global COPY confs/global/ /opt/bunkerized-nginx/confs/global
COPY confs/site/ /opt/bunkerized-nginx/confs/site COPY confs/site/ /opt/bunkerized-nginx/confs/site
COPY scripts/ /opt/bunkerized-nginx/scripts COPY jobs/ /opt/bunkerized-nginx/jobs
COPY settings.json /opt/bunkerized-nginx/ COPY settings.json /opt/bunkerized-nginx/
COPY misc/cron /etc/crontabs/nginx COPY misc/cron /etc/crontabs/nginx
COPY autoconf/* /opt/bunkerized-nginx/entrypoint/ COPY autoconf/* /opt/bunkerized-nginx/entrypoint/
RUN apk add --no-cache py3-pip bash certbot curl openssl && \
pip3 install -r /opt/bunkerized-nginx/gen/requirements.txt && \
pip3 install -r /opt/bunkerized-nginx/entrypoint/requirements.txt && \
pip3 install -r /opt/bunkerized-nginx/jobs/requirements.txt
COPY autoconf/prepare.sh /tmp COPY autoconf/prepare.sh /tmp
RUN chmod +x /tmp/prepare.sh && \ RUN chmod +x /tmp/prepare.sh && \
/tmp/prepare.sh && \ /tmp/prepare.sh && \

View File

@ -1,5 +0,0 @@
#!/bin/sh
# install dependencies
apk add py3-pip bash certbot curl openssl
pip3 install docker requests jinja2

View File

@ -14,6 +14,8 @@ find /opt/bunkerized-nginx -type f -exec chmod 0740 {} \;
find /opt/bunkerized-nginx -type d -exec chmod 0750 {} \; find /opt/bunkerized-nginx -type d -exec chmod 0750 {} \;
chmod ugo+x /opt/bunkerized-nginx/entrypoint/* /opt/bunkerized-nginx/scripts/* chmod ugo+x /opt/bunkerized-nginx/entrypoint/* /opt/bunkerized-nginx/scripts/*
chmod ugo+x /opt/bunkerized-nginx/gen/main.py chmod ugo+x /opt/bunkerized-nginx/gen/main.py
chmod ugo+x /opt/bunkerized-nginx/jobs/main.py
chmod ugo+x /opt/bunkerized-nginx/jobs/reload.py
chmod 770 /opt/bunkerized-nginx chmod 770 /opt/bunkerized-nginx
chmod 440 /opt/bunkerized-nginx/settings.json chmod 440 /opt/bunkerized-nginx/settings.json

View File

@ -1,19 +0,0 @@
#!/usr/bin/python3
import sys, socket, os
if not os.path.exists("/tmp/autoconf.sock") :
sys.exit(1)
try :
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client.connect("/tmp/autoconf.sock")
client.send("reload".encode("utf-8"))
data = client.recv(512)
client.close()
if not data or data.decode("utf-8") != "ok" :
sys.exit(3)
except Exception as e :
sys.exit(2)
sys.exit(0)

View File

@ -0,0 +1,3 @@
docker
requests
jinja2

View File

@ -50,15 +50,17 @@ class Job(abc.ABC) :
for url in self.__data : for url in self.__data :
data = self.__download_data(url) data = self.__download_data(url)
for chunk in data : for chunk in data :
if self.__type == "line" and not re.match(self.__regex, chunk.decode("utf-8")) : if self.__type == "line" :
continue if not re.match(self.__regex, chunk.decode("utf-8")) :
count += 1 continue
chunk = self.__edit(chunk)
if self.__redis == None : if self.__redis == None :
if self.__type == "line" : if self.__type == "line" :
chunk += b"\n" chunk += b"\n"
file.write(chunk) file.write(chunk)
else : else :
pipe.set(self.__name + "_" + chunk, "1") pipe.set(self.__name + "_" + chunk, "1")
count += 1
if self.__redis == None : if self.__redis == None :
file.close() file.close()
@ -89,6 +91,9 @@ class Job(abc.ABC) :
if proc.returncode != 0 : if proc.returncode != 0 :
raise Exception("error code " + str(proc.returncode)) raise Exception("error code " + str(proc.returncode))
def __edit(self, chunk) :
return chunk
def __from_cache(self) : def __from_cache(self) :
if not os.path.isfile("/opt/bunkerized-nginx/cache/" + self.__filename) : if not os.path.isfile("/opt/bunkerized-nginx/cache/" + self.__filename) :
return False return False

View File

@ -9,3 +9,6 @@ class Referrers(Job) :
type = "line" type = "line"
regex = r"^.+$" regex = r"^.+$"
super().__init__(name, data, filename, redis_host=redis_host, type=type, regex=regex, copy_cache=copy_cache) super().__init__(name, data, filename, redis_host=redis_host, type=type, regex=regex, copy_cache=copy_cache)
def __edit(self, chunk) :
return chunk.replace(b".", b"%.").replace(b"-", b"%-")

View File

@ -9,3 +9,6 @@ class UserAgents(Job) :
type = "line" type = "line"
regex = r"^.+$" regex = r"^.+$"
super().__init__(name, data, filename, redis_host=redis_host, type=type, regex=regex, copy_cache=copy_cache) super().__init__(name, data, filename, redis_host=redis_host, type=type, regex=regex, copy_cache=copy_cache)
def __edit(self, chunk) :
return chunk.replace(b"\\ ", b" ").replace(b"\\.", b"%.").replace(b"\\\\", b"\\").replace(b"-", b"%-")

View File

@ -59,10 +59,14 @@ if __name__ == "__main__" :
# TODO : only reload if needed # TODO : only reload if needed
do_reload = True do_reload = True
if do_reload : if do_reload :
if not reload() : ret = reload()
if ret == 0 :
print("[*] Reload operation successfully executed")
elif ret == 1 :
print("[!] Error while doing reload operation") print("[!] Error while doing reload operation")
sys.exit(1) sys.exit(1)
print("[*] Reload operation successfully executed") elif ret == 2 :
print("[*] Skipped reload operation because nginx is not running")
# Done # Done
sys.exit(0) sys.exit(0)

View File

@ -11,8 +11,8 @@ def reload() :
print(proc.stdout.decode("ascii")) print(proc.stdout.decode("ascii"))
if len(proc.stderr.decode("ascii")) > 1 : if len(proc.stderr.decode("ascii")) > 1 :
print(proc.stderr.decode("ascii")) print(proc.stderr.decode("ascii"))
return False return 0
return True return 1
# Autoconf case (Docker, Swarm and Ingress) # Autoconf case (Docker, Swarm and Ingress)
if os.path.exists("/tmp/autoconf.sock") and stat.S_ISSOCK(os.stat("/tmp/autoconf.sock")) : if os.path.exists("/tmp/autoconf.sock") and stat.S_ISSOCK(os.stat("/tmp/autoconf.sock")) :
@ -23,17 +23,21 @@ def reload() :
client.close() client.close()
if not data or data.decode("utf-8") != "ok" : if not data or data.decode("utf-8") != "ok" :
print("[!] Can't reload nginx (data not ok)") print("[!] Can't reload nginx (data not ok)")
return False return 0
return True return 1
return False return 2
if __name__ == "__main__" : if __name__ == "__main__" :
try : try :
print("[*] Starting reload operation ...") print("[*] Starting reload operation ...")
if not reload() : ret = reload()
if ret == 0 :
sys.exit(1) sys.exit(1)
print("[*] Reload operation successfully executed") elif ret == 1 :
print("[*] Reload operation successfully executed")
elif ret == 2 :
print("[*] Skipped reload operation because nginx is not running")
sys.exit(0) sys.exit(0)
except : except :
print("[!] Can't reload nginx (exception)") print("[!] Can't reload nginx (exception)")