Last active
August 22, 2019 00:17
-
-
Save RoyBellingan/4263b5fe1824f97e0f74cbfc2f11406f to your computer and use it in GitHub Desktop.
9GUFilter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QCommandLineOption> | |
#include <QCommandLineParser> | |
#include <QDebug> | |
#include <QLoggingCategory> | |
#include <QProcess> | |
#include <QRegularExpression> | |
#define QSL(str) QStringLiteral(str) | |
void flushRules() { | |
} | |
std::vector<QString> getClient(int port){ | |
std::vector<QString> final; | |
QProcess process; | |
QStringList params; | |
params << "--no-header" << "--numeric" << "--tcp" << QSL("sport = %1").arg(port); | |
process.start("ss", params); | |
process.waitForFinished(5000); //quite difficult this will take more than 5 seconds... | |
QByteArray error = process.readAllStandardError(); | |
if (!error.isEmpty()) { | |
throw error; | |
} | |
QByteArray msg = process.readAllStandardOutput(); | |
msg = R"EOD( | |
ESTAB 0 0 149.202.92.229:22 37.187.132.158:60018 | |
ESTAB 0 1312 2001:41d0:1004:ce5:::22 2a02:8084:4:1a80:3168:d899:1cdc:3dac:34816 | |
)EOD"; | |
auto lines = msg.split('\n'); | |
for (auto&& line : lines) { | |
auto col = line.simplified().split(' '); | |
if(col.size() != 5){ | |
continue; | |
} | |
QString remote = col.at(4); | |
//ss usa una sintassi non standard per IPv6, quindi serve un decoderino custom | |
QRegularExpression re("(.*):\\d*"); | |
auto match = re.match(remote); | |
if(match.hasMatch()){ | |
final.push_back(match.captured(1)); | |
} | |
} | |
return final; | |
} | |
int main(int argc, char* argv[]) { | |
//Ignore any user setting | |
std::setlocale(LC_ALL, "C"); | |
//And any time zone | |
setenv("TZ", "UTC", 1); | |
tzset(); | |
QLoggingCategory::setFilterRules(QStringLiteral("*.debug=true")); | |
qDebug() << "\x1B[33m9GUFilter\x1B[0m\n\n"; | |
qDebug() << "GIT_CURRENT_SHA1: " << GIT_CURRENT_SHA1; | |
qDebug() << "GIT_STATUS: " << GIT_STATUS; | |
qDebug() << "COMPILATION_TIME: " << COMPILATION_TIME; | |
QCoreApplication application(argc, argv); | |
QCoreApplication::setApplicationName("9GUFilter"); | |
QCoreApplication::setApplicationVersion("0.01"); | |
// commandline parser | |
QCommandLineParser parser; | |
parser.addHelpOption(); | |
parser.addVersionOption(); | |
// define options | |
QCommandLineOption relaxed({{"R", "relaxed"}, "no ip filtering"}); | |
parser.addOption(relaxed); | |
// Process the actual command line arguments given by the user | |
parser.process(application); | |
if (parser.isSet(relaxed)) { | |
flushRules(); | |
exit(0); | |
} | |
qDebug() << getClient(22); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment