summaryrefslogtreecommitdiff
path: root/ddns-update
blob: d6785d642d1c7169039607f88dcff4ce58d7bb22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash

# update dns records of
# pool.mirror.archlinux32.org = CNAME pool32.ddns.eckner.net

# either called with 1 argument (the ip to check and add) or without
# arguments (all known mirrors will be checked in parallel)

if [ $# -eq 1 ]; then
  if [ "${1%.*}" != "$1" ]; then
    ipver='A'
  elif [ "${1%:*}" != "$1" ]; then
    ipver='AAAA'
  else
    exit
  fi

  if [ "$(curl \
    -w '%{http_code}' \
    -o /dev/null \
    --connect-timeout 10 \
    --resolve "pool.mirror.archlinux32.org:80:$1" \
    -s 'http://pool.mirror.archlinux32.org/i686/')" != '200' ]; then
    exit
  fi

  printf '%s %s\n' "${ipver}" "$1"
  exit
fi

mirrors=$(
  curl -s 'https://packages.archlinux32.org/mirrors/status.php?tsv'
)

url_column=$(
  printf '%s\n' "${mirrors}" | \
    head -n1 | \
    tr '\t' '\n' | \
    grep -nxF 'url' | \
    cut -d: -f1
)
recently_active_column=$(
  printf '%s\n' "${mirrors}" | \
    head -n1 | \
    tr '\t' '\n' | \
    grep -nxF 'recently_active' | \
    cut -d: -f1
)
if [ -z "${url_column}" ] || [ -z "${recently_active_column}" ]; then
  # https://packages.archlinux32.org/mirrors/status.php?tsv gave bogus content
  exit
fi

{
  for type in 'A' 'AAAA'; do
    printf '%s\n' \
      'zone ddns.eckner.net.' \
      'prereq yxrrset pool32.ddns.eckner.net IN '"${type}" \
      'update delete pool32.ddns.eckner.net IN '"${type}" \
      'send'
  done
  printf 'zone ddns.eckner.net.\n'
  printf '%s\n' "${mirrors}" | \
    sed '1d' | \
    awk '{
      print $'"${recently_active_column}"' "\t" $'"${url_column}"'
    }' | \
    sed -n '
      s/^1\t//
      T
      \|//mirror\.archlinux32\.org/|d
      s|[^:]\+://||
      T
      s|/.*$||
      p
    ' | \
    sort -u | \
    parallel -j0 getent ahosts | \
    sed -n '
      s/\s\+STREAM\(\s.*\)\?$//
      T
      p
    ' | \
    sort -u | \
    parallel -j0 "$0" | \
    sed '
      s/^/update add pool32.ddns.eckner.net. 3600 IN /
    '
  printf '%s\n' \
    'send'
} | \
  nsupdate -l

exit 0