Implemented start of web interface to update Static IP addresses or use DHCP.

Currently saves to files in /etc/raspap/networking, still need to build something to generate a working config for dhcpcd
This commit is contained in:
Lawrence 2017-10-28 02:40:30 +08:00
parent e905a862b2
commit 7749b79e2f
9 changed files with 265 additions and 22 deletions

View File

@ -0,0 +1,4 @@
<?php
exec("ls /sys/class/net | grep -v lo", $interfaces);
echo json_encode($interfaces);
?>

View File

@ -0,0 +1,27 @@
<?php
session_start();
include_once('../../includes/config.php');
include_once('../../includes/functions.php');
if(isset($_POST['interface']) && isset($_POST['csrf_token']) && CSRFValidate()) {
$int = $_POST['interface'];
if(!file_exists(RASPI_CONFIG_NETWORKING.'/DHCP-'.$int)) {
touch(RASPI_CONFIG_NETWORKING.'/DHCP-'.$int.'.ini');
}
if(!file_exists(RASPI_CONFIG_NETWORKING.'/STATIC-'.$int)) {
touch(RASPI_CONFIG_NETWORKING.'/STATIC-'.$int.'.ini');
}
$intDHCPConfig = parse_ini_file(RASPI_CONFIG_NETWORKING.'/DHCP-'.$int.'.ini');
$intStaticConfig = parse_ini_file(RASPI_CONFIG_NETWORKING.'/STATIC-'.$int.'.ini');
$jsonData = ['return'=>1,'output'=>['DHCPConfig'=>$intDHCPConfig,'StaticConfig'=>$intStaticConfig]];
echo json_encode($jsonData);
} else {
$jsonData = ['return'=>2,'output'=>['Error getting data']];
echo json_encode($jsonData);
}
?>

View File

@ -0,0 +1,15 @@
<?php
session_start();
include_once('../../includes/functions.php');
if(isset($_POST['interface']) && isset($_POST['csrf_token']) && CSRFValidate()) {
$int = preg_replace('/[^a-z0-9]/','',$_POST['interface']);
exec('ip a s '.$int,$intOutput,$intResult);
$jsonData = ['return'=>$intResult,'output'=>$intOutput];
echo json_encode($jsonData);
} else {
$jsonData = ['return'=>2,'output'=>['Error getting data']];
echo json_encode($jsonData);
}
?>

View File

@ -0,0 +1,75 @@
<?php
function mask2cidr($mask){
$long = ip2long($mask);
$base = ip2long('255.255.255.255');
return 32-log(($long ^ $base)+1,2);
}
function write_php_ini($array, $file) {
$res = array();
foreach($array as $key => $val) {
if(is_array($val)) {
$res[] = "[$key]";
foreach($val as $skey => $sval) $res[] = "$skey = ".(is_numeric($sval) ? $sval : '"'.$sval.'"');
}
else $res[] = "$key = ".(is_numeric($val) ? $val : '"'.$val.'"');
}
if(safefilerewrite($file, implode("\r\n", $res))) {
return true;
} else {
return false;
}
}
function safefilerewrite($fileName, $dataToSave) {
if ($fp = fopen($fileName, 'w')) {
$startTime = microtime(TRUE);
do {
$canWrite = flock($fp, LOCK_EX);
// If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load
if(!$canWrite) usleep(round(rand(0, 100)*1000));
} while ((!$canWrite)and((microtime(TRUE)-$startTime) < 5));
//file was locked so now we can store information
if ($canWrite) {
fwrite($fp, $dataToSave);
flock($fp, LOCK_UN);
}
fclose($fp);
return true;
} else {
return false;
}
}
session_start();
include_once('../../includes/config.php');
include_once('../../includes/functions.php');
var_dump($_POST);
if(isset($_POST['interface']) && isset($_POST['csrf_token']) && CSRFValidate()) {
$int = $_POST['interface'];
$cfg = [];
if($_POST[$int.'-static'] == 'true') {
$file = "STATIC-".$int.".ini";
$ip = $_POST[$int.'-ipaddress'];
$netmask = mask2cidr($_POST[$int.'-netmask']);
$dns1 = $_POST[$int.'-dnssvr'];
$dns2 = $_POST[$int.'-dnssvralt'];
$cfg['static'] = $_POST[$int.'-static'];
$cfg['interface'] = $int;
$cfg['routers'] = $_POST[$int.'-gateway'];
$cfg['ip_address'] = $ip."/".$netmask;
$cfg['domain_name_server'] = $dns1." ".$dns2;
if(write_php_ini($cfg,RASPI_CONFIG_NETWORKING.'/'.$file)) {
$jsonData = ['return'=>0,'output'=>['Successfully Updated Network Configuration']];
} else {
$jsonData = ['return'=>1,'output'=>['Error saving network configuration']];
}
}
} else {
$jsonData = ['return'=>2,'output'=>['Error saving network configuration']];
}
echo json_encode($jsonData);
?>

