gistfile1.txt
· 1.9 KiB · Text
Brut
#!/usr/bin/env bash
# modules/resolve.module.sh — IP/host resolution chain
# Chains: hosts.json exact match → services.json match → raw IP
# Depends on: hosts.module.sh, net.module.sh
declare -gA _RESOLVE_CACHE=()
# resolve::ip <ip> [port] [proto]
# Resolves an IP to a display name using the full resolution chain.
# Returns raw IP if no match found.
# Respects _WGCTL_RAW=true to bypass resolution.
function resolve::ip() {
local ip="${1:-}" port="${2:-}" proto="${3:-}"
[[ -z "$ip" ]] && echo "" && return 0
[[ "${_WGCTL_RAW:-false}" == "true" ]] && echo "$ip" && return 0
local cache_key="${ip}:${port}:${proto}"
if [[ -z "${_RESOLVE_CACHE[$cache_key]+x}" ]]; then
local result=""
# 1. hosts.json exact IP match
if [[ -f "$(ctx::hosts)" ]]; then
result=$(hosts::resolve_ip "$ip")
fi
# 2. services.json match
if [[ -z "$result" ]]; then
result=$(net::reverse_lookup "$ip" "$port" "$proto" 2>/dev/null) || result=""
fi
# 3. Raw IP fallback
[[ -z "$result" ]] && result="$ip"
_RESOLVE_CACHE[$cache_key]="$result"
fi
echo "${_RESOLVE_CACHE[$cache_key]}"
}
# resolve::dest <ip> [port] [proto]
# Like resolve::ip but builds a formatted destination display string.
# e.g. "pihole:dns-udp" or "vodafone-wan" or "10.0.0.103:853/tcp"
function resolve::dest() {
local ip="${1:-}" port="${2:-}" proto="${3:-}"
[[ -z "$ip" ]] && echo "" && return 0
local name
name=$(resolve::ip "$ip" "$port" "$proto")
if [[ "$name" == "$ip" ]]; then
# No resolution — raw format
if [[ -n "$port" ]]; then
echo "${ip}:${port}/${proto}"
else
[[ -n "$proto" ]] && echo "${ip} (${proto})" || echo "$ip"
fi
else
# Resolved — just the name, no proto suffix
echo "$name"
fi
}
# resolve::clear_cache
# Clears the resolution cache — call between commands if needed.
function resolve::clear_cache() {
_RESOLVE_CACHE=()
}
| 1 | #!/usr/bin/env bash |
| 2 | # modules/resolve.module.sh — IP/host resolution chain |
| 3 | # Chains: hosts.json exact match → services.json match → raw IP |
| 4 | # Depends on: hosts.module.sh, net.module.sh |
| 5 | |
| 6 | declare -gA _RESOLVE_CACHE=() |
| 7 | |
| 8 | # resolve::ip <ip> [port] [proto] |
| 9 | # Resolves an IP to a display name using the full resolution chain. |
| 10 | # Returns raw IP if no match found. |
| 11 | # Respects _WGCTL_RAW=true to bypass resolution. |
| 12 | function resolve::ip() { |
| 13 | local ip="${1:-}" port="${2:-}" proto="${3:-}" |
| 14 | [[ -z "$ip" ]] && echo "" && return 0 |
| 15 | [[ "${_WGCTL_RAW:-false}" == "true" ]] && echo "$ip" && return 0 |
| 16 | |
| 17 | local cache_key="${ip}:${port}:${proto}" |
| 18 | if [[ -z "${_RESOLVE_CACHE[$cache_key]+x}" ]]; then |
| 19 | local result="" |
| 20 | |
| 21 | # 1. hosts.json exact IP match |
| 22 | if [[ -f "$(ctx::hosts)" ]]; then |
| 23 | result=$(hosts::resolve_ip "$ip") |
| 24 | fi |
| 25 | |
| 26 | # 2. services.json match |
| 27 | if [[ -z "$result" ]]; then |
| 28 | result=$(net::reverse_lookup "$ip" "$port" "$proto" 2>/dev/null) || result="" |
| 29 | fi |
| 30 | |
| 31 | # 3. Raw IP fallback |
| 32 | [[ -z "$result" ]] && result="$ip" |
| 33 | |
| 34 | _RESOLVE_CACHE[$cache_key]="$result" |
| 35 | fi |
| 36 | |
| 37 | echo "${_RESOLVE_CACHE[$cache_key]}" |
| 38 | } |
| 39 | |
| 40 | # resolve::dest <ip> [port] [proto] |
| 41 | # Like resolve::ip but builds a formatted destination display string. |
| 42 | # e.g. "pihole:dns-udp" or "vodafone-wan" or "10.0.0.103:853/tcp" |
| 43 | function resolve::dest() { |
| 44 | local ip="${1:-}" port="${2:-}" proto="${3:-}" |
| 45 | [[ -z "$ip" ]] && echo "" && return 0 |
| 46 | |
| 47 | local name |
| 48 | name=$(resolve::ip "$ip" "$port" "$proto") |
| 49 | |
| 50 | if [[ "$name" == "$ip" ]]; then |
| 51 | # No resolution — raw format |
| 52 | if [[ -n "$port" ]]; then |
| 53 | echo "${ip}:${port}/${proto}" |
| 54 | else |
| 55 | [[ -n "$proto" ]] && echo "${ip} (${proto})" || echo "$ip" |
| 56 | fi |
| 57 | else |
| 58 | # Resolved — just the name, no proto suffix |
| 59 | echo "$name" |
| 60 | fi |
| 61 | } |
| 62 | |
| 63 | # resolve::clear_cache |
| 64 | # Clears the resolution cache — call between commands if needed. |
| 65 | function resolve::clear_cache() { |
| 66 | _RESOLVE_CACHE=() |
| 67 | } |