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/get_netcfg.php b/ajax/networking/get_netcfg.php index c2710011..cd36a8a3 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,21 @@ 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'] = 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; + echo json_encode($dhcpdata); } 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 de05908d..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,11 +172,19 @@ 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){ 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); $('#txtrangeend').val(jsonData.RangeEnd); $('#txtrangeleasetime').val(jsonData.leaseTime); @@ -253,9 +193,38 @@ 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 !== '' && !jsonData.FallbackEnabled) { + $('#chkstatic').closest('.btn').button('toggle'); + $('#chkstatic').closest('.btn').button('toggle').blur(); + $('#chkstatic').blur(); + $('#chkfallback').prop('disabled', true); + } else { + $('#chkdhcp').closest('.btn').button('toggle'); + $('#chkdhcp').closest('.btn').button('toggle').blur(); + $('#chkdhcp').blur(); + $('#chkfallback').prop('disabled', false); + } + 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() { $.get('ajax/networking/get_channel.php',function(data){ jsonData = JSON.parse(data); 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 diff --git a/includes/dhcp.php b/includes/dhcp.php index bd2a4a81..fb2e4709 100755 --- a/includes/dhcp.php +++ b/includes/dhcp.php @@ -68,10 +68,14 @@ function SaveDHCPConfig($status) $iface = $_POST['interface']; $return = 1; - if (($_POST['dhcp-iface'] == "1")) { - $errors = ValidateDHCPInput(); - if (empty($errors)) { - $return = UpdateDnsmasqCfg($iface,$status); + // 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); } else { $status->addMessage($errors, 'danger'); } @@ -79,21 +83,19 @@ function SaveDHCPConfig($status) $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger'); return false; } - $return = UpdateDHCPCfg($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); - } + 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() @@ -105,26 +107,38 @@ 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 ? - $errors .= _('Invalid DHCP range start.').'
'.PHP_EOL; + if (!filter_var($_POST['StaticIP'], FILTER_VALIDATE_IP) && !empty($_POST['StaticIP'])) { + $errors .= _('Invalid static IP address.').'
'.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 ? - $errors .= _('Invalid DHCP range end.').'
'.PHP_EOL; + if (!filter_var($_POST['SubnetMask'], FILTER_VALIDATE_IP) && !empty($_POST['SubnetMask'])) { + $errors .= _('Invalid subnet mask.').'
'.PHP_EOL; } - if (!ctype_digit($_POST['RangeLeaseTime']) && $_POST['RangeLeaseTimeUnits'] !== 'infinite') { - $errors .= _('Invalid DHCP lease time, not a number.').'
'.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 (!in_array($_POST['RangeLeaseTimeUnits'], array('m', 'h', 'd', 'infinite'))) { - $errors .= _('Unknown DHCP lease time unit.').'
'.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; } -function UpdateDnsmasqCfg($iface,$status) +function UpdateDnsmasqConfig($iface,$status) { $config = 'interface='.$iface.PHP_EOL. 'dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd']. @@ -173,45 +187,52 @@ 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']; - - // 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'); - } + $cfg[] = '# RaspAP '.$iface.' configuration'; + $cfg[] = 'interface '.$iface; + if (isset($_POST['StaticIP'])) { + $cfg[] = 'static ip_address='.$_POST['StaticIP'].'/'.mask2cidr($_POST['SubnetMask']); } + 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[] = '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)) { + 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; + $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); + $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 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*$)([\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'); @@ -219,7 +240,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 +250,4 @@ function RemoveDHCPCfg($iface,$status) return $result; } + 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 @@ +
-
- -
- aria-describedby="dhcp-iface-description"> - -
-

- -

+
+ +
-
+
+
+
+
+ + +
+

+ +

+
+
+ +
Static IP options
+
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
DHCP options
+
+
+
+
+ + +
+

+ +

+
+
+
+
- +
- +
- +
@@ -55,14 +103,21 @@
- +
- + +
+
+ +
+
+ +
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']; ?> - - - - - -