1
0
mirror of https://github.com/billz/raspap-webgui.git synced 2023-10-10 13:37:24 +02:00
This commit is contained in:
Bill Zimmerman 2016-08-31 16:30:44 +02:00
commit c8cca832c8
12 changed files with 1273 additions and 1080 deletions

View File

@ -2,7 +2,7 @@
# `$ raspap-webgui` [![Release 1.1](https://img.shields.io/badge/Release-1.1-green.svg)](https://github.com/billz/raspap-webgui/releases)
A simple, responsive web interface to control wifi, hostapd and related services on the Raspberry Pi.
This project was inspired by a [**blog post**](http://sirlagz.net/2013/02/06/script-web-configuration-page-for-raspberry-pi/) by SirLagz about using a web page rather than ssh to configure wifi and hostapd settings on the Raspberry Pi. I mostly just prettified the UI by wrapping it in [**SB Admin 2**](https://github.com/IronSummitMedia/startbootstrap-sb-admin-2), a Bootstrap based admin theme.
This project was inspired by a [**blog post**](http://sirlagz.net/2013/02/06/script-web-configuration-page-for-raspberry-pi/) by SirLagz about using a web page rather than ssh to configure wifi and hostapd settings on the Raspberry Pi. I mostly just prettified the UI by wrapping it in [**SB Admin 2**](https://github.com/BlackrockDigital/startbootstrap-sb-admin-2), a Bootstrap based admin theme.
We'd be curious to hear about how you use this with your own Pi-powered access points. Ping us on Twitter ([**@billzimmerman**](https://twitter.com/billzimmerman) and [**@SirLagz**](https://twitter.com/SirLagz)). Until then, here are some screenshots:
@ -52,20 +52,24 @@ Add the following to the end of `/etc/sudoers`:
```sh
www-data ALL=(ALL) NOPASSWD:/sbin/ifdown wlan0,/sbin/ifup wlan0,/bin/cat /etc/wpa_supplicant/wpa_supplicant.conf,/bin/cp /tmp/wifidata /etc/wpa_supplicant/wpa_supplicant.conf,/sbin/wpa_cli scan_results, /sbin/wpa_cli scan,/bin/cp /tmp/hostapddata /etc/hostapd/hostapd.conf, /etc/init.d/hostapd start,/etc/init.d/hostapd stop,/etc/init.d/dnsmasq start, /etc/init.d/dnsmasq stop,/bin/cp /tmp/dhcpddata /etc/dnsmasq.conf, /sbin/shutdown -h now, /sbin/reboot
www-data ALL=(ALL) NOPASSWD:/sbin/wpa_cli reconfigure
```
Once those modifications are done, git clone the files to `/var/www`.
Once those modifications are done, git clone the files to `/var/www/html`.
**Note,** for older versions of Raspbian (before Jessie, May 2016) use
`/var/www` instead.
```sh
sudo git clone https://github.com/billz/raspap-webgui /var/www
sudo rm -rf /var/www/html
sudo git clone https://github.com/billz/raspap-webgui /var/www/html
```
Set the files ownership to `www-data` user.
```sh
sudo chown -R www-data:www-data /var/www
sudo chown -R www-data:www-data /var/www/html
```
Move the RaspAP configuration file to the correct location
```sh
sudo mkdir /etc/raspap
sudo mv /var/www/raspap.php /etc/raspap/
sudo mv /var/www/html/raspap.php /etc/raspap/
sudo chown -R www-data:www-data /etc/raspap
```
Reboot and it should be up and running!

73
dist/js/functions.js vendored
View File

@ -1,74 +1,9 @@
function WiFiDown() {
var down = confirm("Take down wlan0 ?");
if(down) {
} else {
alert("Action cancelled");
}
}
function UpdateNetworks() {
var existing = document.getElementById("networkbox").getElementsByTagName('div').length;
document.getElementById("Networks").value = existing;
}
function AddNetwork() {
// existing = document.getElementById("networkbox").getElementsByTagName('div').length;
// existing++;
Networks++
var Networks = document.getElementById('Networks').value;
document.getElementById('networkbox').innerHTML += '<div id="Networkbox'+Networks+'" class="Networkboxes"><div class="row"><div class="col-lg-12"><h4>Network '+Networks+'</h4> \
<div class="row"><div class="form-group col-md-4"><label for="code">SSID</label><input type="text" class="form-control" name="ssid'+Networks+'" onkeyup="CheckSSID(this)"></div></div> \
<div class="row"><div class="form-group col-md-4"><label for="code">PSK</label><input type="password" class="form-control" name="psk'+Networks+'" onkeyup="CheckPSK(this)"></div></div> \
<div class="row"><div class="form-group col-md-4"><input type="button" class="btn btn-outline btn-primary" value="Cancel" onClick="DeleteNetwork('+Networks+')" /></div></div>';
Networks++;
document.getElementById('Networks').value=Networks;
}
function AddScanned(network) {
existing = document.getElementById("networkbox").getElementsByTagName('div').length;
var Networks = document.getElementById('Networks').value;
//if(existing != 0) {
Networks++;
//}
document.getElementById('Networks').value=Networks;
document.getElementById('networkbox').innerHTML += '<div id="Networkbox'+Networks+'" class="Networkboxes"><div class="col-lg-12"><h4>Network '+Networks+'</h4> \
<div class="row"><div class="form-group col-md-4"><label for="code">SSID</</label><input type="text" class="form-control" name="ssid'+Networks+'" id="ssid'+Networks+'" onkeyup="CheckSSID(this)"></div></div> \
<div class="row"><div class="form-group col-md-4"><label for="code">PSK</label><input type="password" class="form-control" name="psk'+Networks+'" onkeyup="CheckPSK(this)"></div></div> \
<div class="row"><div class="form-group col-md-4"><input type="button" class="btn btn-outline btn-primary" value="Cancel" onClick="DeleteNetwork('+Networks+')" /></div></div>';
document.getElementById('ssid'+Networks).value = network;
if(existing == 0) {
Networks++
document.getElementById('Networks').value = Networks;
}
}
function CheckSSID(ssid) {
if(ssid.value.length>31) {
ssid.style.background='#FFD0D0';
document.getElementById('Save').disabled = true;
} else {
ssid.style.background='#D0FFD0'
document.getElementById('Save').disabled = false;
}
}
function CheckPSK(psk) {
if(psk.value.length < 8) {
function CheckPSK(psk, id) {
if(psk.value.length < 8 || psk.value.length > 63) {
psk.style.background='#FFD0D0';
document.getElementById('Save').disabled = true;
document.getElementById(id).disabled = true;
} else {
psk.style.background='#D0FFD0';
document.getElementById('Save').disabled = false;
document.getElementById(id).disabled = false;
}
}
function DeleteNetwork(network) {
element = document.getElementById('Networkbox'+network);
element.parentNode.removeChild(element);
var Networks = document.getElementById('Networks').value;
Networks--
document.getElementById('Networks').value = Networks;
}

View File

@ -1,37 +1,33 @@
<?php
function Status($message, $level='success', $dismissable=true) {
$status = '<div class="alert alert-'.$level;
if ($dismissable) $status .= ' alert-dismissable';
$status .= '">'.$message;
if ($dismissable) $status .= '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">x</button>';
$status .= '</div>';
return $status;
}
include_once( 'includes/status_messages.php' );
function DisplayAuthConfig($username, $password){
$status = '';
$status = new StatusMessages();
if (isset($_POST['UpdateAdminPassword'])) {
if (CSRFValidate()) {
if (password_verify($_POST['oldpass'], $password)) {
$new_username=trim($_POST['username']);
if ($_POST['newpass'] != $_POST['newpassagain']) {
$status = Status('New passwords do not match', 'danger');
$status->addMessage('New passwords do not match', 'danger');
} else if ($new_username == '') {
$status = Status('Username must not be empty', 'danger');
$status->addMessage('Username must not be empty', 'danger');
} else {
if ($auth_file = fopen(RASPI_ADMIN_DETAILS, 'w')) {
fwrite($auth_file, $new_username.PHP_EOL);
fwrite($auth_file, password_hash($_POST['newpass'], PASSWORD_BCRYPT).PHP_EOL);
fclose($auth_file);
$username = $new_username;
$status = Status('Admin password updated');
$status->addMessage('Admin password updated');
} else {
$status = Status('Failed to update admin password', 'danger');
$status->addMessage('Failed to update admin password', 'danger');
}
}
} else {
$status = Status('Old password does not match', 'danger');
$status->addMessage('Old password does not match', 'danger');
}
} else {
error_log('CSRF violation');
}
}
?>
@ -40,8 +36,9 @@ function DisplayAuthConfig($username, $password){
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-lock fa-fw"></i>Configure Auth</div>
<div class="panel-body">
<p><?php echo $status; ?></p>
<form role="form" action="/?page=admin_conf" method="POST">
<p><?php $status->showMessages(); ?></p>
<form role="form" action="/?page=auth_conf" method="POST">
<?php CSRFToken() ?>
<div class="row">
<div class="form-group col-md-4">
<label for="username">Username</label>

213
includes/configure_client.php Executable file
View File

@ -0,0 +1,213 @@
<?php
/**
*
*
*/
function DisplayWPAConfig(){
$status = new StatusMessages();
$scanned_networks = array();
// Find currently configured networks
exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $known_return);
$network = null;
$ssid = null;
foreach($known_return as $line) {
if (preg_match('/network\s*=/', $line)) {
$network = array('visible' => false, 'configured' => true, 'connected' => false);
} elseif ($network !== null) {
if (preg_match('/^\s*}\s*$/', $line)) {
$networks[$ssid] = $network;
$network = null;
$ssid = null;
} elseif ($lineArr = preg_split('/\s*=\s*/', trim($line))) {
switch(strtolower($lineArr[0])) {
case 'ssid':
$ssid = trim($lineArr[1], '"');
break;
case 'psk':
if (array_key_exists('passphrase', $network)) {
break;
}
case '#psk':
$network['protocol'] = 'WPA';
case 'wep_key0': // Untested
$network['passphrase'] = trim($lineArr[1], '"');
break;
case 'key_mgmt':
if (! array_key_exists('passphrase', $network) && $lineArr[1] === 'NONE') {
$network['protocol'] = 'Open';
}
break;
}
}
}
}
if ( isset($_POST['client_settings']) && CSRFValidate() ) {
$tmp_networks = $networks;
if ($wpa_file = fopen('/tmp/wifidata', 'w')) {
fwrite($wpa_file, 'ctrl_interface=DIR=' . RASPI_WPA_CTRL_INTERFACE . ' GROUP=netdev' . PHP_EOL);
fwrite($wpa_file, 'update_config=1' . PHP_EOL);
foreach(array_keys($_POST) as $post) {
if (preg_match('/delete(\d+)/', $post, $post_match)) {
unset($tmp_networks[$_POST['ssid' . $post_match[1]]]);
} elseif (preg_match('/update(\d+)/', $post, $post_match)) {
// NB, at the moment, the value of protocol from the form may
// contain HTML line breaks
$tmp_networks[$_POST['ssid' . $post_match[1]]] = array(
'protocol' => ( $_POST['protocol' . $post_match[1]] === 'Open' ? 'Open' : 'WPA' ),
'passphrase' => $_POST['passphrase' . $post_match[1]],
'configured' => true
);
}
}
$ok = true;
foreach($tmp_networks as $ssid => $network) {
if ($network['protocol'] === 'Open') {
fwrite($wpa_file, "network={".PHP_EOL);
fwrite($wpa_file, "\tssid=\"".$ssid."\"".PHP_EOL);
fwrite($wpa_file, "\tkey_mgmt=NONE".PHP_EOL);
fwrite($wpa_file, "}".PHP_EOL);
} else {
if (strlen($network['passphrase']) >=8 && strlen($network['passphrase']) <= 63) {
exec( 'wpa_passphrase '.escapeshellarg($ssid). ' ' . escapeshellarg($network['passphrase']),$wpa_passphrase );
foreach($wpa_passphrase as $line) {
fwrite($wpa_file, $line.PHP_EOL);
}
} else {
$status->addMessage('WPA passphrase must be between 8 and 63 characters', 'danger');
$ok = false;
}
}
}
if ($ok) {
system( 'sudo cp /tmp/wifidata ' . RASPI_WPA_SUPPLICANT_CONFIG, $returnval );
if( $returnval == 0 ) {
exec('sudo wpa_cli reconfigure', $reconfigure_out, $reconfigure_return );
if ($reconfigure_return == 0) {
$status->addMessage('Wifi settings updated successfully', 'success');
$networks = $tmp_networks;
} else {
$status->addMessage('Wifi settings updated but cannot restart (cannon execute "wpa_cli reconfigure")', 'danger');
}
} else {
$status->addMessage('Wifi settings failed to be updated', 'danger');
}
}
} else {
$status->addMessage('Failed to updated wifi settings', 'danger');
}
}
exec( 'sudo wpa_cli scan' );
sleep(3);
exec( 'sudo wpa_cli scan_results',$scan_return );
for( $shift = 0; $shift < 2; $shift++ ) {
array_shift($scan_return);
}
// display output
foreach( $scan_return as $network ) {
$arrNetwork = preg_split("/[\t]+/",$network);
if (array_key_exists($arrNetwork[4], $networks)) {
$networks[$arrNetwork[4]]['visible'] = true;
$networks[$arrNetwork[4]]['channel'] = ConvertToChannel($arrNetwork[1]);
// TODO What if the security has changed?
} else {
$networks[$arrNetwork[4]] = array(
'configured' => false,
'protocol' => ConvertToSecurity($arrNetwork[3]),
'channel' => ConvertToChannel($arrNetwork[1]),
'passphrase' => '',
'visible' => true,
'connected' => false
);
}
}
exec( 'iwconfig wlan0', $iwconfig_return );
foreach ($iwconfig_return as $line) {
if (preg_match( '/ESSID:\"(.+)\"/i',$line,$iwconfig_ssid )) {
$networks[$iwconfig_ssid[1]]['connected'] = true;
}
}
?>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-signal fa-fw"></i> Configure client</div>
<!-- /.panel-heading -->
<div class="panel-body">
<p><?php $status->showMessages(); ?></p>
<h4>Client settings</h4>
<form method="POST" action="?page=wpa_conf" name="wpa_conf_form">
<?php CSRFToken() ?>
<input type="hidden" name="client_settings" ?>
<table class="table table-responsive table-striped">
<tr>
<th></th>
<th>SSID</th>
<th>Channel</th>
<th>Security</th>
<th>Passphrase</th>
<th></th>
</tr>
<?php $index = 0; ?>
<?php foreach ($networks as $ssid => $network) { ?>
<tr>
<td>
<?php if ($network['configured']) { ?>
<i class="fa fa-check-circle fa-fw"></i>
<?php } ?>
<?php if ($network['connected']) { ?>
<i class="fa fa-exchange fa-fw"></i>
<?php } ?>
</td>
<td>
<input type="hidden" name="ssid<?php echo $index ?>" value="<?php echo $ssid ?>" />
<?php echo $ssid ?>
</td>
<?php if ($network['visible']) { ?>
<td><?php echo $network['channel'] ?></td>
<?php } else { ?>
<td><span class="label label-warning">X</span></td>
<?php } ?>
<td><input type="hidden" name="protocol<?php echo $index ?>" value="<?php echo $network['protocol'] ?>" /><?php echo $network['protocol'] ?></td>
<?php if ($network['protocol'] === 'Open') { ?>
<td><input type="hidden" name="passphrase<?php echo $index ?>" value="" />---</td>
<?php } else { ?>
<td><input type="text" class="form-control" name="passphrase<?php echo $index ?>" value="<?php echo $network['passphrase'] ?>" onKeyUp="CheckPSK(this, 'update<?php echo $index?>')" />
<?php } ?>
<td>
<div class="btn-group btn-block">
<?php if ($network['configured']) { ?>
<input type="submit" class="col-md-6 btn btn-warning" value="Update" id="update<?php echo $index ?>" name="update<?php echo $index ?>"<?php echo ($network['protocol'] === 'Open' ? ' disabled' : '')?> />
<?php } else { ?>
<input type="submit" class="col-md-6 btn btn-info" value="Add" id="update<?php echo $index ?>" name="update<?php echo $index ?>" <?php echo ($network['protocol'] === 'Open' ? '' : ' disabled')?> />
<?php } ?>
<input type="submit" class="col-md-6 btn btn-danger" value="Delete" name="delete<?php echo $index ?>"<?php echo ($network['configured'] ? '' : ' disabled')?> />
</div>
</td>
</tr>
<?php $index += 1; ?>
<?php } ?>
</table>
</form>
</div><!-- ./ Panel body -->
<div class="panel-footer"><strong>Note,</strong> WEP access points appear as 'Open'. RaspAP does not currently support connecting to WEP.</div>
</div><!-- /.panel-primary -->
</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<?php
}
?>

140
includes/dashboard.php Executable file
View File

@ -0,0 +1,140 @@
<?php
/**
*
*
*/
function DisplayDashboard(){
$status = new StatusMessages();
exec( 'ifconfig wlan0', $return );
exec( 'iwconfig wlan0', $return );
$strWlan0 = implode( " ", $return );
$strWlan0 = preg_replace( '/\s\s+/', ' ', $strWlan0 );
// Parse results from ifconfig/iwconfig
preg_match( '/HWaddr ([0-9a-f:]+)/i',$strWlan0,$result );
$strHWAddress = $result[1];
preg_match( '/inet addr:([0-9.]+)/i',$strWlan0,$result );
$strIPAddress = $result[1];
preg_match( '/Mask:([0-9.]+)/i',$strWlan0,$result );
$strNetMask = $result[1];
preg_match( '/RX packets:(\d+)/',$strWlan0,$result );
$strRxPackets = $result[1];
preg_match( '/TX packets:(\d+)/',$strWlan0,$result );
$strTxPackets = $result[1];
preg_match( '/RX bytes:(\d+ \(\d+.\d+ [K|M|G]iB\))/i',$strWlan0,$result );
$strRxBytes = $result[1];
preg_match( '/TX Bytes:(\d+ \(\d+.\d+ [K|M|G]iB\))/i',$strWlan0,$result );
$strTxBytes = $result[1];
preg_match( '/ESSID:\"([a-zA-Z0-9\s]+)\"/i',$strWlan0,$result );
$strSSID = str_replace( '"','',$result[1] );
preg_match( '/Access Point: ([0-9a-f:]+)/i',$strWlan0,$result );
$strBSSID = $result[1];
preg_match( '/Bit Rate=([0-9\.]+ Mb\/s)/i',$strWlan0,$result );
$strBitrate = $result[1];
preg_match( '/Tx-Power=([0-9]+ dBm)/i',$strWlan0,$result );
$strTxPower = $result[1];
preg_match( '/Link Quality=([0-9]+)/i',$strWlan0,$result );
$strLinkQuality = $result[1];
preg_match( '/Signal level=(-?[0-9]+ dBm)/i',$strWlan0,$result );
$strSignalLevel = $result[1];
preg_match('/Frequency:(\d+.\d+ GHz)/i',$strWlan0,$result);
$strFrequency = $result[1];
if(strpos( $strWlan0, "UP" ) !== false && strpos( $strWlan0, "RUNNING" ) !== false ) {
$status->addMessage('Interface is up', 'success');
$wlan0up = true;
} else {
$status->addMessage('Interface is down', 'warning');
}
if( isset($_POST['ifdown_wlan0']) ) {
exec( 'ifconfig wlan0 | grep -i running | wc -l',$test );
if($test[0] == 1) {
exec( 'sudo ifdown wlan0',$return );
} else {
echo 'Interface already down';
}
} elseif( isset($_POST['ifup_wlan0']) ) {
exec( 'ifconfig wlan0 | grep -i running | wc -l',$test );
if($test[0] == 0) {
exec( 'sudo ifup wlan0',$return );
} else {
echo 'Interface already up';
}
}
?>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-dashboard fa-fw"></i> Dashboard </div>
<div class="panel-body">
<p><?php $status->showMessages(); ?></p>
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<h4>Interface Information</h4>
<div class="info-item">Interface Name</div> wlan0</br>
<div class="info-item">IP Address</div> <?php echo $strIPAddress ?></br>
<div class="info-item">Subnet Mask</div> <?php echo $strNetMask ?></br>
<div class="info-item">Mac Address</div> <?php echo $strHWAddress ?></br></br>
<h4>Interface Statistics</h4>
<div class="info-item">Received Packets</div> <?php echo $strRxPackets ?></br>
<div class="info-item">Received Bytes</div> <?php echo $strRxBytes ?></br></br>
<div class="info-item">Transferred Packets</div> <?php echo $strTxPackets ?></br>
<div class="info-item">Transferred Bytes</div> <?php echo $strTxBytes ?></br>
</div><!-- /.panel-body -->
</div><!-- /.panel-default -->
</div><!-- /.col-md-6 -->
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-body wireless">
<h4>Wireless Information</h4>
<div class="info-item">Connected To</div> <?php echo $strSSID ?></br>
<div class="info-item">AP Mac Address</div> <?php echo $strBSSID ?></br>
<div class="info-item">Bitrate</div> <?php echo $strBitrate ?></br>
<div class="info-item">Signal Level</div> <?php echo $strSignalLevel ?></br>
<div class="info-item">Transmit Power</div> <?php echo $strTxPower ?></br>
<div class="info-item">Frequency</div> <?php echo $strFrequency ?></br></br>
<div class="info-item">Link Quality</div>
<div class="progress">
<div class="progress-bar progress-bar-info progress-bar-striped active"
role="progressbar"
aria-valuenow="<?php echo $strLinkQuality ?>" aria-valuemin="0" aria-valuemax="100"
style="width: <?php echo $strLinkQuality ?>%;"><?php echo $strLinkQuality ?>%
</div>
</div>
</div><!-- /.panel-body -->
</div><!-- /.panel-default -->
</div><!-- /.col-md-6 -->
</div><!-- /.row -->
<div class="col-lg-12">
<div class="row">
<form action="?page=wlan0_info" method="POST">
<?php if ( !$wlan0up ) {
echo '<input type="submit" class="btn btn-success" value="Start wlan0" name="ifup_wlan0" />';
} else {
echo '<input type="submit" class="btn btn-warning" value="Stop wlan0" name="ifdown_wlan0" />';
}
?>
<input type="button" class="btn btn-outline btn-primary" value="Refresh" onclick="document.location.reload(true)" />
</form>
</div>
</div>
</div><!-- /.panel-body -->
<div class="panel-footer">Information provided by ifconfig and iwconfig</div>
</div><!-- /.panel-default -->
</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<?php
}
?>

220
includes/dhcp.php Executable file
View File

@ -0,0 +1,220 @@
<?php
include_once( 'includes/status_messages.php' );
/**
*
* Manage DHCP configuration
*
*/
function DisplayDHCPConfig() {
$status = new StatusMessages();
if( isset( $_POST['savedhcpdsettings'] ) ) {
if (CSRFValidate()) {
$config = 'interface='.$_POST['interface'].PHP_EOL
.'dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd'].',255.255.255.0,'.$_POST['RangeLeaseTime'].''.$_POST['RangeLeaseTimeUnits'];
exec( 'echo "'.$config.'" > /tmp/dhcpddata',$temp );
system( 'sudo cp /tmp/dhcpddata '. RASPI_DNSMASQ_CONFIG, $return );
if( $return == 0 ) {
$status->addMessage('Dnsmasq configuration updated successfully', 'success');
} else {
$status->addMessage('Dnsmasq configuration failed to be updated', 'danger');
}
} else {
error_log('CSRF violation');
}
}
exec( 'pidof dnsmasq | wc -l',$dnsmasq );
$dnsmasq_state = ($dnsmasq[0] > 0);
if( isset( $_POST['startdhcpd'] ) ) {
if (CSRFValidate()) {
if ($dnsmasq_state) {
$status->addMessage('dnsmasq already running', 'info');
} else {
exec('sudo /etc/init.d/dnsmasq start', $dnsmasq, $return);
if ($return == 0) {
$status->addMessage('Successfully started dnsmasq', 'success');
$dnsmasq_state = true;
} else {
$status->addMessage('Failed to start dnsmasq', 'danger');
}
}
} else {
error_log('CSRF violation');
}
} elseif( isset($_POST['stopdhcpd'] ) ) {
if (CSRFValidate()) {
if ($dnsmasq_state) {
exec('sudo /etc/init.d/dnsmasq stop', $dnsmasq, $return);
if ($return == 0) {
$status->addMessage('Successfully stopped dnsmasq', 'success');
$dnsmasq_state = false;
} else {
$status->addMessage('Failed to stop dnsmasq', 'danger');
}
} else {
$status->addMessage('dnsmasq already stopped', 'info');
}
} else {
error_log('CSRF violation');
}
} else {
if( $dnsmasq_state ) {
$status->addMessage('Dnsmasq is running', 'success');
} else {
$status->addMessage('Dnsmasq is not running', 'warning');
}
}
exec( 'cat '. RASPI_DNSMASQ_CONFIG, $return );
$conf = ParseConfig($return);
$arrRange = explode( ",", $conf['dhcp-range'] );
$RangeStart = $arrRange[0];
$RangeEnd = $arrRange[1];
$RangeMask = $arrRange[2];
preg_match( '/([0-9]*)([a-z])/i', $arrRange[3], $arrRangeLeaseTime );
switch( $arrRangeLeaseTime[2] ) {
case "h":
$hselected = " selected";
break;
case "m":
$mselected = " selected";
break;
case "d":
$dselected = " selected";
break;
}
?>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-exchange fa-fw"></i> Configure DHCP
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<p><?php $status->showMessages(); ?></p>
<!-- Nav tabs -->
<ul class="nav nav-tabs">
<li class="active"><a href="#server-settings" data-toggle="tab">Server settings</a>
</li>
<li><a href="#client-list" data-toggle="tab">Client list</a>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div class="tab-pane fade in active" id="server-settings">
<h4>DHCP server settings</h4>
<form method="POST" action="?page=dhcpd_conf">
<?php CSRFToken() ?>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Interface</label>
<select class="form-control" name="interface">
<?php
exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces);
foreach( $interfaces as $int ) {
$select = '';
if( $int == $conf['interface'] ) {
$select = " selected";
}
echo '<option value="'.$int.'"'.$select.'>'.$int.'</option>';
}
?>
</select>
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Starting IP Address</label>
<input type="text" class="form-control"name="RangeStart" value="<?php echo $RangeStart; ?>" />
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Ending IP Address</label>
<input type="text" class="form-control" name="RangeEnd" value="<?php echo $RangeEnd; ?>" />
</div>
</div>
<div class="row">
<div class="form-group col-xs-2 col-sm-2">
<label for="code">Lease Time</label>
<input type="text" class="form-control" name="RangeLeaseTime" value="<?php echo $arrRangeLeaseTime[1]; ?>" />
</div>
<div class="col-xs-2 col-sm-2">
<label for="code">Interval</label>
<select name="RangeLeaseTimeUnits" class="form-control" ><option value="m" <?php echo $mselected; ?>>Minutes</option><option value="h" <?php echo $hselected; ?>>Hours</option><option value="d" <?php echo $dselected; ?>>Days</option><option value="infinite">Infinite</option></select>
</div>
</div>
<input type="submit" class="btn btn-outline btn-primary" value="Save settings" name="savedhcpdsettings" />
<?php
if ( $dnsmasq_state ) {
echo '<input type="submit" class="btn btn-warning" value="Stop dnsmasq" name="stopdhcpd" />';
} else {
echo'<input type="submit" class="btn btn-success" value="Start dnsmasq" name="startdhcpd" />';
}
?>
</form>
</div><!-- /.tab-pane -->
<div class="tab-pane fade in" id="client-list">
<h4>Client list</h4>
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
Active DHCP leases
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Expire time</th>
<th>MAC Address</th>
<th>IP Address</th>
<th>Host name</th>
<th>Client ID</th>
</tr>
</thead>
<tbody>
<tr>
<?php
exec( 'cat ' . RASPI_DNSMASQ_LEASES, $leases );
foreach( $leases as $lease ) {
$lease_items = explode(' ', $lease);
foreach( $lease_items as $lease_item ) {
echo '<td>' . $lease_item . '</td>';
}
echo '</tr>';
};
?>
</tr>
</tbody>
</table>
</div><!-- /.table-responsive -->
</div><!-- /.panel-body -->
</div><!-- /.panel -->
</div><!-- /.col-lg-6 -->
</div><!-- /.tab-pane -->
</div><!-- /.tab-content -->
</div><!-- ./ Panel body -->
<div class="panel-footer"> Information provided by Dnsmasq</div>
</div><!-- /.panel-primary -->
</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<?php
}
?>

View File

@ -1,5 +1,59 @@
<?php
/**
*
* Add CSRF Token to form
*
*/
function CSRFToken() {
?>
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>" />
<?php
}
/**
*
* Validate CSRF Token
*
*/
function CSRFValidate() {
if ( hash_equals($_POST['csrf_token'], $_SESSION['csrf_token']) ) {
return true;
} else {
error_log('CSRF violation');
return false;
}
}
/**
* Test whether array is associative
*/
function isAssoc($arr) {
return array_keys($arr) !== range(0, count($arr) - 1);
}
/**
*
* Display a selector field for a form. Arguments are:
* $name: Field name
* $options: Array of options
* $selected: Selected option (optional)
* If $options is an associative array this should be the key
*
*/
function SelectorOptions($name, $options, $selected = null) {
echo "<select class=\"form-control\" name=\"$name\">";
foreach ( $options as $opt => $label) {
$select = '';
$key = isAssoc($options) ? $opt : $label;
if( $key == $selected ) {
$select = " selected";
}
echo "<option value=\"$key\"$select>$label</options>";
}
echo "</select>";
}
/**
*
* @param string $input
@ -21,9 +75,10 @@ function GetDistString( $input,$string,$offset,$separator ) {
function ParseConfig( $arrConfig ) {
$config = array();
foreach( $arrConfig as $line ) {
if( $line[0] != "#" ) {
$line = trim($line);
if( $line != "" && $line[0] != "#" ) {
$arrLine = explode( "=",$line );
$config[$arrLine[0]] = $arrLine[1];
$config[$arrLine[0]] = ( count($arrLine) > 1 ? $arrLine[1] : true );
}
}
return $config;
@ -35,19 +90,13 @@ function ParseConfig( $arrConfig ) {
* @return $channel
*/
function ConvertToChannel( $freq ) {
$base = 2412;
$channel = 1;
for( $x = 0; $x < 13; $x++ ) {
if( $freq != $base ) {
$base = $base + 5;
$channel++;
} else {
$channel = ($freq - 2407)/5;
if ($channel > 0 && $channel < 14) {
return $channel;
} else {
return 'Invalid Channel';
}
}
return "Invalid Channel";
}
/**
* Converts WPA security string to readable format
@ -55,661 +104,28 @@ function ConvertToChannel( $freq ) {
* @return string
*/
function ConvertToSecurity( $security ) {
switch( $security ) {
case "[WPA2-PSK-CCMP][ESS]":
return "WPA2-PSK (AES)";
break;
case "[WPA2-PSK-TKIP][ESS]":
return "WPA2-PSK (TKIP)";
break;
case "[WPA2-PSK-CCMP][WPS][ESS]":
return "WPA/WPA2-PSK (TKIP/AES)";
break;
case "[WPA2-PSK-TKIP+CCMP][WPS][ESS]":
return "WPA2-PSK (TKIP/AES) with WPS";
break;
case "[WPA-PSK-TKIP+CCMP][WPS][ESS]":
return "WPA-PSK (TKIP/AES) with WPS";
break;
case "[WPA-PSK-TKIP][WPA2-PSK-CCMP][WPS][ESS]":
return "WPA/WPA2-PSK (TKIP/AES)";
break;
case "[WPA-PSK-TKIP+CCMP][WPA2-PSK-TKIP+CCMP][ESS]":
return "WPA/WPA2-PSK (TKIP/AES)";
break;
case "[WPA-PSK-TKIP][ESS]":
return "WPA-PSK (TKIP)";
break;
case "[WEP][ESS]":
return "WEP";
break;
}
}
/**
*
*
*/
function DisplayDashboard(){
exec( 'ifconfig wlan0', $return );
exec( 'iwconfig wlan0', $return );
$strWlan0 = implode( " ", $return );
$strWlan0 = preg_replace( '/\s\s+/', ' ', $strWlan0 );
// Parse results from ifconfig/iwconfig
preg_match( '/HWaddr ([0-9a-f:]+)/i',$strWlan0,$result );
$strHWAddress = $result[1];
preg_match( '/inet addr:([0-9.]+)/i',$strWlan0,$result );
$strIPAddress = $result[1];
preg_match( '/Mask:([0-9.]+)/i',$strWlan0,$result );
$strNetMask = $result[1];
preg_match( '/RX packets:(\d+)/',$strWlan0,$result );
$strRxPackets = $result[1];
preg_match( '/TX packets:(\d+)/',$strWlan0,$result );
$strTxPackets = $result[1];
preg_match( '//RX bytes:(\d+)/i',$strWlan0,$result );
$strRxBytes = $result[1];
preg_match( '/TX Bytes:(\d+ \(\d+.\d+ [K|M|G]iB\))/i',$strWlan0,$result );
$strTxBytes = $result[1];
preg_match( '/ESSID:\"([a-zA-Z0-9\s]+)\"/i',$strWlan0,$result );
$strSSID = str_replace( '"','',$result[1] );
preg_match( '/Access Point: ([0-9a-f:]+)/i',$strWlan0,$result );
$strBSSID = $result[1];
preg_match( '/Bit Rate=([0-9]+ Mb\/s)/i',$strWlan0,$result );
$strBitrate = $result[1];
preg_match( '/Tx-Power=([0-9]+ dBm)/i',$strWlan0,$result );
$strTxPower = $result[1];
preg_match( '/Link Quality=([0-9]+)/i',$strWlan0,$result );
$strLinkQuality = $result[1];
preg_match( '/Signal Level=([0-9]+)/i',$strWlan0,$result );
$strSignalLevel = $result[1];
preg_match('/Frequency:(\d+.\d+ GHz)/i',$strWlan0,$result);
$strFrequency = $result[1];
if(strpos( $strWlan0, "UP" ) !== false && strpos( $strWlan0, "RUNNING" ) !== false ) {
$status = '<div class="alert alert-success alert-dismissable">Interface is up
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>';
$wlan0up = true;
$options = array();
preg_match_all('/\[([^\]]+)\]/s', $security, $matches);
foreach($matches[1] as $match) {
if (preg_match('/^(WPA\d?)/', $match, $protocol_match)) {
$protocol = $protocol_match[1];
$matchArr = explode('-', $match);
if (count($matchArr) > 2) {
$options[] = $protocol . ' ('. $matchArr[2] .')';
} else {
$status = '<div class="alert alert-warning alert-dismissable">Interface is down
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>';
$options[] = $protocol;
}
}
}
if( isset($_POST['ifdown_wlan0']) ) {
exec( 'ifconfig wlan0 | grep -i running | wc -l',$test );
if($test[0] == 1) {
exec( 'sudo ifdown wlan0',$return );
if (count($options) === 0) {
// This could also be WEP but wpa_supplicant doesn't have a way to determine
// this.
// And you shouldn't be using WEP these days anyway.
return 'Open';
} else {
echo 'Interface already down';
return implode('<br />', $options);
}
} elseif( isset($_POST['ifup_wlan0']) ) {
exec( 'ifconfig wlan0 | grep -i running | wc -l',$test );
if($test[0] == 0) {
exec( 'sudo ifup wlan0',$return );
} else {
echo 'Interface already up';
}
}
?>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-dashboard fa-fw"></i> Dashboard </div>
<div class="panel-body">
<p><?php echo $status; ?></p>
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<h4>Interface Information</h4>
<div class="info-item">Interface Name</div> wlan0</br>
<div class="info-item">IP Address</div> <?php echo $strIPAddress ?></br>
<div class="info-item">Subnet Mask</div> <?php echo $strNetMask ?></br>
<div class="info-item">Mac Address</div> <?php echo $strHWAddress ?></br></br>
<h4>Interface Statistics</h4>
<div class="info-item">Received Packets</div> <?php echo $strRxPackets ?></br>
<div class="info-item">Received Bytes</div> <?php echo $strRxBytes ?></br></br>
<div class="info-item">Transferred Packets</div> <?php echo $strTxPackets ?></br>
<div class="info-item">Transferred Bytes</div> <?php echo $strTxBytes ?></br>
</div><!-- /.panel-body -->
</div><!-- /.panel-default -->
</div><!-- /.col-md-6 -->
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-body wireless">
<h4>Wireless Information</h4>
<div class="info-item">Connected To</div> <?php echo $strSSID ?></br>
<div class="info-item">AP Mac Address</div> <?php echo $strBSSID ?></br>
<div class="info-item">Bitrate</div> <?php echo $strBitrate ?></br>
<div class="info-item">Transmit Power</div> <?php echo $strTxPower ?></br>
<div class="info-item">Frequency</div> <?php echo $strFrequency ?></br></br>
<div class="info-item">Link Quality</div>
<div class="progress">
<div class="progress-bar progress-bar-info progress-bar-striped active"
role="progressbar"
aria-valuenow="<?php echo $strLinkQuality ?>" aria-valuemin="0" aria-valuemax="100"
style="width: <?php echo $strLinkQuality ?>%;"><?php echo $strLinkQuality ?>%
</div>
</div>
<div class="info-item">Signal Level</div>
<div class="progress">
<div class="progress-bar progress-bar-info progress-bar-striped active"
role="progressbar"
aria-valuenow="<?php echo $strSignalLevel ?>" aria-valuemin="0" aria-valuemax="100"
style="width: <?php echo $strSignalLevel ?>%;"><?php echo $strSignalLevel ?>%
</div>
</div>
</div><!-- /.panel-body -->
</div><!-- /.panel-default -->
</div><!-- /.col-md-6 -->
</div><!-- /.row -->
<div class="col-lg-12">
<div class="row">
<form action="?page=wlan0_info" method="POST">
<?php if ( !$wlan0up ) {
echo '<input type="submit" class="btn btn-success" value="Start wlan0" name="ifup_wlan0" />';
} else {
echo '<input type="submit" class="btn btn-warning" value="Stop wlan0" name="ifdown_wlan0" />';
}
?>
<input type="button" class="btn btn-outline btn-primary" value="Refresh" onclick="document.location.reload(true)" />
</form>
</div>
</div>
</div><!-- /.panel-body -->
<div class="panel-footer">Information provided by ifconfig and iwconfig</div>
</div><!-- /.panel-default -->
</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<?php
}
/**
*
*
*/
function DisplayWPAConfig(){
?>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-signal fa-fw"></i> Configure client
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<?php echo $status; ?>
<h4>Client settings</h4>
<div class="row">
<div class="col-lg-12">
<?php
// save WPA settings
if( isset($_POST['SaveWPAPSKSettings']) ) {
$config = 'ctrl_interface=DIR='. RASPI_WPA_CTRL_INTERFACE .' GROUP=netdev
update_config=1
';
$networks = $_POST['Networks'];
for( $x = 0; $x < $networks; $x++ ) {
$network = '';
$ssid = escapeshellarg( $_POST['ssid'.$x] );
$psk = escapeshellarg( $_POST['psk'.$x] );
if ( strlen($psk) >2 ) {
exec( 'wpa_passphrase '.$ssid. ' ' . $psk,$network );
foreach($network as $b) {
$config .= "$b
";
}
}
}
exec( "echo '$config' > /tmp/wifidata", $return );
system( 'sudo cp /tmp/wifidata ' . RASPI_WPA_SUPPLICANT_CONFIG, $returnval );
if( $returnval == 0 ) {
echo '<div class="alert alert-success alert-dismissable">Wifi settings updated successfully
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>';
} else {
echo '<div class="alert alert-danger alert-dismissable">Wifi settings failed to be updated
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>';
}
// scan networks
} elseif( isset($_POST['Scan']) ) {
$return = '';
exec( 'sudo wpa_cli scan',$return );
sleep(3);
exec( 'sudo wpa_cli scan_results',$return );
for( $shift = 0; $shift < 4; $shift++ ) {
array_shift($return);
}
// display output
echo '<form method="POST" action="?page=wpa_conf" id="wpa_conf_form"><input type="hidden" id="Networks" name="Networks" /><div class="network" id="networkbox"></div>';
echo '<div class="row"><div class="col-lg-6"><input type="submit" class="btn btn-primary" value="Scan for networks" name="Scan" /> <input type="button" class="btn btn-primary" value="Add network" onClick="AddNetwork();" /> <input type="submit" class="btn btn-primary" value="Save" name="SaveWPAPSKSettings" onmouseover="UpdateNetworks(this)" id="Save" disabled /></div></div>';
echo '<h4>Networks found</h4><div class="table-responsive"><table class="table table-hover">';
echo '<thead><tr><th></th><th>SSID</th><th>Channel</th><th>Signal</th><th>Security</th></tr></thead><tbody>';
foreach( $return as $network ) {
$arrNetwork = preg_split("/[\t]+/",$network);
$bssid = $arrNetwork[0];
$channel = ConvertToChannel($arrNetwork[1]);
$signal = $arrNetwork[2] . " dBm";
$security = $arrNetwork[3];
$ssid = $arrNetwork[4];
echo '<tr><td><input type="button" class="btn btn-outline btn-primary" value="Connect" onClick="AddScanned(\''.$ssid.'\')" /></td> <td><strong>' . $ssid . "</strong></td> <td>" . $channel . "</td><td>" . $signal . "</td><td>". ConvertToSecurity($security) ."</td></tr>";
}
echo '</tbody></table>';
} else {
// default action, output configured network(s)
exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $return);
$ssid = array();
$psk = array();
foreach($return as $a) {
if(preg_match('/SSID/i',$a)) {
$arrssid = explode("=",$a);
$ssid[] = str_replace('"','',$arrssid[1]);
}
if(preg_match('/psk/i',$a)) {
$arrpsk = explode("=",$a);
$psk[] = str_replace('"','',$arrpsk[1]);
}
}
$numSSIDs = count($ssid);
$output = '<form method="POST" action="?page=wpa_conf" id="wpa_conf_form"><input type="hidden" id="Networks" name="Networks" /><div class="network" id="networkbox">';
if ( $numSSIDs > 0 ) {
for( $ssids = 0; $ssids < $numSSIDs; $ssids++ ) {
$output .= '<div id="Networkbox'.$ssids.'" class="NetworkBoxes">
<div class="row"><div class="form-group col-md-4"><label for="code">Network '.$ssids.'</label></div></div>
<div class="row"><div class="form-group col-md-4"><label for="code" id="lssid0">SSID</label><input type="text" class="form-control" id="ssid0" name="ssid'.$ssids.'" value="'.$ssid[$ssids].'" onkeyup="CheckSSID(this)" /></div></div>
<div class="row"><div class="form-group col-md-4"><label for="code" id="lpsk0">PSK</label><input type="password" class="form-control" id="psk0" name="psk'.$ssids.'" value="'.$psk[$ssids].'" onkeyup="CheckPSK(this)" /></div></div>
<div class="row"><div class="form-group col-md-4"><input type="button" class="btn btn-outline btn-primary" value="Delete" onClick="DeleteNetwork('.$ssids.')" /></div></div>';
}
$output .= '</div><!-- /#Networkbox -->';
} else {
$status = '<div class="alert alert-warning alert-dismissable">Not connected
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>';
}
$output .= '<div class="row"><div class="col-lg-6"><input type="submit" class="btn btn-primary" value="Scan for networks" name="Scan" /> <input type="button" class="btn btn-primary" value="Add network" onClick="AddNetwork();" /> <input type="submit" class="btn btn-primary" value="Save" name="SaveWPAPSKSettings" onmouseover="UpdateNetworks(this)" id="Save" disabled />';
$output .= '</form>';
echo $output;
}
?>
<script type="text/Javascript">UpdateNetworks(this)</script>
</form>
</div><!-- ./ Panel body -->
</div><!-- /.panel-primary -->
</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<?php
}
/**
*
*
*/
function DisplayHostAPDConfig(){
exec( 'cat '. RASPI_HOSTAPD_CONFIG, $return );
exec( 'pidof hostapd | wc -l', $hostapdstatus);
if( $hostapdstatus[0] == 0 ) {
$status = '<div class="alert alert-warning alert-dismissable">HostAPD is not running
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>';
} else {
$status = '<div class="alert alert-success alert-dismissable">HostAPD is running
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>';
}
$arrConfig = array();
$arrChannel = array('a','b','g');
$arrSecurity = array( 1 => 'WPA', 2 => 'WPA2',3=> 'WPA+WPA2');
$arrEncType = array('TKIP' => 'TKIP', 'CCMP' => 'CCMP', 'TKIP CCMP' => 'TKIP+CCMP');
foreach( $return as $a ) {
if( $a[0] != "#" ) {
$arrLine = explode( "=",$a) ;
$arrConfig[$arrLine[0]]=$arrLine[1];
}
};
?>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-dot-circle-o fa-fw"></i> Configure hotspot
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<!-- Nav tabs -->
<ul class="nav nav-tabs">
<li class="active"><a href="#basic" data-toggle="tab">Basic</a>
</li>
<li><a href="#security" data-toggle="tab">Security</a>
</li>
<li><a href="#advanced" data-toggle="tab">Advanced</a>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<p><?php echo $status; ?></p>
<div class="tab-pane fade in active" id="basic">
<h4>Basic settings</h4>
<form role="form" action="/?page=save_hostapd_conf" method="POST">
<div class="row">
<div class="form-group col-md-4">
<label for="code">Interface</label>
<select class="form-control" name="interface">
<?php
exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces);
foreach( $interfaces as $int ) {
$select = '';
if( $int == $arrConfig['interface'] ) {
$select = " selected";
}
echo '<option value="'.$int.'"'.$select.'>'.$int.'</option>';
}
?>
</select>
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="code">SSID</label>
<input type="text" class="form-control" name="ssid" value="<?php echo $arrConfig['ssid']; ?>" />
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Wireless Mode</label>
<select class="form-control" name="hw_mode">
<?php
foreach( $arrChannel as $Mode ) {
$select = '';
if( $arrConfig['hw_mode'] == $Mode ) {
$select = ' selected';
}
echo '<option value="'.$Mode.'"'.$select.'>'.$Mode.'</option>';
}
?>
</select>
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Channel</label>
<select class="form-control" name="channel">'
<?php
for( $channel = 1; $channel < 14; $channel++ ) {
$select = '';
if( $channel == $arrConfig['channel'] ) {
$select = " selected";
}
echo '<option value="'.$channel.'"'.$select.'>'.$channel.'</option>';
}
?>
</select>
</div>
</div>
</div>
<div class="tab-pane fade" id="security">
<h4>Security settings</h4>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Security type</label>
<select class="form-control" name="wpa">
<?php
foreach( $arrSecurity as $SecVal => $SecMode ) {
$select = '';
if( $SecVal == $arrConfig['wpa'] ) {
$select = ' selected';
}
echo '<option value="'.$SecVal.'"'.$select.'>'.$SecMode.'</option>';
}
?>
</select>
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Encryption Type</label>
<select class="form-control" name="wpa_pairwise">
<?php
foreach( $arrEncType as $EncConf => $Enc ) {
$select = '';
if( $Enc == $arrConfig['wpa_pairwise'] ) {
$select = ' selected';
}
echo '<option value="'.$EncConf.'"'.$select.'>'.$Enc.'</option>';
} ?>
</select>
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="code">PSK</label>
<input type="text" class="form-control" name="wpa_passphrase" value="<?php echo $arrConfig['wpa_passphrase'] ?>" />
</div>
</div>
</div>
<div class="tab-pane fade in" id="advanced">
<h4>Advanced settings</h4>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Country Code</label>
<input type="text" class="form-control" name="country_code" value="<?php echo $arrConfig['country_code'] ?>" />
</div>
</div>
</div>
<input type="submit" class="btn btn-outline btn-primary" name="SaveHostAPDSettings" value="Save settings" />
<?php
if($hostapdstatus[0] == 0) {
echo '<input type="submit" class="btn btn-success" name="StartHotspot" value="Start hotspot" />';
} else {
echo '<input type="submit" class="btn btn-warning" name="StopHotspot" value="Stop hotspot" />';
};
?>
</form>
</div><!-- ./ Panel body -->
</div><!-- /.panel-primary -->
<div class="panel-footer"> Information provided by hostapd</div>
</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<?php
}
/**
*
*
*/
function DisplayDHCPConfig() {
exec( 'cat '. RASPI_DNSMASQ_CONFIG, $return );
$conf = ParseConfig($return);
$arrRange = explode( ",", $conf['dhcp-range'] );
$RangeStart = $arrRange[0];
$RangeEnd = $arrRange[1];
$RangeMask = $arrRange[2];
preg_match( '/([0-9]*)([a-z])/i', $arrRange[3], $arrRangeLeaseTime );
switch( $arrRangeLeaseTime[2] ) {
case "h":
$hselected = " selected";
break;
case "m":
$mselected = " selected";
break;
case "d":
$dselected = " selected";
break;
}
exec( 'pidof dnsmasq | wc -l',$dnsmasq );
if( $dnsmasq[0] == 0 ) {
$status = '<div class="alert alert-warning alert-dismissable">Dnsmasq is not running<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>';
} else {
$status = '<div class="alert alert-success alert-dismissable">Dnsmasq is running<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>';
}
?>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-exchange fa-fw"></i> Configure DHCP
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<!-- Nav tabs -->
<ul class="nav nav-tabs">
<li class="active"><a href="#server-settings" data-toggle="tab">Server settings</a>
</li>
<li><a href="#client-list" data-toggle="tab">Client list</a>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<p><?php echo $status; ?></p>
<div class="tab-pane fade in active" id="server-settings">
<h4>DHCP server settings</h4>
<form method="POST" action="?page=dhcpd_conf">
<div class="row">
<div class="form-group col-md-4">
<label for="code">Interface</label>
<select class="form-control" name="interface">
<?php
exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces);
foreach( $interfaces as $int ) {
$select = '';
if( $int == $conf['interface'] ) {
$select = " selected";
}
echo '<option value="'.$int.'"'.$select.'>'.$int.'</option>';
}
?>
</select>
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Starting IP Address</label>
<input type="text" class="form-control"name="RangeStart" value="<?php echo $RangeStart; ?>" />
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Ending IP Address</label>
<input type="text" class="form-control" name="RangeEnd" value="<?php echo $RangeEnd; ?>" />
</div>
</div>
<div class="row">
<div class="form-group col-xs-2 col-sm-2">
<label for="code">Lease Time</label>
<input type="text" class="form-control" name="RangeLeaseTime" value="<?php echo $arrRangeLeaseTime[1]; ?>" />
</div>
<div class="col-xs-2 col-sm-2">
<label for="code">Interval</label>
<select name="RangeLeaseTimeUnits" class="form-control" ><option value="m" <?php echo $mselected; ?>>Minutes</option><option value="h" <?php echo $hselected; ?>>Hours</option><option value="d" <?php echo $dselected; ?>>Days</option><option value="infinite">Infinite</option></select>
</div>
</div>
<input type="submit" class="btn btn-outline btn-primary" value="Save settings" name="savedhcpdsettings" />
<?php
if ( $dnsmasq[0] == 0 ) {
echo'<input type="submit" class="btn btn-success" value="Start dnsmasq" name="startdhcpd" />';
} else {
echo '<input type="submit" class="btn btn-warning" value="Stop dnsmasq" name="stopdhcpd" />';
}
?>
</form>
</div><!-- /.tab-pane -->
<div class="tab-pane fade in" id="client-list">
<h4>Client list</h4>
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
Active DHCP leases
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Expire time</th>
<th>MAC Address</th>
<th>IP Address</th>
<th>Host name</th>
<th>Client ID</th>
</tr>
</thead>
<tbody>
<tr>
<?php
exec( 'cat ' . RASPI_DNSMASQ_LEASES, $leases );
foreach( $leases as $lease ) {
$lease_items = explode(' ', $lease);
foreach( $lease_items as $lease_item ) {
echo '<td>' . $lease_item . '</td>';
}
echo '</tr>';
};
?>
</tr>
</tbody>
</table>
</div><!-- /.table-responsive -->
</div><!-- /.panel-body -->
</div><!-- /.panel -->
</div><!-- /.col-lg-6 -->'
<?php
if( isset( $_POST['savedhcpdsettings'] ) ) {
$config = 'interface='.$_POST['interface'].'
dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd'].',255.255.255.0,'.$_POST['RangeLeaseTime'].''.$_POST['RangeLeaseTimeUnits'];
exec( 'echo "'.$config.'" > /tmp/dhcpddata',$temp );
system( 'sudo cp /tmp/dhcpddata '. RASPI_DNSMASQ_CONFIG, $return );
if( $return == 0 ) {
echo "Dnsmasq configuration updated successfully";
} else {
echo "Dnsmasq configuration failed to be updated";
}
}
if( isset( $_POST['startdhcpd'] ) ) {
$line = system('sudo /etc/init.d/dnsmasq start',$return);
echo "Attempting to start dnsmasq";
}
if( isset($_POST['stopdhcpd'] ) ) {
$line = system('sudo /etc/init.d/dnsmasq stop',$return);
echo "Stopping dnsmasq";
}
?>
</div><!-- /.tab-pane -->
</div><!-- /.tab-content -->
</div><!-- ./ Panel body -->
<div class="panel-footer"> Information provided by Dnsmasq</div>
</div><!-- /.panel-primary -->
</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<?php
}
/**
@ -989,145 +405,11 @@ function DisplayTorProxyConfig(){
*
*
*/
function DisplaySystem(){
// hostname
exec("hostname -f", $hostarray);
$hostname = $hostarray[0];
// uptime
$uparray = explode(" ", exec("cat /proc/uptime"));
$seconds = round($uparray[0], 0);
$minutes = $seconds / 60;
$hours = $minutes / 60;
$days = floor($hours / 24);
$hours = floor($hours - ($days * 24));
$minutes = floor($minutes - ($days * 24 * 60) - ($hours * 60));
$uptime= '';
if ($days != 0) { $uptime .= $days . ' day' . (($days > 1)? 's ':' '); }
if ($hours != 0) { $uptime .= $hours . ' hour' . (($hours > 1)? 's ':' '); }
if ($minutes != 0) { $uptime .= $minutes . ' minute' . (($minutes > 1)? 's ':' '); }
// mem used
exec("free -m | awk '/Mem:/ { total=$2 } /buffers\/cache/ { used=$3 } END { print used/total*100}'", $memarray);
$memused = floor($memarray[0]);
if ($memused > 90) { $memused_status = "danger"; }
elseif ($memused > 75) { $memused_status = "warning"; }
elseif ($memused > 0) { $memused_status = "success"; }
// cpu load
$cores = exec("grep -c ^processor /proc/cpuinfo");
$loadavg = exec("awk '{print $1}' /proc/loadavg");
$cpuload = floor(($loadavg * 100) / $cores);
if ($cpuload > 90) { $cpuload_status = "danger"; }
elseif ($cpuload > 75) { $cpuload_status = "warning"; }
elseif ($cpuload > 0) { $cpuload_status = "success"; }
?>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-cube fa-fw"></i> System</div>
<div class="panel-body">
<?php
if (isset($_POST['system_reboot'])) {
echo '<div class="alert alert-warning">System Rebooting Now!</div>';
$result = shell_exec("sudo /sbin/reboot");
}
if (isset($_POST['system_shutdown'])) {
echo '<div class="alert alert-warning">System Shutting Down Now!</div>';
$result = shell_exec("sudo /sbin/shutdown -h now");
}
?>
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<h4>System Information</h4>
<div class="info-item">Hostname</div> <?php echo $hostname ?></br>
<div class="info-item">Uptime</div> <?php echo $uptime ?></br></br>
<div class="info-item">Memory Used</div>
<div class="progress">
<div class="progress-bar progress-bar-<?php echo $memused_status ?> progress-bar-striped active"
role="progressbar"
aria-valuenow="<?php echo $memused ?>" aria-valuemin="0" aria-valuemax="100"
style="width: <?php echo $memused ?>%;"><?php echo $memused ?>%
</div>
</div>
<div class="info-item">CPU Load</div>
<div class="progress">
<div class="progress-bar progress-bar-<?php echo $cpuload_status ?> progress-bar-striped active"
role="progressbar"
aria-valuenow="<?php echo $cpuload ?>" aria-valuemin="0" aria-valuemax="100"
style="width: <?php echo $cpuload ?>%;"><?php echo $cpuload ?>%
</div>
</div>
</div><!-- /.panel-body -->
</div><!-- /.panel-default -->
</div><!-- /.col-md-6 -->
</div><!-- /.row -->
<form action="?page=system_info" method="POST">
<input type="submit" class="btn btn-warning" name="system_reboot" value="Reboot" />
<input type="submit" class="btn btn-warning" name="system_shutdown" value="Shutdown" />
<input type="button" class="btn btn-outline btn-primary" value="Refresh" onclick="document.location.reload(true)" />
</form>
</div><!-- /.panel-body -->
</div><!-- /.panel-primary -->
</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<?php
}
/**
*
*
*/
function SaveHostAPDConfig(){
if( isset($_POST['SaveHostAPDSettings']) ) {
$config = 'driver=nl80211'.PHP_EOL
.'ctrl_interface='.RASPI_HOSTAPD_CTRL_INTERFACE.PHP_EOL
.'ctrl_interface_group=0'.PHP_EOL
.'beacon_int=100'.PHP_EOL
.'auth_algs=1'.PHP_EOL
.'wpa_key_mgmt=WPA-PSK'.PHP_EOL;
$config .= "interface=".$_POST['interface'].PHP_EOL;
$config .= "ssid=".$_POST['ssid'].PHP_EOL;
$config .= "hw_mode=".$_POST['hw_mode'].PHP_EOL;
$config .= "channel=".$_POST['channel'].PHP_EOL;
$config .= "wpa=".$_POST['wpa'].PHP_EOL;
$config .='wpa_passphrase='.$_POST['wpa_passphrase'].PHP_EOL;
$config .="wpa_pairwise=".$_POST['wpa_pairwise'].PHP_EOL;
$config .="country_code=".$_POST['country_code'];
exec( "echo '$config' > /tmp/hostapddata", $return );
system( "sudo cp /tmp/hostapddata " . RASPI_HOSTAPD_CONFIG, $return );
if( $return == 0 ) {
echo "Wifi Hotspot settings saved";
} else {
echo "Wifi Hotspot settings failed to be saved";
}
} elseif( isset($_POST['SaveOpenVPNSettings']) ) {
function SaveTORAndVPNConfig(){
if( isset($_POST['SaveOpenVPNSettings']) ) {
// TODO
} elseif( isset($_POST['SaveTORProxySettings']) ) {
// TODO
} elseif( isset($_POST['StartHotspot']) ) {
echo "Attempting to start hotspot";
exec( 'sudo /etc/init.d/hostapd start', $return );
foreach( $return as $line ) {
echo $line."<br />";
}
} elseif( isset($_POST['StopHotspot']) ) {
echo "Attempting to stop hotspot";
exec( 'sudo /etc/init.d/hostapd stop', $return );
foreach( $return as $line ) {
echo $line."<br />";
}
} elseif( isset($_POST['StartOpenVPN']) ) {
echo "Attempting to start openvpn";
exec( 'sudo /etc/init.d/openvpn start', $return );
@ -1155,4 +437,3 @@ function SaveHostAPDConfig(){
}
}
?>

227
includes/hostapd.php Executable file
View File

@ -0,0 +1,227 @@
<?php
include_once( 'includes/status_messages.php' );
/**
*
*
*/
function DisplayHostAPDConfig(){
$status = new StatusMessages();
$arrConfig = array();
$arrChannel = array('a','b','g');
$arrSecurity = array( 1 => 'WPA', 2 => 'WPA2',3=> 'WPA+WPA2');
$arrEncType = array('TKIP' => 'TKIP', 'CCMP' => 'CCMP', 'TKIP CCMP' => 'TKIP+CCMP');
exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces);
if( isset($_POST['SaveHostAPDSettings']) ) {
if (CSRFValidate()) {
SaveHostAPDConfig($arrSecurity, $arrEncType, $arrChannel, $interfaces, $status);
} else {
error_log('CSRF violation');
}
} elseif( isset($_POST['StartHotspot']) ) {
if (CSRFValidate()) {
$status->addMessage('Attempting to start hotspot', 'info');
exec( 'sudo /etc/init.d/hostapd start', $return );
foreach( $return as $line ) {
$status->addMessage($line, 'info');
}
} else {
error_log('CSRF violation');
}
} elseif( isset($_POST['StopHotspot']) ) {
if (CSRFValidate()) {
$status->addMessage('Attempting to stop hotspot', 'info');
exec( 'sudo /etc/init.d/hostapd stop', $return );
foreach( $return as $line ) {
$status->addMessage($line, 'info');
}
} else {
error_log('CSRF violation');
}
}
exec( 'cat '. RASPI_HOSTAPD_CONFIG, $return );
exec( 'pidof hostapd | wc -l', $hostapdstatus);
if( $hostapdstatus[0] == 0 ) {
$status->addMessage('HostAPD is not running', 'warning');
} else {
$status->addMessage('HostAPD is running', 'success');
}
foreach( $return as $a ) {
if( $a[0] != "#" ) {
$arrLine = explode( "=",$a) ;
$arrConfig[$arrLine[0]]=$arrLine[1];
}
};
?>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-dot-circle-o fa-fw"></i> Configure hotspot</div>
<!-- /.panel-heading -->
<div class="panel-body">
<form role="form" action="/?page=hostapd_conf" method="POST">
<!-- Nav tabs -->
<ul class="nav nav-tabs">
<li class="active"><a href="#basic" data-toggle="tab">Basic</a></li>
<li><a href="#security" data-toggle="tab">Security</a></li>
<li><a href="#advanced" data-toggle="tab">Advanced</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<p><?php $status->showMessages(); ?></p>
<div class="tab-pane fade in active" id="basic">
<h4>Basic settings</h4>
<?php CSRFToken() ?>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Interface</label>
<?php
SelectorOptions('interface', $interfaces, $arrConfig['interface']);
?>
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="code">SSID</label>
<input type="text" class="form-control" name="ssid" value="<?php echo $arrConfig['ssid']; ?>" />
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Wireless Mode</label>
<?php SelectorOptions('hw_mode', $arrChannel, $arrConfig['hw_mode']); ?>
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Channel</label>
<?php SelectorOptions('channel', range(1, 14), intval($arrConfig['channel'])) ?>
</div>
</div>
</div>
<div class="tab-pane fade" id="security">
<h4>Security settings</h4>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Security type</label>
<?php SelectorOptions('wpa', $arrSecurity, $arrConfig['wpa']); ?>
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Encryption Type</label>
<?php SelectorOptions('wpa_pairwise', $arrEncType, $arrConfig['wpa_pairwise']); ?>
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="code">PSK</label>
<input type="text" class="form-control" name="wpa_passphrase" value="<?php echo $arrConfig['wpa_passphrase'] ?>" />
</div>
</div>
</div>
<div class="tab-pane fade" id="advanced">
<h4>Advanced settings</h4>
<div class="row">
<div class="form-group col-md-4">
<label for="code">Country Code</label>
<input type="text" class="form-control" name="country_code" value="<?php echo $arrConfig['country_code'] ?>" />
</div>
</div>
</div><!-- ./ Panel body -->
<input type="submit" class="btn btn-outline btn-primary" name="SaveHostAPDSettings" value="Save settings" />
<?php
if($hostapdstatus[0] == 0) {
echo '<input type="submit" class="btn btn-success" name="StartHotspot" value="Start hotspot" />';
} else {
echo '<input type="submit" class="btn btn-warning" name="StopHotspot" value="Stop hotspot" />';
};
?>
</form>
</div><!-- /.panel-primary -->
<div class="panel-footer"> Information provided by hostapd</div>
</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<?php
}
function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) {
// It should not be possible to send bad data for these fields so clearly
// someone is up to something if they fail. Fail silently.
if (!(array_key_exists($_POST['wpa'], $wpa_array) && array_key_exists($_POST['wpa_pairwise'], $enc_types) && in_array($_POST['hw_mode'], $modes))) {
error_log("Attempting to set hostapd config with wpa='".$_POST['wpa']."', wpa_pairwise='".$_POST['wpa_pairwise']."' and hw_mode='".$_POST['hw_mode']."'");
return false;
}
if ((!filter_var($_POST['channel'], FILTER_VALIDATE_INT)) || intval($_POST['channel']) < 1 || intval($_POST['channel']) > 14) {
error_log("Attempting to set channel to '".$_POST['channel']."'");
return false;
}
$good_input = true;
// Verify input
if (strlen($_POST['ssid']) == 0 || strlen($_POST['ssid']) > 32) {
// Not sure of all the restrictions of SSID
$status->addMessage('SSID must be between 1 and 32 characters', 'danger');
$good_input = false;
}
if (strlen($_POST['wpa_passphrase']) < 8 || strlen($_POST['wpa_passphrase']) > 63) {
$status->addMessage('WPA passphrase must be between 8 and 63 characters', 'danger');
$good_input = false;
}
if (! in_array($_POST['interface'], $interfaces)) {
// The user is probably up to something here but it may also be a
// genuine error.
$status->addMessage('Unknown interface '.$_POST['interface'], 'danger');
$good_input = false;
}
if (strlen($_POST['country_code']) != 0 && strlen($_POST['country_code']) != 2) {
$status->addMessage('Country code must be blank or two characters', 'danger');
$good_input = false;
}
if ($good_input) {
if ($tmp_file = fopen('/tmp/hostapddata', 'w')) {
// Fixed values
fwrite($tmp_file, 'driver=nl80211'.PHP_EOL);
fwrite($tmp_file, 'ctrl_interface='.RASPI_HOSTAPD_CTRL_INTERFACE.PHP_EOL);
fwrite($tmp_file, 'ctrl_interface_group=0'.PHP_EOL);
fwrite($tmp_file, 'beacon_int=100'.PHP_EOL);
fwrite($tmp_file, 'auth_algs=1'.PHP_EOL);
fwrite($tmp_file, 'wpa_key_mgmt=WPA-PSK'.PHP_EOL);
fwrite($tmp_file, 'ssid='.$_POST['ssid'].PHP_EOL);
fwrite($tmp_file, 'channel='.$_POST['channel'].PHP_EOL);
fwrite($tmp_file, 'hw_mode='.$_POST['hw_mode'].PHP_EOL);
fwrite($tmp_file, 'wpa_passphrase='.$_POST['wpa_passphrase'].PHP_EOL);
fwrite($tmp_file, 'interface='.$_POST['interface'].PHP_EOL);
fwrite($tmp_file, 'wpa='.$_POST['wpa'].PHP_EOL);
fwrite($tmp_file, 'wpa_pairwise='.$_POST['wpa_pairwise'].PHP_EOL);
fwrite($tmp_file, 'country_code='.$_POST['country_code'].PHP_EOL);
fclose($tmp_file);
system( "sudo cp /tmp/hostapddata " . RASPI_HOSTAPD_CONFIG, $return );
if( $return == 0 ) {
$status->addMessage('Wifi Hotspot settings saved', 'success');
} else {
$status->addMessage('Unable to save wifi hotspot settings', 'danger');
}
} else {
$status->addMessage('Unable to save wifi hotspot settings', 'danger');
return false;
}
}
return true;
}
?>

View File

@ -0,0 +1,22 @@
<?php
class StatusMessages {
public $messages = array();
public function addMessage($message, $level='success', $dismissable=true) {
$status = '<div class="alert alert-'.$level;
if ($dismissable) $status .= ' alert-dismissable';
$status .= '">'.$message;
if ($dismissable) $status .= '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">x</button>';
$status .= '</div>';
array_push($this->messages, $status);
}
public function showMessages($clear = true) {
foreach($this->messages as $message) {
echo $message;
}
if ( $clear ) $this->messages = array();
}
}
?>

139
includes/system.php Executable file
View File

@ -0,0 +1,139 @@
<?php
/**
*
* Find the version of the Raspberry Pi
* Currently only used for the system information page but may useful elsewhere
*
*/
function RPiVersion() {
// Lookup table from http://www.raspberrypi-spy.co.uk/2012/09/checking-your-raspberry-pi-board-version/
$revisions = array(
'0002' => 'Model B Revision 1.0',
'0003' => 'Model B Revision 1.0 + ECN0001',
'0004' => 'Model B Revision 2.0 (256 MB)',
'0005' => 'Model B Revision 2.0 (256 MB)',
'0006' => 'Model B Revision 2.0 (256 MB)',
'0007' => 'Model A',
'0008' => 'Model A',
'0009' => 'Model A',
'000d' => 'Model B Revision 2.0 (512 MB)',
'000e' => 'Model B Revision 2.0 (512 MB)',
'000f' => 'Model B Revision 2.0 (512 MB)',
'0010' => 'Model B+',
'0013' => 'Model B+',
'0011' => 'Compute Module',
'0012' => 'Model A+',
'a01041' => 'a01041',
'a21041' => 'a21041',
'900092' => 'PiZero',
'a02082' => 'Pi 3 Model B',
'a22082' => 'Pi 3 Model B'
);
exec('cat /proc/cpuinfo', $cpuinfo_array);
$rev = trim(array_pop(explode(':',array_pop(preg_grep("/^Revision/", $cpuinfo_array)))));
if (array_key_exists($rev, $revisions)) {
return $revisions[$rev];
} else {
return 'Unknown Pi';
}
}
/**
*
*
*/
function DisplaySystem(){
// hostname
exec("hostname -f", $hostarray);
$hostname = $hostarray[0];
// uptime
$uparray = explode(" ", exec("cat /proc/uptime"));
$seconds = round($uparray[0], 0);
$minutes = $seconds / 60;
$hours = $minutes / 60;
$days = floor($hours / 24);
$hours = floor($hours - ($days * 24));
$minutes = floor($minutes - ($days * 24 * 60) - ($hours * 60));
$uptime= '';
if ($days != 0) { $uptime .= $days . ' day' . (($days > 1)? 's ':' '); }
if ($hours != 0) { $uptime .= $hours . ' hour' . (($hours > 1)? 's ':' '); }
if ($minutes != 0) { $uptime .= $minutes . ' minute' . (($minutes > 1)? 's ':' '); }
// mem used
exec("free -m | awk '/Mem:/ { total=$2 } /buffers\/cache/ { used=$3 } END { print used/total*100}'", $memarray);
$memused = floor($memarray[0]);
if ($memused > 90) { $memused_status = "danger"; }
elseif ($memused > 75) { $memused_status = "warning"; }
elseif ($memused > 0) { $memused_status = "success"; }
// cpu load
$cores = exec("grep -c ^processor /proc/cpuinfo");
$loadavg = exec("awk '{print $1}' /proc/loadavg");
$cpuload = floor(($loadavg * 100) / $cores);
if ($cpuload > 90) { $cpuload_status = "danger"; }
elseif ($cpuload > 75) { $cpuload_status = "warning"; }
elseif ($cpuload > 0) { $cpuload_status = "success"; }
?>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-cube fa-fw"></i> System</div>
<div class="panel-body">
<?php
if (isset($_POST['system_reboot'])) {
echo '<div class="alert alert-warning">System Rebooting Now!</div>';
$result = shell_exec("sudo /sbin/reboot");
}
if (isset($_POST['system_shutdown'])) {
echo '<div class="alert alert-warning">System Shutting Down Now!</div>';
$result = shell_exec("sudo /sbin/shutdown -h now");
}
?>
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<h4>System Information</h4>
<div class="info-item">Hostname</div> <?php echo $hostname ?></br>
<div class="info-item">Pi Revision</div> <?php echo RPiVersion() ?></br>
<div class="info-item">Uptime</div> <?php echo $uptime ?></br></br>
<div class="info-item">Memory Used</div>
<div class="progress">
<div class="progress-bar progress-bar-<?php echo $memused_status ?> progress-bar-striped active"
role="progressbar"
aria-valuenow="<?php echo $memused ?>" aria-valuemin="0" aria-valuemax="100"
style="width: <?php echo $memused ?>%;"><?php echo $memused ?>%
</div>
</div>
<div class="info-item">CPU Load</div>
<div class="progress">
<div class="progress-bar progress-bar-<?php echo $cpuload_status ?> progress-bar-striped active"
role="progressbar"
aria-valuenow="<?php echo $cpuload ?>" aria-valuemin="0" aria-valuemax="100"
style="width: <?php echo $cpuload ?>%;"><?php echo $cpuload ?>%
</div>
</div>
</div><!-- /.panel-body -->
</div><!-- /.panel-default -->
</div><!-- /.col-md-6 -->
</div><!-- /.row -->
<form action="?page=system_info" method="POST">
<input type="submit" class="btn btn-warning" name="system_reboot" value="Reboot" />
<input type="submit" class="btn btn-warning" name="system_shutdown" value="Shutdown" />
<input type="button" class="btn btn-outline btn-primary" value="Refresh" onclick="document.location.reload(true)" />
</form>
</div><!-- /.panel-body -->
</div><!-- /.panel-primary -->
</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<?php
}
?>

View File

@ -38,12 +38,27 @@ define('RASPI_OPENVPN_ENABLED', false );
define('RASPI_TORPROXY_ENABLED', false );
include_once( RASPI_CONFIG.'/raspap.php' );
include_once( 'includes/functions.php' );
include_once( 'includes/dashboard.php' );
include_once( 'includes/authenticate.php' );
include_once( 'includes/admin.php' );
include_once( 'includes/functions.php' );
include_once( 'includes/dhcp.php' );
include_once( 'includes/hostapd.php' );
include_once( 'includes/system.php' );
include_once( 'includes/configure_client.php' );
$output = $return = 0;
$page = $_GET['page'];
session_start();
if (empty($_SESSION['csrf_token'])) {
if (function_exists('mcrypt_create_iv')) {
$_SESSION['csrf_token'] = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
} else {
$_SESSION['csrf_token'] = bin2hex(openssl_random_pseudo_bytes(32));
}
}
$csrf_token = $_SESSION['csrf_token'];
?>
<!DOCTYPE html>
@ -175,7 +190,7 @@ $page = $_GET['page'];
DisplayAuthConfig($config['admin_user'], $config['admin_pass']);
break;
case "save_hostapd_conf":
SaveHostAPDConfig();
SaveTORAndVPNConfig();
break;
case "system_info":
DisplaySystem();

View File

@ -106,7 +106,7 @@ function move_config_file() {
function patch_system_files() {
install_log "Patching system sudoers file"
# patch /etc/sudoers file
sudo bash -c 'echo "www-data ALL=(ALL) NOPASSWD:/sbin/ifdown wlan0,/sbin/ifup wlan0,/bin/cat /etc/wpa_supplicant/wpa_supplicant.conf,/bin/cp /tmp/wifidata /etc/wpa_supplicant/wpa_supplicant.conf,/sbin/wpa_cli scan_results, /sbin/wpa_cli scan,/bin/cp /tmp/hostapddata /etc/hostapd/hostapd.conf, /etc/init.d/hostapd start,/etc/init.d/hostapd stop,/etc/init.d/dnsmasq start, /etc/init.d/dnsmasq stop,/bin/cp /tmp/dhcpddata /etc/dnsmasq.conf, /sbin/shutdown -h now, /sbin/reboot" | (EDITOR="tee -a" visudo)' \
sudo bash -c 'echo "www-data ALL=(ALL) NOPASSWD:/sbin/ifdown wlan0,/sbin/ifup wlan0,/bin/cat /etc/wpa_supplicant/wpa_supplicant.conf,/bin/cp /tmp/wifidata /etc/wpa_supplicant/wpa_supplicant.conf,/sbin/wpa_cli scan_results, /sbin/wpa_cli scan,/sbin/wpa_cli reconfigure:/bin/cp /tmp/hostapddata /etc/hostapd/hostapd.conf, /etc/init.d/hostapd start,/etc/init.d/hostapd stop,/etc/init.d/dnsmasq start, /etc/init.d/dnsmasq stop,/bin/cp /tmp/dhcpddata /etc/dnsmasq.conf, /sbin/shutdown -h now, /sbin/reboot" | (EDITOR="tee -a" visudo)' \
|| install_error "Unable to patch /etc/sudoers"
}