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+ }
0 commit comments