Auto update wireguard tunnels when disabling/enabling bridging

This commit is contained in:
bergware
2023-08-15 14:16:33 +02:00
parent 08b216556c
commit 29b96374f2
2 changed files with 54 additions and 36 deletions

View File

@@ -1,7 +1,7 @@
#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2018, Lime Technology
* Copyright 2012-2018, Bergware International.
/* Copyright 2005-2023, Lime Technology
* Copyright 2012-2023, Bergware International.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2,
@@ -17,6 +17,21 @@ $run = $set != 'none';
$ini = parse_ini_file('/var/local/emhttp/network.ini',true); ksort($ini,SORT_NATURAL);
$cfg = '/boot/config/network.cfg';
function update_wireguard($ifname) {
if (!in_array($ifname,['br0','bond0','eth0'])) return;
$nic = file_exists('/sys/class/net/br0') ? 'br0' : (file_exists('/sys/class/net/bond0') ? 'bond0' : 'eth0');
exec("wg show interfaces",$active);
foreach (glob("/etc/wireguard/*.conf",GLOB_NOSORT) as $wg) {
$vtun = basename($wg,'.conf');
// interface has changed?
if (exec("grep -Pom1 ' dev $nic ' $wg")=='') {
exec("logger -t netconfig 'updated wireguard $vtun configuration'");
exec("sed -ri 's/ dev (br0|bond0|eth0) / dev $nic /' $wg");
}
// restart active wireguard tunnels
if (in_array($vtun,$active)) exec("wg-quick down $vtun; sleep 1; wg-quick up $vtun");
}
}
function ifname($name) {
global $old;
if (!$name) return;
@@ -113,6 +128,7 @@ file_put_contents($cfg,implode("\r\n",$new)."\r\n");
if ($run) {
exec("/etc/rc.d/rc.inet1 {$ifname}_start >/dev/null 2>&1");
exec("/usr/local/sbin/create_network_ini $ifname >/dev/null 2>&1 &");
update_wireguard($ifname);
}
exit(0);
?>