Skip to content

Commit

Permalink
Fix saving/loading trace(sniff) files
Browse files Browse the repository at this point in the history
Fix file extension for RRG repo
Fix overwriting existing file
Add support for RRG v4.16717(path in prefs)
  • Loading branch information
wh201906 committed Jun 27, 2023
1 parent 0738aff commit 184a9ed
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
4 changes: 3 additions & 1 deletion config/config_rrgv4.16717.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@
"cmd": "hf mf darkside"
},
"save sniff": {
"cmd": "trace save -f <filename>"
"cmd": "trace save -f <filename>",
"path cmd":"prefs show",
"path pattern":"trace save path\\.+\\s*(.+)$"
},
"load sniff": {
"cmd": "trace load -f <filename>",
Expand Down
15 changes: 15 additions & 0 deletions src/module/mifare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1375,3 +1375,18 @@ quint16 Mifare::getTrailerBlockId(quint8 sectorId, qint8 cardTypeId)
// other cardTypeId: use current cardtype(include default -1)
return (cardType.blks[sectorId] + cardType.blk[sectorId] - 1);
}

QString Mifare::getTraceSavePath()
{
QVariantMap config = configMap["save sniff"].toMap();
QString pathCmd = config["path cmd"].toString();
QString patternText = config["path pattern"].toString();
QRegularExpression pattern = QRegularExpression(patternText, QRegularExpression::MultilineOption);
if(pathCmd.isEmpty() || patternText.isEmpty())
return QString();
QString result = util->execCMDWithOutput(pathCmd, 500);
QRegularExpressionMatch reMatch = pattern.match(result);
if(!reMatch.hasMatch())
return QString();
return reMatch.captured(1).trimmed();
}
1 change: 1 addition & 0 deletions src/module/mifare.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class Mifare : public QObject
QString data_getUID();
quint16 getTrailerBlockId(quint8 sectorId, qint8 cardTypeId = -1); // -1: use current cardtype
void setConfigMap(const QVariantMap& configMap);
QString getTraceSavePath();
public slots:
signals:

Expand Down
47 changes: 38 additions & 9 deletions src/ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -956,18 +956,31 @@ void MainWindow::on_MF_Sniff_loadButton_clicked() // use a tmp file to support c
{
QString title = "";
QString filename = "";
QString defaultExtension;
QDir clientTracePath;

if(Util::getClientType() == Util::CLIENTTYPE_OFFICIAL)
defaultExtension = ".trc";
else if(Util::getClientType() == Util::CLIENTTYPE_ICEMAN)
defaultExtension = ".trace";

QString userTraceSavePath = mifare->getTraceSavePath();
if(userTraceSavePath.isEmpty())
clientTracePath = *clientWorkingDir;
else
clientTracePath = QDir(userTraceSavePath); // For v4.16717 and later

title = tr("Plz select the trace file:");
filename = QFileDialog::getOpenFileName(this, title, clientWorkingDir->absolutePath(), tr("Trace Files(*.trc)") + ";;" + tr("All Files(*.*)"));
filename = QFileDialog::getOpenFileName(this, title, clientTracePath.absolutePath(), tr("Trace Files") + "(*" + defaultExtension + ")" + ";;" + tr("All Files(*.*)"));
qDebug() << filename;
if(filename != "")
{
QString tmpFile = "tmp" + QString::number(QDateTime::currentDateTime().toTime_t()) + ".trc";
if(QFile::copy(filename, clientWorkingDir->absolutePath() + "/" + tmpFile))
QString tmpFile = "tmp" + QString::number(QDateTime::currentDateTimeUtc().toTime_t()) + defaultExtension;
if(QFile::copy(filename, clientTracePath.absolutePath() + "/" + tmpFile))
{
mifare->loadSniff(tmpFile);
util->delay(3000);
QFile::remove(clientWorkingDir->absolutePath() + "/" + tmpFile);
QFile::remove(clientTracePath.absolutePath() + "/" + tmpFile);
}
else
{
Expand All @@ -980,25 +993,41 @@ void MainWindow::on_MF_Sniff_saveButton_clicked()
{
QString title = "";
QString filename = "";
QString defaultExtension;
QDir clientTracePath;

if(Util::getClientType() == Util::CLIENTTYPE_OFFICIAL)
defaultExtension = ".trc";
else if(Util::getClientType() == Util::CLIENTTYPE_ICEMAN)
defaultExtension = ".trace";

QString userTraceSavePath = mifare->getTraceSavePath();
if(userTraceSavePath.isEmpty())
clientTracePath = *clientWorkingDir;
else
clientTracePath = QDir(userTraceSavePath); // For v4.16717 and later

title = tr("Plz select the location to save trace file:");
filename = QFileDialog::getSaveFileName(this, title, clientWorkingDir->absolutePath(), tr("Trace Files(*.trc)"));
filename = QFileDialog::getSaveFileName(this, title, clientTracePath.absolutePath(), tr("Trace Files") + "(*" + defaultExtension + ")");
qDebug() << filename;
if(filename != "")
{
QString tmpFile = "tmp" + QString::number(QDateTime::currentDateTime().toTime_t()) + ".trc";
QString tmpFile = "tmp" + QString::number(QDateTime::currentDateTimeUtc().toTime_t()) + defaultExtension;
mifare->saveSniff(tmpFile);
for(int i = 0; i < 100; i++)
{
util->delay(100);
if(QFile::exists(clientWorkingDir->absolutePath() + "/" + tmpFile))
if(QFile::exists(clientTracePath.absolutePath() + "/" + tmpFile))
break;
}
if(!QFile::copy(clientWorkingDir->absolutePath() + "/" + tmpFile, filename))
// filename is not empty -> the user has chosen to overwrite the existing file
if(QFile::exists(filename))
QFile::remove(filename);
if(!QFile::copy(clientTracePath.absolutePath() + "/" + tmpFile, filename))
{
QMessageBox::information(this, tr("Info"), tr("Failed to save to") + "\n" + filename);
}
QFile::remove(clientWorkingDir->absolutePath() + "/" + tmpFile);
QFile::remove(clientTracePath.absolutePath() + "/" + tmpFile);
}

}
Expand Down

0 comments on commit 184a9ed

Please sign in to comment.