Last active
September 15, 2023 23:56
-
-
Save kernelkaribou/2643c7847eb2589fb4b738d973c81536 to your computer and use it in GitHub Desktop.
Technitium DNS ad blocking manager via API
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
#!/bin/bash | |
########################################################### | |
# Purpose: Simple script to enable and disable ad blocking | |
# for technitium DNS server via API calls | |
# | |
# Usage: provide either 'enable' to enable ad blocking | |
# or 'disable DURATION' to disable | |
# | |
# Author: kernelkaribou@github | |
########################################################### | |
declare -A technitiumDNSHosts=( | |
# Enter [HOST]=[token] one each line, include http:// or https:// and any ports if needed. | |
# Example: ["https://example.com:53443"]=[abc123] | |
["https://dns01.example.com"]="myL0ngTok3n" | |
["https://dns02.example.com"]="my0th3rL0ngTok3n" | |
) | |
# k=Skip TLS verify, useful if not through reverse proxy with cert and using https | |
# s=Silent progress of curl command | |
curl_options="ks" | |
duration=5 # default duration for disabling ad blocking | |
for host in "${!technitiumDNSHosts[@]}" | |
do | |
token=${technitiumDNSHosts[$host]} | |
case $1 in | |
"enable") | |
api_action="set" | |
api_param="&enableBlocking=true" | |
message="Enabling Blocking for $host" | |
;; | |
"disable") | |
api_action="temporaryDisableBlocking" | |
if [[ ! -z $2 ]]; then duration=$2; fi; | |
api_param="&minutes=$duration" | |
message="Disable Blocking for $duration minutes on $host" | |
;; | |
*) | |
echo "invalid action, options: enable and disable (with duration)" | |
exit 1; | |
;; | |
esac | |
curl_command="curl -$curl_options $host/api/settings/$api_action?token=$token$api_param" | |
result=`$curl_command` | |
status=${result#*\"status\":\"} | |
status=$(echo $status | cut -d '"' -f 1) | |
# set request status | |
message+="\nRequest Result: "$status | |
# Getting relevant status depending on call | |
if [[ $1 == "disable" ]]; then | |
{ | |
blocking_date=${result#*temporaryDisableBlockingTill\":\"} | |
blocking_date=$(echo $blocking_date | cut -d '.' -f 1) | |
message+="\nBlocking Until: "$blocking_date | |
} | |
else | |
{ | |
blocking_state=${result#*enableBlocking\":} | |
blocking_state=$(echo $blocking_state | cut -d ',' -f 1) | |
message+="\nBlocking Ads: "$blocking_state | |
} | |
fi | |
echo -e $message"\n" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment