mirror of
https://github.com/unraid/webgui.git
synced 2026-02-09 10:29:09 -06:00
Merge pull request #1075 from bergware/master
Wireguard: fix initial network creation
This commit is contained in:
@@ -34,7 +34,7 @@ function mask2cidr($mask) {
|
||||
$base = ip2long('255.255.255.255');
|
||||
return 32-log(($long ^ $base)+1,2);
|
||||
}
|
||||
function thisnet($ethX='eth0') {
|
||||
function thisNet($ethX='eth0') {
|
||||
extract(parse_ini_file('state/network.ini',true));
|
||||
$net = long2ip(ip2long($$ethX['IPADDR:0']) & ip2long($$ethX['NETMASK:0'])).'/'.mask2cidr($$ethX['NETMASK:0']);
|
||||
return [$net,$$ethX['GATEWAY:0']];
|
||||
@@ -58,8 +58,13 @@ function ipfilter(&$list) {
|
||||
function host($ip) {
|
||||
return strpos($ip,'/')!==false ? $ip : (ipv4($ip) ? "$ip/32" : "$ip/128");
|
||||
}
|
||||
function noNet($network) {
|
||||
return empty(exec("ip rule|grep -Pom1 'from $network'"));
|
||||
function isNet($network) {
|
||||
return !empty(exec("ip rule|grep -Pom1 'from $network'"));
|
||||
}
|
||||
function newNet($vtun) {
|
||||
global $dockernet;
|
||||
$i = substr($vtun,2)+200;
|
||||
return [$i,"$dockernet.$i.0/24"];
|
||||
}
|
||||
function wgState($vtun,$state,$type=0) {
|
||||
global $t1,$etc;
|
||||
@@ -92,34 +97,32 @@ function dockerNet($vtun) {
|
||||
return empty(exec("docker network ls --filter name='$vtun' --format='{{.Name}}'"));
|
||||
}
|
||||
function addDocker($vtun) {
|
||||
global $dockerd,$dockernet;
|
||||
global $dockerd;
|
||||
$error = false;
|
||||
[$index,$network] = newNet($vtun);
|
||||
if ($dockerd && dockerNet($vtun)) {
|
||||
$index = substr($vtun,2)+200;
|
||||
$network = "$dockernet.$index.0/24";
|
||||
exec("docker network create $vtun --subnet=$network 2>/dev/null");
|
||||
$error = dockerNet($vtun);
|
||||
if (!$error && noNet($network)) {
|
||||
[$thisnet,$gateway] = thisnet();
|
||||
exec("ip -4 rule add from $network table $index");
|
||||
exec("ip -4 route add unreachable default table $index");
|
||||
exec("ip -4 route add $thisnet via $gateway table $index");
|
||||
}
|
||||
}
|
||||
if (!$error && !isNet($network)) {
|
||||
[$thisnet,$gateway] = thisNet();
|
||||
exec("ip -4 rule add from $network table $index");
|
||||
exec("ip -4 route add unreachable default table $index");
|
||||
exec("ip -4 route add $thisnet via $gateway table $index");
|
||||
}
|
||||
return $error;
|
||||
}
|
||||
function delDocker($vtun) {
|
||||
global $dockerd,$dockernet;
|
||||
global $dockerd;
|
||||
$error = false;
|
||||
[$index,$network] = newNet($vtun);
|
||||
if ($dockerd && !dockerNet($vtun)) {
|
||||
$index = substr($vtun,2)+200;
|
||||
$network = "$dockernet.$index.0/24";
|
||||
exec("docker network rm $vtun 2>/dev/null");
|
||||
$error = !dockerNet($vtun);
|
||||
if (!$error && exec("ip rule|grep -Pom1 'from $network'")) {
|
||||
exec("ip -4 route flush table $index");
|
||||
exec("ip -4 rule del from $network table $index");
|
||||
}
|
||||
}
|
||||
if (!$error && isNet($network)) {
|
||||
exec("ip -4 route flush table $index");
|
||||
exec("ip -4 rule del from $network table $index");
|
||||
}
|
||||
return $error;
|
||||
}
|
||||
@@ -210,7 +213,7 @@ function createIPs($list) {
|
||||
return implode(', ',array_map('host',array_filter(array_map('trim',explode(',',$list)))));
|
||||
}
|
||||
function parseInput($vtun,&$input,&$x) {
|
||||
global $conf,$user,$var,$default,$default6,$vpn,$dockernet;
|
||||
global $conf,$user,$var,$default,$default6,$vpn;
|
||||
$section = 0; $addPeer = false;
|
||||
foreach ($input as $key => $value) {
|
||||
if ($key[0]=='#') continue;
|
||||
@@ -218,9 +221,8 @@ function parseInput($vtun,&$input,&$x) {
|
||||
if ($i != $section) {
|
||||
if ($section==0) {
|
||||
// add WG routing for docker containers. Only IPv4 supported
|
||||
$index = substr($vtun,2)+200;
|
||||
$network = "$dockernet.$index.0/24";
|
||||
[$thisnet,$gateway] = thisnet();
|
||||
[$index,$network] = newNet($vtun);
|
||||
[$thisnet,$gateway] = thisNet();
|
||||
$conf[] = "PostUp=ip -4 route flush table $index";
|
||||
$conf[] = "PostUp=ip -4 route add default via $tunip table $index";
|
||||
$conf[] = "PostUp=ip -4 route add $thisnet via $gateway table $index";
|
||||
@@ -380,9 +382,11 @@ case 'toggle':
|
||||
echo status($vtun) ? 1 : 0;
|
||||
break;
|
||||
case 'start':
|
||||
$index = substr($vtun,2)+200;
|
||||
$network = "$dockernet.$index.0/24";
|
||||
if (noNet($network)) exec("ip -4 rule add from $network table $index");
|
||||
[$index,$network] = newNet($vtun);
|
||||
if (!isNet($network)) {
|
||||
exec("ip -4 rule add from $network table $index");
|
||||
exec("ip -4 route add unreachable default table $index");
|
||||
}
|
||||
wgState($vtun,'up',$_POST['#type']);
|
||||
echo status($vtun) ? 0 : 1;
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user