Esempio n. 1
0
function wrk_netconfig($redis, $action, $args = null, $configonly = null)
{
    $updateh = 0;
    switch ($action) {
        case 'setnics':
            // nics blacklist
            $excluded_nics = array('ifb0', 'ifb1', 'p2p0', 'bridge');
            // flush nics Redis hash table
            $transaction = $redis->multi();
            $transaction->del('nics');
            $interfaces = sysCmd("ip addr |grep \"BROADCAST,\" |cut -d':' -f1-2 |cut -d' ' -f2");
            $interfaces = array_diff($interfaces, $excluded_nics);
            foreach ($interfaces as $interface) {
                $ip = sysCmd("ip addr list " . $interface . " |grep \"inet \" |cut -d' ' -f6|cut -d/ -f1");
                $netmask = sysCmd("ip addr list " . $interface . " |grep \"inet \" |cut -d' ' -f6|cut -d/ -f2");
                if (isset($netmask[0])) {
                    $netmask = netmask($netmask[0]);
                } else {
                    unset($netmask);
                }
                $gw = sysCmd("route -n |grep \"0.0.0.0\" |grep \"UG\" |cut -d' ' -f10");
                $dns = sysCmd("cat /etc/resolv.conf |grep \"nameserver\" |cut -d' ' -f2");
                $type = sysCmd("iwconfig " . $interface . " 2>&1 | grep \"no wireless\"");
                runelog('interface type', $type[0]);
                // if (empty(sysCmd("iwlist ".$interface." scan 2>&1 | grep \"Interface doesn't support scanning\""))) {
                if (empty($type[0])) {
                    $speed = sysCmd("iwconfig " . $interface . " 2>&1 | grep 'Bit Rate' | cut -d '=' -f 2 | cut -d ' ' -f 1-2");
                    $currentSSID = sysCmd("iwconfig " . $interface . " | grep 'ESSID' | cut -d ':' -f 2 | cut -d '\"' -f 2");
                    $currentSSID = sysCmd("iwconfig " . $interface . " | grep 'ESSID' | cut -d ':' -f 2 | cut -d '\"' -f 2");
                    $transaction->hSet('nics', $interface, json_encode(array('ip' => $ip[0], 'netmask' => $netmask, 'gw' => $gw[0], 'dns1' => $dns[0], 'dns2' => $dns[1], 'speed' => $speed[0], 'wireless' => 1, 'currentssid' => $currentSSID[0])));
                    //// $scanwifi = 1;
                } else {
                    $speed = sysCmd("ethtool " . $interface . " 2>&1 | grep -i speed | cut -d':' -f2");
                    $transaction->hSet('nics', $interface, json_encode(array('ip' => $ip[0], 'netmask' => $netmask, 'gw' => $gw[0], 'dns1' => $dns[0], 'dns2' => $dns[1], 'speed' => $speed[0], 'wireless' => 0)));
                }
            }
            //// if ($scanwifi) sysCmdAsync('/var/www/command/refresh_nics');
            // check wpa_supplicant auto-start and purge interface list
            $wpa_supplicant_start = sysCmd("ls -lah /etc/systemd/system/multi-user.target.wants/wpa_supplicant@*.service | cut -d '@' -f 2 | cut -d '.' -f 1");
            $disable_wpa_supplicant = array_diff($wpa_supplicant_start, $interfaces);
            if (!empty($disable_wpa_supplicant)) {
                foreach ($disable_wpa_supplicant as $interface_name) {
                    unlink('/etc/systemd/system/multi-user.target.wants/wpa_supplicant@' . $interface_name . '.service');
                }
            }
            $transaction->exec();
            break;
        case 'getnics':
            foreach ($redis->hGetAll('nics') as $interface => $details) {
                $interfaces[$interface] = json_decode($details);
            }
            return $interfaces;
            break;
        case 'writecfg':
            // ArchLinux netctl config for wired ethernet
            $nic = "Description='" . $args->name . " connection'\n";
            $nic .= "Interface=" . $args->name . "\n";
            $nic .= "ForceConnect=yes\n";
            $nic .= "SkipNoCarrier=yes\n";
            if ($args->wireless === '1') {
                // Wireless configuration
                $nic .= "Connection=wireless\n";
                $nic .= "Security=wpa-config\n";
                $nic .= "WPAConfigFile='/etc/wpa_supplicant/wpa_supplicant.conf'\n";
            } else {
                // Wired configuration
                $nic .= "Connection=ethernet\n";
            }
            if ($args->dhcp === '1') {
                // DHCP configuration
                $nic .= "IP=dhcp\n";
                // Prepare data object for Redis record
                $args = array('name' => $args->name, 'dhcp' => $args->dhcp);
                $args = (object) $args;
            } else {
                // STATIC configuration
                $nic .= "AutoWired=yes\n";
                $nic .= "IP=static\n";
                $nic .= "Address=('" . $args->ip . "/" . $args->netmask . "')\n";
                $nic .= "Gateway='" . $args->gw . "'\n";
                if (!empty($args->dns2)) {
                    $nic .= "DNS=('" . $args->dns1 . "' '" . $args->dns2 . "')\n";
                } else {
                    $nic .= "DNS=('" . $args->dns1 . "')\n";
                }
            }
            // ntp sync after connect
            $nic .= "ExecUpPost='/usr/bin/ntpd -gq || true'\n";
            // set advanced DNS options
            $newArray = wrk_replaceTextLine('/etc/resolvconf.conf', '', 'resolv_conf_options=', "resolv_conf_options=('timeout:" . $redis->hGet('resolvconf', 'timeout') . " attempts:" . $redis->hGet('resolvconf', 'attempts') . "')", '#name_servers=127.0.0.1', 1);
            // Commit changes to /etc/resolvconf.conf
            $fp = fopen('/etc/resolvconf.conf', 'w');
            fwrite($fp, implode("", $newArray));
            fclose($fp);
            // tell the system to update /etc/resolv.conf
            sysCmd('resolvconf -u');
            // write current network config
            $redis->set($args->name, json_encode($args));
            $fp = fopen('/etc/netctl/' . $args->name, 'w');
            fwrite($fp, $nic);
            fclose($fp);
            if (!isset($configonly)) {
                $updateh = 1;
            }
            break;
        case 'manual':
            $file = '/etc/netctl/' . $args['name'];
            $fp = fopen($file, 'w');
            fwrite($fp, $args['config']);
            fclose($fp);
            $updateh = 1;
            break;
        case 'reset':
            wrk_netconfig($redis, 'setnics');
            $args = new stdClass();
            $args->dhcp = '1';
            $args->name = 'eth0';
            wrk_netconfig($redis, 'writecfg', $args);
            $updateh = 1;
            break;
    }
    if ($updateh === 1) {
        runelog('wireless NIC?', $args->wireless);
        // activate configuration (RuneOS)
        wrk_mpdPlaybackStatus($redis, 'record');
        sysCmd('netctl stop ' . $args->name);
        sysCmd('ip addr flush dev ' . $args->name);
        if ($args->dhcp === '1') {
            // dhcp configuration
            if ($args->wireless !== '1') {
                // $cmd = 'systemctl enable ifplugd@'.$args->name;
                sysCmd("ln -s '/usr/lib/systemd/system/netctl-ifplugd@.service' '/etc/systemd/system/multi-user.target.wants/netctl-ifplugd@" . $args->name . ".service'");
                sysCmd('systemctl daemon-reload');
                sysCmd('systemctl start netctl-ifplugd@' . $args->name);
            }
            // sysCmd('systemctl daemon-reload');
        } else {
            // static configuration
            if ($args->wireless !== '1') {
                // $cmd = 'systemctl disable ifplugd@'.$args->name;
                sysCmd("rm '/etc/systemd/system/multi-user.target.wants/netctl-ifplugd@" . $args->name . ".service'");
                sysCmd('systemctl daemon-reload');
                // kill ifplugd
                sysCmd('killall ifplugd');
            }
            // get pids of dhcpcd
            $pids = sysCmd('pgrep -xf "dhcpcd -4 -q -t 30 -L ' . $args->name . '"');
            foreach ($pids as $pid) {
                // debug
                runelog('kill pid:', $pid);
                // kill dhcpcd
                sysCmd('kill ' . $pid);
            }
        }
        // start netctl profile for wired nics
        if ($args->wireless !== '1') {
            sysCmd('netctl start ' . $args->name);
        }
        sysCmd('systemctl restart mpd');
        // set process priority
        sysCmdAsync('sleep 1 && rune_prio nice');
    }
    // update hash if necessary
    $updateh === 0 || $redis->set($args->name . '_hash', md5_file('/etc/netctl/' . $args->name));
    if (wrk_mpdPlaybackStatus($redis, 'laststate') === 'playing') {
        sysCmd('mpc play');
    }
}
Esempio n. 2
0
                $jobID[] = wrk_control($redis, 'newjob', $data = array('wrkcmd' => 'wificfg', 'action' => 'delete', 'args' => $_POST['wifiprofile']));
                break;
            case 'disconnect':
                $jobID[] = wrk_control($redis, 'newjob', $data = array('wrkcmd' => 'wificfg', 'action' => 'disconnect', 'args' => $_POST['wifiprofile']));
                break;
        }
    }
    // if (isset($_POST['wifidelete'])) {
    // $jobID[] = wrk_control($redis,'newjob', $data = array( 'wrkcmd' => 'wificfg', 'action' => 'delete', 'args' =>  $_POST['wifidelete'] ));
    // }
    if (isset($_POST['wpa_cli'])) {
        $jobID[] = wrk_control($redis, 'newjob', $data = array('wrkcmd' => 'wificfg', 'action' => 'wpa_cli', 'args' => $_POST['wpa_cli']));
    }
}
waitSyWrk($redis, $jobID);
$template->nics = wrk_netconfig($redis, 'getnics');
$template->wlan_autoconnect = $redis->Get('wlan_autoconnect');
if ($redis->hExists('wlan_profiles', urldecode($template->uri(4)))) {
    $template->stored = 1;
}
if (isset($template->action)) {
    // check if we are into interface details (ex. http://runeaudio/network/edit/eth0)
    if (isset($template->arg)) {
        // check if there is a stored profile for current nic
        $nic_stored_profile = json_decode($redis->Get($template->uri(3)));
        // runelog('nic stored profile: ',$nic_stored_profile);
        if (!empty($nic_stored_profile)) {
            if ($nic_stored_profile->dhcp === '0') {
                // read nic stored profile
                $template->nic_stored = $nic_stored_profile;
            }