Create rules & templates
-
AI create this template.
Aliases, rules.<?php // Protection against direct execution outside the shell if (!function_exists('conf_mount_rw')) { require_once("config.inc"); require_once("filter.inc"); } echo "=== Starting batch pfSense rule and alias creation task ===\n"; // 1. Create a configuration backup $backup_file = backup_config(); if ($backup_file) { echo "[OK] Configuration backup successfully created: {$backup_file}\n"; } else { echo "[ERROR] Failed to create configuration backup!\n"; return; } // 2. Check if the wan interface exists $target_interface = 'wan'; if (!isset($config['interfaces'][$target_interface])) { echo "[ERROR] Interface '{$target_interface}' not found in configuration.\n"; return; } // 3. Template array for aliases and rules $rules_template = array( array( 'alias' => 'RU_IPV4', 'url' => 'https://raw.githubusercontent.com/ipverse/country-ip-blocks/master/country/ru/ipv4-aggregated.txt', 'descr' => 'Block RU source IPs' ), array( 'alias' => 'IR_IPV4', 'url' => 'https://raw.githubusercontent.com/ipverse/country-ip-blocks/master/country/ir/ipv4-aggregated.txt', 'descr' => 'Block IR source IPs' ), // Add more items here following the same structure if needed ); // Ensure arrays exist in configuration if (!isset($config['aliases']['alias'])) { $config['aliases']['alias'] = array(); } if (!isset($config['filter']['rule'])) { $config['filter']['rule'] = array(); } $added_count = 0; // 4. Loop through each item in the template foreach ($rules_template as $item) { $alias_name = $item['alias']; $alias_url = $item['url']; $alias_descr = $item['descr'] . ' (URL Table)'; $alias_type = 'urltable'; $rule_descr = $item['descr']; // Check and create/update alias $alias_found = false; foreach ($config['aliases']['alias'] as &$existing_alias) { if ($existing_alias['name'] === $alias_name) { $existing_alias['url'] = $alias_url; $existing_alias['type'] = $alias_type; $existing_alias['descr'] = $alias_descr; $alias_found = true; echo "[OK] Alias '{$alias_name}' already exists — parameters updated.\n"; break; } } unset($existing_alias); if (!$alias_found) { $new_alias = array( 'name' => $alias_name, 'type' => $alias_type, 'url' => $alias_url, 'descr' => $alias_descr ); $config['aliases']['alias'][] = $new_alias; echo "[OK] Alias '{$alias_name}' successfully added to configuration.\n"; } // Check if a rule for this interface with this source alias ALREADY exists (reliable uniqueness check) $rule_exists = false; foreach ($config['filter']['rule'] as $rule) { $rule_if = isset($rule['interface']) ? $rule['interface'] : ''; $rule_src = isset($rule['source']['address']) ? $rule['source']['address'] : ''; // Match by interface and exact source alias name if ($rule_if === $target_interface && $rule_src === $alias_name) { $rule_exists = true; break; } } if ($rule_exists) { echo "[INFO] A rule for interface '{$target_interface}' with source alias '{$alias_name}' already exists. Skipping rule creation.\n"; continue; } // Create a new block rule $new_rule = array( 'type' => 'block', 'interface' => $target_interface, 'ipprotocol' => 'inet', // 'protocol' => '', 'source' => array( 'address' => $alias_name ), 'destination' => array( 'any' => '' ), 'descr' => $rule_descr, 'created' => array( 'time' => time(), 'username' => 'pfSsh_template_script' ) ); // Add the rule to the top of the rules list array_unshift($config['filter']['rule'], $new_rule); echo "[OK] Block rule for alias '{$alias_name}' added to the top of the list.\n"; $added_count++; } // 5. Save configuration if any changes were made write_config("Added/updated URL Table aliases and block rules via batch template script"); echo "[OK] Configuration successfully saved.\n"; // 6. Apply firewall rules filter_configure(); echo "[OK] Firewall rules successfully applied.\n"; echo "=== Task completed successfully! ===\n"; ?>
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login
Copyright 2026 Rubicon Communications LLC (Netgate). All rights reserved.
Privacy Policy · Cookie Policy
Privacy Policy · Cookie Policy