View File

@ -7,7 +7,7 @@
*/
function CSRFToken() {
?>
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>" />
<input id="csrf_token" type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>" />
<?php
}

View File

@ -15,8 +15,9 @@ function DisplayNetworkingConfig(){
foreach($interfaces as $interface) {
exec("ip a show $interface",$$interface);
}
?>
CSRFToken();
?>
<div class="row">
<div class="col-md-12">
@ -42,30 +43,26 @@ function DisplayNetworkingConfig(){
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">'.$interface.'</div>
<div class="panel-body">
';
foreach(${$interface} as $line) {
echo $line.'<br />';
}
echo '
<div class="panel-body" id="'.$interface.'-summary">
</div>
</div>
</div>
</div>';
}
?>
<a href="#" class="btn btn-success btn-lg" id="btnSummaryRefresh"><i class="fa fa-refresh"></i> Refresh</a>
</div>
<?php
foreach($interfaces as $interface) {
echo '<div role="tabpanel" class="tab-pane" id="'.$interface.'">
<div class="row">
<div class="col-lg-6">
<form>
<form id="frm-'.$interface.'">
<div class="form-group">
<h4>Adapter IP Address Settings:</h4>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="'.$interface.'-addresstype" id="'.$interface.'-dhcp" autocomplete="off" checked>DHCP
<label class="btn btn-primary">
<input type="radio" name="'.$interface.'-addresstype" id="'.$interface.'-dhcp" autocomplete="off">DHCP
</label>
<label class="btn btn-primary">
<input type="radio" name="'.$interface.'-addresstype" id="'.$interface.'-static" autocomplete="off">Static IP
@ -73,11 +70,11 @@ function DisplayNetworkingConfig(){
</div>
<h4>Enable Fallback to Static Option:</h4>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" name="'.$interface.'-addresstype" id="'.$interface.'-failover" autocomplete="off" checked>Enabled
<label class="btn btn-primary">
<input type="radio" name="'.$interface.'-dhcpfailover" id="'.$interface.'-failover" autocomplete="off">Enabled
</label>
<label class="btn btn-danger">
<input type="radio" name="'.$interface.'-addresstype" id="'.$interface.'-nofailver" autocomplete="off">Disabled
<label class="btn btn-warning">
<input type="radio" name="'.$interface.'-dhcpfailover" id="'.$interface.'-nofailover" autocomplete="off">Disabled
</label>
</div>
</div>
@ -99,6 +96,12 @@ function DisplayNetworkingConfig(){
<label for="'.$interface.'-dnssvr">DNS Server</label>
<input type="text" class="form-control" id="'.$interface.'-dnssvr" placeholder="0.0.0.0">
</div>
<div class="form-group">
<label for="'.$interface.'-dnssvralt">Alternate DNS Server</label>
<input type="text" class="form-control" id="'.$interface.'-dnssvralt" placeholder="0.0.0.0">
</div>
<a href="#" class="btn btn-primary btn-lg active intsave" data-int="'.$interface.'">Save Settings</a>
<a href="#" class="btn btn-success btn-lg active intapply" data-int="'.$interface.'">Apply Settings</a>
</form>
</div>
</div>

View File

@ -17,16 +17,11 @@
* @link https://github.com/billz/raspap-webgui
* @see http://sirlagz.net/2013/02/08/raspap-webgui/
*/
/*
define('RASPI_CONFIG', '/etc/raspap');
define('RASPI_CONFIG_NETWORKING',RASPI_CONFIG.'/networking');
define('RASPI_ADMIN_DETAILS', RASPI_CONFIG.'/raspap.auth');
//if(file_exists(RASPI_CONFIG.'/raspap.auth')) {
// define('RASPI_ADMIN_DETAILS', RASPI_CONFIG.'/raspap.auth');
//} else {
// define('RASPI_ADMIN_DETAILS','');
//}
// Constants for configuration file paths.
// These are typical for default RPi installs. Modify if needed.
define('RASPI_DNSMASQ_CONFIG', '/etc/dnsmasq.conf');
@ -42,7 +37,9 @@ define('RASPI_TORPROXY_CONFIG', '/etc/tor/torrc');
// Optional services, set to true to enable.
define('RASPI_OPENVPN_ENABLED', false );
define('RASPI_TORPROXY_ENABLED', false );
*/
include_once( 'includes/config.php' );
include_once( RASPI_CONFIG.'/raspap.php' );
include_once( 'includes/functions.php' );
include_once( 'includes/dashboard.php' );
@ -248,5 +245,8 @@ $theme_url = 'dist/css/' . $theme;
<!-- Custom Theme JavaScript -->
<script src="dist/js/sb-admin-2.js"></script>
<!-- Custom RaspAP JS -->
<script src="js/custom.js"></script>
</body>
</html>

View File

@ -84,10 +84,18 @@ function create_raspap_directories() {
sudo mv $raspap_dir "$raspap_dir.`date +%F-%R`" || install_error "Unable to move old '$raspap_dir' out of the way"
fi
sudo mkdir -p "$raspap_dir" || install_error "Unable to create directory '$raspap_dir'"
# Create a directory for existing file backups.
sudo mkdir -p "$raspap_dir/backups"
# Create a directory to store networking configs
sudo mkdir -p "$raspap_dir/networking"
# Copy existing dhcpcd.conf to use as base config
cat /etc/dhcpcd.conf > "$raspap_dir/networking/defaults"
sudo chown -R $raspap_user:$raspap_user "$raspap_dir" || install_error "Unable to change file ownership for '$raspap_dir'"
}
# Fetches latest files from github to webroot

111
js/custom.js Normal file
View File

@ -0,0 +1,111 @@
function createNetmaskAddr(bitCount) {
var mask=[];
for(i=0;i<4;i++) {
var n = Math.min(bitCount, 8);
mask.push(256 - Math.pow(2, 8-n));
bitCount -= n;
}
return mask.join('.');
}
function loadSummary(strInterface) {
$.post('/ajax/networking/get_ip_summary.php',{interface:strInterface,csrf_token:csrf},function(data){
jsonData = JSON.parse(data);
console.log(jsonData);
if(jsonData['return'] == 0) {
$('#'+strInterface+'-summary').html(jsonData['output'].join('<br />'));
} else if(jsonData['return'] == 2) {
$('#'+strInterface+'-summary').append('<div class="alert alert-danger alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+jsonData['output'].join('<br />')+'</div>');
}
});
}
function getAllInterfaces() {
$.get('/ajax/networking/get_all_interfaces.php',function(data){
jsonData = JSON.parse(data);
$.each(jsonData,function(ind,value){loadSummary(value)});
});
}
function setupTabs() {
$('a[data-toggle="tab"]').on('shown.bs.tab',function(e){
var target = $(e.target).attr('href');
if(!target.match('summary')) {
var int = target.replace("#","");
loadCurrentSettings(int);
}
});
}
function loadCurrentSettings(strInterface) {
$.post('/ajax/networking/get_int_config.php',{interface:strInterface,csrf_token:csrf},function(data){
jsonData = JSON.parse(data);
//console.log(data);
$.each(jsonData['output'],function(i,v) {
//console.log(i);
var int = v['interface'];
//console.log('interface : '+int);
$.each(v,function(i2,v2) {
//console.log(i2+":"+v2);
switch(i2) {
case "static":
if(v2 == 1) {
$('#'+int+'-static').click();
}
break;
case "interface":
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 setupBtns() {
$('#btnSummaryRefresh').click(function(){getAllInterfaces();});
$('.intsave').click(function(){
var int = $(this).data('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;
arrFormData['csrf_token'] = csrf;
$.post('/ajax/networking/save_int_config.php',arrFormData,function(data){
console.log(data);
});
});
}
$().ready(function(){
csrf = $('#csrf_token').val();
pageCurrent = window.location.href.split("?")[1].split("=")[1];
pageCurrent = pageCurrent.replace("#","");
switch(pageCurrent) {
case "network_conf":
getAllInterfaces();
setupTabs();
setupBtns();
break;
}
});