Ostatnio aktywny 2 months ago

nuno zrewidował ten Gist 2 months ago. Przejdź do rewizji

1 file changed, 61 insertions

gistfile1.txt(stworzono plik)

@@ -0,0 +1,61 @@
1 + function rule::apply() {
2 + local rule_name="$1"
3 + local client_ip="$2"
4 +
5 + rule::require_exists "$rule_name" || return 1
6 +
7 + # Check if already applied
8 + local peer_name
9 + peer_name=$(peers::find_by_ip "$client_ip")
10 + if [[ -n "$peer_name" ]]; then
11 + # Check if already applied via iptables
12 + if rule::is_applied "$rule_name" "$client_ip"; then
13 + log::wg "Rule '${rule_name}' already applied to: ${client_ip}"
14 + return 0
15 + fi
16 + fi
17 +
18 + local rule_file
19 + rule_file="$(ctx::rule::path "${rule_name}.rule")"
20 +
21 + # Process allow_ips first (inserted before blocks)
22 + while IFS= read -r ip; do
23 + [[ -z "$ip" ]] && continue
24 + firewall::allow_ip "$client_ip" "$ip"
25 + done < <(rule::get "$rule_name" "allow_ips")
26 +
27 + # Process block_ips
28 + while IFS= read -r ip; do
29 + [[ -z "$ip" ]] && continue
30 + firewall::block_ip "$client_ip" "$ip"
31 + done < <(rule::get "$rule_name" "block_ips")
32 +
33 + # Process block_ports
34 + while IFS= read -r entry; do
35 + [[ -z "$entry" ]] && continue
36 + local target port proto
37 + IFS=":" read -r target port proto <<< "$entry"
38 + proto="${proto:-tcp}"
39 + firewall::block_port "$client_ip" "$target" "$port" "$proto"
40 + done < <(rule::get "$rule_name" "block_ports")
41 +
42 + # Persist rule assignment in meta
43 + local peer_name
44 + peer_name=$(peers::find_by_ip "$client_ip")
45 + log::debug "rule::apply: peer_name=$peer_name ip=$client_ip"
46 + if [[ -n "$peer_name" ]]; then
47 + peers::set_meta "$peer_name" "rule" "$rule_name"
48 + log::debug "rule::apply: set meta rule=$rule_name for $peer_name"
49 + fi
50 +
51 + local dns_redirect
52 + dns_redirect=$(rule::get "$rule_name" "dns_redirect")
53 +
54 + if [[ "$dns_redirect" == "true" ]]; then
55 + local subnet
56 + subnet=$(config::subnet_for "$(peers::get_meta "$peer_name" "subtype")")
57 + rule::apply_dns_redirect "${subnet}.0/24"
58 + fi
59 +
60 + log::wg_preset "Applied rule '${rule_name}' to: ${client_ip}"
61 + }
Nowsze Starsze