nuno hat die Gist bearbeitet 2 months ago. Zu Änderung gehen
1 file changed, 52 insertions
gistfile1.txt(Datei erstellt)
| @@ -0,0 +1,52 @@ | |||
| 1 | + | cat /etc/wireguard/wgctl/commands/rule.command.sh | grep -A 50 "function cmd::rule::list" | |
| 2 | + | function cmd::rule::list() { | |
| 3 | + | local rules_dir | |
| 4 | + | rules_dir="$(ctx::rules)" | |
| 5 | + | ||
| 6 | + | local rules=("${rules_dir}"/*.rule) | |
| 7 | + | if [[ ! -f "${rules[0]}" ]]; then | |
| 8 | + | log::wg "No rules configured" | |
| 9 | + | return 0 | |
| 10 | + | fi | |
| 11 | + | ||
| 12 | + | local rules=("${rules_dir}"/*.rule) | |
| 13 | + | echo "DEBUG: first=${rules[0]}" | |
| 14 | + | echo "DEBUG: count=${#rules[@]}" | |
| 15 | + | ||
| 16 | + | log::section "Firewall Rules" | |
| 17 | + | ||
| 18 | + | printf "\n %-20s %-35s %-8s %-8s %s\n" \ | |
| 19 | + | "NAME" "DESCRIPTION" "ALLOWS" "BLOCKS" "PEERS" | |
| 20 | + | printf " %s\n" "$(printf '─%.0s' {1..80})" | |
| 21 | + | ||
| 22 | + | for rule_file in "${rules_dir}"/*.rule; do | |
| 23 | + | [[ -f "$rule_file" ]] || continue | |
| 24 | + | ||
| 25 | + | local name desc peer_count | |
| 26 | + | name=$(json::get "$rule_file" "name") | |
| 27 | + | desc=$(json::get "$rule_file" "desc") | |
| 28 | + | ||
| 29 | + | local n_allows n_block_ips n_block_ports blocks | |
| 30 | + | ||
| 31 | + | n_allows=$(json::get "$rule_file" "allow_ips" | grep -v '^$' | wc -l | tr -d ' ') | |
| 32 | + | n_block_ips=$(json::get "$rule_file" "block_ips" | grep -v '^$' | wc -l | tr -d ' ') | |
| 33 | + | n_block_ports=$(json::get "$rule_file" "block_ports" | grep -v '^$' | wc -l | tr -d ' ') | |
| 34 | + | # wc -l returns 0 for empty, so this is safe | |
| 35 | + | ||
| 36 | + | blocks=$(( n_block_ips + n_block_ports )) | |
| 37 | + | ||
| 38 | + | # Count assigned peers | |
| 39 | + | peer_count=$(peers::with_rule "$name" | grep -c .) | |
| 40 | + | ||
| 41 | + | local short_desc="${desc:0:33}" | |
| 42 | + | [[ ${#desc} -gt 33 ]] && short_desc="${short_desc}..." | |
| 43 | + | ||
| 44 | + | printf " %-20s %-36s %-8s %-8s %s\n" \ | |
| 45 | + | "$name" \ | |
| 46 | + | "${short_desc:-—}" \ | |
| 47 | + | "${n_allows}" \ | |
| 48 | + | "${blocks}" \ | |
| 49 | + | "${peer_count} peers" | |
| 50 | + | done | |
| 51 | + | ||
| 52 | + | printf "\n" | |
Neuer
Älter