void DialogVarConfig::guardar()
{
    QStringList retorno;
    retorno<<ui->txt1->text();
    retorno<<ui->txt2->currentText();
    retorno<<ui->txt3->text();
    retorno<<ui->txt4->text();
    retorno<<ui->txt5->text();
    retorno<<ui->txt6->text();
    retorno<<ui->txt7->text();
    retorno<<ui->txt8->text();
    retorno<<ui->txt9->text();
    retorno<<ui->txt10->text();
    if(estado == 2){
        datos.append(retorno.join(":::"));
    } else if(estado == 1){
        datos.removeAt(index);
        datos.append(retorno.join(":::"));
    }
    QFile f;
    f.setFileName("scriptsconfig");
    f.remove();
    f.open(QIODevice::WriteOnly);
    f.write(datos.join("\n").toLatin1());
    f.waitForBytesWritten(0);
    f.close();
}
void DialogVarConfig::eliminar()
{
    datos.removeAt(index);
    QFile f;
    f.setFileName("scriptsconfig");
    f.remove();
    f.open(QIODevice::WriteOnly);
    f.write(datos.join("\n").toLatin1());
    f.waitForBytesWritten(0);
    f.close();
    cargarDatos();
}
Esempio n. 3
0
void ImportConfig::on_cmdImport_clicked()
{
    if (!m_ui->txtExistingOvpn->text().isEmpty()) {
        // Import existing file
        QString configName (m_ui->txtExistingOvpn->text().replace(".ovpn", ""));
        configName = configName.right(configName.size() - configName.lastIndexOf("/") - 1);

        // Anderer name als Dateiname
        if (m_ui->rbSaveAsName->isChecked()) {
            if (!m_ui->txtNewName->text().isEmpty()) {
                configName = m_ui->txtNewName->text();
            }
        }

        QString pathToConfig (m_ui->txtExistingOvpn->text());
        // Copy config to local app data and rename it, if an other name is specified
        // Only in service mode!
        if(!Settings::getInstance()->getIsPortableClient()) {

            // If file is available copy it
            // First create folder
            QString newConfigFolderPath (QString("%1/config/%2")
                                         .arg(AppFunc::getAppSavePath())
                                         .arg(configName));
            QDir newConfigFolderPathDirectory (newConfigFolderPath);
            if (newConfigFolderPathDirectory.exists(newConfigFolderPath)){
                // A config with this name is already existing
                Message::error(QObject::tr("A configuration with this name is already existing!"));
                //
                return;
            }

            // Create path
            newConfigFolderPathDirectory.mkpath(newConfigFolderPath);

            // Now copy Files
            QString newConfigPath (QString("%1/%2.ovpn")
                                   .arg(newConfigFolderPath)
                                   .arg(configName));

            QFile::copy(pathToConfig, newConfigPath);
            //
            QString sourceDirectory (pathToConfig.left(pathToConfig.lastIndexOf("/")));
            //
            // Override old path
            pathToConfig = newConfigPath;
            // Ca

			auto copyConfigFile = [&](const QString &key)
			{
				auto keyValue = (ConfigValues::instance()->valueFromConfigKey(pathToConfig, key));

				if(!keyValue.isEmpty())
				{
					QString sourcePath (QString("%1/%2")
						.arg(sourceDirectory)
						.arg(keyValue));

					QString destName (keyValue);

					if (ConfigValues::instance()->isGivenPathAbsolute(keyValue)) {
						// Yes, override path
						sourcePath = keyValue;
						// Get the file name from path
						destName = ConfigValues::instance()->fileNameOfAbsolutePath(keyValue);
						// Change value in config
						ConfigValues::instance()->changeKeyValueInConfig(pathToConfig, key, QString("\"%1\"")
							.arg(destName));
					}
					// Copy
					QFile::copy(sourcePath, QString("%1/%2")
						.arg(newConfigFolderPath)
						.arg(destName));
				}
			};

			for(const auto &key : keys)
			{
				copyConfigFile(key);
			}
        }

        Preferences::instance()->addNewConfigToDatabase(configName, pathToConfig);
        Preferences::instance()->refreshConfigList();
        Preferences::instance()->setConnectionStatus();
        Preferences::instance()->setIcon();

        // Fertig
        Message::information(QObject::tr("Import successfully ended!"), QObject::tr("Import Configuration"));
        Preferences::instance()->refreshDialog();
        this->close();
    } else {
        // Import crypt file
        if (m_ui->txtPassword->text().isEmpty()) {
            Message::error(QObject::tr("No password specified!"), QObject::tr("Import Configuration"));
            return;
        }
        if (!m_ui->txtImportPath->text().isEmpty()) {
            if (m_ui->rbSaveAsName->isChecked() && m_ui->txtNewName->text().isEmpty()) {
                Message::error(QObject::tr("No import name specified!"), QObject::tr("Import Configuration"));
                return;
            }

            // Portale oder install
            QString dirPath;
            QString configName;

            if (!m_ui->rbSaveAsName->isChecked()) {
                configName = m_ui->txtImportPath->text().right(m_ui->txtImportPath->text().size() - m_ui->txtImportPath->text().lastIndexOf("/") -1);
                configName = configName.left(configName.size()-6);
            } else {
                configName = m_ui->txtNewName->text().trimmed();
            }

            dirPath = AppFunc::getAppSavePath() + QString ("/config/") + configName;

            // Verzeichnis da?
            QDir dirobj (dirPath);
            if (!dirobj.exists(dirPath)){
                //Verzeichnis existiert nicht
                // Pfad erstellen
                if (!dirobj.mkpath(dirPath)) {
                    // Pfad konnte nicht erstellt werden
                    Message::error(QObject::tr("Unable to create directory!"), QObject::tr("Import Configuration"));
                    return;
                }
            } else {
                // Verzeichnis existiert
                Message::error(QObject::tr("A diretory with this name already exists!"), QObject::tr("Import Configuration"));
                return;
            }
            // Datei ins neue Verzeichnis kopieren
            //QFile importFileCrypt (m_ui->txtImportPath->text());
            QString packFile = dirPath + QString("/") + configName + QString(".zip");

            // Erstmal entschlüsseln
            if(QFile::exists(packFile)) {
                QFile::remove(packFile);
            }

            // Die Daten einlesen
            {
                QFile crypted (m_ui->txtImportPath->text());
                if (crypted.open(QIODevice::ReadOnly)) {
                    // Nun das Ziel öfffnen
                    QFile targetFile (packFile);
                    if (targetFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
                        // Alles offen, kann losgehen
                        QByteArray data = crypted.readAll();
                        Crypt crypt;
                        crypt.setSecretKey(m_ui->txtPassword->text());
                        targetFile.write(crypt.cryptToPlainTextExt(data));
                        targetFile.waitForBytesWritten(3000);
                        targetFile.flush();
                        targetFile.waitForBytesWritten(3000);

                        // Alles Ok
                        targetFile.close();
                        crypted.close();
                    }
                } else {
                    Message::error(QObject::tr("Can't open crypted file"));
                    return;
                }
            }

            // Nun die Datei entpacken
            if (!Zip::extract(packFile, dirPath)) {
                Message::error(QObject::tr("Can't open zip file"));
                return;
            }

            // Nun ist alles entpackt, das Ziparchiv nun löschen
            QFile configZip (packFile);
            if (!configZip.remove()) {
                Message::error(configZip.errorString(), QObject::tr("Import Configuration"));
            }

            QString saveName;
            QString savePath;
            QString ovpnFilePath = m_ui->txtImportPath->text().right(m_ui->txtImportPath->text().size() - m_ui->txtImportPath->text().lastIndexOf("/") -1);

            saveName = ovpnFilePath.left(ovpnFilePath.size()-6);
            savePath = dirPath + QString("/") + ovpnFilePath.left(ovpnFilePath.size()-6) + QString(".ovpn");

            if (m_ui->rbSaveAsName->isChecked()) {
                // ovpn umbennen
                QFile ovpnFile (savePath);
                if (ovpnFile.exists()) {
                    // umbenennen
                    ovpnFile.rename(dirPath + QString("/") + configName + QString(".ovpn"));
                    saveName = configName;
                }
            }
            savePath = dirPath + QString("/") + saveName + QString(".ovpn");

            QFile ovpnFile (dirPath + QString("/") + configName + QString(".ovpn"));
            if (!ovpnFile.exists()) {
                Message::error(QObject::tr("Import failed! Removing empty directory."), QObject::tr("Import Configuration"));
                dirobj.rmdir(dirPath);
            } else {
                Preferences::instance()->addNewConfigToDatabase(saveName, savePath.replace("\\", "/"));
                Preferences::instance()->refreshConfigList();
                Preferences::instance()->setConnectionStatus();
                Message::information(QObject::tr("Import successfully ended!"), QObject::tr("Import Configuration"));
                Preferences::instance()->refreshDialog();
                Preferences::instance()->setIcon();
                this->close();
            }
        } else {
            Message::error(QObject::tr("No import file selected!"), QObject::tr("Import Configuration"));
        }
    }
}
Esempio n. 4
0
void Debug::log(const QString &message, DebugLevel::Level level, const QString &filename)
{
    // Schreibt die Logmeldung, wenn ein bestimmtes Level gesetzt ist
    if (debugEnabled) {
        // Level überprüfen
        if (level <= debugLevel) {
            // Soll das Datum mit erfasst werden
            QString timeDate ("");
            if (dateTimeEnabled) {
                QString dateFormat (QLatin1String("dd.MM.yyyy hh:mm:ss"));
                // Millisekunden ausgeben
                if (mSecsEnabled) {
                    dateFormat = QLatin1String("dd.MM.yyyy hh:mm:ss.zzz");
                }
                timeDate = QDateTime::currentDateTime().toString(dateFormat);
            }

            // Ist der Pfad gefüllt
            if (debugPath.isEmpty()) {
                return;
            }
            // Ist der Pfad vorhanden
            QDir debugDir (debugPath);
            if(!debugDir.exists()) {
                // Pfad erstellen
                if (!debugDir.mkpath(debugPath)) {
                    // Fehler
                    return;
                }
            }

            // Soll eine extra Datei erstellt werden?
            QString fileName ("debug_vpn.txt");
            if (!filename.isEmpty()) {
                fileName = filename;
            }

            // Nun die Einrückrung
            QString indent;
            // Die Stackgröße spiegelt die Einrücktiefe wieder
            indent = indent.fill(QChar('\t'), debugCategories.size());

            // Das Level herausfinden
            QString levelName ("");
            switch (level) {
                case DebugLevel::Error:
                    levelName = QLatin1String("Error");
                    if (debugLevel == DebugLevel::Error) {
                        // Die Einrücktiefe ist 0
                        indent.clear();
                    }
                    break;
                case DebugLevel::Category:
                    levelName = QLatin1String("Category");
                    break;
                case DebugLevel::Function:
                    levelName = QLatin1String("Function");
                    break;
                case DebugLevel::Comment:
                    levelName = QLatin1String("Comment");
                    break;                
                case DebugLevel::Database:
                    levelName = QLatin1String("Database");
                    break;
                case DebugLevel::Debug:
                    levelName = QLatin1String("Debug");
                    break;
                case DebugLevel::Construktor:
                    levelName = QLatin1String("Construktor");
                    break;
                case DebugLevel::Destruktor:
                    levelName = QLatin1String("Destruktor");
                    break;
                default:
                    // Diesen Fall sollte es nicht geben
                    // Da es nur ein Flag für die Ausgabesteuerung ist
                    levelName = QLatin1String("ALL");
            }

            // LineFeed bestimmen
            QLatin1String lineFeed ("\r\n");
#ifndef Q_OS_WIN32
            lineFeed = QLatin1String ("\n");
#endif

            // Alles bearbeitet, nun den String ausgeben
            // Datei öffnen
            QFile debugFile (debugPath + fileName);
            if (debugFile.open(QIODevice::WriteOnly | QIODevice::Append)) {
                // Datei ist offen nun schreiben
                QString data (indent + timeDate + QLatin1String (" ") + levelName  + QLatin1String (" -> ") + message + lineFeed);
                debugFile.write(data.toAscii());
                // Kurz warten
                debugFile.waitForBytesWritten(1000);
                // Datei wieder schließen
                debugFile.close();
            }
        }
    }
}
void ExportWidget::on_cmdExport_clicked()
{
    QString exportKey = "insecure";

    if(!ui.txtExportKey->text().isEmpty())
        exportKey = ui.txtExportKey->text();
    else
    {
        // TODO: notify user password is not secure
    }

    bool fError (false);
    QString errMes ("");

    if (ui.txtDestFile->text().isEmpty()) {
        fError = true;
        errMes = QObject::tr("Invalid path specify[empty]!");
    }

    if (!fError) {
        if (exportKey.isEmpty()) {
            fError = true;
            errMes = QObject::tr("Invalid password specify[empty]!");
        }
    }
    // Viel zu tun
    // Dateien ermitteln, packen, verschlüsseln und löschen
    if (!fError) {
        if (!this->configPath.isEmpty()) {
            // Config öffen und Zertifikate auslesen
            QFile configFile (this->configPath);

            if (!configFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
                Message::error(QObject::tr("Can't open config!"), QObject::tr("Export Configuration"), this);
                return;
            }

            // Besteht das Verzeichnis schon?
            QString dirPath = ui.txtDestFile->text();
            QString zipFile = dirPath + QLatin1String("/export.zip");
            QString configName = this->configPath.right(this->configPath.size() - this->configPath.lastIndexOf("/") -1);
            configName = configName.left(configName.size()-5);
            QString cryptFile = dirPath + QLatin1String("/") + configName + QLatin1String(".crypt");
            QDir dirobj (dirPath);
            if (!dirobj.exists(dirPath)){
                //Verzeichnis existiert nicht
                // Pfad erstellen
                if (!dirobj.mkpath(dirPath)) {
                    // Pfad konnte nicht erstellt werden
                    Message::error(QObject::tr("Unable to create directory!"), QObject::tr("Export Configuration"), this);
                    return;
                }
            } else {
                // Verzeichnis existiert
                QFile file(cryptFile);
                if (file.exists()) {
                    // Datei existiert bereits
                    // löschen
                    if (!file.remove()) {
                        Message::error(file.errorString(), QObject::tr("Export Configuration"), this);
                        return;
                    }
                }
            }

            // Alle Daten da, packen
            bool error (false);
			if (!CZip::archive(zipFile, this->configPath.left(this->configPath.lastIndexOf("/")), false)) {
                error = true;
            }

            if (error) {
                Message::error(QObject::tr("Can't zip the data."), QObject::tr("Export Configuration"), this);
                return;
            }

            // Nun verschlüsseln
            if (!fError) {
                QFile zipToCrypt (zipFile);
                if (zipToCrypt.open(QIODevice::ReadOnly)) {
                    QFile cryptedFile (cryptFile);
                    if (cryptedFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
                        // Daten einlesen, verschlüsseln und wieder schreiben
                        QByteArray data;
                        data = zipToCrypt.readAll();
                        // Crypt bauen

                        auto oldKey = Crypt::secretKey;
                        // Den Schlüssel setzen
                        Crypt::setSecretKey(exportKey);
                        // Verschlüsseln und schreiben
                        cryptedFile.write(Crypt::encodePlaintextAscii(data).toLatin1());
                        cryptedFile.waitForBytesWritten(2000);
                        cryptedFile.flush();
                        cryptedFile.waitForBytesWritten(2000);

                        // Nun alles wieder schliessen
                        cryptedFile.close();
                        zipToCrypt.close();

                        Crypt::setSecretKey(oldKey);
                        // Alles fertig
                    } else {
                        Message::error(QObject::tr("Can't open crypt file."), QObject::tr("Export Configuration"), this);
                    }
                } else {
                    Message::error(QObject::tr("Can't read zip data."), QObject::tr("Export Configuration"), this);
                }            
            }

            // Das Zip-File nun löschen
            QFile configZip (zipFile);
            if (configZip.exists()) {
                if (!configZip.remove()) {
                    fError = true;
                    errMes = configZip.errorString();
                }
            }
        } else {
           FrmMain::instance()->mainWidget()->showWidget(MainView);
           Message::error(QObject::tr("No configuration"), QObject::tr("Export Configuration"), this);
        }
    }

    if (fError) {
        Message::error(errMes, QObject::tr("Export Configuration"), this);
        return;
    } else {
        Message::information(QObject::tr("Export successfully ended!"), QObject::tr("Export Configuration"), this);
        FrmMain::instance()->mainWidget()->showWidget(MainView);
    }
}