[#4] 增加自动更新域名IP
This commit is contained in:
@@ -27,9 +27,10 @@ COPY db.argus.com /etc/bind/db.argus.com
|
||||
# Copy startup and reload scripts
|
||||
COPY startup.sh /usr/local/bin/startup.sh
|
||||
COPY reload-bind9.sh /usr/local/bin/reload-bind9.sh
|
||||
COPY argus_dns_sync.sh /usr/local/bin/argus_dns_sync.sh
|
||||
|
||||
# Make scripts executable
|
||||
RUN chmod +x /usr/local/bin/startup.sh /usr/local/bin/reload-bind9.sh
|
||||
RUN chmod +x /usr/local/bin/startup.sh /usr/local/bin/reload-bind9.sh /usr/local/bin/argus_dns_sync.sh
|
||||
|
||||
# Set proper ownership for BIND9 files
|
||||
RUN chown bind:bind /etc/bind/named.conf.local /etc/bind/db.argus.com
|
||||
@@ -41,4 +42,4 @@ EXPOSE 53/tcp 53/udp
|
||||
USER root
|
||||
|
||||
# Start with startup script
|
||||
CMD ["/usr/local/bin/startup.sh"]
|
||||
CMD ["/usr/local/bin/startup.sh"]
|
||||
|
||||
100
src/bind/build/argus_dns_sync.sh
Normal file
100
src/bind/build/argus_dns_sync.sh
Normal file
@@ -0,0 +1,100 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
WATCH_DIR="/private/argus/etc"
|
||||
ZONE_DB="/private/argus/bind/db.argus.com"
|
||||
LOCKFILE="/var/lock/argus_dns_sync.lock"
|
||||
BACKUP_DIR="/private/argus/bind/.backup"
|
||||
SLEEP_SECONDS=10
|
||||
RELOAD_SCRIPT="/usr/local/bin/reload-bind9.sh" # 这里放你已有脚本的路径
|
||||
|
||||
mkdir -p "$(dirname "$LOCKFILE")" "$BACKUP_DIR"
|
||||
|
||||
is_ipv4() {
|
||||
local ip="$1"
|
||||
[[ "$ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] || return 1
|
||||
IFS='.' read -r a b c d <<<"$ip"
|
||||
for n in "$a" "$b" "$c" "$d"; do
|
||||
(( n >= 0 && n <= 255 )) || return 1
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
get_current_ip() {
|
||||
local name="$1"
|
||||
sed -n -E "s/^${name}[[:space:]]+IN[[:space:]]+A[[:space:]]+([0-9.]+)[[:space:]]*$/\1/p" "$ZONE_DB" | head -n1
|
||||
}
|
||||
|
||||
upsert_record() {
|
||||
local name="$1"
|
||||
local new_ip="$2"
|
||||
local ts
|
||||
ts="$(date +%Y%m%d-%H%M%S)"
|
||||
local changed=0
|
||||
|
||||
cp -a "$ZONE_DB" "$BACKUP_DIR/db.argus.com.$ts.bak"
|
||||
|
||||
local cur_ip
|
||||
cur_ip="$(get_current_ip "$name" || true)"
|
||||
|
||||
if [[ -z "$cur_ip" ]]; then
|
||||
# Ensure the file ends with a newline before adding new record
|
||||
if [[ -s "$ZONE_DB" ]] && [[ $(tail -c1 "$ZONE_DB" | wc -l) -eq 0 ]]; then
|
||||
echo "" >> "$ZONE_DB"
|
||||
fi
|
||||
printf "%-20s IN A %s\n" "$name" "$new_ip" >> "$ZONE_DB"
|
||||
echo "[ADD] ${name} -> ${new_ip}"
|
||||
changed=1
|
||||
elif [[ "$cur_ip" != "$new_ip" ]]; then
|
||||
awk -v n="$name" -v ip="$new_ip" '
|
||||
{
|
||||
if ($1==n && $2=="IN" && $3=="A") {
|
||||
printf "%-20s IN A %s\n", n, ip
|
||||
} else {
|
||||
print
|
||||
}
|
||||
}
|
||||
' "$ZONE_DB" > "${ZONE_DB}.tmp" && mv "${ZONE_DB}.tmp" "$ZONE_DB"
|
||||
echo "[UPDATE] ${name}: ${cur_ip} -> ${new_ip}"
|
||||
changed=1
|
||||
else
|
||||
echo "[SKIP] ${name} unchanged (${new_ip})"
|
||||
fi
|
||||
|
||||
return $changed
|
||||
}
|
||||
|
||||
while true; do
|
||||
exec 9>"$LOCKFILE"
|
||||
if flock -n 9; then
|
||||
shopt -s nullglob
|
||||
NEED_RELOAD=0
|
||||
|
||||
for f in "$WATCH_DIR"/*.argus.com; do
|
||||
base="$(basename "$f")"
|
||||
name="${base%.argus.com}"
|
||||
ip="$(grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' "$f" | tail -n1 || true)"
|
||||
|
||||
if [[ -z "$ip" ]] || ! is_ipv4 "$ip"; then
|
||||
echo "[WARN] $f 未找到有效 IPv4,跳过"
|
||||
continue
|
||||
fi
|
||||
|
||||
if upsert_record "$name" "$ip"; then
|
||||
NEED_RELOAD=1
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ $NEED_RELOAD -eq 1 ]]; then
|
||||
echo "[INFO] 检测到 db.argus.com 变更,执行 reload-bind9.sh"
|
||||
bash "$RELOAD_SCRIPT"
|
||||
fi
|
||||
|
||||
flock -u 9
|
||||
else
|
||||
echo "[INFO] 已有同步任务在运行,跳过本轮"
|
||||
fi
|
||||
|
||||
sleep "$SLEEP_SECONDS"
|
||||
done
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
# Set /private permissions to 777 as requested
|
||||
chmod 777 /private 2>/dev/null || true
|
||||
|
||||
# Create persistent directory for BIND9 configs
|
||||
# Create persistent directories for BIND9 configs and DNS sync
|
||||
mkdir -p /private/argus/bind
|
||||
mkdir -p /private/argus/etc
|
||||
|
||||
# Copy configuration files to persistent storage if they don't exist
|
||||
if [ ! -f /private/argus/bind/named.conf.local ]; then
|
||||
|
||||
@@ -21,4 +21,17 @@ autostart=true
|
||||
autorestart=true
|
||||
stderr_logfile=/var/log/supervisor/bind9.err.log
|
||||
stdout_logfile=/var/log/supervisor/bind9.out.log
|
||||
priority=10
|
||||
priority=10
|
||||
|
||||
[program:argus-dns-sync]
|
||||
command=/usr/local/bin/argus_dns_sync.sh
|
||||
autostart=true
|
||||
autorestart=true
|
||||
startsecs=3
|
||||
stopsignal=TERM
|
||||
user=root
|
||||
stdout_logfile=/var/log/argus_dns_sync.out.log
|
||||
stderr_logfile=/var/log/argus_dns_sync.err.log
|
||||
; 根据环境调整环境变量(可选)
|
||||
; environment=RNDC_RELOAD="yes"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user