nuno bu gisti düzenledi 1 month ago. Düzenlemeye git
1 file changed, 46 insertions
gistfile1.txt(dosya oluşturuldu)
| @@ -0,0 +1,46 @@ | |||
| 1 | + | function cmd::rule::add() { | |
| 2 | + | local name="" desc="" | |
| 3 | + | local allow_ips=() block_ips=() block_ports=() | |
| 4 | + | local dns_redirect=false | |
| 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 | + | if rule::exists "$name"; then | |
| 28 | + | log::error "Rule already exists: ${name}" | |
| 29 | + | return 1 | |
| 30 | + | fi | |
| 31 | + | ||
| 32 | + | local rule_file | |
| 33 | + | rule_file="$(ctx::rule::path "${name}.rule")" | |
| 34 | + | ||
| 35 | + | local allow_str block_str port_str | |
| 36 | + | ||
| 37 | + | allow_str=$(IFS=','; echo "${allow_ips[*]}") | |
| 38 | + | block_str=$(IFS=','; echo "${block_ips[*]}") | |
| 39 | + | port_str=$(IFS=','; echo "${block_ports[*]}") | |
| 40 | + | ||
| 41 | + | json::create_rule "$rule_file" "$name" "$desc" \ | |
| 42 | + | "$($dns_redirect && echo true || echo false)" \ | |
| 43 | + | "$allow_str" "$block_str" "$port_str" || return 1 | |
| 44 | + | ||
| 45 | + | log::wg_success "Rule created: ${name}" | |
| 46 | + | } | |
Daha yeni
Daha eski