Files
opnsense-carp-dhcp/opnsense-carp-dhcp-backup-gateway.php

108 lines
3.8 KiB
PHP

#!/usr/local/bin/php
<?php
require_once("config.inc");
require_once("interfaces.inc");
require_once("util.inc");
require_once("filter.inc");
require_once("util.inc");
require_once("system.inc");
require_once('rrd.inc');
require_once('plugins.inc.d/webgui.inc');
#############
# Variables #
#############
$wanInterface = 'wan';
$gatewayName = 'WAN_GW';
###################
# Start of Script #
###################
$randomString = getRandString(5);
function getRandString($n) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $n; $i++) {
$index = rand(0, strlen($characters) - 1);
$randomString .= $characters[$index];
}
return $randomString;
}
function write_config_and_restart_services ($interface) {
write_config();
system_hosts_generate(true);
system_resolvconf_generate(true);
interface_bring_down($interface);
interface_configure(true, $interface, true);
plugins_configure('monitor', true);
filter_configure_sync(true);
webgui_configure_do(true);
rrd_configure(true);
}
function echo_write_log ($message) {
global $randomString;
$dateTime = date("Y-m-d H:i:s");
$folder = '/var/log/opnsense_carp_dhcp';
$message = $dateTime . " - " . $randomString . " - " . $message . "\n";
if (!is_dir($folder)) {
mkdir($folder);
}
file_put_contents("$folder/log_".date("Ymd").".log", $message, FILE_APPEND);
echo $message;
}
echo_write_log("Start of Script " . $argv[0]);
echo_write_log("WAN Interface: " . $wanInterface);
echo_write_log("Name of static WAN Gateway: " . $gatewayName);
$intf_details = legacy_interfaces_details();
$intf = get_real_interface($wanInterface);
echo_write_log("Real WAN Interface: " . $intf);
echo_write_log("Looping through VIP CARP Interfaces...");
# loop through CARP addresses and update the wan ip address
foreach ($config['virtualip']['vip'] as $i => $vip) {
if ($vip['interface'] == $wanInterface) {
echo_write_log("VIP CARP WAN Interface and vhid: " . convert_friendly_interface_to_friendly_descr($vip['interface']) . "@{$vip['vhid']}");
$carpStatus = $intf_details[$intf]['carp'][$vip['vhid']]['status'];
echo_write_log("VIP CARP WAN Status: " . $carpStatus);
if ($carpStatus == "BACKUP") {
$description = $config['virtualip']['vip'][$i]['descr'];
echo_write_log("Description from VIP WAN: " . $description);
# VIP WAN (GW: $gatewayIP)
$gatewayIP = trim(substr(explode(":", $description)[1], 0, -1));
echo_write_log("CARP Gateway IP from Description: " . $gatewayIP);
if (filter_var($gatewayIP, FILTER_VALIDATE_IP)) {
echo_write_log("Looping through Gateways...");
foreach ($config['gateways']['gateway_item'] as $i => $gateway) {
if ($gateway['name'] == $gatewayName) {
echo_write_log("Gateway Name: " . $gateway['name']);
echo_write_log("Current Gateway IP: " . $gateway['gateway']);
if ($gateway['gateway'] != $gatewayIP) {
echo_write_log("Updating Gateway IPs");
$config['gateways']['gateway_item'][$i]['gateway'] = $gatewayIP;
write_config_and_restart_services($wanInterface);
} else {
echo_write_log("Gateway IPs are the same, not updating");
}
}
}
} else {
echo_write_log("Gateway IP is not a valid IP address");
}
} else {
echo_write_log("Router is currently CARP master, not updating");
}
}
}
echo_write_log("End of Script " . $argv[0]);