Read/apply dhcp settings from json

This commit is contained in:
billz 2020-12-01 22:52:15 +00:00
parent 15fb1b714c
commit 8420bbb0da

View file

@ -284,11 +284,14 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status)
}
if ($wifiAPEnable == 1) {
$config.= 'interface=uap0'.PHP_EOL;
$ap_iface = "uap0";
} elseif ($bridgedEnable == 1) {
$config.='interface='.$_POST['interface'].PHP_EOL;
$config.= 'bridge=br0'.PHP_EOL;
$ap_iface = "br0";
} else {
$config.= 'interface='.$_POST['interface'].PHP_EOL;
$ap_iface = $_POST['interface'];
}
$config.= 'wpa='.$_POST['wpa'].PHP_EOL;
$config.= 'wpa_pairwise='.$_POST['wpa_pairwise'].PHP_EOL;
@ -297,94 +300,90 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status)
if (isset($_POST['max_num_sta'])) {
$config.= 'max_num_sta='.$_POST['max_num_sta'].PHP_EOL;
}
file_put_contents("/tmp/hostapddata", $config);
system("sudo cp /tmp/hostapddata " . RASPI_HOSTAPD_CONFIG, $return);
// Fetch dhcp-range, lease time from system config
$dhcpConfig = parse_ini_file(RASPI_DNSMASQ_PREFIX.$_SESSION['ap_interface'].'conf', false, INI_SCANNER_RAW);
$syscfg = parse_ini_file(RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', false, INI_SCANNER_RAW);
if ($wifiAPEnable == 1) {
// Enable uap0 configuration in dnsmasq for Wifi client AP mode
// Enable uap0 configuration for AP-STA mode
// Set dhcp-range from system config. If undefined, fallback to default
$dhcp_range = ($dhcpConfig['dhcp-range'] =='10.3.141.50,10.3.141.255,255.255.255.0,12h' ||
$dhcpConfig['dhcp-range'] =='') ? '192.168.50.50,192.168.50.150,12h' : $dhcpConfig['dhcp-range'];
$config = 'interface=lo,uap0 # Enable uap0 interface for wireless client AP mode'.PHP_EOL;
$config.= 'bind-dynamic # Hybrid between --bind-interfaces and default'.PHP_EOL;
$config.= 'server=8.8.8.8 # Forward DNS requests to Google DNS'.PHP_EOL;
$config.= 'domain-needed # Don\'t forward short names'.PHP_EOL;
$config.= 'bogus-priv # Never forward addresses in the non-routed address spaces'.PHP_EOL;
$config.= 'dhcp-range='.$dhcp_range.PHP_EOL;
if (!empty($dhcpConfig['dhcp-option'])) {
$config.= 'dhcp-option='.$dhcpConfig['dhcp-option'].PHP_EOL;
$dhcp_range = ($syscfg['dhcp-range'] =='10.3.141.50,10.3.141.255,255.255.255.0,12h' ||
$dhcpConfig['dhcp-range'] =='') ? '192.168.50.50,192.168.50.150,12h' : $syscfg['dhcp-range'];
$config = [ '# RaspAP uap0 configuration' ];
$config[] = 'interface=lo,uap0 # Enable uap0 interface for wireless client AP mode';
$config[] = 'bind-dynamic # Hybrid between --bind-interfaces and default';
$config[] = 'server=8.8.8.8 # Forward DNS requests to Google DNS';
$config[] = 'domain-needed # Don\'t forward short names';
$config[] = 'bogus-priv # Never forward addresses in the non-routed address spaces';
$config[] = 'dhcp-range='.$dhcp_range;
if (!empty($syscfg['dhcp-option'])) {
$config[] = 'dhcp-option='.$syscfg['dhcp-option'];
}
} else {
// Set dhcp-range from system config. If undefined, fallback to default
$dhcp_range = ($dhcpConfig['dhcp-range'] =='192.168.50.50,192.168.50.150,12h' ||
$dhcpConfig['dhcp-range'] =='') ? '10.3.141.50,10.3.141.255,255.255.255.0,12h' : $dhcpConfig['dhcp-range'];
$config = 'domain-needed'.PHP_EOL;
$config.= 'interface='.$_POST['interface'].PHP_EOL;
$config.= 'dhcp-range='.$dhcp_range.PHP_EOL;
if (!empty($dhcpConfig['dhcp-option'])) {
$config.= 'dhcp-option='.$dhcpConfig['dhcp-option'].PHP_EOL;
$dhcp_range = ($syscfg['dhcp-range'] =='192.168.50.50,192.168.50.150,12h' ||
$syscfg['dhcp-range'] =='') ? '10.3.141.50,10.3.141.255,255.255.255.0,12h' : $syscfg['dhcp-range'];
$config = [ '# RaspAP '.$_POST['interface'].' configuration' ];
$config[] = 'interface='.$_POST['interface'];
$config[] = 'domain-needed';
$config[] = 'dhcp-range='.$dhcp_range;
$ap_iface = $_POST['interface'];
if (!empty($syscfg['dhcp-option'])) {
$config[] = 'dhcp-option='.$syscfg['dhcp-option'];
}
}
$config[] = PHP_EOL;
$config = join(PHP_EOL, $config);
file_put_contents("/tmp/dnsmasqdata", $config);
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$_SESSION['ap_interface'].'conf', $return);
system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', $return);
// Set dnsmasq values from ini, fallback to default if undefined
$intConfig = parse_ini_file(RASPI_CONFIG_NETWORKING.'/'.$_POST['interface'].'.ini', false, INI_SCANNER_RAW);
$domain_name_server = ($intConfig['domain_name_server'] =='') ? '1.1.1.1 8.8.8.8' : $intConfig['domain_name_server'];
$routers = ($intConfig['routers'] == '') ? '10.3.141.1' : $intConfig['routers'];
// write options to dhcpcd.conf
$config = [ '# RaspAP '.$_POST['interface'].' configuration' ];
$config[] = 'hostname';
$config[] = 'clientid';
$config[] = 'persistent';
$config[] = 'option rapid_commit';
$config[] = 'option domain_name_servers, domain_name, domain_search, host_name';
$config[] = 'option classless_static_routes';
$config[] = 'option ntp_servers';
$config[] = 'require dhcp_server_identifier';
$config[] = 'slaac private';
$config[] = 'nohook lookup-hostname';
// fetch dhcp settings for selected interface
// todo: replace fallback values with defaults from network.json
$jsonData = json_decode(getNetConfig($ap_iface), true);
$domain_name_server = ($jsonData['StaticDNS'] =='') ? '1.1.1.1 8.8.8.8' : $jsonData['StaticDNS'];
$routers = ($jsonData['StaticRouters'] == '') ? '10.3.141.1' : $jsonData['StaticRouters'];
// Set dhcp values from system config, fallback to default if undefined
if ($bridgedEnable == 1) {
$config[] = '# RaspAP br0 configuration';
$config[] = 'interface '.$ap_iface;
$config[] = 'denyinterfaces eth0 wlan0';
$config[] = 'interface br0';
} elseif ($wifiAPEnable == 1) {
// Enable uap0 configuration in dhcpcd for Wifi client AP mode
$intConfig = parse_ini_file(RASPI_CONFIG_NETWORKING.'/uap0.ini', false, INI_SCANNER_RAW);
$ip_address = ($intConfig['ip_address'] == '') ? '192.168.50.1/24' : $intConfig['ip_address'];
$config[] = 'interface uap0';
// Enable uap0 configuration for AP-STA
$ip_address = ($jsonData['StaticIP'] == '') ? '192.168.50.1/24' : $jsonData['StaticIP'];
$config = [ '# RaspAP uap0 configuration' ];
$config[] = 'interface '.$ap_ifacee;
$config[] = 'static ip_address='.$ip_address;
$config[] = 'nohook wpa_supplicant';
} else {
// Default config
$ip_address = "10.3.141.1/24"; // fallback IP
// default IP of the AP xxx.xxx.xxx.1/24 of the selected dhcp range
// Default wlan0 config
$ip_address = ($jsonData['StaticIP'] == '') ? '10.3.141.1/24' : $jsonData['StaticIP'];
$def_ip = array();
if (preg_match("/^([0-9]{1,3}\.){3}/",$dhcp_range,$def_ip) ) $ip_address = $def_ip[0]."1/24";
// use static IP assigned to interface only, if consistent with the selected dhcp range
if (preg_match("/^([0-9]{1,3}\.){3}/",$intConfig['ip_address'],$int_ip) && $def_ip[0] === $int_ip[0]) $ip_address = $intConfig['ip_address'];
$config[] = 'interface '.$_POST['interface'];
if (preg_match("/^([0-9]{1,3}\.){3}/",$jsonData['StaticIP'],$int_ip) && $def_ip[0] === $int_ip[0]) $ip_address = $jsonData['StaticIP'];
$config = [ '# RaspAP wlan0 configuration' ];
$config[] = 'interface wlan0';
$config[] = 'static ip_address='.$ip_address;
$config[] = 'static routers='.$routers;
$config[] = 'static domain_name_server='.$domain_name_server;
// write the static IP back to the $_POST['interface'].ini file
$intConfig['interface'] = $_POST['interface'];
$intConfig['ip_address'] = $ip_address;
$intConfig['domain_name_server'] = $domain_name_server;
$intConfig['routers'] = $routers;
$intConfig['static'] = "true";
$intConfig['failover'] = "false";
write_php_ini($intConfig, RASPI_CONFIG_NETWORKING.'/'.$_POST['interface'].".ini");
$config[] = 'metric '.$jsonData['Metric'];
}
$config = join(PHP_EOL, $config);
$dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG);
$config = preg_replace('/^#\sRaspAP\s.*?(?=\s*^\s*$)/ms', $config, $dhcp_cfg, 1);
file_put_contents("/tmp/dhcpddata", $config);
if (!preg_match('/^interface\s'.$ap_iface.'$/m', $dhcp_cfg)) {
$config[] = PHP_EOL;
$config= join(PHP_EOL, $config);
$dhcp_cfg .= $config;
$status->addMessage('DHCP configuration for '.$ap_iface.' added.', 'success');
} else {
$config = join(PHP_EOL, $config);
$dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$ap_iface.'\s.*?(?=\s*^\s*$)/ms', $config, $dhcp_cfg, 1);
$status->addMessage('DHCP configuration for '.$ap_iface.' updated.', 'success');
}
file_put_contents("/tmp/dhcpddata", $dhcp_cfg);
system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $return);
if ($return == 0) {