gistfile1.txt
· 1.4 KiB · Text
Исходник
function cmd::rule::update() {
local name="" desc=""
local allow_ips=() block_ips=() block_ports=()
local dns_redirect=""
while [[ $# -gt 0 ]]; do
case "$1" in
--name) name="$2"; shift 2 ;;
--desc) desc="$2"; shift 2 ;;
--allow-ip) allow_ips+=("$2"); shift 2 ;;
--block-ip) block_ips+=("$2"); shift 2 ;;
--block-port) block_ports+=("$2"); shift 2 ;;
--dns-redirect) dns_redirect=true; shift ;;
--help) cmd::rule::help; return ;;
*)
log::error "Unknown flag: $1"
return 1
;;
esac
done
if [[ -z "$name" ]]; then
log::error "Missing required flag: --name"
return 1
fi
rule::require_exists "$name" || return 1
# Update individual fields if provided
[[ -n "$desc" ]] && json::set "$(ctx::rule::path "${name}.rule")" "desc" "\"$desc\""
[[ -n "$dns_redirect" ]] && json::set "$(ctx::rule::path "${name}.rule")" "dns_redirect" "true"
for ip in "${allow_ips[@]}"; do
json::append "$(ctx::rule::path "${name}.rule")" "allow_ips" "$ip"
done
for ip in "${block_ips[@]}"; do
json::append "$(ctx::rule::path "${name}.rule")" "block_ips" "$ip"
done
for port in "${block_ports[@]}"; do
json::append "$(ctx::rule::path "${name}.rule")" "block_ports" "$port"
done
log::wg_success "Rule updated: ${name}"
# Re-apply to all assigned peers
rule::reapply_all "$name"
}
| 1 | function cmd::rule::update() { |
| 2 | local name="" desc="" |
| 3 | local allow_ips=() block_ips=() block_ports=() |
| 4 | local dns_redirect="" |
| 5 | |
| 6 | while [[ $# -gt 0 ]]; do |
| 7 | case "$1" in |
| 8 | --name) name="$2"; shift 2 ;; |
| 9 | --desc) desc="$2"; shift 2 ;; |
| 10 | --allow-ip) allow_ips+=("$2"); shift 2 ;; |
| 11 | --block-ip) block_ips+=("$2"); shift 2 ;; |
| 12 | --block-port) block_ports+=("$2"); shift 2 ;; |
| 13 | --dns-redirect) dns_redirect=true; shift ;; |
| 14 | --help) cmd::rule::help; return ;; |
| 15 | *) |
| 16 | log::error "Unknown flag: $1" |
| 17 | return 1 |
| 18 | ;; |
| 19 | esac |
| 20 | done |
| 21 | |
| 22 | if [[ -z "$name" ]]; then |
| 23 | log::error "Missing required flag: --name" |
| 24 | return 1 |
| 25 | fi |
| 26 | |
| 27 | rule::require_exists "$name" || return 1 |
| 28 | |
| 29 | # Update individual fields if provided |
| 30 | [[ -n "$desc" ]] && json::set "$(ctx::rule::path "${name}.rule")" "desc" "\"$desc\"" |
| 31 | [[ -n "$dns_redirect" ]] && json::set "$(ctx::rule::path "${name}.rule")" "dns_redirect" "true" |
| 32 | |
| 33 | for ip in "${allow_ips[@]}"; do |
| 34 | json::append "$(ctx::rule::path "${name}.rule")" "allow_ips" "$ip" |
| 35 | done |
| 36 | for ip in "${block_ips[@]}"; do |
| 37 | json::append "$(ctx::rule::path "${name}.rule")" "block_ips" "$ip" |
| 38 | done |
| 39 | for port in "${block_ports[@]}"; do |
| 40 | json::append "$(ctx::rule::path "${name}.rule")" "block_ports" "$port" |
| 41 | done |
| 42 | |
| 43 | log::wg_success "Rule updated: ${name}" |
| 44 | |
| 45 | # Re-apply to all assigned peers |
| 46 | rule::reapply_all "$name" |
| 47 | } |