Naposledy aktivní 1 month ago

Revize 4368db6a800da024e5b625bf62b274070bc17668

gistfile1.txt Raw
1function cmd::inspect::_rule_info() {
2 local name="${1:-}"
3 local rule
4 rule=$(peers::get_meta "$name" "rule")
5 [[ -z "$rule" ]] && return 0
6 rule::exists "$rule" || return 0
7
8 cmd::inspect::_section "Rule: ${rule}"
9
10 local rule_file
11 rule_file="$(rule::path "$rule")"
12
13 # Check for inheritance
14 local extends_raw=()
15 mapfile -t extends_raw < <(json::get "$rule_file" "extends" 2>/dev/null || true)
16
17 if [[ ${#extends_raw[@]} -gt 0 && -n "${extends_raw[0]:-}" ]]; then
18 # Show inheritance tree
19 for base_name in "${extends_raw[@]}"; do
20 [[ -z "$base_name" ]] && continue
21 printf "\n \033[0;37m↳ %s\033[0m\n" "$base_name"
22
23 local base_allows base_blocks
24 base_allows=$(rule::get "$base_name" "allow_ports")$'\n'$(rule::get "$base_name" "allow_ips")
25 base_blocks=$(rule::get "$base_name" "block_ips")$'\n'$(rule::get "$base_name" "block_ports")
26
27 while IFS= read -r e; do
28 [[ -z "$e" ]] && continue
29 net::print_entry "+" "$e"
30 done <<< "$base_allows"
31
32 while IFS= read -r e; do
33 [[ -z "$e" ]] && continue
34 net::print_entry "-" "$e"
35 done <<< "$base_blocks"
36
37 local base_dns
38 base_dns=$(rule::get_own "$base_name" "dns_redirect")
39 [[ "${base_dns,,}" == "true" ]] && \
40 printf " \033[0;36m↺\033[0m DNS → %s\n" "$(config::dns)"
41 done
42
43 # Own rules
44 local own_allows own_blocks
45 own_allows=$(json::get "$rule_file" "allow_ports" 2>/dev/null)$'\n'$(json::get "$rule_file" "allow_ips" 2>/dev/null)
46 own_blocks=$(json::get "$rule_file" "block_ips" 2>/dev/null)$'\n'$(json::get "$rule_file" "block_ports" 2>/dev/null)
47 local has_own=false
48
49 while IFS= read -r e; do [[ -n "$e" ]] && has_own=true && break; done <<< "$own_allows$own_blocks"
50
51 if $has_own; then
52 printf "\n \033[0;37mOwn:\033[0m\n"
53 while IFS= read -r e; do
54 [[ -z "$e" ]] && continue
55 net::print_entry "+" "$e"
56 done <<< "$own_allows"
57
58 while IFS= read -r e; do
59 [[ -z "$e" ]] && continue
60 net::print_entry "-" "$e"
61 done <<< "$own_blocks"
62 fi
63
64 else
65 # No inheritance — flat view
66 local allow_ports allow_ips block_ips block_ports
67 allow_ports=$(rule::get "$rule" "allow_ports")
68 allow_ips=$(rule::get "$rule" "allow_ips")
69 block_ips=$(rule::get "$rule" "block_ips")
70 block_ports=$(rule::get "$rule" "block_ports")
71
72 if [[ -n "$allow_ports" || -n "$allow_ips" ]]; then
73 printf "\n"
74 while IFS= read -r e; do
75 [[ -z "$e" ]] && continue
76 net::print_entry "+" "$e"
77 done <<< "$allow_ports"$'\n'"$allow_ips"
78 fi
79
80 if [[ -n "$block_ips" || -n "$block_ports" ]]; then
81 printf "\n"
82 while IFS= read -r e; do
83 [[ -z "$e" ]] && continue
84 net::print_entry "-" "$e"
85 done <<< "$block_ips"$'\n'"$block_ports"
86 fi
87
88 if [[ -z "$allow_ports" && -z "$allow_ips" && \
89 -z "$block_ips" && -z "$block_ports" ]]; then
90 printf "\n full access (no restrictions)\n"
91 fi
92
93 local dns_redirect
94 dns_redirect=$(rule::get_own "$rule" "dns_redirect")
95 [[ "${dns_redirect,,}" == "true" ]] && \
96 printf "\n \033[0;36m↺\033[0m DNS → %s\n" "$(config::dns)"
97 fi
98
99 return 0
100}
101
102function cmd::inspect::_blocks_info() {
103 local name="${1:-}"
104 block::has_file "$name" || return 0
105
106 cmd::inspect::_section "Peer Blocks"
107
108 local blocked_direct
109 blocked_direct=$(block::is_blocked_direct "$name")
110 [[ "$blocked_direct" == "true" ]] && \
111 printf " \033[1;31m🚫\033[0m blocked directly\n"
112
113 local blocked_groups
114 blocked_groups=$(block::get_groups "$name")
115 [[ -n "$blocked_groups" ]] && \
116 printf " \033[1;31m🚫\033[0m blocked by groups: %s\n" "$blocked_groups"
117
118 block::format_rules "$name"
119
120 return 0
121}