Ejemplo n.º 1
0
void ActivateAP::run()
{
   QSettings *GlobalSettings = new QSettings("/root/.WiFiHostapdAP/WiFi_Hostapd_AP.conf",QSettings::NativeFormat); // создание нового объекта
    char tmp[400], command[150] = "ifconfig ", *temp;
    bool starting = true;    
    int i = 0;
    QString WhoIAm = tr("Activate system");
    QDateTime TimeNow;
    QString dateUNIXNow;


	// Последовательность активации:
	qDebug()<< "Start activation.\n";

	////////////////////////////////////////////////////////////////////////////////////////////
	// move 1: disable interface
	QString temp_QT = GlobalSettings->value("AP/Iface", "wlan0").toString().toLocal8Bit();
	//temp = temp_QT.toAscii().data();
	temp = temp_QT.toUtf8().data();
	strcat(command, temp);
	strcat(command, " down");

	console(command, tmp);
	emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("Preparation of the device has successfully completed"), QString("1")));
	dateUNIXNow.clear(); dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
	emit send_step(tr("Preparation of the device has successfully completed"));
	emit setPercent(10);
	qDebug()<< "Preparation device" << " ... " << "OK";
	////////////////////////////////////////////////////////////////////////////////////////////
	sleep(0.5);
	////////////////////////////////////////////////////////////////////////////////////////////
	// move 2: set addresses
	////////////////////////////////////////////////////////////////////////////////////////////
	if(starting==true) {
		strcpy(command, "ifconfig ");
		strcpy(temp, "");
		temp_QT = GlobalSettings->value("AP/Iface", "wlan0").toString().toLocal8Bit();
		//temp = temp_QT.toAscii().data();
		temp = temp_QT.toUtf8().data();
		strcat(command, temp);
		strcat(command, " ");
		temp_QT = GlobalSettings->value("AP/IP_SERVER", "192.168.0.1").toString().toLocal8Bit();
		//temp = temp_QT.toAscii().data();
		temp = temp_QT.toUtf8().data();
		strcat(command, temp);
		strcat(command, " netmask ");
		strcpy(temp, "");
		temp_QT = GlobalSettings->value("AP/MASK", "255.255.255.0").toString().toLocal8Bit();
		//temp = temp_QT.toAscii().data();
		temp = temp_QT.toUtf8().data();
		strcat(command, temp);
		strcat(command, " up");
		console(command, tmp);
		strcpy(temp, "");
		sleep(0.5);

		dateUNIXNow.clear(); dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
		emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("Address setting is completed"), QString("1")));
		emit send_step(tr("Address setting is completed"));
		emit setPercent(20);
		qDebug()<< "Check device" << " ... "<< "OK";

	}
	////////////////////////////////////////////////////////////////////////////////////////////
	sleep(0.5);
	////////////////////////////////////////////////////////////////////////////////////////////
	// move 3: raise the interface
	////////////////////////////////////////////////////////////////////////////////////////////
	if(starting==true) {
		strcpy(command, "ifconfig ");
		temp_QT = GlobalSettings->value("AP/Iface", "wlan0").toString().toLocal8Bit();
		//temp = temp_QT.toAscii().data();
		temp = temp_QT.toUtf8().data();
		strcat(command, temp);
		strcat(command, " up");
		console(command, tmp);
		sleep(1);

		/* check */
		CheckProcess CheckInterface(1, GlobalSettings->value("AP/Iface", "wlan0").toString());
		if(!CheckInterface.init()) {
			dateUNIXNow.clear(); dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
			emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("The device is not ready for use!"), QString("2")));
			emit send_step(tr("The device is not ready for use!"));
			qDebug()<< "Starting device" << " ... "<< "FAIL";
			starting = false;
		} else {
			dateUNIXNow.clear(); dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
			emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("The device is ready for use"), QString("1")));
			emit send_step(tr("The device is ready for use"));
			emit setPercent(30);
			qDebug()<<"Starting device"<< " ... " << "OK"; 
		}
	}
	////////////////////////////////////////////////////////////////////////////////////////////
	sleep(0.5);
	////////////////////////////////////////////////////////////////////////////////////////////
	// move 4: to include IPv4 forwarding 
	////////////////////////////////////////////////////////////////////////////////////////////
	if(starting==true) {
		console("sysctl -w net.ipv4.ip_forward=\"1\"", tmp);
		sleep(0.5);
		/* check */
		CheckProcess CheckIPForwarding(2, "");

		if(!CheckIPForwarding.init()) {
			dateUNIXNow.clear(); dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
			emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("Failed to activate IP Forvard!"), QString("2")));
			qDebug()<<"Enable IP Forwardng"<< " ... "<< "FAIL";
			starting = false;
		}
		else {
			dateUNIXNow.clear(); dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
			emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("IP Forward is successfully activated"), QString("1")));
			emit send_step(tr("IP Forward is successfully activated"));
			emit setPercent(45);
			qDebug()<<"Enable IP Forwardng"<< " ... " << "OK"; 
		}
	}
	////////////////////////////////////////////////////////////////////////////////////////////
	sleep(0.5);
	////////////////////////////////////////////////////////////////////////////////////////////
	// move 5: include NAT
	////////////////////////////////////////////////////////////////////////////////////////////
	if(starting==true) {
		strcpy(command, "iptables -t nat -A POSTROUTING -o  ");
		temp_QT = GlobalSettings->value("DHCP/Internet_iface", "eth0").toString().toLocal8Bit();
		//temp = temp_QT.toAscii().data();
		temp = temp_QT.toUtf8().data();
		strcat(command, temp);
		strcat(command, "  -j MASQUERADE");
		sleep(0.5);
		system(command);

		dateUNIXNow.clear(); dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
		emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("NAT is successfully activated"), QString("1")));
		emit send_step(tr("NAT is successfully activated"));
		emit setPercent(65);
		qDebug()<<"Set rules to IPTABLES"<< " ... " << "OK";
	}
	////////////////////////////////////////////////////////////////////////////////////////////
	sleep(0.5);
	////////////////////////////////////////////////////////////////////////////////////////////
	// move 6: raising DNSMASQ
	////////////////////////////////////////////////////////////////////////////////////////////
	if(starting==true) {
		system(QString(Platform.CommandToStartDNSMASQ).toLocal8Bit().data());

		/* check */
		sleep(3);
		CheckProcess CheckDNSMASQ("dnsmasq");
		sleep(0.1);

		if(!CheckDNSMASQ.init()) {
			//TODO: Error info to log?
			dateUNIXNow.clear(); dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
			emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("Failed to start DNSMASQ!"), QString("2")));
			emit send_step(tr("Failed to start DNSMASQ!"));
			qDebug()<< "Starting DNSMASQ" << " ... "<< "FAIL";
			starting=false;
		}
		else {
			dateUNIXNow.clear(); dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
			emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("DNSMASQ successfully launched"), QString("1")));
			emit send_step(tr("DNSMASQ successfully launched"));
			emit setPercent(80);
			qDebug()<< "Starting DNSMASQ" << " ... "<< "OK";
		}
	}
	////////////////////////////////////////////////////////////////////////////////////////////
	sleep(0.5);
	////////////////////////////////////////////////////////////////////////////////////////////
	// move 7: raising HOSTAPD
	////////////////////////////////////////////////////////////////////////////////////////////
	if(starting==true) {
		system(QString(Platform.CommandToStartHostapd).toLocal8Bit().data());
		sleep(4);
		/* check */
		CheckProcess CheckHostapd("hostapd");
		sleep(0.1);

		if(!CheckHostapd.init()) {
			dateUNIXNow.clear(); dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
			 emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("Failed to start Hostapd!"), QString("2")));
			emit send_step(tr("Failed to start Hostapd!"));
			qDebug()<<command<< " ... "<< "FAIL";
			starting = false;
		}
		else {
			dateUNIXNow.clear(); dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
			emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("Hostapd successfully launched"), QString("1")));
			emit send_step(tr("Hostapd successfully launched"));
			emit setPercent(95);
			qDebug()<<command<< " ... " << "OK"; 
		}
	}
	////////////////////////////////////////////////////////////////////////////////////////////
	sleep(0.5);
	////////////////////////////////////////////////////////////////////////////////////////////
	// move 8: checking and debriefing
	////////////////////////////////////////////////////////////////////////////////////////////
	if(starting==true) {
		// This means that all steps are performed and the access point is started
		 qDebug()<< tr("AP is running!\n");
		 emit setPercent(100);
		 emit send_step(tr("AP successfully activated"));
		 emit reset_status_active();
	} else {
		// This means that the access point is not started!
		qDebug()<< tr("AP is NOT running!\n");
		emit reset_status_active();
	}
	////////////////////////////////////////////////////////////////////////////////////////////

	dateUNIXNow.clear(); dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
	emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, QString(tr("Activation sequence is completed")), QString("0")));
	delete GlobalSettings;


	exec();
}
Ejemplo n.º 2
0
void DisActiveAP::run()
{
    QSettings *GlobalSettings = new QSettings("/root/.WiFiHostapdAP/WiFi_Hostapd_AP.conf",QSettings::NativeFormat); // создание нового объекта
    char tmp[400],  command[150] = "ifconfig ", *temp;
    bool starting = true;
    QString temp_QT;
    QDateTime TimeNow;
    QString dateUNIXNow;
    QString WhoIAm = tr("Disactivate system");
    bool StatusTest = false;
    int step = 0;
// Последовательность дезактивации:

    qDebug()<< "Start disactivation.\n";

    if(starting) {
////////////////////////////////////////////////////////////////////////////////////////////
// Шаг 1: отключить интерфейс
        temp_QT = GlobalSettings->value("AP/Iface", "wlan0").toString().toLocal8Bit();
        temp = temp_QT.toLatin1().data();
        strcat(command, temp);
        strcat(command, " down");
        console(command, tmp);

        dateUNIXNow.clear();
        dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
        emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("The device is successfully disabled"), QString("1")));
        emit send_step(tr("The device is successfully disabled"));
        emit setPercent(25);
        qDebug()<<"Stoping device "<< " ... " << "OK";
    }
