at master 2.1 kB view raw
1{ 2 cfg, 3 config, 4 lib, 5 pkgs, 6}: 7 8let 9 pihole = cfg.piholePackage; 10 makePayload = 11 list: 12 builtins.toJSON { 13 inherit (list) type enabled; 14 address = list.url; 15 comment = list.description; 16 }; 17 payloads = map makePayload cfg.lists; 18 macvendorURL = lib.strings.escapeShellArg cfg.macvendorURL; 19in 20'' 21 # Can't use -u (unset) because api.sh uses API_URL before it is set 22 set -eo pipefail 23 pihole="${lib.getExe pihole}" 24 jq="${lib.getExe pkgs.jq}" 25 26 ${lib.getExe pkgs.curl} --retry 3 --retry-delay 5 "${macvendorURL}" -o "${cfg.settings.files.macvendor}" || echo "Failed to download MAC database from ${macvendorURL}" 27 28 # If the database doesn't exist, it needs to be created with gravity.sh 29 if [ ! -f '${cfg.settings.files.gravity}' ]; then 30 $pihole -g 31 # Send SIGRTMIN to FTL, which makes it reload the database, opening the newly created one 32 ${lib.getExe' pkgs.procps "kill"} -s SIGRTMIN $(systemctl show --property MainPID --value ${config.systemd.services.pihole-ftl.name}) 33 fi 34 35 source ${pihole}/share/pihole/advanced/Scripts/api.sh 36 source ${pihole}/share/pihole/advanced/Scripts/utils.sh 37 38 any_failed=0 39 40 addList() { 41 local payload="$1" 42 43 echo "Adding list: $payload" 44 local result=$(PostFTLData "lists" "$payload") 45 46 local error="$($jq '.error' <<< "$result")" 47 if [[ "$error" != "null" ]]; then 48 echo "Error: $error" 49 any_failed=1 50 return 51 fi 52 53 id="$($jq '.lists.[].id?' <<< "$result")" 54 if [[ "$id" == "null" ]]; then 55 any_failed=1 56 error="$($jq '.processed.errors.[].error' <<< "$result")" 57 echo "Error: $error" 58 return 59 fi 60 61 echo "Added list ID $id: $result" 62 } 63 64 for i in 1 2 3; do 65 (TestAPIAvailability) && break 66 echo "Retrying API shortly..." 67 ${lib.getExe' pkgs.coreutils "sleep"} .5s 68 done; 69 70 LoginAPI 71 72 ${builtins.concatStringsSep "\n" ( 73 map ( 74 payload: 75 lib.pipe payload [ 76 lib.strings.escapeShellArg 77 (payload: "addList ${payload}") 78 ] 79 ) payloads 80 )} 81 82 # Run gravity.sh to load any new lists 83 $pihole -g 84 exit $any_failed 85''