From 668267949399ceceab852f5e1b1ca7849a0cde3a Mon Sep 17 00:00:00 2001 From: devaine Date: Thu, 5 Mar 2026 01:06:55 -0600 Subject: [PATCH] refactor(repo): most of these scripts are outdated, refactored desec --- .forgejo/workflows/start.yaml | 6 + .gitignore | 2 + README.md | 7 +- {daemons => archived/daemons}/battery-daemon | Bin .../daemons}/battery-daemon.cpp | 0 {notifs => archived/notifs}/battery-status | 0 {notifs => archived/notifs}/brightness | 0 {notifs => archived/notifs}/info | 0 {notifs => archived/notifs}/time | 0 {notifs => archived/notifs}/volume | 0 {notifs => archived/notifs}/window | 0 screenshot => archived/screenshot | 0 sunset => archived/sunset | 0 server-scripts/desec-ip-sync/main.py | 127 ++++++++++++++++++ server-scripts/desec-ip-sync/run.bash | 13 ++ server-scripts/desec/desec.py | 114 ---------------- server-scripts/desec/dev.bash | 11 -- server-scripts/desec/run.bash | 7 - 18 files changed, 154 insertions(+), 133 deletions(-) create mode 100644 .forgejo/workflows/start.yaml rename {daemons => archived/daemons}/battery-daemon (100%) rename {daemons => archived/daemons}/battery-daemon.cpp (100%) rename {notifs => archived/notifs}/battery-status (100%) rename {notifs => archived/notifs}/brightness (100%) rename {notifs => archived/notifs}/info (100%) rename {notifs => archived/notifs}/time (100%) rename {notifs => archived/notifs}/volume (100%) rename {notifs => archived/notifs}/window (100%) rename screenshot => archived/screenshot (100%) rename sunset => archived/sunset (100%) create mode 100755 server-scripts/desec-ip-sync/main.py create mode 100755 server-scripts/desec-ip-sync/run.bash delete mode 100755 server-scripts/desec/desec.py delete mode 100755 server-scripts/desec/dev.bash delete mode 100755 server-scripts/desec/run.bash diff --git a/.forgejo/workflows/start.yaml b/.forgejo/workflows/start.yaml new file mode 100644 index 0000000..e1fb196 --- /dev/null +++ b/.forgejo/workflows/start.yaml @@ -0,0 +1,6 @@ +on: [push] +jobs: + test: + runs-on: docker + steps: + - run: echo "hello world" diff --git a/.gitignore b/.gitignore index 3afbece..fb09f0e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .env server-scripts/desec/public_ip +data +venv diff --git a/README.md b/README.md index 8078066..810126b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ # scripts -Scripts I made that made my life slightly easier (terrible code btw) \ No newline at end of file +Programs made to better my workflows + +## Why? +It was made fundamentally for programming practice foy myself and my niche likings for Linux WM's (it still is, honestly). +Currently, it's turned to an output to create programs that better my workflow or produce a use for my servers. + diff --git a/daemons/battery-daemon b/archived/daemons/battery-daemon similarity index 100% rename from daemons/battery-daemon rename to archived/daemons/battery-daemon diff --git a/daemons/battery-daemon.cpp b/archived/daemons/battery-daemon.cpp similarity index 100% rename from daemons/battery-daemon.cpp rename to archived/daemons/battery-daemon.cpp diff --git a/notifs/battery-status b/archived/notifs/battery-status similarity index 100% rename from notifs/battery-status rename to archived/notifs/battery-status diff --git a/notifs/brightness b/archived/notifs/brightness similarity index 100% rename from notifs/brightness rename to archived/notifs/brightness diff --git a/notifs/info b/archived/notifs/info similarity index 100% rename from notifs/info rename to archived/notifs/info diff --git a/notifs/time b/archived/notifs/time similarity index 100% rename from notifs/time rename to archived/notifs/time diff --git a/notifs/volume b/archived/notifs/volume similarity index 100% rename from notifs/volume rename to archived/notifs/volume diff --git a/notifs/window b/archived/notifs/window similarity index 100% rename from notifs/window rename to archived/notifs/window diff --git a/screenshot b/archived/screenshot similarity index 100% rename from screenshot rename to archived/screenshot diff --git a/sunset b/archived/sunset similarity index 100% rename from sunset rename to archived/sunset diff --git a/server-scripts/desec-ip-sync/main.py b/server-scripts/desec-ip-sync/main.py new file mode 100755 index 0000000..6e68023 --- /dev/null +++ b/server-scripts/desec-ip-sync/main.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python + +import os +from time import sleep +from dotenv import load_dotenv +import requests + +load_dotenv() + +# Constants +DOMAIN = os.getenv("CURRENT_DOMAIN") +DESEC_TOKEN = os.getenv("DESEC_TOKEN") +IGNORED_SUBDOMAINS_PREFIXES = ["mail._domainkey.mail", "mail", "_dmarc.mail", "skyguy"] +TIMEOUT = 10 # In seconds, 600 = 10min, 900 = 15m, 1800 = 30min + +# Credits: +# https://desec.readthedocs.io/en/latest/dns/rrsets.html#modifying-an-rrset +# for documentation + + +def getSubDomains(): + subDomainsLink = "https://desec.io/api/v1/domains/" + DOMAIN + "/rrsets/" + + subDomainsHeader = {"Authorization": "Token " + DESEC_TOKEN} + + subDomainsRequest = requests.get(subDomainsLink, headers=subDomainsHeader) + subDomainsJSON = subDomainsRequest.json() + + return subDomainsJSON + + +def filterRecords(): + filtered_prefixes = [] + + getRequestIP = requests.get("https://ifconfig.me") + requestIP = getRequestIP.text + + IGNORED_SUBDOMAINS = [] + for i in range(len(IGNORED_SUBDOMAINS_PREFIXES)): + IGNORED_SUBDOMAINS.append(IGNORED_SUBDOMAINS_PREFIXES[i] + "." + DOMAIN + ".") + + for rrset in getSubDomains(): + IS_NOT_IGNORED = rrset["name"] not in IGNORED_SUBDOMAINS + CONTAINS_A_RECORD = rrset["type"] == "A" + NOT_CURRENT_IP = rrset["records"][0] != requestIP + + if IS_NOT_IGNORED and CONTAINS_A_RECORD and NOT_CURRENT_IP: + filtered_prefixes.append(rrset["subname"]) + + if len(filtered_prefixes) > 0: + return filtered_prefixes + + +def changeRecords(): + prefixes = filterRecords() + + # If there are no outdated subdomains... + if prefixes is None: + print("No available subdomains to change") + return + + getRequestIP = requests.get("https://ifconfig.me") + requestIP = getRequestIP.text + + for prefix in prefixes: + subDomainsLink = ( + "https://desec.io/api/v1/domains/" + DOMAIN + "/rrsets/" + prefix + "/A/" + ) + subDomainsHeader = { + "Authorization": "Token " + DESEC_TOKEN, + "Content-Type": "application/json", + } + subDomainsData = { + "subname": prefix, + "type": "A", + "records": [requestIP], + "ttl": 3600, + } + + changeSubDomainRequest = requests.put( + subDomainsLink, json=subDomainsData, headers=subDomainsHeader + ) + + print("for prefix: " + prefix) + print(changeSubDomainRequest.text) + + sleep(3) + + +def checkIP(): + if not os.path.exists("data"): + os.mknod("data") + + dataFile = open("data", "r") + + getRequestIP = requests.get("https://ifconfig.me") + requestIP = getRequestIP.text + + dataFileIP = dataFile.readline().replace("\n", "") + + if requestIP != dataFileIP: + changeRecords() + with open("data", "w") as dataFile: + dataFile.write(requestIP) + + +def checkInternet(): + print("Checking Connection...") + try: + testRequest = requests.get("https://ifconfig.me", timeout=15) + except (requests.Timeout, requests.ConnectionError) as exception: + print("Connection Failed!\nReason: " + exception) + checkInternet() + + if testRequest.text is not None: + return True + + +def main(): + print("Starting Script") + while checkInternet(): + checkIP() + sleep(TIMEOUT) + + +if __name__ == "__main__": + main() diff --git a/server-scripts/desec-ip-sync/run.bash b/server-scripts/desec-ip-sync/run.bash new file mode 100755 index 0000000..934c33f --- /dev/null +++ b/server-scripts/desec-ip-sync/run.bash @@ -0,0 +1,13 @@ +#!/bin/bash + +# If virtual environment does exist.. +if [ ! -d venv ]; then + python3 -m venv venv + source venv/bin/activate + pip install -U python-dotenv requests + pip install -U pip # Update pip to the latest version +else + source venv/bin/activate +fi + +python main.py diff --git a/server-scripts/desec/desec.py b/server-scripts/desec/desec.py deleted file mode 100755 index ed34e37..0000000 --- a/server-scripts/desec/desec.py +++ /dev/null @@ -1,114 +0,0 @@ -#!/usr/bin/env python - -import subprocess -import json -import os -import asyncio -from time import sleep -from dotenv import load_dotenv -import urllib3 - -load_dotenv() # Loads up .env file - -current_domain = os.getenv("CURRENT_DOMAIN") -desec_token = os.getenv("DESEC_TOKEN") -excluded_subdomains = ["mail._domainkey.mail", "mail", "_dmarc.mail"] -timeout = 1800 # In seconds, 600 = 10min, 900 = 15m, 1800 = 30min - -# Credits: -# https://desec.readthedocs.io/en/latest/dns/rrsets.html#modifying-an-rrset -# for documentation - - -async def modifyRecords(newIP): - args = "curl https://desec.io/api/v1/domains/" + current_domain + \ - "/rrsets/ --header 'Authorization: Token " + desec_token + "'" - - data_binary = subprocess.run( - args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) - data = data_binary.stdout.decode("utf-8") - - # convert to python object from str - json_data = json.loads(data) - - for i in range(len(excluded_subdomains)): - excluded_subdomains[i] = excluded_subdomains[i] + \ - "." + current_domain + "." - - for entry in json_data: - # Only allow "A" record subdomains at the moment - if entry["name"] not in excluded_subdomains and entry["type"] == "A": - if (entry["records"][0] != newIP): - subname = str(entry["subname"]) # Subdomain Name - - # Basically runs a PATCH method for the api to change - # the ip record of all "A" record subdomains to the new - # public ip address - - change_record_arg = \ - "curl -X PATCH https://desec.io/api/v1/domains/" + \ - current_domain + "/rrsets/" + subname + \ - "/A/" + " --header 'Authorization: Token " \ - + desec_token + "'" + " --header " + \ - "'Content-Type: application/json' " + \ - "--data @- <<< '{\"records\": [\"" + newIP + "\"] }'" - - # print(change_record_arg) - subprocess.run(change_record_arg, shell=True) - await asyncio.sleep(5) - - print("done with changing records!") - - -def getCurrentIP(): - print("getting current ip...") - - # Get the current Public IP to a "public_ip" file - subprocess.run( - ["curl", "ifconfig.me", "-o", "public_ip"], stderr=subprocess.DEVNULL) - - -async def newIPCheck(): - print("checking for new ips...") - presentIPFile = open("public_ip", "r") - - newIP_curl = subprocess.run(["curl", "ifconfig.me"], - stdout=subprocess.PIPE, - stderr=subprocess.DEVNULL) - - newIP = newIP_curl.stdout.decode("utf-8") - - if (presentIPFile.readline() == newIP): - await asyncio.sleep(timeout) - await newIPCheck() - - else: - print("uh oh! public ip updated!") - await modifyRecords(newIP) - getCurrentIP() # update current ip - await newIPCheck() - -def waitForConnection(): - while True: - try: - response = urllib3.request("GET", "https://ifconfig.me") - return - except urllib3.exceptions.MaxRetryError: - print("Failed connection!") - sleep(1) - pass - -def main(): - waitForConnection() - - if not os.path.exists("public_ip"): - getCurrentIP() - elif not os.path.exists(".env"): - print("no visible .env file for token!") - exit(1) - - asyncio.run(newIPCheck()) - - -if __name__ == "__main__": - main() diff --git a/server-scripts/desec/dev.bash b/server-scripts/desec/dev.bash deleted file mode 100755 index 24a745d..0000000 --- a/server-scripts/desec/dev.bash +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# If virtual environment does exist.. -if [ ! -d .venv ]; then - python3 -m venv .venv - source .venv/bin/activate - pip install -U python-dotenv urllib3 - pip install -U pip # Update pip to the latest version -fi - -echo 'Make sure to run ". .venv/bin/activate" to enter the development environment' diff --git a/server-scripts/desec/run.bash b/server-scripts/desec/run.bash deleted file mode 100755 index 4c833f9..0000000 --- a/server-scripts/desec/run.bash +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -cd /home/user/scripts/desec - -. .venv/bin/activate - -./desec.py