gistfile1.txt
· 7.2 KiB · Text
Ham
function cmd::rule::show() {
local name="" show_peers=true color=false show_resolved=false
while [[ $# -gt 0 ]]; do
case "$1" in
--name) util::require_flag "--name" "${2:-}" || return 1
name="$2"; shift 2 ;;
--no-peers) show_peers=false; shift ;;
--color) color=true; shift ;;
--resolved) show_resolved=true; shift ;;
--help) cmd::rule::help; return ;;
*) log::error "Unknown flag: $1"; return 1 ;;
esac
done
[[ -z "$name" ]] && log::error "Missing required flag: --name" && return 1
rule::require_exists "$name" || return 1
local rule_file
rule_file="$(rule::path "$name")"
local dns_redirect
dns_redirect=$(rule::get_own "$name" "dns_redirect")
dns_redirect="${dns_redirect:-false}"
local resolved_dns
resolved_dns=$(rule::get "$name" "dns_redirect")
resolved_dns="${resolved_dns:-false}"
local dns_display
if [[ "${resolved_dns,,}" == "true" && "${dns_redirect,,}" != "true" ]]; then
dns_display="true (inherited)"
elif [[ "${dns_redirect,,}" == "true" ]]; then
dns_display="true"
else
dns_display="false"
fi
log::section "Rule: ${name}"
printf "\n"
# ── Header info ──────────────────────────────
local desc group dns_redirect
desc=$(json::get "$rule_file" "desc")
group=$(json::get "$rule_file" "group")
ui::row "Description" "${desc:-—}"
ui::row "Group" "${group:-—}"
ui::row "DNS" "$dns_display"
# ── Extends section ──────────────────────────
local extends_raw=()
mapfile -t extends_raw < <(json::get "$rule_file" "extends" 2>/dev/null || true)
if [[ ${#extends_raw[@]} -gt 0 && -n "${extends_raw[0]}" ]]; then
cmd::rule::_show_section "Extends"
for base_name in "${extends_raw[@]}"; do
[[ -z "$base_name" ]] && continue
printf "\n \033[0;37m↳ %s\033[0m\n" "$base_name"
# Show resolved entries for this base
local base_allow_ports base_allow_ips base_block_ips base_block_ports base_dns
base_allow_ports=$(rule::get "$base_name" "allow_ports" 2>/dev/null || true)
base_allow_ips=$(rule::get "$base_name" "allow_ips" 2>/dev/null || true)
base_block_ips=$(rule::get "$base_name" "block_ips" 2>/dev/null || true)
base_block_ports=$(rule::get "$base_name" "block_ports" 2>/dev/null || true)
base_dns=$(json::get "$(rule::path "$base_name")" "dns_redirect" 2>/dev/null || true)
while IFS= read -r e; do
[[ -z "$e" ]] && continue
net::print_entry "+" "$e"
done <<< "$base_allow_ports"
while IFS= read -r e; do
[[ -z "$e" ]] && continue
net::print_entry "-" "$e"
done <<< "$base_allow_ips"
while IFS= read -r e; do
[[ -z "$e" ]] && continue
net::print_entry "-" "$e"
done <<< "$base_block_ips"
while IFS= read -r e; do
[[ -z "$e" ]] && continue
net::print_entry "-" "$e"
done <<< "$base_block_ports"
[[ "$base_dns" == "true" ]] && \
printf " \033[0;36m↺\033[0m DNS → %s\n" "$(config::dns)"
done
fi
# ── Own Rules section ─────────────────────────
local own_allow_ports own_allow_ips own_block_ips own_block_ports
own_allow_ports=$(json::get "$rule_file" "allow_ports")
own_allow_ips=$(json::get "$rule_file" "allow_ips")
own_block_ips=$(json::get "$rule_file" "block_ips")
own_block_ports=$(json::get "$rule_file" "block_ports")
local has_own=false
[[ -n "$own_allow_ports" || -n "$own_allow_ips" || \
-n "$own_block_ips" || -n "$own_block_ports" ]] && has_own=true
if $has_own; then
if [[ ${#extends_raw[@]} -gt 0 && -n "${extends_raw[0]}" ]]; then
cmd::rule::_show_section "Own Rules"
printf "\n"
while IFS= read -r e; do
[[ -z "$e" ]] && continue
net::print_entry "+" "$e"
done <<< "$own_allow_ports"
while IFS= read -r e; do
[[ -z "$e" ]] && continue
net::print_entry "+" "$e"
done <<< "$own_allow_ips"
while IFS= read -r e; do
[[ -z "$e" ]] && continue
net::print_entry "-" "$e"
done <<< "$own_block_ips"
while IFS= read -r e; do
[[ -z "$e" ]] && continue
net::print_entry "-" "$e"
done <<< "$own_block_ports"
[[ "$dns_redirect" == "true" ]] && \
printf " \033[0;36m↺\033[0m DNS → %s\n" "$(config::dns)"
else
# No inheritance — use Allow/Block sections
if [[ -n "$own_allow_ports" || -n "$own_allow_ips" ]]; then
cmd::rule::_show_section "Allow"
printf "\n"
while IFS= read -r e; do
[[ -z "$e" ]] && continue
printf " \033[0;32m+\033[0m %s\n" "$e"
done <<< "$own_allow_ports"
while IFS= read -r e; do
[[ -z "$e" ]] && continue
printf " \033[0;32m+\033[0m %s\n" "$e"
done <<< "$own_allow_ips"
fi
if [[ -n "$own_block_ips" || -n "$own_block_ports" ]]; then
cmd::rule::_show_section "Block"
printf "\n"
while IFS= read -r e; do
[[ -z "$e" ]] && continue
printf " \033[0;31m-\033[0m %s\n" "$e"
done <<< "$own_block_ips"
while IFS= read -r e; do
[[ -z "$e" ]] && continue
printf " \033[0;31m-\033[0m %s\n" "$e"
done <<< "$own_block_ports"
fi
if [[ "$dns_redirect" == "true" ]]; then
cmd::rule::_show_section "DNS"
printf "\n \033[0;36m↺\033[0m Redirect all DNS → %s\n" "$(config::dns)"
fi
fi
elif [[ ${#extends_raw[@]} -eq 0 || -z "${extends_raw[0]}" ]]; then
printf "\n"
ui::row "Access" "full (no restrictions)"
fi
# ── Resolved section (optional) ──────────────
if $show_resolved; then
cmd::rule::_show_section "Resolved (applied to peers)"
local res_allow_ports res_allow_ips res_block_ips res_block_ports
res_allow_ports=$(rule::get "$name" "allow_ports")
res_allow_ips=$(rule::get "$name" "allow_ips")
res_block_ips=$(rule::get "$name" "block_ips")
res_block_ports=$(rule::get "$name" "block_ports")
while IFS= read -r e; do [[ -n "$e" ]] && \
printf " \033[0;32m+\033[0m %s\n" "$e"; done <<< "$res_allow_ports"
while IFS= read -r e; do [[ -n "$e" ]] && \
printf " \033[0;32m+\033[0m %s\n" "$e"; done <<< "$res_allow_ips"
while IFS= read -r e; do [[ -n "$e" ]] && \
printf " \033[0;31m-\033[0m %s\n" "$e"; done <<< "$res_block_ips"
while IFS= read -r e; do [[ -n "$e" ]] && \
printf " \033[0;31m-\033[0m %s\n" "$e"; done <<< "$res_block_ports"
fi
# ── Peers section ─────────────────────────────
cmd::rule::_show_section "Peers"
local peer_list=()
mapfile -t peer_list < <(peers::with_rule "$name")
local peer_count=${#peer_list[@]}
ui::row "Assigned" "$peer_count"
if $show_peers && [[ $peer_count -gt 0 ]]; then
printf "\n"
for peer_name in "${peer_list[@]}"; do
local ip
ip=$(peers::get_ip "$peer_name")
printf " %-28s %s\n" "$peer_name" "$ip"
done
fi
printf "\n"
}
| 1 | function cmd::rule::show() { |
| 2 | local name="" show_peers=true color=false show_resolved=false |
| 3 | |
| 4 | while [[ $# -gt 0 ]]; do |
| 5 | case "$1" in |
| 6 | --name) util::require_flag "--name" "${2:-}" || return 1 |
| 7 | name="$2"; shift 2 ;; |
| 8 | --no-peers) show_peers=false; shift ;; |
| 9 | --color) color=true; shift ;; |
| 10 | --resolved) show_resolved=true; shift ;; |
| 11 | --help) cmd::rule::help; return ;; |
| 12 | *) log::error "Unknown flag: $1"; return 1 ;; |
| 13 | esac |
| 14 | done |
| 15 | |
| 16 | [[ -z "$name" ]] && log::error "Missing required flag: --name" && return 1 |
| 17 | rule::require_exists "$name" || return 1 |
| 18 | |
| 19 | local rule_file |
| 20 | rule_file="$(rule::path "$name")" |
| 21 | |
| 22 | local dns_redirect |
| 23 | dns_redirect=$(rule::get_own "$name" "dns_redirect") |
| 24 | dns_redirect="${dns_redirect:-false}" |
| 25 | |
| 26 | local resolved_dns |
| 27 | resolved_dns=$(rule::get "$name" "dns_redirect") |
| 28 | resolved_dns="${resolved_dns:-false}" |
| 29 | |
| 30 | local dns_display |
| 31 | if [[ "${resolved_dns,,}" == "true" && "${dns_redirect,,}" != "true" ]]; then |
| 32 | dns_display="true (inherited)" |
| 33 | elif [[ "${dns_redirect,,}" == "true" ]]; then |
| 34 | dns_display="true" |
| 35 | else |
| 36 | dns_display="false" |
| 37 | fi |
| 38 | |
| 39 | |
| 40 | log::section "Rule: ${name}" |
| 41 | printf "\n" |
| 42 | |
| 43 | # ── Header info ────────────────────────────── |
| 44 | local desc group dns_redirect |
| 45 | desc=$(json::get "$rule_file" "desc") |
| 46 | group=$(json::get "$rule_file" "group") |
| 47 | |
| 48 | ui::row "Description" "${desc:-—}" |
| 49 | ui::row "Group" "${group:-—}" |
| 50 | ui::row "DNS" "$dns_display" |
| 51 | |
| 52 | # ── Extends section ────────────────────────── |
| 53 | local extends_raw=() |
| 54 | mapfile -t extends_raw < <(json::get "$rule_file" "extends" 2>/dev/null || true) |
| 55 | |
| 56 | if [[ ${#extends_raw[@]} -gt 0 && -n "${extends_raw[0]}" ]]; then |
| 57 | cmd::rule::_show_section "Extends" |
| 58 | |
| 59 | for base_name in "${extends_raw[@]}"; do |
| 60 | [[ -z "$base_name" ]] && continue |
| 61 | printf "\n \033[0;37m↳ %s\033[0m\n" "$base_name" |
| 62 | |
| 63 | # Show resolved entries for this base |
| 64 | local base_allow_ports base_allow_ips base_block_ips base_block_ports base_dns |
| 65 | base_allow_ports=$(rule::get "$base_name" "allow_ports" 2>/dev/null || true) |
| 66 | base_allow_ips=$(rule::get "$base_name" "allow_ips" 2>/dev/null || true) |
| 67 | base_block_ips=$(rule::get "$base_name" "block_ips" 2>/dev/null || true) |
| 68 | base_block_ports=$(rule::get "$base_name" "block_ports" 2>/dev/null || true) |
| 69 | base_dns=$(json::get "$(rule::path "$base_name")" "dns_redirect" 2>/dev/null || true) |
| 70 | |
| 71 | while IFS= read -r e; do |
| 72 | [[ -z "$e" ]] && continue |
| 73 | net::print_entry "+" "$e" |
| 74 | done <<< "$base_allow_ports" |
| 75 | |
| 76 | while IFS= read -r e; do |
| 77 | [[ -z "$e" ]] && continue |
| 78 | net::print_entry "-" "$e" |
| 79 | done <<< "$base_allow_ips" |
| 80 | |
| 81 | while IFS= read -r e; do |
| 82 | [[ -z "$e" ]] && continue |
| 83 | net::print_entry "-" "$e" |
| 84 | done <<< "$base_block_ips" |
| 85 | |
| 86 | while IFS= read -r e; do |
| 87 | [[ -z "$e" ]] && continue |
| 88 | net::print_entry "-" "$e" |
| 89 | done <<< "$base_block_ports" |
| 90 | |
| 91 | [[ "$base_dns" == "true" ]] && \ |
| 92 | printf " \033[0;36m↺\033[0m DNS → %s\n" "$(config::dns)" |
| 93 | done |
| 94 | fi |
| 95 | |
| 96 | # ── Own Rules section ───────────────────────── |
| 97 | local own_allow_ports own_allow_ips own_block_ips own_block_ports |
| 98 | own_allow_ports=$(json::get "$rule_file" "allow_ports") |
| 99 | own_allow_ips=$(json::get "$rule_file" "allow_ips") |
| 100 | own_block_ips=$(json::get "$rule_file" "block_ips") |
| 101 | own_block_ports=$(json::get "$rule_file" "block_ports") |
| 102 | |
| 103 | local has_own=false |
| 104 | [[ -n "$own_allow_ports" || -n "$own_allow_ips" || \ |
| 105 | -n "$own_block_ips" || -n "$own_block_ports" ]] && has_own=true |
| 106 | |
| 107 | if $has_own; then |
| 108 | if [[ ${#extends_raw[@]} -gt 0 && -n "${extends_raw[0]}" ]]; then |
| 109 | cmd::rule::_show_section "Own Rules" |
| 110 | printf "\n" |
| 111 | while IFS= read -r e; do |
| 112 | [[ -z "$e" ]] && continue |
| 113 | net::print_entry "+" "$e" |
| 114 | done <<< "$own_allow_ports" |
| 115 | |
| 116 | while IFS= read -r e; do |
| 117 | [[ -z "$e" ]] && continue |
| 118 | net::print_entry "+" "$e" |
| 119 | done <<< "$own_allow_ips" |
| 120 | |
| 121 | while IFS= read -r e; do |
| 122 | [[ -z "$e" ]] && continue |
| 123 | net::print_entry "-" "$e" |
| 124 | done <<< "$own_block_ips" |
| 125 | |
| 126 | while IFS= read -r e; do |
| 127 | [[ -z "$e" ]] && continue |
| 128 | net::print_entry "-" "$e" |
| 129 | done <<< "$own_block_ports" |
| 130 | |
| 131 | [[ "$dns_redirect" == "true" ]] && \ |
| 132 | printf " \033[0;36m↺\033[0m DNS → %s\n" "$(config::dns)" |
| 133 | else |
| 134 | # No inheritance — use Allow/Block sections |
| 135 | if [[ -n "$own_allow_ports" || -n "$own_allow_ips" ]]; then |
| 136 | cmd::rule::_show_section "Allow" |
| 137 | printf "\n" |
| 138 | while IFS= read -r e; do |
| 139 | [[ -z "$e" ]] && continue |
| 140 | printf " \033[0;32m+\033[0m %s\n" "$e" |
| 141 | done <<< "$own_allow_ports" |
| 142 | while IFS= read -r e; do |
| 143 | [[ -z "$e" ]] && continue |
| 144 | printf " \033[0;32m+\033[0m %s\n" "$e" |
| 145 | done <<< "$own_allow_ips" |
| 146 | fi |
| 147 | |
| 148 | if [[ -n "$own_block_ips" || -n "$own_block_ports" ]]; then |
| 149 | cmd::rule::_show_section "Block" |
| 150 | printf "\n" |
| 151 | while IFS= read -r e; do |
| 152 | [[ -z "$e" ]] && continue |
| 153 | printf " \033[0;31m-\033[0m %s\n" "$e" |
| 154 | done <<< "$own_block_ips" |
| 155 | while IFS= read -r e; do |
| 156 | [[ -z "$e" ]] && continue |
| 157 | printf " \033[0;31m-\033[0m %s\n" "$e" |
| 158 | done <<< "$own_block_ports" |
| 159 | fi |
| 160 | |
| 161 | if [[ "$dns_redirect" == "true" ]]; then |
| 162 | cmd::rule::_show_section "DNS" |
| 163 | printf "\n \033[0;36m↺\033[0m Redirect all DNS → %s\n" "$(config::dns)" |
| 164 | fi |
| 165 | fi |
| 166 | elif [[ ${#extends_raw[@]} -eq 0 || -z "${extends_raw[0]}" ]]; then |
| 167 | printf "\n" |
| 168 | ui::row "Access" "full (no restrictions)" |
| 169 | fi |
| 170 | |
| 171 | # ── Resolved section (optional) ────────────── |
| 172 | if $show_resolved; then |
| 173 | cmd::rule::_show_section "Resolved (applied to peers)" |
| 174 | local res_allow_ports res_allow_ips res_block_ips res_block_ports |
| 175 | res_allow_ports=$(rule::get "$name" "allow_ports") |
| 176 | res_allow_ips=$(rule::get "$name" "allow_ips") |
| 177 | res_block_ips=$(rule::get "$name" "block_ips") |
| 178 | res_block_ports=$(rule::get "$name" "block_ports") |
| 179 | |
| 180 | while IFS= read -r e; do [[ -n "$e" ]] && \ |
| 181 | printf " \033[0;32m+\033[0m %s\n" "$e"; done <<< "$res_allow_ports" |
| 182 | while IFS= read -r e; do [[ -n "$e" ]] && \ |
| 183 | printf " \033[0;32m+\033[0m %s\n" "$e"; done <<< "$res_allow_ips" |
| 184 | while IFS= read -r e; do [[ -n "$e" ]] && \ |
| 185 | printf " \033[0;31m-\033[0m %s\n" "$e"; done <<< "$res_block_ips" |
| 186 | while IFS= read -r e; do [[ -n "$e" ]] && \ |
| 187 | printf " \033[0;31m-\033[0m %s\n" "$e"; done <<< "$res_block_ports" |
| 188 | fi |
| 189 | |
| 190 | # ── Peers section ───────────────────────────── |
| 191 | cmd::rule::_show_section "Peers" |
| 192 | |
| 193 | local peer_list=() |
| 194 | mapfile -t peer_list < <(peers::with_rule "$name") |
| 195 | local peer_count=${#peer_list[@]} |
| 196 | ui::row "Assigned" "$peer_count" |
| 197 | |
| 198 | if $show_peers && [[ $peer_count -gt 0 ]]; then |
| 199 | printf "\n" |
| 200 | for peer_name in "${peer_list[@]}"; do |
| 201 | local ip |
| 202 | ip=$(peers::get_ip "$peer_name") |
| 203 | printf " %-28s %s\n" "$peer_name" "$ip" |
| 204 | done |
| 205 | fi |
| 206 | |
| 207 | printf "\n" |
| 208 | } |