From d3e842f6db572f529ee0f28a317152967235b200 Mon Sep 17 00:00:00 2001 From: billz Date: Thu, 26 Nov 2020 17:15:42 +0000 Subject: [PATCH 01/11] Update ajax w/ dhcpcd settings --- ajax/networking/get_netcfg.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/ajax/networking/get_netcfg.php b/ajax/networking/get_netcfg.php index c2710011..650c9fef 100644 --- a/ajax/networking/get_netcfg.php +++ b/ajax/networking/get_netcfg.php @@ -6,10 +6,10 @@ require_once '../../includes/config.php'; $interface = $_GET['iface']; if (isset($interface)) { + // fetch dnsmasq.conf settings for interface exec('cat '. RASPI_DNSMASQ_PREFIX.$interface.'.conf', $return); $conf = ParseConfig($return); - // populate data $dhcpdata['DHCPEnabled'] = empty($conf) ? false : true; $arrRange = explode(",", $conf['dhcp-range']); $dhcpdata['RangeStart'] = $arrRange[0]; @@ -20,12 +20,11 @@ if (isset($interface)) { $dhcpHost = empty($dhcpHost) ? [] : $dhcpHost; $dhcpdata['dhcpHost'] = is_array($dhcpHost) ? $dhcpHost : [ $dhcpHost ]; $upstreamServers = is_array($conf['server']) ? $conf['server'] : [ $conf['server'] ]; - $dhcpdata['upstreamServers'] = array_filter($upstreamServers); $dhcpdata['upstreamServersEnabled'] = empty($conf['server']) ? false: true; + $dhcpdata['upstreamServers'] = array_filter($upstreamServers); preg_match('/([0-9]*)([a-z])/i', $dhcpdata['leaseTime'], $arrRangeLeaseTime); $dhcpdata['leaseTime'] = $arrRangeLeaseTime[1]; $dhcpdata['leaseTimeInterval'] = $arrRangeLeaseTime[2]; - if (isset($conf['dhcp-option'])) { $arrDns = explode(",", $conf['dhcp-option']); if ($arrDns[0] == '6') { @@ -37,5 +36,20 @@ if (isset($interface)) { } } } + + // fetch dhcpcd.conf settings for interface + $conf = file_get_contents(RASPI_DHCPCD_CONFIG); + preg_match('/^#\sRaspAP\s'.$interface.'.*?(?=\s*^\s*$)/ms', $conf, $matched); + preg_match('/metric\s(\d*)/', $matched[0], $metric); + preg_match('/static\sip_address=(.*)/', $matched[0], $static_ip); + preg_match('/static\srouters=(.*)/', $matched[0], $static_routers); + preg_match('/static\sdomain_name_servers=(.*)/', $matched[0], $static_dns); + preg_match('/fallback\sstatic_'.$interface.'/', $matched[0], $fallback); + $dhcpdata['Metric'] = $metric[1]; + $dhcpdata['StaticIP'] = $static_ip[1]; + $dhcpdata['StaticRouters'] = $static_routers[1]; + $dhcpdata['StaticDNS'] = $static_dns[1]; + $dhcpdata['FallbackEnabled'] = empty($fallback) ? false: true; + echo json_encode($dhcpdata); } From 6d331c3ebdc21108a919c518043fea84b7061f37 Mon Sep 17 00:00:00 2001 From: billz Date: Thu, 26 Nov 2020 17:16:54 +0000 Subject: [PATCH 02/11] Add static IP fields, fallback, toggle state --- app/js/custom.js | 11 +++++++ templates/dhcp/general.php | 63 +++++++++++++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/app/js/custom.js b/app/js/custom.js index de05908d..b8876be5 100644 --- a/app/js/custom.js +++ b/app/js/custom.js @@ -245,6 +245,9 @@ function loadInterfaceDHCPSelect() { $.get('ajax/networking/get_netcfg.php?iface='+iface,function(data){ jsonData = JSON.parse(data); $('#dhcp-iface')[0].checked = jsonData.DHCPEnabled; + $('#txtipaddress').val(jsonData.StaticIP); + $('#txtgateway').val(jsonData.StaticRouters); + $('#chkfallback')[0].checked = jsonData.FallbackEnabled; $('#txtrangestart').val(jsonData.RangeStart); $('#txtrangeend').val(jsonData.RangeEnd); $('#txtrangeleasetime').val(jsonData.leaseTime); @@ -253,6 +256,14 @@ function loadInterfaceDHCPSelect() { $('#cbxrangeleasetimeunits').val(jsonData.leaseTimeInterval); $('#no-resolv')[0].checked = jsonData.upstreamServersEnabled; $('#cbxdhcpupstreamserver').val(jsonData.upstreamServers[0]); + $('#txtmetric').val(jsonData.Metric); + if (jsonData.StaticIP !== null && jsonData.StaticIP !== '') { + $('#chkstatic').closest('.btn').button('toggle'); + $('#chkstatic').closest('.btn').blur(); + } else { + $('#chkdhcp').closest('.btn').button('toggle'); + $('#chkdhcp').closest('.btn').blur(); + } }); } diff --git a/templates/dhcp/general.php b/templates/dhcp/general.php index 60f5b228..178a7a95 100644 --- a/templates/dhcp/general.php +++ b/templates/dhcp/general.php @@ -7,6 +7,48 @@ +
+
+
+
+ + +
+
+
+ +
+
+
+ + +
+

+ +

+
+
+ +
Static IP options
+
+
+ + +
+
+ +
+
+ + +
+
+ +
DHCP options
@@ -16,30 +58,30 @@

- +

- +
- +
- +
- +
@@ -55,14 +97,21 @@
- +
- + +
+
+ +
+
+ +
From 827f37385f46fa444a4758643bb38088027999b7 Mon Sep 17 00:00:00 2001 From: billz Date: Thu, 26 Nov 2020 17:18:04 +0000 Subject: [PATCH 03/11] Decouple default settings from wlan0 cfg --- config/dhcpcd.conf | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config/dhcpcd.conf b/config/dhcpcd.conf index 13fb76fd..994861b4 100644 --- a/config/dhcpcd.conf +++ b/config/dhcpcd.conf @@ -1,4 +1,4 @@ -# RaspAP wlan0 configuration +# RaspAP default configuration hostname clientid persistent @@ -9,6 +9,8 @@ option ntp_servers require dhcp_server_identifier slaac private nohook lookup-hostname + +# RaspAP wlan0 configuration interface wlan0 static ip_address=10.3.141.1/24 static routers=10.3.141.1 From 5045cbfd33035cd83c6bd8b765c7ed6d348101f3 Mon Sep 17 00:00:00 2001 From: billz Date: Thu, 26 Nov 2020 17:20:12 +0000 Subject: [PATCH 04/11] Fixup dhcp cfg save/update --- includes/dhcp.php | 95 +++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 44 deletions(-) diff --git a/includes/dhcp.php b/includes/dhcp.php index bd2a4a81..28a36447 100755 --- a/includes/dhcp.php +++ b/includes/dhcp.php @@ -69,9 +69,9 @@ function SaveDHCPConfig($status) $return = 1; if (($_POST['dhcp-iface'] == "1")) { - $errors = ValidateDHCPInput(); - if (empty($errors)) { - $return = UpdateDnsmasqCfg($iface,$status); + $errors = ValidateDHCPInput(); + if (empty($errors)) { + $return = UpdateDHCPConfig($iface,$status); } else { $status->addMessage($errors, 'danger'); } @@ -79,12 +79,12 @@ function SaveDHCPConfig($status) $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger'); return false; } - $return = UpdateDHCPCfg($iface,$status); + $return = UpdateDnsmasqConfig($iface,$status); // process disable dhcp option } elseif (($_POST['dhcp-iface'] == "0") && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) { // remove dhcp conf for selected interface - $return = RemoveDHCPCfg($iface,$status); + $return = RemoveDHCPConfig($iface,$status); } if ($return == 0) { @@ -105,14 +105,16 @@ function ValidateDHCPInput() ) { $errors .= _('Invalid interface name.').'
'.PHP_EOL; } - if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeStart']) - && !empty($_POST['RangeStart']) - ) { // allow ''/null ? + if (!filter_var($_POST['StaticIP'], FILTER_VALIDATE_IP) && !empty($_POST['StaticIP'])) { + $errors .= _('Invalid static IP address.').'
'.PHP_EOL; + } + if (!filter_var($_POST['DefaultGateway'], FILTER_VALIDATE_IP)) { + $errors .= _('Invalid default gateway.').'
'.PHP_EOL; + } + if (!filter_var($_POST['RangeStart'], FILTER_VALIDATE_IP) && !empty($_POST['RangeStart'])) { $errors .= _('Invalid DHCP range start.').'
'.PHP_EOL; } - if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeEnd']) - && !empty($_POST['RangeEnd']) - ) { // allow ''/null ? + if (!filter_var($_POST['RangeEnd'], FILTER_VALIDATE_IP) && !empty($_POST['RangeEnd'])) { $errors .= _('Invalid DHCP range end.').'
'.PHP_EOL; } if (!ctype_digit($_POST['RangeLeaseTime']) && $_POST['RangeLeaseTimeUnits'] !== 'infinite') { @@ -121,10 +123,13 @@ function ValidateDHCPInput() if (!in_array($_POST['RangeLeaseTimeUnits'], array('m', 'h', 'd', 'infinite'))) { $errors .= _('Unknown DHCP lease time unit.').'
'.PHP_EOL; } + if ($_POST['Metric'] !== '' && !ctype_digit($_POST['Metric'])) { + $errors .= _('Invalid metric value, not a number.').'
'.PHP_EOL; + } return $errors; } -function UpdateDnsmasqCfg($iface,$status) +function UpdateDnsmasqConfig($iface,$status) { $config = 'interface='.$iface.PHP_EOL. 'dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd']. @@ -173,45 +178,46 @@ function UpdateDnsmasqCfg($iface,$status) return $result; } -function UpdateDHCPCfg($iface,$status) +function UpdateDHCPConfig($iface,$status) { - $net_cfg = RASPI_CONFIG_NETWORKING.'/'.$iface.'.ini'; - if (!file_exists($net_cfg) || filesize($net_cfg) ==0 ) { - $status->addMessage('Static IP address for '.$iface.' not found.', 'danger'); - $status->addMessage('Configure this interface in Networking > '.$iface.'.', 'danger'); - $return = 1; - } else { - $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); - if (!preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) { - // set dhcp values from ini - $iface_cfg = parse_ini_file($net_cfg, false, INI_SCANNER_RAW); - $ip_address = $iface_cfg['ip_address']; - $domain_name_server = ($iface_cfg['domain_name_server'] =='') ? '1.1.1.1 8.8.8.8' : $iface_cfg['domain_name_server']; + $cfg[] = '# RaspAP '.$iface.' configuration'; + $cfg[] = 'interface '.$iface; + $cfg[] = 'static ip_address='.$_POST['StaticIP']; + if (isset($_POST['DefaultGateway'])) { + $cfg[] = 'static routers='.$_POST['DefaultGateway']; + } + if ($_POST['DNS1'] !== '' || $_POST['DNS2'] !== '') { + $cfg[] = 'static domain_name_server='.$_POST['DNS1'].' '.$_POST['DNS2']; + } + if ($_POST['Metric'] !== '') { + $cfg[] = 'metric '.$_POST['Metric']; + } + if ($_POST['Fallback'] == 1) { + $cfg[] = 'fallback static_'.$iface; + } + $cfg[] = PHP_EOL; + $cfg = join(PHP_EOL, $cfg); - // append interface config to dhcpcd.conf - $cfg = $dhcp_conf; - $cfg[] = '# RaspAP '.$iface.' configuration'; - $cfg[] = 'interface '.$iface; - $cfg[] = 'static ip_address='.$ip_address; - $cfg[] = 'static domain_name_server='.$domain_name_server; - $cfg[] = PHP_EOL; - $cfg = join(PHP_EOL, $cfg); - $dhcp_cfg .= $cfg; - file_put_contents("/tmp/dhcpddata", $dhcp_cfg); - system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result); - $status->addMessage('DHCP configuration for '.$iface.' added.', 'success'); - } else { - $status->addMessage('DHCP for '.$iface.' already enabled.', 'success'); - } + $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); + + if (!preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) { + $dhcp_cfg .= $cfg; + file_put_contents("/tmp/dhcpddata", $dhcp_cfg); + system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result); + $status->addMessage('DHCP configuration for '.$iface.' added.', 'success'); + } else { + $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)/ms', $cfg, $dhcp_cfg, 1); + file_put_contents("/tmp/dhcpddata", rtrim($dhcp_cfg).PHP_EOL); + $status->addMessage('DHCP configuration for '.$iface.' updated.', 'success'); } return $result; } -function RemoveDHCPCfg($iface,$status) +function RemoveDHCPConfig($iface,$status) { $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); - $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'.*\n(.*\n){3}/m', '', $dhcp_cfg); - file_put_contents("/tmp/dhcpddata", rtrim($dhcp_cfg).PHP_EOL); + $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)/ms', '', $dhcp_cfg, 1); + file_put_contents("/tmp/dhcpddata", rtrim($dhcp_cfg).PHP_EOL.PHP_EOL); system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result); if ($result == 0) { $status->addMessage('DHCP configuration for '.$iface.' removed.', 'success'); @@ -219,7 +225,7 @@ function RemoveDHCPCfg($iface,$status) $status->addMessage('Failed to remove DHCP configuration for '.$iface.'.', 'danger'); return $result; } - // remove dnsmasq eth0 conf + // remove dnsmasq conf system('sudo rm '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $result); if ($result == 0) { $status->addMessage('Dnsmasq configuration for '.$iface.' removed.', 'success'); @@ -229,3 +235,4 @@ function RemoveDHCPCfg($iface,$status) return $result; } + From 57174245ad615da0c2c95307466d335171ffa358 Mon Sep 17 00:00:00 2001 From: billz Date: Fri, 27 Nov 2020 16:25:46 +0000 Subject: [PATCH 05/11] Updated w/ set toggle states --- app/js/custom.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/js/custom.js b/app/js/custom.js index b8876be5..25f121e5 100644 --- a/app/js/custom.js +++ b/app/js/custom.js @@ -259,14 +259,23 @@ function loadInterfaceDHCPSelect() { $('#txtmetric').val(jsonData.Metric); if (jsonData.StaticIP !== null && jsonData.StaticIP !== '') { $('#chkstatic').closest('.btn').button('toggle'); - $('#chkstatic').closest('.btn').blur(); + $('#chkstatic').closest('.btn').button('toggle').blur(); + $('#chkstatic').blur(); + $('#chkfallback').prop('disabled', true); } else { $('#chkdhcp').closest('.btn').button('toggle'); - $('#chkdhcp').closest('.btn').blur(); + $('#chkdhcp').closest('.btn').button('toggle').blur(); + $('#chkdhcp').blur(); + $('#chkfallback').prop('disabled', false); } }); } +function setDHCPToggles(state) { + $('#chkfallback').prop('disabled', state); + $('#dhcp-iface').prop('disabled', !state); +} + function loadChannel() { $.get('ajax/networking/get_channel.php',function(data){ jsonData = JSON.parse(data); From f8bc24ce6fa5a67ec5d568af66f5bf7cb8bd54ea Mon Sep 17 00:00:00 2001 From: billz Date: Fri, 27 Nov 2020 16:26:25 +0000 Subject: [PATCH 06/11] Template fixes --- templates/dhcp/general.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/templates/dhcp/general.php b/templates/dhcp/general.php index 178a7a95..484c2690 100644 --- a/templates/dhcp/general.php +++ b/templates/dhcp/general.php @@ -10,12 +10,12 @@
-
-
@@ -25,7 +25,7 @@
- +

@@ -52,9 +52,8 @@

-
- aria-describedby="dhcp-iface-description"> +

From 2ff8f6f179161b29c05845fb9a6046190ee6474f Mon Sep 17 00:00:00 2001 From: billz Date: Fri, 27 Nov 2020 16:28:37 +0000 Subject: [PATCH 07/11] Bugfix: add/update/delete config --- ajax/networking/get_netcfg.php | 2 +- includes/dhcp.php | 77 ++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/ajax/networking/get_netcfg.php b/ajax/networking/get_netcfg.php index 650c9fef..10d86ad7 100644 --- a/ajax/networking/get_netcfg.php +++ b/ajax/networking/get_netcfg.php @@ -39,7 +39,7 @@ if (isset($interface)) { // fetch dhcpcd.conf settings for interface $conf = file_get_contents(RASPI_DHCPCD_CONFIG); - preg_match('/^#\sRaspAP\s'.$interface.'.*?(?=\s*^\s*$)/ms', $conf, $matched); + preg_match('/^#\sRaspAP\s'.$interface.'\s.*?(?=\s*+$)/ms', $conf, $matched); preg_match('/metric\s(\d*)/', $matched[0], $metric); preg_match('/static\sip_address=(.*)/', $matched[0], $static_ip); preg_match('/static\srouters=(.*)/', $matched[0], $static_routers); diff --git a/includes/dhcp.php b/includes/dhcp.php index 28a36447..09f56603 100755 --- a/includes/dhcp.php +++ b/includes/dhcp.php @@ -68,7 +68,11 @@ function SaveDHCPConfig($status) $iface = $_POST['interface']; $return = 1; - if (($_POST['dhcp-iface'] == "1")) { + // handle disable dhcp option + if (!isset($_POST['dhcp-iface']) && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) { + // remove dhcp conf for selected interface + $return = RemoveDHCPConfig($iface,$status); + } else { $errors = ValidateDHCPInput(); if (empty($errors)) { $return = UpdateDHCPConfig($iface,$status); @@ -79,21 +83,19 @@ function SaveDHCPConfig($status) $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger'); return false; } - $return = UpdateDnsmasqConfig($iface,$status); - // process disable dhcp option - } elseif (($_POST['dhcp-iface'] == "0") && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) { - // remove dhcp conf for selected interface - $return = RemoveDHCPConfig($iface,$status); - } + if (($_POST['dhcp-iface'] == "1")) { + $return = UpdateDnsmasqConfig($iface,$status); + } - if ($return == 0) { - $status->addMessage('Dnsmasq configuration updated successfully.', 'success'); - } else { - $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger'); - return false; + if ($return == 0) { + $status->addMessage('Dnsmasq configuration updated successfully.', 'success'); + } else { + $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger'); + return false; + } + return true; } - return true; } function ValidateDHCPInput() @@ -111,20 +113,22 @@ function ValidateDHCPInput() if (!filter_var($_POST['DefaultGateway'], FILTER_VALIDATE_IP)) { $errors .= _('Invalid default gateway.').'
'.PHP_EOL; } - if (!filter_var($_POST['RangeStart'], FILTER_VALIDATE_IP) && !empty($_POST['RangeStart'])) { - $errors .= _('Invalid DHCP range start.').'
'.PHP_EOL; - } - if (!filter_var($_POST['RangeEnd'], FILTER_VALIDATE_IP) && !empty($_POST['RangeEnd'])) { - $errors .= _('Invalid DHCP range end.').'
'.PHP_EOL; - } - if (!ctype_digit($_POST['RangeLeaseTime']) && $_POST['RangeLeaseTimeUnits'] !== 'infinite') { - $errors .= _('Invalid DHCP lease time, not a number.').'
'.PHP_EOL; - } - if (!in_array($_POST['RangeLeaseTimeUnits'], array('m', 'h', 'd', 'infinite'))) { - $errors .= _('Unknown DHCP lease time unit.').'
'.PHP_EOL; - } - if ($_POST['Metric'] !== '' && !ctype_digit($_POST['Metric'])) { - $errors .= _('Invalid metric value, not a number.').'
'.PHP_EOL; + if (($_POST['dhcp-iface'] == "1")) { + if (!filter_var($_POST['RangeStart'], FILTER_VALIDATE_IP) && !empty($_POST['RangeStart'])) { + $errors .= _('Invalid DHCP range start.').'
'.PHP_EOL; + } + if (!filter_var($_POST['RangeEnd'], FILTER_VALIDATE_IP) && !empty($_POST['RangeEnd'])) { + $errors .= _('Invalid DHCP range end.').'
'.PHP_EOL; + } + if (!ctype_digit($_POST['RangeLeaseTime']) && $_POST['RangeLeaseTimeUnits'] !== 'infinite') { + $errors .= _('Invalid DHCP lease time, not a number.').'
'.PHP_EOL; + } + if (!in_array($_POST['RangeLeaseTimeUnits'], array('m', 'h', 'd', 'infinite'))) { + $errors .= _('Unknown DHCP lease time unit.').'
'.PHP_EOL; + } + if ($_POST['Metric'] !== '' && !ctype_digit($_POST['Metric'])) { + $errors .= _('Invalid metric value, not a number.').'
'.PHP_EOL; + } } return $errors; } @@ -195,29 +199,32 @@ function UpdateDHCPConfig($iface,$status) if ($_POST['Fallback'] == 1) { $cfg[] = 'fallback static_'.$iface; } - $cfg[] = PHP_EOL; - $cfg = join(PHP_EOL, $cfg); - $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); + if (!preg_match('/^$\s*\z/m', $dhcp_cfg) && !preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) { + echo '===== no ending newline found ====
'; + } if (!preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) { + $cfg[] = PHP_EOL; + $cfg = join(PHP_EOL, $cfg); $dhcp_cfg .= $cfg; - file_put_contents("/tmp/dhcpddata", $dhcp_cfg); - system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result); $status->addMessage('DHCP configuration for '.$iface.' added.', 'success'); } else { + $cfg = join(PHP_EOL, $cfg); $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)/ms', $cfg, $dhcp_cfg, 1); - file_put_contents("/tmp/dhcpddata", rtrim($dhcp_cfg).PHP_EOL); $status->addMessage('DHCP configuration for '.$iface.' updated.', 'success'); } + file_put_contents("/tmp/dhcpddata", $dhcp_cfg); + system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result); + return $result; } function RemoveDHCPConfig($iface,$status) { $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); - $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)/ms', '', $dhcp_cfg, 1); - file_put_contents("/tmp/dhcpddata", rtrim($dhcp_cfg).PHP_EOL.PHP_EOL); + $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'\s.*?(?=\s*^\s*$)([\s]+)/ms', '', $dhcp_cfg, 1); + file_put_contents("/tmp/dhcpddata", $dhcp_cfg); system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result); if ($result == 0) { $status->addMessage('DHCP configuration for '.$iface.' removed.', 'success'); From 5cc7794cb0be2598079b1484434ded01d0e97752 Mon Sep 17 00:00:00 2001 From: billz Date: Mon, 30 Nov 2020 12:09:22 +0000 Subject: [PATCH 08/11] Update fallback static profile, subnet mask --- includes/dhcp.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/includes/dhcp.php b/includes/dhcp.php index 09f56603..fb2e4709 100755 --- a/includes/dhcp.php +++ b/includes/dhcp.php @@ -110,8 +110,13 @@ function ValidateDHCPInput() if (!filter_var($_POST['StaticIP'], FILTER_VALIDATE_IP) && !empty($_POST['StaticIP'])) { $errors .= _('Invalid static IP address.').'
'.PHP_EOL; } - if (!filter_var($_POST['DefaultGateway'], FILTER_VALIDATE_IP)) { + if (!filter_var($_POST['SubnetMask'], FILTER_VALIDATE_IP) && !empty($_POST['SubnetMask'])) { + $errors .= _('Invalid subnet mask.').'
'.PHP_EOL; + } + if (!filter_var($_POST['DefaultGateway'], FILTER_VALIDATE_IP) && !empty($_POST['DefaultGateway'])) { $errors .= _('Invalid default gateway.').'
'.PHP_EOL; + var_dump($_POST['DefaultGateway']); + die(); } if (($_POST['dhcp-iface'] == "1")) { if (!filter_var($_POST['RangeStart'], FILTER_VALIDATE_IP) && !empty($_POST['RangeStart'])) { @@ -186,7 +191,9 @@ function UpdateDHCPConfig($iface,$status) { $cfg[] = '# RaspAP '.$iface.' configuration'; $cfg[] = 'interface '.$iface; - $cfg[] = 'static ip_address='.$_POST['StaticIP']; + if (isset($_POST['StaticIP'])) { + $cfg[] = 'static ip_address='.$_POST['StaticIP'].'/'.mask2cidr($_POST['SubnetMask']); + } if (isset($_POST['DefaultGateway'])) { $cfg[] = 'static routers='.$_POST['DefaultGateway']; } @@ -197,7 +204,8 @@ function UpdateDHCPConfig($iface,$status) $cfg[] = 'metric '.$_POST['Metric']; } if ($_POST['Fallback'] == 1) { - $cfg[] = 'fallback static_'.$iface; + $cfg[] = 'profile static_'.$iface; + $cfg[] = 'fallback static_'.$iface; } $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG); if (!preg_match('/^$\s*\z/m', $dhcp_cfg) && !preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) { From 5b1325803a2225ba6a49cb374c47c113b63d1773 Mon Sep 17 00:00:00 2001 From: billz Date: Mon, 30 Nov 2020 12:09:56 +0000 Subject: [PATCH 09/11] Added cidr2mask() --- includes/functions.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/includes/functions.php b/includes/functions.php index 1304d61f..9ed854e7 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -1,6 +1,12 @@ Date: Mon, 30 Nov 2020 12:10:44 +0000 Subject: [PATCH 10/11] Added subnet mask display/edit --- ajax/networking/get_netcfg.php | 3 ++- app/js/custom.js | 6 +++++- templates/dhcp/general.php | 7 +++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ajax/networking/get_netcfg.php b/ajax/networking/get_netcfg.php index 10d86ad7..cd36a8a3 100644 --- a/ajax/networking/get_netcfg.php +++ b/ajax/networking/get_netcfg.php @@ -46,7 +46,8 @@ if (isset($interface)) { preg_match('/static\sdomain_name_servers=(.*)/', $matched[0], $static_dns); preg_match('/fallback\sstatic_'.$interface.'/', $matched[0], $fallback); $dhcpdata['Metric'] = $metric[1]; - $dhcpdata['StaticIP'] = $static_ip[1]; + $dhcpdata['StaticIP'] = substr($static_ip[1], 0, strpos($static_ip[1],'/')); + $dhcpdata['SubnetMask'] = cidr2mask($static_ip[1]); $dhcpdata['StaticRouters'] = $static_routers[1]; $dhcpdata['StaticDNS'] = $static_dns[1]; $dhcpdata['FallbackEnabled'] = empty($fallback) ? false: true; diff --git a/app/js/custom.js b/app/js/custom.js index 25f121e5..a7eaa3cc 100644 --- a/app/js/custom.js +++ b/app/js/custom.js @@ -246,6 +246,7 @@ function loadInterfaceDHCPSelect() { jsonData = JSON.parse(data); $('#dhcp-iface')[0].checked = jsonData.DHCPEnabled; $('#txtipaddress').val(jsonData.StaticIP); + $('#txtsubnetmask').val(jsonData.SubnetMask); $('#txtgateway').val(jsonData.StaticRouters); $('#chkfallback')[0].checked = jsonData.FallbackEnabled; $('#txtrangestart').val(jsonData.RangeStart); @@ -257,7 +258,7 @@ function loadInterfaceDHCPSelect() { $('#no-resolv')[0].checked = jsonData.upstreamServersEnabled; $('#cbxdhcpupstreamserver').val(jsonData.upstreamServers[0]); $('#txtmetric').val(jsonData.Metric); - if (jsonData.StaticIP !== null && jsonData.StaticIP !== '') { + if (jsonData.StaticIP !== null && jsonData.StaticIP !== '' && !jsonData.FallbackEnabled) { $('#chkstatic').closest('.btn').button('toggle'); $('#chkstatic').closest('.btn').button('toggle').blur(); $('#chkstatic').blur(); @@ -268,6 +269,9 @@ function loadInterfaceDHCPSelect() { $('#chkdhcp').blur(); $('#chkfallback').prop('disabled', false); } + if (jsonData.FallbackEnabled) { + $('#dhcp-iface').prop('disabled', true); + } }); } diff --git a/templates/dhcp/general.php b/templates/dhcp/general.php index 484c2690..49758696 100644 --- a/templates/dhcp/general.php +++ b/templates/dhcp/general.php @@ -41,6 +41,13 @@

