143 lines
6.2 KiB
Bash
143 lines
6.2 KiB
Bash
#!/usr/bin/env bash
|
||
# EasyDNS developer helper. Runs everything from source with `cargo` (debug build),
|
||
# with a throw-away configuration and state in ./.dev (git-ignored).
|
||
#
|
||
# ./dev.sh run primary+recursive+forwarder+API on 127.0.0.1:5353 (default)
|
||
# ./dev.sh run-secondary second instance on 127.0.0.1:5354, secondary of the first one
|
||
# ./dev.sh dnssec like `run`, with example.com signed online (keys in .dev/keys)
|
||
# ./dev.sh q NAME [TYPE] query the dev server with `ed` (cargo run)
|
||
# ./dev.sh api PATH GET an API path with the dev admin token (e.g. ./dev.sh api /status)
|
||
# ./dev.sh tool NAME ... run any tool, e.g. ./dev.sh tool eh -p 5353 example.com 127.0.0.1
|
||
# ./dev.sh check fmt + clippy -D warnings + tests (the CI acceptance commands)
|
||
# ./dev.sh test [ARGS] cargo test --workspace [ARGS]
|
||
# ./dev.sh e2e release build + scripts/e2e.sh (needs dig/host/nslookup)
|
||
# ./dev.sh watch restart `run` on source changes (needs cargo-watch)
|
||
# ./dev.sh reset delete ./.dev
|
||
#
|
||
# Environment: DEV_PORT (5353), DEV_API (9953), DEV_METRICS (9090), DEV_LOG (debug),
|
||
# DEV_UPSTREAMS (forwarder upstreams, default "1.1.1.1,9.9.9.9").
|
||
set -euo pipefail
|
||
cd "$(dirname "$0")"
|
||
ROOT="$PWD"
|
||
DEV="$ROOT/.dev"
|
||
PORT="${DEV_PORT:-5353}"
|
||
API="${DEV_API:-9953}"
|
||
METRICS="${DEV_METRICS:-9090}"
|
||
LOG="${DEV_LOG:-debug,hyper=info,h2=info}"
|
||
UPSTREAMS="${DEV_UPSTREAMS:-1.1.1.1,9.9.9.9}"
|
||
|
||
mkdir -p "$DEV"
|
||
# Stable dev secrets (generated once, never committed).
|
||
[ -f "$DEV/tsig.secret" ] || head -c 32 /dev/urandom | base64 > "$DEV/tsig.secret"
|
||
[ -f "$DEV/admin.token" ] || echo "dev-$(head -c 12 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9')" > "$DEV/admin.token"
|
||
chmod 600 "$DEV"/*.secret "$DEV"/*.token 2>/dev/null || true
|
||
mkdir -p "$DEV/data/zones" # same layout as an installation: zone files in <data_dir>/zones/
|
||
[ -f "$DEV/data/zones/example.com.zone" ] || cp "$ROOT/zones/example.com.zone" "$DEV/data/zones/example.com.zone"
|
||
[ -f "$DEV/data/zones/reverse.zone" ] || cp "$ROOT/zones/2.0.192.in-addr.arpa.zone" "$DEV/data/zones/reverse.zone"
|
||
[ -f "$DEV/block.txt" ] || printf 'ads.example\n*.tracker.example\n' > "$DEV/block.txt"
|
||
|
||
upstreams_yaml() { local IFS=,; for u in $UPSTREAMS; do printf '"%s", ' "$u"; done | sed 's/, $//'; }
|
||
|
||
write_primary_config() {
|
||
local dnssec="${1:-}"
|
||
cat > "$DEV/config.yaml" <<C
|
||
# generated by dev.sh – edit freely, it is only rewritten when deleted
|
||
server: { mode: hybrid, listen: ["127.0.0.1:$PORT"], data_dir: data, identity: "easydns-dev" }
|
||
logging: { level: info, format: text, query_log: true, anonymize_clients: false }
|
||
keys: [ { name: xfr-key, algorithm: hmac-sha256, secret: "file:tsig.secret" } ]
|
||
recursion: { enabled: true, allowed_clients: ["127.0.0.0/8", "::1/128"], dnssec_validation: true }
|
||
zones:
|
||
- name: example.com
|
||
type: primary
|
||
file: zones/example.com.zone
|
||
transfer_acl: ["127.0.0.1/32"]
|
||
transfer_tsig_key: xfr-key
|
||
notify: ["127.0.0.1:$((PORT+1))"]
|
||
notify_tsig_key: xfr-key
|
||
update_keys: [xfr-key]
|
||
update_acl: ["127.0.0.1/32"]$dnssec
|
||
- { name: 2.0.192.in-addr.arpa, type: primary, file: zones/reverse.zone }
|
||
forward_zones: []
|
||
upstreams: { strategy: fastest, servers: [$(upstreams_yaml)] }
|
||
resolution: { default: recursive }
|
||
policy: { blocklists: [ { name: dev, file: block.txt, category: ads } ] }
|
||
rate_limit: { enabled: false }
|
||
api: { listen: "127.0.0.1:$API", tokens: [ { name: dev-admin, role: admin, token: "file:admin.token" } ] }
|
||
metrics: { listen: "127.0.0.1:$METRICS" }
|
||
C
|
||
}
|
||
|
||
run_easydns() { # $1 = config
|
||
echo ">> EasyDNS dev: DNS 127.0.0.1:$PORT API http://127.0.0.1:$API (token: $(cat "$DEV/admin.token")) metrics http://127.0.0.1:$METRICS/metrics"
|
||
echo ">> try: ./dev.sh q www.example.com | ./dev.sh api /status | dig @127.0.0.1 -p $PORT example.com SOA"
|
||
EASYDNS_LOG="$LOG" cargo run -q -p dns-cli --bin easydns -- run --config "$1"
|
||
}
|
||
|
||
cmd="${1:-run}"; shift || true
|
||
case "$cmd" in
|
||
run)
|
||
[ -f "$DEV/config.yaml" ] || write_primary_config
|
||
cargo run -q -p dns-cli --bin easydns -- check-config --config "$DEV/config.yaml"
|
||
run_easydns "$DEV/config.yaml"
|
||
;;
|
||
dnssec)
|
||
write_primary_config $'\n dnssec: { keys_dir: keys, auto_generate: true }'
|
||
echo ">> DS for the parent will be printed by: cargo run -p dns-cli --bin easydns -- dnssec ds --zone example.com --dir .dev/keys"
|
||
run_easydns "$DEV/config.yaml"
|
||
;;
|
||
run-secondary)
|
||
mkdir -p "$DEV/secondary"
|
||
cp "$DEV/tsig.secret" "$DEV/secondary/tsig.secret"
|
||
cat > "$DEV/secondary/config.yaml" <<C
|
||
server: { mode: secondary, listen: ["127.0.0.1:$((PORT+1))"], data_dir: data }
|
||
logging: { level: info }
|
||
keys: [ { name: xfr-key, algorithm: hmac-sha256, secret: "file:tsig.secret" } ]
|
||
zones:
|
||
- { name: example.com, type: secondary, primaries: ["127.0.0.1:$PORT"], transfer: { prefer: ixfr, tsig_key: xfr-key } }
|
||
api: { enabled: false }
|
||
metrics: { enabled: false }
|
||
C
|
||
echo ">> secondary on 127.0.0.1:$((PORT+1)) (primary: ./dev.sh run)"
|
||
EASYDNS_LOG="$LOG" cargo run -q -p dns-cli --bin easydns -- run --config "$DEV/secondary/config.yaml"
|
||
;;
|
||
q)
|
||
[ $# -ge 1 ] || { echo "usage: ./dev.sh q NAME [TYPE] [+opts]" >&2; exit 1; }
|
||
cargo run -q -p dns-tools --bin ed -- @127.0.0.1 -p "$PORT" "$@"
|
||
;;
|
||
api)
|
||
path="${1:-/status}"
|
||
curl -fsS -H "Authorization: Bearer $(cat "$DEV/admin.token")" "http://127.0.0.1:$API/api/v1$path"; echo
|
||
;;
|
||
tool)
|
||
[ $# -ge 1 ] || { echo "usage: ./dev.sh tool NAME [ARGS]" >&2; exit 1; }
|
||
t="$1"; shift
|
||
cargo run -q -p dns-tools --bin "$t" -- "$@"
|
||
;;
|
||
check)
|
||
cargo fmt --all -- --check
|
||
cargo clippy --workspace --all-targets --all-features -- -D warnings
|
||
cargo test --workspace --all-features
|
||
;;
|
||
test)
|
||
cargo test --workspace "$@"
|
||
;;
|
||
e2e)
|
||
cargo build --release -p dns-cli -p dns-tools
|
||
BIN=target/release ./scripts/e2e.sh
|
||
;;
|
||
watch)
|
||
command -v cargo-watch >/dev/null || { echo "install with: cargo install cargo-watch" >&2; exit 1; }
|
||
[ -f "$DEV/config.yaml" ] || write_primary_config
|
||
EASYDNS_LOG="$LOG" cargo watch -w crates -x "run -q -p dns-cli --bin easydns -- run --config $DEV/config.yaml"
|
||
;;
|
||
reset)
|
||
rm -rf "$DEV"; echo "removed $DEV"
|
||
;;
|
||
-h|--help|help)
|
||
sed -n '2,20p' "$0"
|
||
;;
|
||
*)
|
||
echo "unknown command: $cmd (see ./dev.sh help)" >&2; exit 1
|
||
;;
|
||
esac
|