From d35397f09ef53bbc278107888b2761ab1190262a Mon Sep 17 00:00:00 2001 From: billz Date: Sat, 5 Dec 2020 09:14:07 +0000 Subject: [PATCH] Remove hardcoded default values, implement defaults.json --- config/config.php | 2 +- config/defaults.json | 22 ++++++++++++++++++++++ config/network.json | 15 --------------- includes/defaults.php | 2 +- includes/hostapd.php | 33 ++++++++++++++------------------- installers/common.sh | 2 ++ 6 files changed, 40 insertions(+), 36 deletions(-) create mode 100644 config/defaults.json delete mode 100644 config/network.json diff --git a/config/config.php b/config/config.php index 790338fc..31bb9376 100755 --- a/config/config.php +++ b/config/config.php @@ -2,7 +2,7 @@ define('RASPI_BRAND_TEXT', 'RaspAP'); define('RASPI_CONFIG', '/etc/raspap'); -define('RASPI_CONFIG_NETWORK', RASPI_CONFIG.'/networking/network.json'); +define('RASPI_CONFIG_NETWORK', RASPI_CONFIG.'/networking/defaults.json'); define('RASPI_ADMIN_DETAILS', RASPI_CONFIG.'/raspap.auth'); define('RASPI_WIFI_AP_INTERFACE', 'wlan0'); define('RASPI_CACHE_PATH', sys_get_temp_dir() . '/raspap'); diff --git a/config/defaults.json b/config/defaults.json new file mode 100644 index 00000000..59f6f4a4 --- /dev/null +++ b/config/defaults.json @@ -0,0 +1,22 @@ +{ + "dhcp": { + "wlan0": { + "static ip_address": [ "10.3.141.1/24" ], + "static routers": [ "10.3.141.1" ], + "static domain_name_server": [ "1.1.1.1 8.8.8.8" ] + }, + "uap0": { + "static ip_address": [ "192.168.50.1/24" ], + "static routers": [ "192.168.50.1" ], + "static domain_name_server": [ "1.1.1.1 8.8.8.8" ] + } + }, + "dnsmasq": { + "wlan0": { + "dhcp-range": [ "10.3.141.50,10.3.141.255,255.255.255.0,12h" ], + "uap0": { + "dhcp-range": [ "192.168.50.50,192.168.50.150,12h" ] + } + } +} + diff --git a/config/network.json b/config/network.json deleted file mode 100644 index 5de32e81..00000000 --- a/config/network.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "wlan0": { - "static ip_address": [ "10.3.141.1/24" ], - "static routers": [ "10.3.141.1" ], - "static domain_name_server": [ "1.1.1.1 8.8.8.8" ], - "dhcp-range": [ "10.3.141.50,10.3.141.255,255.255.255.0,12h" ] - }, - "uap0": { - "static ip_address": [ "192.168.50.1/24" ], - "static routers": [ "192.168.50.1" ], - "static domain_name_server": [ "1.1.1.1 8.8.8.8" ], - "dhcp-range": [ "192.168.50.50,192.168.50.150,12h" ] - } -} - diff --git a/includes/defaults.php b/includes/defaults.php index 022b5347..88b52fe9 100755 --- a/includes/defaults.php +++ b/includes/defaults.php @@ -7,7 +7,7 @@ if (!defined('RASPI_CONFIG')) { $defaults = [ 'RASPI_BRAND_TEXT' => 'RaspAP', 'RASPI_VERSION' => '2.5.1', - 'RASPI_CONFIG_NETWORK' => RASPI_CONFIG.'/networking/network.json', + 'RASPI_CONFIG_NETWORK' => RASPI_CONFIG.'/networking/defaults.json', 'RASPI_ADMIN_DETAILS' => RASPI_CONFIG.'/raspap.auth', 'RASPI_WIFI_AP_INTERFACE' => 'wlan0', 'RASPI_CACHE_PATH' => sys_get_temp_dir() . '/raspap', diff --git a/includes/hostapd.php b/includes/hostapd.php index 3e5eee42..52b2a66b 100755 --- a/includes/hostapd.php +++ b/includes/hostapd.php @@ -178,6 +178,10 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) exec('sudo '.RASPI_CONFIG.'/hostapd/disablelog.sh'); } } + // set AP interface default, override for ap-sta & bridged options + $ap_iface = $_POST['interface']; + if ($wifiAPEnable) { $ap_iface = 'uap0'; } + if ($bridgedEnable) { $ap_iface = 'br0'; } // persist user options to /etc/raspap $cfg = []; @@ -189,7 +193,6 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) $cfg['WifiManaged'] = $_POST['interface']; write_php_ini($cfg, RASPI_CONFIG.'/hostapd.ini'); $_SESSION['ap_interface'] = $_POST['interface']; - $ap_iface = $_POST['interface']; // Verify input if (empty($_POST['ssid']) || strlen($_POST['ssid']) > 32) { @@ -249,10 +252,9 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) $syscfg = parse_ini_file(RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', false, INI_SCANNER_RAW); if ($wifiAPEnable == 1) { - // Enable uap0 configuration for AP-STA mode - // Set dhcp-range from system config. If undefined, fallback to default - $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']; + // Enable uap0 configuration for ap-sta mode + // Set dhcp-range from system config, fallback to default if undefined + $dhcp_range = ($syscfg['dhcp-range'] == '') ? getDefaultNetValue('dnsmasq','uap0','dhcp-range') : $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'; @@ -269,9 +271,7 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) file_put_contents("/tmp/dnsmasqdata", $config); system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', $return); } elseif ($bridgedEnable !==1) { - // Set dhcp-range from system config. If undefined, fallback to default - $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']; + $dhcp_range = ($syscfg['dhcp-range'] =='') ? getDefaultNetValue('dnsmasq','wlan0','dhcp-range') : $syscfg['dhcp-range']; $config = [ '# RaspAP '.$_POST['interface'].' configuration' ]; $config[] = 'interface='.$_POST['interface']; $config[] = 'domain-needed'; @@ -285,13 +285,10 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$ap_iface.'.conf', $return); } - // 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 + $jsonData = json_decode(getNetConfig($ap_iface), true); + $domain_name_server = ($jsonData['StaticDNS'] =='') ? getDefaultNetValue('dhcp','wlan0','static domain_name_server') : $jsonData['StaticDNS']; + $routers = ($jsonData['StaticRouters'] == '') ? getDefaultNetValue('dhcp','wlan0','static routers') : $jsonData['StaticRouters']; if ($bridgedEnable == 1) { $config = defaultHeader(); $config[] = PHP_EOL.'# RaspAP br0 configuration'; @@ -299,8 +296,8 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) $config[] = 'denyinterfaces eth0 wlan0'; $config[] = PHP_EOL; } elseif ($wifiAPEnable == 1) { - // Enable uap0 configuration for AP-STA - $ip_address = ($jsonData['StaticIP'] == '') ? '192.168.50.1/24' : $jsonData['StaticIP']; + // Enable uap0 configuration for ap-sta + $ip_address = ($jsonData['StaticIP'] == '') ? getDefaultNetValue('dhcp','uap0','static ip_address') : $jsonData['StaticIP']; $config = defaultHeader(); $config[] = PHP_EOL.'# RaspAP uap0 configuration'; $config[] = 'interface uap0'; @@ -309,7 +306,7 @@ function SaveHostAPDConfig($wpa_array, $enc_types, $modes, $interfaces, $status) $config[] = PHP_EOL; } else { // Default wlan0 config - $ip_address = ($jsonData['StaticIP'] == '') ? '10.3.141.1/24' : $jsonData['StaticIP']; + $ip_address = ($jsonData['StaticIP'] == '') ? getDefaultNetValue('dhcp','wlan0','static ip_address') : $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 @@ -408,11 +405,9 @@ function updateHostapdConfig() } 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']; diff --git a/installers/common.sh b/installers/common.sh index ce5ea8f6..4f000a8e 100755 --- a/installers/common.sh +++ b/installers/common.sh @@ -21,6 +21,7 @@ readonly raspap_sudoers="/etc/sudoers.d/090_raspap" readonly raspap_dnsmasq="/etc/dnsmasq.d/090_wlan0.conf" readonly raspap_adblock="/etc/dnsmasq.d/090_adblock.conf" readonly raspap_sysctl="/etc/sysctl.d/90_raspap.conf" +readonly raspap_network="$raspap_dir/networking/" readonly rulesv4="/etc/iptables/rules.v4" readonly notracking_url="https://raw.githubusercontent.com/notracking/hosts-blocklists/master/" webroot_dir="/var/www/html" @@ -378,6 +379,7 @@ function _default_configuration() { sudo cp $webroot_dir/config/hostapd.conf /etc/hostapd/hostapd.conf || _install_status 1 "Unable to move hostapd configuration file" sudo cp $webroot_dir/config/090_wlan0.conf $raspap_dnsmasq || _install_status 1 "Unable to move dnsmasq configuration file" sudo cp $webroot_dir/config/dhcpcd.conf /etc/dhcpcd.conf || _install_status 1 "Unable to move dhcpcd configuration file" + sudo cp $webroot_dir/config/defaults.json $raspap_network || _install_status 1 "Unable to move defaults.json settings" echo "Checking for existence of /etc/dnsmasq.d" [ -d /etc/dnsmasq.d ] || sudo mkdir /etc/dnsmasq.d