////////////////////////////////////////////////////////////////////////////////////////////

    if(starting) {
////////////////////////////////////////////////////////////////////////////////////////////
// Шаг 2: отключить DNSMASQ
        CheckProcess CheckDNSMASQ("dnsmasq");
        StatusTest = CheckDNSMASQ.init();
        step = 0;
        if(StatusTest) {
            while(true) {
                system(QString(Platform.CommandToStoptDNSMASQ).toLocal8Bit().data());
                sleep(1);
                StatusTest = CheckDNSMASQ.init();

                if(!StatusTest || step >2)
                    break;
                step++;
            }
        }
        StatusTest = CheckDNSMASQ.init();

        if(StatusTest) {
            dateUNIXNow.clear();
            dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
            emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("Failed to disable the DNSMASQ!"), QString("2")));
            emit send_step(tr("Failed to disable the DNSMASQ!"));
            qDebug()<<"Stoping DNSMASQ "<< " ... "<< "FAIL";
            starting = false;
        }
        else {
            dateUNIXNow.clear();
            dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
            emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("DNSMASQ is disabled successfully"), QString("1")));
            emit send_step(tr("DNSMASQ is disabled successfully"));
            emit setPercent(50);
            qDebug()<<"Stoping DNSMASQ "<< " ... "<< "OK";
        }
    }
