Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 50 additions & 29 deletions src/src/aria2/aria2rpcinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@

Aria2RPCInterface *Aria2RPCInterface::m_instance = new Aria2RPCInterface;

QStringList runPipeProcess(const QString &command, const QString &filter)
{
QProcess process;
process.start(command);
process.waitForFinished();

QString comStr = process.readAllStandardOutput();
QStringList lines = comStr.split('\n');
QStringList filteredLines;
if(filter.isEmpty()) {
return lines; //返回所有
}
// 过滤包含指定关键字的行
for (const QString &line : lines) {
if (line.contains(filter, Qt::CaseInsensitive)) {
filteredLines.append(line);
}
}

// 合并过滤后的行并返回
return filteredLines; // 返回以换行符分隔的字符串
}

Aria2RPCInterface *Aria2RPCInterface::instance()
{
// qDebug() << "[Aria2RPC] instance function started";
Expand Down Expand Up @@ -90,15 +113,10 @@
*设置aria2c session 路径 时间 input 路径
*/
QString sessionCacheFile = QDir::homePath() + "/.cache/uos-aria2c.session"; //session 文件路径
QString inputFile = QDir::homePath() + "/.cache/uos-aria2c.input"; //.input文件路径
QString dhtFile = QDir::homePath() + "/.config/uos/downloader/dht.dat"; //
QString dht6File = QDir::homePath() + "/.config/uos/downloader/dht6.dat"; //
QString saveSessionInterval = "30"; //秒

qDebug() << "创建session缓存文件: " << sessionCacheFile;
QProcess::execute("touch", QStringList() << sessionCacheFile); //创建session缓存文件
//QProcess::execute("touch", QStringList() << dhtFile); //创建dht文件
//QProcess::execute("touch", QStringList() << dht6File); //创建dht6文件

#if QT_VERSION_MAJOR > 5
qDebug() << "[Aria2RPC] Using Qt version > 5";
Expand Down Expand Up @@ -133,8 +151,6 @@
opt += "--save-session-interval=" + saveSessionInterval;
opt += "--enable-dht=true"; //启动dht文件
opt += "--enable-dht6=false"; //禁用dht6文件
opt += "--dht-file-path=" + dhtFile;
opt += "--dht-file-path6=" + dht6File;
opt += "--follow-metalink=false";
if(QSysInfo::currentCpuArchitecture() == "loongarch64"){
qDebug() << "[Aria2RPC] Using loongarch64 architecture, disabling async-dns";
Expand Down Expand Up @@ -181,8 +197,6 @@
opt += " --save-session-interval=" + saveSessionInterval;
opt += " --enable-dht=true"; //启动dht文件
opt += " --enable-dht6=false"; //禁用dht6文件
opt += " --dht-file-path=" + dhtFile;
opt += " --dht-file-path6=" + dht6File;
opt += " --follow-metalink=false";
if(QSysInfo::currentCpuArchitecture() == "loongarch64"){
qDebug() << "[Aria2RPC] Using loongarch64 architecture, disabling async-dns";
Expand All @@ -194,7 +208,7 @@
QProcess proc; // = new QProcess;
proc.setStandardOutputFile("/dev/null");
proc.setStandardErrorFile("/dev/null");
proc.startDetached("sh -c \"" + m_basePath + m_aria2cCmd + " " + opt + "\"");
proc.startDetached(m_basePath + m_aria2cCmd + " " + opt);
proc.waitForStarted();
#endif

Expand All @@ -210,7 +224,7 @@
// wwwww = proc.readAllStandardOutput();

bCheck = checkAria2cProc();
qDebug() << "启动aria2c完成! " << proc.state() << bCheck;
qDebug() << "启动aria2c完成! " << bCheck;
return bCheck;
}

Expand All @@ -234,17 +248,9 @@
bool Aria2RPCInterface::checkAria2cProc()
{
qDebug() << "[Aria2RPC] checkAria2cProc function started";
QProcess proc;
QStringList opt;
opt << "-c";
//opt << "ps aux | grep aria2c";
opt << "ps aux|grep " + m_aria2cCmd;
proc.start("/bin/bash", opt);
proc.waitForFinished();
QString output = QString::fromLocal8Bit(proc.readAllStandardOutput());
QStringList lineList = output.split("\n");
QStringList lineList = runPipeProcess("ps aux", m_aria2cCmd);
int cnt = 0;
for (QString t : lineList) {

Check warning on line 253 in src/src/aria2/aria2rpcinterface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Variable 't' is used to iterate by value. It could be declared as a const reference which is usually faster and recommended in C++.
if (t.isEmpty()) {
// qDebug() << "[Aria2RPC] Empty line found, continuing";
continue;
Expand All @@ -271,12 +277,28 @@
int Aria2RPCInterface::killAria2cProc()
{
qDebug() << "[Aria2RPC] killAria2cProc function started";
QStringList opt;
opt << "-c";
opt << "ps -ef|grep " + m_aria2cCmd + "|grep -v grep|awk '{print $2}'|xargs kill -9";
int result = QProcess::execute("/bin/bash", opt);
qDebug() << "[Aria2RPC] killAria2cProc function ended with result:" << result;
return result;
QStringList lines = runPipeProcess("ps -eo pid,lstart,cmd", m_aria2cCmd);
QStringList processPids;

for (const QString &line : lines) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
QStringList parts = line.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
#else
QStringList parts = line.split(QRegExp("\\s+"), QString::SkipEmptyParts);
#endif
if (parts.size() < 3) continue;
if (parts.size() <= 6 || !parts[6].contains(m_aria2cCmd)) continue;
processPids.append(parts[0]);
}

// 杀死以前启动的进程
for (const QString &pid : processPids) {
QProcess killProcess;
killProcess.start("kill", QStringList() << "-9" << pid);
killProcess.waitForFinished();
qInfo() << "Killed process with PID:" << pid;
}
return 0;
}

void Aria2RPCInterface::setDefaultDownLoadDir(QString dir)
Expand Down Expand Up @@ -1014,9 +1036,8 @@

QProcess *proc = new QProcess;
QStringList opt;
opt << "-c";
opt << "df " + path;
proc->start("/bin/bash", opt);
opt << path;
proc->start("df", opt);
proc->waitForFinished();
QByteArray rt = proc->readAllStandardOutput();
proc->close();
Expand Down
31 changes: 25 additions & 6 deletions src/src/extensionService/extensionservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,33 @@
#include <QDebug>

#include "websocketclientwrapper.h"
#include "websockettransport.h"

Check warning on line 48 in src/src/extensionService/extensionservice.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "websockettransport.h" not found.
#include "websockethandle.h"

Check warning on line 49 in src/src/extensionService/extensionservice.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "websockethandle.h" not found.

QStringList runPipeProcess(const QString &command, const QString &filter)
{
QProcess process;
process.start(command);
process.waitForFinished();

QString comStr = process.readAllStandardOutput();
QStringList lines = comStr.split('\n');
QStringList filteredLines;
if(filter.isEmpty()) {
return lines; //返回所有
}
// 过滤包含指定关键字的行
for (const QString &line : lines) {
if (line.contains(filter, Qt::CaseInsensitive)) {
filteredLines.append(line);
}
}

// 合并过滤后的行并返回
return filteredLines; // 返回以换行符分隔的字符串
}


extensionService::extensionService()
{
qDebug() << "[ExtensionService] Initializing WebSocket service";
Expand Down Expand Up @@ -112,14 +136,9 @@
{
qDebug() << "[ExtensionService] Checking WebSocket connections";

QProcess p;
QStringList options;
options << "-c" << "netstat -apn | grep dlmextensions";
p.start("/bin/bash", options);
p.waitForFinished();
QList<QByteArray> strList = p.readAllStandardOutput().split('\n');
QStringList strList = runPipeProcess("netstat -apn", "dlmextensions");
for(QString str : strList) {

Check warning on line 140 in src/src/extensionService/extensionservice.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Variable 'str' is used to iterate by value. It could be declared as a const reference which is usually faster and recommended in C++.
if(str.contains("ESTABLISHED")) { //存在websocket链接

Check warning on line 141 in src/src/extensionService/extensionservice.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Consider using std::any_of algorithm instead of a raw loop.
qDebug() << "[ExtensionService] Active WebSocket connection found";
return;
}
Expand Down
18 changes: 0 additions & 18 deletions src/src/ui/mainFrame/mainframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,24 +440,6 @@ void MainFrame::updateDHTFile()
}
QFile::remove(QDir::homePath() + "/.config/uos/downloader/dht.dat");
QFile::remove(QDir::homePath() + "/.config/uos/downloader/dht6.dat");

QString dhtpah = QDir::homePath() + "/.config/uos/downloader/";
static QProcess p;
#if QT_VERSION_MAJOR > 5
p.start("curl", {"--connect-timeout", "10", "-m", "20", "https://github.com/P3TERX/aria2.conf/raw/master/dht6.dat", "-o", dhtpah, "dht6.dat -O"});
#else
p.start("curl --connect-timeout 10 -m 20 https://github.com/P3TERX/aria2.conf/raw/master/dht6.dat -o" + dhtpah + "dht6.dat -O");
#endif
p.setStandardOutputFile("/dev/null");

static QProcess p2;
#if QT_VERSION_MAJOR > 5
p2.start("curl", {"--connect-timeout", "10", "-m", "20", "https://github.com/P3TERX/aria2.conf/raw/master/dht.dat", "-o", dhtpah, "dht.dat -O"});
#else
p2.start("curl --connect-timeout 10 -m 20 https://github.com/P3TERX/aria2.conf/raw/master/dht.dat -o" + dhtpah + "dht.dat -O");
#endif
p2.setStandardOutputFile("/dev/null");
qDebug() << "[MainFrame] updateDHTFile function ended";
}

void MainFrame::initConnection()
Expand Down