Skip to content

Commit 2aa67fa

Browse files
committed
Add deserialization of config string
1 parent d28098c commit 2aa67fa

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

proxyserver/proxyserver.cpp

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "proxyserver.h"
2+
#include "serialization.h"
23
#include <QJsonObject>
34
#include <QJsonArray>
45
#include <QJsonDocument>
@@ -204,22 +205,22 @@ void ProxyServer::setupRoutes()
204205
m_server.route("/api/v1/config", QHttpServerRequest::Method::Post, [this] (const QHttpServerRequest &request) {
205206
QJsonObject response;
206207

207-
QJsonParseError parseError;
208-
QJsonDocument doc = QJsonDocument::fromJson(request.body(), &parseError);
209-
210-
if (parseError.error != QJsonParseError::NoError) {
208+
QString configStr = QString::fromUtf8(request.body());
209+
if (configStr.isEmpty()) {
211210
response["status"] = "error";
212-
response["message"] = "Invalid JSON: " + parseError.errorString();
211+
response["message"] = "Config string cannot be empty";
213212
return response;
214213
}
215214

216-
QJsonObject config = doc.object();
215+
QJsonObject config = deserializeConfig(configStr);
217216
if (config.isEmpty()) {
218217
response["status"] = "error";
219-
response["message"] = "Config cannot be empty";
218+
response["message"] = "Failed to deserialize config string";
220219
return response;
221220
}
222221

222+
config = addInbounds(config);
223+
223224
if (writeConfig(config)) {
224225
response["status"] = "success";
225226
response["message"] = "Config saved successfully";
@@ -308,4 +309,54 @@ void ProxyServer::showSettings()
308309
{
309310
// TODO: Implement settings window
310311
qDebug() << "Show settings";
311-
}
312+
}
313+
314+
QJsonObject ProxyServer::addInbounds(const QJsonObject &config)
315+
{
316+
QJsonObject resultConfig = config;
317+
318+
QJsonArray inbounds;
319+
QJsonObject socksInbound;
320+
socksInbound["listen"] = "127.0.0.1";
321+
socksInbound["port"] = 10808;
322+
socksInbound["protocol"] = "socks";
323+
324+
QJsonObject settings;
325+
settings["udp"] = true;
326+
socksInbound["settings"] = settings;
327+
328+
inbounds.append(socksInbound);
329+
resultConfig["inbounds"] = inbounds;
330+
331+
return resultConfig;
332+
}
333+
334+
QJsonObject ProxyServer::deserializeConfig(const QString &configStr)
335+
{
336+
QJsonObject outConfig;
337+
338+
QString prefix;
339+
QString errormsg;
340+
341+
if (configStr.startsWith("vless://")) {
342+
outConfig = amnezia::serialization::vless::Deserialize(configStr, &prefix, &errormsg);
343+
}
344+
345+
if (configStr.startsWith("vmess://") && configStr.contains("@")) {
346+
outConfig = amnezia::serialization::vmess_new::Deserialize(configStr, &prefix, &errormsg);
347+
}
348+
349+
if (configStr.startsWith("vmess://")) {
350+
outConfig = amnezia::serialization::vmess::Deserialize(configStr, &prefix, &errormsg);
351+
}
352+
353+
if (configStr.startsWith("trojan://")) {
354+
outConfig = amnezia::serialization::trojan::Deserialize(configStr, &prefix, &errormsg);
355+
}
356+
357+
if (configStr.startsWith("ss://") && !configStr.contains("plugin=")) {
358+
outConfig = amnezia::serialization::ss::Deserialize(configStr, &prefix, &errormsg);
359+
}
360+
361+
return outConfig;
362+
}

proxyserver/proxyserver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ private slots:
3030
QString getConfigPath() const;
3131
QJsonObject readConfig() const;
3232
bool writeConfig(const QJsonObject &config);
33+
QJsonObject addInbounds(const QJsonObject &config);
34+
QJsonObject deserializeConfig(const QString &configStr);
3335

3436
QString getXrayExecutablePath() const;
3537
QStringList getXrayArguments(const QString &configPath) const;

0 commit comments

Comments
 (0)