+
+
+ + +
+
+
From a21009e049861c565df948a0436d9d08d5bdb701 Mon Sep 17 00:00:00 2001 From: billz Date: Mon, 30 Nov 2020 17:33:17 +0000 Subject: [PATCH 11/11] Remove networking dhcp config (deprecated) --- ajax/networking/gen_int_config.php | 48 ----------------- ajax/networking/get_int_config.php | 23 -------- ajax/networking/save_int_config.php | 35 ------------ app/js/custom.js | 83 +++++------------------------ templates/networking.php | 6 --- 5 files changed, 14 insertions(+), 181 deletions(-) delete mode 100644 ajax/networking/gen_int_config.php delete mode 100644 ajax/networking/get_int_config.php delete mode 100644 ajax/networking/save_int_config.php diff --git a/ajax/networking/gen_int_config.php b/ajax/networking/gen_int_config.php deleted file mode 100644 index 4b3be680..00000000 --- a/ajax/networking/gen_int_config.php +++ /dev/null @@ -1,48 +0,0 @@ - $file) { - if ($index != "defaults") { - $cnfFile = parse_ini_file(RASPI_CONFIG_NETWORKING.'/'.$file, false, INI_SCANNER_RAW); - if ($cnfFile['static'] === 'true') { - $strConfFile .= "#Static IP configured for ".$cnfFile['interface']."\n"; - $strConfFile .= "interface ".$cnfFile['interface']."\n"; - if (isset($cnfFile['metric'])) { - $strConfFile .= "metric ".$cnfFile['metric']."\n"; - } - $strConfFile .= "static ip_address=".$cnfFile['ip_address']."\n"; - $strConfFile .= "static routers=".$cnfFile['routers']."\n"; - $strConfFile .= "static domain_name_servers=".$cnfFile['domain_name_server']."\n\n"; - } elseif ($cnfFile['static'] === 'false' && $cnfFile['failover'] === 'true') { - $strConfFile .= "#Failover static IP configured for ".$cnfFile['interface']."\n"; - $strConfFile .= "profile static_".$cnfFile['interface']."\n"; - $strConfFile .= "static ip_address=".$cnfFile['ip_address']."\n"; - $strConfFile .= "static routers=".$cnfFile['routers']."\n"; - $strConfFile .= "static domain_name_servers=".$cnfFile['domain_name_server']."\n\n"; - $strConfFile .= "interface ".$cnfFile['interface']."\n"; - if (isset($cnfFile['metric'])) { - $strConfFile .= "metric ".$cnfFile['metric']."\n"; - } - $strConfFile .= "fallback static_".$cnfFile['interface']."\n\n"; - } else { - $strConfFile .= "#DHCP configured for ".pathinfo($file, PATHINFO_FILENAME)."\n\n"; - } - } - } - - if (file_put_contents(RASPI_CONFIG_NETWORKING.'/dhcpcd.conf', $strConfFile)) { - exec('sudo /bin/cp '.RASPI_CONFIG_NETWORKING.'/dhcpcd.conf '.RASPI_DHCPCD_CONFIG); - $output = ['return'=>0,'output'=>'Settings successfully applied']; - } else { - $output = ['return'=>2,'output'=>'Unable to write to apply settings']; - } - echo json_encode($output); -} diff --git a/ajax/networking/get_int_config.php b/ajax/networking/get_int_config.php deleted file mode 100644 index 70be083a..00000000 --- a/ajax/networking/get_int_config.php +++ /dev/null @@ -1,23 +0,0 @@ -1,'output'=>['intConfig'=>$intConfig]]; - echo json_encode($jsonData); - - // Todo - get dhcp lease information from `dhcpcd -U eth0` ? maybe ? -} else { - $jsonData = ['return'=>2,'output'=>['Error getting data']]; - echo json_encode($jsonData); -} diff --git a/ajax/networking/save_int_config.php b/ajax/networking/save_int_config.php deleted file mode 100644 index f2a3f7b4..00000000 --- a/ajax/networking/save_int_config.php +++ /dev/null @@ -1,35 +0,0 @@ -0,'output'=>['Successfully Updated Network Configuration']]; - } else { - $jsonData = ['return'=>1,'output'=>['Error saving network configuration to file']]; - } -} else { - $jsonData = ['return'=>2,'output'=>'Unable to detect interface']; -} - -echo json_encode($jsonData); diff --git a/app/js/custom.js b/app/js/custom.js index a7eaa3cc..ba394f9d 100644 --- a/app/js/custom.js +++ b/app/js/custom.js @@ -49,74 +49,6 @@ function setupTabs() { }); } -function loadCurrentSettings(strInterface) { - $.post('ajax/networking/get_int_config.php',{interface:strInterface},function(data){ - jsonData = JSON.parse(data); - $.each(jsonData['output'],function(i,v) { - var int = v['interface']; - $.each(v,function(i2,v2) { - switch(i2) { - case "static": - if(v2 == 'true') { - $('#'+int+'-static').click(); - $('#'+int+'-nofailover').click(); - } else { - $('#'+int+'-dhcp').click(); - } - break; - case "failover": - if(v2 === 'true') { - $('#'+int+'-failover').click(); - } else { - $('#'+int+'-nofailover').click(); - } - break; - case "ip_address": - var arrIPNetmask = v2.split('/'); - $('#'+int+'-ipaddress').val(arrIPNetmask[0]); - $('#'+int+'-netmask').val(createNetmaskAddr(arrIPNetmask[1])); - break; - case "routers": - $('#'+int+'-gateway').val(v2); - break; - case "domain_name_server": - svrsDNS = v2.split(" "); - $('#'+int+'-dnssvr').val(svrsDNS[0]); - $('#'+int+'-dnssvralt').val(svrsDNS[1]); - break; - } - }); - }); - }); -} - -function saveNetworkSettings(int) { - var frmInt = $('#frm-'+int).find(':input'); - var arrFormData = {}; - $.each(frmInt,function(i3,v3){ - if($(v3).attr('type') == 'radio') { - arrFormData[$(v3).attr('id')] = $(v3).prop('checked'); - } else { - arrFormData[$(v3).attr('id')] = $(v3).val(); - } - }); - arrFormData['interface'] = int; - $.post('ajax/networking/save_int_config.php',arrFormData,function(data){ - var jsonData = JSON.parse(data); - $('#msgNetworking').html(msgShow(jsonData['return'],jsonData['output'])); - }); -} - -function applyNetworkSettings() { - var int = $(this).data('int'); - arrFormData = {}; - arrFormData['generate'] = ''; - $.post('ajax/networking/gen_int_config.php',arrFormData,function(data){ - var jsonData = JSON.parse(data); - $('#msgNetworking').html(msgShow(jsonData['return'],jsonData['output'])); - }); -} - $(document).on("click", ".js-add-dhcp-static-lease", function(e) { e.preventDefault(); var container = $(".js-new-dhcp-static-lease"); @@ -240,6 +172,10 @@ function loadWifiStations(refresh) { } $(".js-reload-wifi-stations").on("click", loadWifiStations(true)); +/* +Populates the DHCP server form fields +Option toggles are set dynamically depending on the loaded configuration +*/ function loadInterfaceDHCPSelect() { var iface = $('#cbxdhcpiface').val(); $.get('ajax/networking/get_netcfg.php?iface='+iface,function(data){ @@ -258,6 +194,7 @@ function loadInterfaceDHCPSelect() { $('#no-resolv')[0].checked = jsonData.upstreamServersEnabled; $('#cbxdhcpupstreamserver').val(jsonData.upstreamServers[0]); $('#txtmetric').val(jsonData.Metric); + if (jsonData.StaticIP !== null && jsonData.StaticIP !== '' && !jsonData.FallbackEnabled) { $('#chkstatic').closest('.btn').button('toggle'); $('#chkstatic').closest('.btn').button('toggle').blur(); @@ -269,15 +206,23 @@ function loadInterfaceDHCPSelect() { $('#chkdhcp').blur(); $('#chkfallback').prop('disabled', false); } - if (jsonData.FallbackEnabled) { + if (jsonData.FallbackEnabled || $('#chkdhcp').is(':checked')) { $('#dhcp-iface').prop('disabled', true); } }); } function setDHCPToggles(state) { + if ($('#chkfallback').is(':checked') && state) { + $('#chkfallback').prop('checked', state); + } + if ($('#dhcp-iface').is(':checked') && !state) { + $('#dhcp-iface').prop('checked', state); + } + $('#chkfallback').prop('disabled', state); $('#dhcp-iface').prop('disabled', !state); + //$('#dhcp-iface').prop('checked', state); } function loadChannel() { diff --git a/templates/networking.php b/templates/networking.php index 3d229c9e..bbcb84c5 100755 --- a/templates/networking.php +++ b/templates/networking.php @@ -18,12 +18,6 @@ // defaults to false $bridgedEnabled = $arrHostapdConf['BridgedEnable']; ?> - - - - - -