Proyecto

General

Perfil

Descargar (4,43 KB) Estadísticas
| Rama: | Revisión:
/*
* DLink SSH Connection API
* Alejandro Antúnez - SODITEC S.L
* (C) 2015 - 2016 */

#include "dlinkpasswordgen.h"


DLinkPasswordGen::DLinkPasswordGen(QObject *parent) : QObject(parent)
{
ssh = new QSshSocket();
connect(ssh, SIGNAL(loginSuccessful()), this, SLOT(successLog()));
connect(ssh, SIGNAL(error(QSshSocket::SshError)), this, SLOT(error(QSshSocket::SshError)));
connect(ssh, SIGNAL(onCommandReceived(QString)), this, SLOT(onRouterResponse(QString)));
}

QString DLinkPasswordGen::GetSystemPWD(){
QFile *devNameFile;
devNameFile = new QFile("/tmp/clave.txt");
//if (!devNameFile->exists()) { QMessageBox::information(this, "Error", "No se ha encontrado el fichero de clave"); exit (-1); }
devNameFile->open(QFile::ReadWrite);
QString pwd(devNameFile->readAll());
pwd = pwd.remove('\n') ;
devNameFile->close();
//char* pwd = getenv("SYS_PWD_DLINKPWD");
return QString(pwd);
}

DLinkPasswordGen::~DLinkPasswordGen(){
delete ssh;
}

void DLinkPasswordGen::ConnectRouter(){
ssh->connectToHost(HOST_ROUTER);
//qDebug()<<"LOGIN"<<USER_DEFAULT<<"@"<<PWD_DEFAULT;
ssh->login(USER_DEFAULT, PWD_DEFAULT);
}

void DLinkPasswordGen::DisconnectRouter(){
//if (Ready)
// while (!Ready){qDebug()<<"Espera Ready";};
ssh->disconnectFromHost();

}

void DLinkPasswordGen::DesconectarWIFI(){
QString ssh_command("ifconfig ra0 down");
ssh->executeCommand(ssh_command);
}

void DLinkPasswordGen::ConectarWIFI(){
QString ssh_command("ifconfig ra0 up");
ssh->executeCommand(ssh_command);
}

void DLinkPasswordGen::testConsulta(QString respuesta){
QString ssh_command("nvram get wl0_ssid");
QString respuest("nada");
ssh->executeCommandResponse(ssh_command, respuesta);
QThread::sleep(3);
//qDebug()<<"AQUI"<<respuesta<<"ALLI";
}

void DLinkPasswordGen::GeneratePWD(QString routerUser, QString routerPass, char pass[9]){
/*QString ssh_command(COMMAND_SSH);
QString command_with_pwd = ssh_command
.arg("%1")
.arg(pass);
command_with_pwd += ";nvram commit; sleep 3; reboot;";
ssh->executeCommand(command_with_pwd);
*/
QString comandCurl("curl -X POST --user \"%1:%2\" -d \"submit_button=WL_WPATable&action=ApplyTake&change_action=gozila_cgi&submit_type=save&security_varname=&security_mode_last=&wl_wep_last=&filter_mac_value=&wl0_security_mode=psk2&wl0_crypto=tkip%5aes&wl0_wpa_psk=%3&wl0_wl_unmask=0&wl0_wpa_gtk_rekey=3600&wl1_security_mode=disabled\" -H \"User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36\" -H \"Referer: http://%4/WL_WPATable.asp\" http://%4/apply.cgi >/dev/null 2>&1; sleep 40; ");
QString command_with_pwd = comandCurl.arg(routerUser, routerPass, pass, "127.0.0.1", "%2B");
ssh->executeCommand(command_with_pwd);
QThread::sleep(1);
//return GetSystemPWD();
}


void DLinkPasswordGen::onRouterResponse(QString response){
if (operations.size() == 0) return;
switch (operations.dequeue()){
case 1: // Respuesta de si hay Wifi
//qDebug() << "Solicitud WIFI respondida";
emit onWifiStateAsyncResponse(response.contains("up", Qt::CaseInsensitive));
break;
case 2: // Respuesta del SSID
//qDebug() << "Solicitud SSID respondida";
emit onWifiSSIDAsyncResponse(response);
break;
case 3: // Respuesta del Pass
//qDebug() << "Solicitud Pass respondida";
emit onWifiPassAsyncResponse(response);
break;
}
}

void DLinkPasswordGen::HayWifiAsync(){
//qDebug() << "Pido WIFI";
operations.enqueue(1);
QString ssh_consulta="cat /sys/class/net/ra0/operstate";
ssh->executeCommand(ssh_consulta);
QThread::sleep(1);
}


void DLinkPasswordGen::QueSsidAsync(){
//qDebug() << "Pido SSID";
operations.enqueue(2);
QString ssh_consulta="nvram get wl0_ssid;";
ssh->executeCommand(ssh_consulta);
QThread::sleep(1);
}

void DLinkPasswordGen::QuePassAsync(){
//qDebug() << "Pido Pass";
operations.enqueue(3);
QString ssh_consulta="nvram get wl0_wpa_psk;";
ssh->executeCommand(ssh_consulta);
QThread::sleep(1);
}

void DLinkPasswordGen::error(QSshSocket::SshError err){
Ready = false;
}


void DLinkPasswordGen::successLog(){
Ready = true;
emit onLoginSuccess();
}
(15-15/45)