Skip to content

Commit

Permalink
GetConnections, GetGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
moodyhunter committed Jun 2, 2021
1 parent 138b11e commit 44031eb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
39 changes: 37 additions & 2 deletions src/RESTfulProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

#include "qhttpserver.h"

using namespace details;

#define TokenCheck \
do \
{ \
if (request.headers().value("Qv2ray-Remote-Token") != token && request.query().queryItemValue("token") != token) \
return makeResponce(Result::INVALID_TOKEN); \
} while (0)

RESTfulProcessor::RESTfulProcessor()
{
m_manager = Qv2rayPlugin::PluginInstance->ConnectionManager();
Expand All @@ -11,8 +20,34 @@ RESTfulProcessor::~RESTfulProcessor()
{
}

void RESTfulProcessor::SetServer(QHttpServer *s)
void RESTfulProcessor::Initialize(QHttpServer *s, Qv2rayPlugin::connections::IConnectionManager *manager)
{
this->m_server = s;
s->route("/", []() { return "Hello world"; });
this->m_manager = manager;
s->route("/", []() { return makeResponce(Result::OK, "See https://github.com/moodyhunter/Qv2ray-RemoteManagement"); });

s->route("/me/address", [this](const QHttpServerRequest &request) {
TokenCheck;
return makeResponce(Result::OK, request.remoteAddress().toString());
});

s->route("/plugin", [this](const QHttpServerRequest &request) {
TokenCheck;
return makeResponce(Result::OK, "Qv2ray Remote Management Plugin");
});

s->route("/groups", [this](const QHttpServerRequest &request) {
TokenCheck;
return makeResponce(Result::OK, JsonStructHelper::Serialize(m_manager->AllGroups()));
});

s->route("/connections", [this](const QHttpServerRequest &request) {
TokenCheck;
return makeResponce(Result::OK, JsonStructHelper::Serialize(m_manager->GetConnections()));
});

s->route("/connections/<arg>", [this](const QString &gid, const QHttpServerRequest &request) {
TokenCheck;
return makeResponce(Result::OK, JsonStructHelper::Serialize(m_manager->GetConnections(GroupId(gid))));
});
}
23 changes: 22 additions & 1 deletion src/RESTfulProcessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,35 @@
#include "plugin-interface/connections/ConnectionsBase.hpp"
#include "qhttpserver.h"

namespace details
{
Q_NAMESPACE
enum class Result
{
OK,
INVALID_TOKEN
};
Q_ENUM_NS(Result)
} // namespace details

class RESTfulProcessor
{
public:
RESTfulProcessor();
virtual ~RESTfulProcessor();
void SetServer(QHttpServer *router);
void Initialize(QHttpServer *server, Qv2rayPlugin::connections::IConnectionManager *manager);

private:
static QJsonObject makeResponce(details::Result r, QJsonValue v = {})
{
const auto object = QJsonObject{ { "Message", QMetaEnum::fromType<details::Result>().valueToKey((int) r) }, { "Code", (int) r }, { "Responce", v } };
return object;
}

private:
QHttpServer *m_server;
Qv2rayPlugin::connections::IConnectionManager *m_manager;
QString token = "114514";
};

Q_DECLARE_METATYPE(QRegularExpressionMatch)
2 changes: 1 addition & 1 deletion src/RemotePlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ bool RemoteManagementPlugin::InitializePlugin()
const auto address = m_Settings["listenAddress"].toString("127.0.0.1");
server = new QHttpServer(this);
server->listen(QHostAddress(address), port);
processor.SetServer(server);
processor.Initialize(server, ConnectionManager());
return true;
}

0 comments on commit 44031eb

Please sign in to comment.