mirror of
https://github.com/Gerg-L/nixos.git
synced 2025-12-10 08:53:56 -05:00
move ddns service script to it's own file
This commit is contained in:
parent
d40280751d
commit
550831f4d9
2 changed files with 64 additions and 58 deletions
|
|
@ -1,4 +1,8 @@
|
||||||
{ config, pkgs }:
|
{
|
||||||
|
config,
|
||||||
|
pkgs,
|
||||||
|
_dir,
|
||||||
|
}:
|
||||||
{
|
{
|
||||||
sops.secrets.cloudflare = { };
|
sops.secrets.cloudflare = { };
|
||||||
|
|
||||||
|
|
@ -26,62 +30,6 @@
|
||||||
pkgs.curl
|
pkgs.curl
|
||||||
];
|
];
|
||||||
|
|
||||||
script = ''
|
script = builtins.readFile "${_dir}/ddns_script.sh";
|
||||||
if ! nc -zw1 google.com 443 &>/dev/null; then
|
|
||||||
echo No Internet access... bailing early
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
IP=$(grep -oP '^((?!fe80).).{22}ffee.{5}' /proc/net/if_inet6 | sed -E 's/(.{4})/\1:/g; s/.$//')
|
|
||||||
|
|
||||||
func () {
|
|
||||||
RECORD="$1"
|
|
||||||
ZONE="$2"
|
|
||||||
PROXY="''${3:-"true"}"
|
|
||||||
|
|
||||||
|
|
||||||
REQ=$(curl --silent \
|
|
||||||
--request GET \
|
|
||||||
--url "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records" \
|
|
||||||
--header 'Content-Type: application/json' \
|
|
||||||
--header "Authorization: Bearer $AUTH"
|
|
||||||
)
|
|
||||||
|
|
||||||
readarray -t AR < <(jq -r '.result[].name' <<< "$REQ")
|
|
||||||
|
|
||||||
for i in "''${!AR[@]}"; do
|
|
||||||
if [ "''${AR[i]}" == "$RECORD" ]; then
|
|
||||||
ID=$(jq -r ".result[$i].id" <<< "$REQ")
|
|
||||||
if [ "$(jq -r ".result[$i].content" <<< "$REQ")" == "$IP" ]; then
|
|
||||||
echo "IP was the same, returing early"
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
|
|
||||||
curl --silent \
|
|
||||||
--request PATCH \
|
|
||||||
--url "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records/$ID" \
|
|
||||||
--header "Authorization: Bearer $AUTH" \
|
|
||||||
--header "Content-Type: application/json" \
|
|
||||||
--data '{
|
|
||||||
"content": "'"$IP"'",
|
|
||||||
"name": "'"$RECORD"'",
|
|
||||||
"proxied": '"$PROXY"',
|
|
||||||
"type": "AAAA",
|
|
||||||
"comment": "",
|
|
||||||
"tags": [],
|
|
||||||
"ttl": 1
|
|
||||||
}'
|
|
||||||
}
|
|
||||||
|
|
||||||
func "*.gerg-l.com" "8f76f071c5edbc0f947a5c5f9c5df9f8"
|
|
||||||
func "gerg-l.com" "8f76f071c5edbc0f947a5c5f9c5df9f8" "false"
|
|
||||||
func "minecraft.gerg-l.com" "8f76f071c5edbc0f947a5c5f9c5df9f8" "false"
|
|
||||||
func "*.nix-fu.com" "cc2df9163c3730f58b866409ac5a108c"
|
|
||||||
func "nix-fu.com" "cc2df9163c3730f58b866409ac5a108c"
|
|
||||||
'';
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
58
hosts/gerg-desktop/services/ddns_script.sh
Executable file
58
hosts/gerg-desktop/services/ddns_script.sh
Executable file
|
|
@ -0,0 +1,58 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
if ! nc -zw1 api.cloudflare.com 443 &>/dev/null; then
|
||||||
|
echo No Internet access... bailing early
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
IP=$(grep -oP '^((?!fe80).).{22}ffee.{5}' /proc/net/if_inet6 | sed -E 's/(.{4})/\1:/g; s/.$//')
|
||||||
|
|
||||||
|
func () {
|
||||||
|
RECORD="$1"
|
||||||
|
ZONE="$2"
|
||||||
|
PROXY="${3:-"true"}"
|
||||||
|
|
||||||
|
|
||||||
|
REQ=$(curl --silent \
|
||||||
|
--request GET \
|
||||||
|
--url "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records" \
|
||||||
|
--header 'Content-Type: application/json' \
|
||||||
|
--header "Authorization: Bearer $AUTH"
|
||||||
|
)
|
||||||
|
|
||||||
|
readarray -t AR < <(jq -r '.result[].name' <<< "$REQ")
|
||||||
|
|
||||||
|
for i in "${!AR[@]}"; do
|
||||||
|
if [ "${AR[i]}" == "$RECORD" ]; then
|
||||||
|
ID=$(jq -r ".result[$i].id" <<< "$REQ")
|
||||||
|
if [ "$(jq -r ".result[$i].content" <<< "$REQ")" == "$IP" ]; then
|
||||||
|
echo "IP was the same, returing early"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
|
curl --silent \
|
||||||
|
--request PATCH \
|
||||||
|
--url "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records/$ID" \
|
||||||
|
--header "Authorization: Bearer $AUTH" \
|
||||||
|
--header "Content-Type: application/json" \
|
||||||
|
--data '{
|
||||||
|
"content": "'"$IP"'",
|
||||||
|
"name": "'"$RECORD"'",
|
||||||
|
"proxied": '"$PROXY"',
|
||||||
|
"type": "AAAA",
|
||||||
|
"comment": "",
|
||||||
|
"tags": [],
|
||||||
|
"ttl": 1
|
||||||
|
}'
|
||||||
|
}
|
||||||
|
|
||||||
|
func "*.gerg-l.com" "8f76f071c5edbc0f947a5c5f9c5df9f8"
|
||||||
|
func "gerg-l.com" "8f76f071c5edbc0f947a5c5f9c5df9f8" "false"
|
||||||
|
func "minecraft.gerg-l.com" "8f76f071c5edbc0f947a5c5f9c5df9f8" "false"
|
||||||
|
func "*.nix-fu.com" "cc2df9163c3730f58b866409ac5a108c"
|
||||||
|
func "nix-fu.com" "cc2df9163c3730f58b866409ac5a108c"
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue