Merge branch 'feature/dhcp-eth0' of https://github.com/billz/raspap-webgui into feature/dhcp-eth0

This commit is contained in:
Bill Zimmerman 2020-11-17 10:15:14 +01:00
commit 01aedfbcf7
9 changed files with 144 additions and 82 deletions

View File

@ -0,0 +1,41 @@
<?php
require '../../includes/csrf.php';
require_once '../../includes/config.php';
$interface = $_GET['iface'];
if (isset($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];
$dhcpdata['RangeEnd'] = $arrRange[1];
$dhcpdata['RangeMask'] = $arrRange[2];
$dhcpdata['leaseTime'] = $arrRange[3];
$dhcpHost = $conf["dhcp-host"];
$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);
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') {
if (count($arrDns) > 1) {
$dhcpdata['DNS1'] = $arrDns[1];
}
if (count($arrDns) > 2) {
$dhcpdata['DNS2'] = $arrDns[2];
}
}
}
echo json_encode($dhcpdata);
}

View File

@ -236,9 +236,28 @@ function loadWifiStations(refresh) {
.load('ajax/networking/wifi_stations.php'+qs, complete);
};
}
$(".js-reload-wifi-stations").on("click", loadWifiStations(true));
function loadInterfaceDHCP(selected) {
}
function loadInterfaceDHCPSelect() {
var interface = $('#cbxdhcpiface').val();
console.log(interface);
$.get('ajax/networking/get_netcfg.php?iface='+interface,function(data){
jsonData = JSON.parse(data);
$('#dhcp-iface')[0].checked = jsonData.DHCPEnabled;
$('#txtrangestart').val(jsonData.RangeStart);
$('#txtrangeend').val(jsonData.RangeEnd);
$('#txtrangeleasetime').val(jsonData.leaseTime);
$('#txtdns1').val(jsonData.DNS1);
$('#txtdns2').val(jsonData.DNS2);
$('#cbxrangeleasetimeunits').val(jsonData.leaseTimeInterval);
});
}
function loadChannel() {
$.get('ajax/networking/get_channel.php',function(data){
jsonData = JSON.parse(data);

View File

@ -11,6 +11,7 @@ define('RASPI_CACHE_PATH', sys_get_temp_dir() . '/raspap');
// These are typical for default RPi installs. Modify if needed.
define('RASPI_DNSMASQ_CONFIG', '/etc/dnsmasq.d/090_raspap.conf');
define('RASPI_DNSMASQ_LEASES', '/var/lib/misc/dnsmasq.leases');
define('RASPI_DNSMASQ_PREFIX', '/etc/dnsmasq.d/090_');
define('RASPI_ADBLOCK_LISTPATH', '/etc/raspap/adblock/');
define('RASPI_ADBLOCK_CONFIG', '/etc/dnsmasq.d/090_adblock.conf');
define('RASPI_HOSTAPD_CONFIG', '/etc/hostapd/hostapd.conf');

View File

@ -16,6 +16,7 @@ $defaults = [
// These are typical for default RPi installs. Modify if needed.
'RASPI_DNSMASQ_CONFIG' => '/etc/dnsmasq.d/090_raspap.conf',
'RASPI_DNSMASQ_LEASES' => '/var/lib/misc/dnsmasq.leases',
'RASPI_DNSMASQ_PREFIX' => '/etc/dnsmasq.d/090_',
'RASPI_ADBLOCK_LISTPATH' => '/etc/raspap/adblock/',
'RASPI_ADBLOCK_CONFIG' => '/etc/dnsmasq.d/090_adblock.conf',
'RASPI_HOSTAPD_CONFIG' => '/etc/hostapd/hostapd.conf',

View File

@ -81,15 +81,63 @@ function DisplayDHCPConfig()
$config .= "log-facility=/tmp/dnsmasq.log".PHP_EOL;
$config .= "conf-dir=/etc/dnsmasq.d".PHP_EOL;
file_put_contents("/tmp/dnsmasqdata", $config);
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_CONFIG, $return);
$iface = $_POST['interface'];
// handle DHCP for selected interface option
if ($_POST['dhcp-iface'] == "1") {
$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 '.$_POST['interface'].' configuration';
$cfg[] = 'interface '.$_POST['interface'];
$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, $return);
$status->addMessage('DHCP configuration for '.$iface.' added.', 'success');
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $return);
$status->addMessage('Dnsmasq configuration for '.$iface.' added.', 'success');
} else {
$status->addMessage('DHCP for '.$iface.' already enabled.', 'danger');
}
}
} elseif (($_POST['dhcp-iface'] == "0") && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) {
// remove dhcp conf for selected interface
$dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG);
// todo: improve by removing extra blank lines
$dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'.*\n(.*\n){3}/m', '', $dhcp_cfg);
file_put_contents("/tmp/dhcpddata", $dhcp_cfg);
system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $return);
$status->addMessage('DHCP configuration for '.$iface.' removed.', 'success');
// remove dnsmasq eth0 conf
system('sudo rm '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $return);
$status->addMessage('Dnsmasq configuration for '.$iface.' removed.', 'success');
} else {
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_CONFIG, $return);
}
} else {
$status->addMessage($errors, 'danger');
}
if ($return == 0) {
$status->addMessage('Dnsmasq configuration updated successfully', 'success');
$status->addMessage('Dnsmasq configuration updated successfully.', 'success');
} else {
$status->addMessage('Dnsmasq configuration failed to be updated.', 'danger');
}
@ -128,55 +176,6 @@ function DisplayDHCPConfig()
}
$serviceStatus = $dnsmasq_state ? "up" : "down";
exec('cat '. RASPI_DNSMASQ_CONFIG, $return);
$conf = ParseConfig($return);
$arrRange = explode(",", $conf['dhcp-range']);
$RangeStart = $arrRange[0];
$RangeEnd = $arrRange[1];
$RangeMask = $arrRange[2];
$leaseTime = $arrRange[3];
$dhcpHost = $conf["dhcp-host"];
$dhcpHost = empty($dhcpHost) ? [] : $dhcpHost;
$dhcpHost = is_array($dhcpHost) ? $dhcpHost : [ $dhcpHost ];
$upstreamServers = is_array($conf['server']) ? $conf['server'] : [ $conf['server'] ];
$upstreamServers = array_filter($upstreamServers);
$DNS1 = '';
$DNS2 = '';
if (isset($conf['dhcp-option'])) {
$arrDns = explode(",", $conf['dhcp-option']);
if ($arrDns[0] == '6') {
if (count($arrDns) > 1) {
$DNS1 = $arrDns[1];
}
if (count($arrDns) > 2) {
$DNS2 = $arrDns[2];
}
}
}
$hselected = '';
$mselected = '';
$dselected = '';
$infiniteselected = '';
preg_match('/([0-9]*)([a-z])/i', $leaseTime, $arrRangeLeaseTime);
if ($leaseTime === 'infinite') {
$infiniteselected = ' selected="selected"';
} else {
switch ($arrRangeLeaseTime[2]) {
case 'h':
$hselected = ' selected="selected"';
break;
case 'm':
$mselected = ' selected="selected"';
break;
case 'd':
$dselected = ' selected="selected"';
break;
}
}
exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces);
exec('cat ' . RASPI_DNSMASQ_LEASES, $leases);
@ -184,16 +183,6 @@ function DisplayDHCPConfig()
"dhcp", compact(
"status",
"serviceStatus",
"RangeStart",
"RangeEnd",
"DNS1",
"DNS2",
"upstreamServers",
"arrRangeLeaseTime",
"mselected",
"hselected",
"dselected",
"infiniteselected",
"dnsmasq_state",
"conf",
"dhcpHost",

View File

@ -266,7 +266,7 @@ function _prompt_install_openvpn() {
function _install_openvpn() {
_install_log "Installing OpenVPN and enabling client configuration"
echo "Adding packages via apt-get"
sudo apt-get install $apt_option openvpn || _install_status 1 "Unable to install openvpn"
sudo apt-get install -y openvpn || _install_status 1 "Unable to install openvpn"
sudo sed -i "s/\('RASPI_OPENVPN_ENABLED', \)false/\1true/g" "$webroot_dir/includes/config.php" || _install_status 1 "Unable to modify config.php"
echo "Enabling openvpn-client service on boot"
sudo systemctl enable openvpn-client@client || _install_status 1 "Unable to enable openvpn-client daemon"

View File

@ -21,6 +21,8 @@ www-data ALL=(ALL) NOPASSWD:/bin/systemctl disable openvpn-client@client
www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/ovpnclient.ovpn /etc/openvpn/client/client.conf
www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/authdata /etc/openvpn/client/login.conf
www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/dnsmasqdata /etc/dnsmasq.d/090_raspap.conf
www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/dnsmasqdata /etc/dnsmasq.d/090_*.conf
www-data ALL=(ALL) NOPASSWD:/bin/rm /etc/dnsmasq.d/090_*.conf
www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/dhcpddata /etc/dhcpcd.conf
www-data ALL=(ALL) NOPASSWD:/sbin/shutdown -h now
www-data ALL=(ALL) NOPASSWD:/sbin/reboot

View File

@ -4,7 +4,6 @@
<div class="row">
<div class="col-md-6">
<h5><?php echo _("Upstream DNS servers") ?></h5>
<div class="input-group">
<input type="hidden" name="no-resolv" value="0">
<div class="custom-control custom-switch">

View File

@ -3,41 +3,51 @@
<div class="row">
<div class="form-group col-md-6">
<label for="code">Interface</label>
<select class="form-control" name="interface">
<?php foreach ($interfaces as $if) : ?>
<?php $if_quoted = htmlspecialchars($if, ENT_QUOTES) ?>
<?php $selected = $if === $conf['interface'] ? ' selected="selected"' : '' ?>
<option value="<?php echo $if_quoted ?>"<?php echo $selected ?>><?php echo $if_quoted ?></option>
<?php endforeach ?>
</select>
<?php SelectorOptions('interface', $interfaces, $conf['interface'], 'cbxdhcpiface', 'loadInterfaceDHCPSelect', $DHCPDisabled); ?>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<div class="input-group">
<input type="hidden" name="dhcp-iface" value="0">
<div class="custom-control custom-switch">
<input class="custom-control-input" id="dhcp-iface" type="checkbox" name="dhcp-iface" value="1" <?php echo $dhcp_iface_enable ? ' checked="checked"' : "" ?> aria-describedby="dhcp-iface-description">
<label class="custom-control-label" for="dhcp-iface"><?php echo _("Enable DHCP for this interface") ?></label>
</div>
<p id="dhcp-iface-description">
<small><?php echo _("Enable this option if you want RaspAP to assign IP addresses on the selected interface.") ?></small>
</p>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label for="code"><?php echo _("Starting IP Address"); ?></label>
<input type="text" class="form-control"name="RangeStart" value="<?php echo htmlspecialchars($RangeStart, ENT_QUOTES); ?>" />
<input type="text" class="form-control" id="txtrangestart" name="RangeStart" value="<?php echo htmlspecialchars($RangeStart, ENT_QUOTES); ?>" />
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label for="code"><?php echo _("Ending IP Address"); ?></label>
<input type="text" class="form-control" name="RangeEnd" value="<?php echo htmlspecialchars($RangeEnd, ENT_QUOTES); ?>" />
<input type="text" class="form-control" id="txtrangeend" name="RangeEnd" value="<?php echo htmlspecialchars($RangeEnd, ENT_QUOTES); ?>" />
</div>
</div>
<div class="row">
<div class="form-group col-xs-3 col-sm-3">
<label for="code"><?php echo _("Lease Time"); ?></label>
<input type="text" class="form-control" name="RangeLeaseTime" value="<?php echo htmlspecialchars($arrRangeLeaseTime[1], ENT_QUOTES); ?>" />
<input type="text" class="form-control" id="txtrangeleasetime" name="RangeLeaseTime" value="<?php echo htmlspecialchars($arrRangeLeaseTime[1], ENT_QUOTES); ?>" />
</div>
<div class="col-xs-3 col-sm-3">
<label for="code"><?php echo _("Interval"); ?></label>
<select name="RangeLeaseTimeUnits" class="form-control" >
<option value="m"<?php echo $mselected; ?>><?php echo _("Minute(s)"); ?></option>
<option value="h"<?php echo $hselected; ?>><?php echo _("Hour(s)"); ?></option>
<option value="d"<?php echo $dselected; ?>><?php echo _("Day(s)"); ?></option>
<option value="infinite"<?php echo $infiniteselected; ?>><?php echo _("Infinite"); ?></option>
<select id="cbxrangeleasetimeunits" name="RangeLeaseTimeUnits" class="form-control" >
<option value="m"><?php echo _("Minute(s)"); ?></option>
<option value="h"><?php echo _("Hour(s)"); ?></option>
<option value="d"><?php echo _("Day(s)"); ?></option>
<option value="infinite"><?php echo _("Infinite"); ?></option>
</select>
</div>
</div>
@ -45,14 +55,14 @@
<div class="row">
<div class="form-group col-md-6">
<label for="code"><?php echo _("DNS Server"); ?> 1</label>
<input type="text" class="form-control"name="DNS1" value="<?php echo htmlspecialchars($DNS1, ENT_QUOTES); ?>" />
<input type="text" class="form-control" id="txtdns1" name="DNS1" value="<?php echo htmlspecialchars($DNS1, ENT_QUOTES); ?>" />
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label for="code"><?php echo _("DNS Server"); ?> 2</label>
<input type="text" class="form-control" name="DNS2" value="<?php echo htmlspecialchars($DNS2, ENT_QUOTES); ?>" />
<input type="text" class="form-control" id="txtdns2" name="DNS2" value="<?php echo htmlspecialchars($DNS2, ENT_QUOTES); ?>" />
</div>
</div>