////////////////////////////////////////////////////////////////////////////////////////////

    if(starting) {
////////////////////////////////////////////////////////////////////////////////////////////
// Шаг 3: отключить HOSTAPD
////////////////////////////////////////////////////////////////////////////////////////////

        StatusTest = false;
        CheckProcess CheckHostapd("hostapd");
        StatusTest = CheckHostapd.init();
        step = 0;
        if(StatusTest) {
            while(true) {
                system(QString(Platform.CommandToStoptHostapd).toLocal8Bit().data());
                sleep(2);
                StatusTest = CheckHostapd.init();

                if(!StatusTest || step>4)
                    break;
                step++;
            }
        }

        StatusTest = CheckHostapd.init();

        if(StatusTest) {
            dateUNIXNow.clear();
            dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
            emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("Failed to disable the Hostapd!"), QString("2")));
            emit send_step(tr("Failed to disable the Hostapd!"));
            qDebug()<<"Stoping Hostapd "<< " ... "<< "FAIL";
            starting = false;
        }
        else {
            dateUNIXNow.clear();
            dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
            emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("Hostapd is disabled successfully"), QString("1")));
            emit send_step(tr("Hostapd is disabled successfully"));
            emit setPercent(75);
            qDebug()<<"Stoping Hostapd "<< " ... "<< "OK";
        }
    }
////////////////////////////////////////////////////////////////////////////////////////////

    if(starting) {
////////////////////////////////////////////////////////////////////////////////////////////
// Шаг 4: отключаем форвардинг
////////////////////////////////////////////////////////////////////////////////////////////
        console("sysctl -w net.ipv4.ip_forward=\"0\"", tmp);

// Проверяем
        CheckProcess CheckIPForwarding(2, "");

        if(CheckIPForwarding.init()) {
            dateUNIXNow.clear();
            dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
            emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("Failed to disable the IP Forward!"), QString("2")));
            emit send_step(tr("Failed to disable the IP Forward!"));
            qDebug()<<"Disabled IPForwarding "<< " ... "<< "FAIL";
            starting = false;

        } else {
            dateUNIXNow.clear();
            dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
            emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("IP Forward successfully disabled"), QString("1")));
            emit send_step(tr("IP Forward successfully disabled"));
            emit setPercent(95);
            qDebug()<<"Disabled IPForwarding "<< " ... "<< "OK";
        }
    }

////////////////////////////////////////////////////////////////////////////////////////////
// Шаг 5: проверка и разбор полёта
////////////////////////////////////////////////////////////////////////////////////////////
    if(starting==true) {
// Это означает, что все шаги выполнены и точка доступа остановлена
        qDebug()<<"AP is stoped!\n";
        emit setPercent(100);
        sleep(0.5);
        emit send_step(tr("AP successfully deactivated"));
        emit reset_status_disactive();
    } else {
// А это означает, что точка доступа не остановлена!
        qDebug()<<"AP is NOT stoped!\n";
        sleep(0.25);
        emit reset_status_disactive();
    }
////////////////////////////////////////////////////////////////////////////////////////////
    dateUNIXNow.clear();
    dateUNIXNow = QString::number(TimeNow.currentDateTime().toTime_t());
    emit toLog(QString("%1|%2|%3|%4").arg(WhoIAm, dateUNIXNow, tr("Shutdown sequence is completed"), QString("0")));

    exec();
    delete GlobalSettings;
}