From db63389a2f84b45956393fb4f99e4f9bbdca4b9e Mon Sep 17 00:00:00 2001 From: Qv2ray-dev <59914293+Qv2ray-dev@users.noreply.github.com> Date: Tue, 28 Apr 2020 17:53:33 +0800 Subject: [PATCH] refactor: Migrate JSON backend from X2struct to QJsonStruct --- libs/QJsonStruct | 2 +- src/base/JsonHelpers.hpp | 37 +------- src/base/models/CoreObjectModels.hpp | 71 ++++++++-------- src/base/models/QvConfigIdentifier.hpp | 22 ++--- src/base/models/QvSettingsObject.hpp | 89 +++++++++++--------- src/common/QvHelpers.hpp | 21 ----- src/components/plugins/toolbar/QvToolbar.cpp | 2 +- src/components/route/RouteSchemeIO.hpp | 2 +- src/core/CoreUtils.cpp | 11 +-- src/core/connection/Generation.cpp | 4 +- src/core/connection/Serialization.cpp | 6 +- src/core/connection/Serialization_vmess.cpp | 4 +- src/core/settings/SettingsBackend.cpp | 2 +- src/core/settings/SettingsUpgrade.cpp | 3 +- src/main.cpp | 2 +- src/ui/editors/w_OutboundEditor.cpp | 16 ++-- src/ui/editors/w_RoutesEditor.cpp | 4 +- src/ui/widgets/RouteSettingsMatrix.cpp | 4 +- src/ui/widgets/StreamSettingsWidget.cpp | 16 ++-- 19 files changed, 131 insertions(+), 187 deletions(-) diff --git a/libs/QJsonStruct b/libs/QJsonStruct index 1bd58ce0..7f71fcde 160000 --- a/libs/QJsonStruct +++ b/libs/QJsonStruct @@ -1 +1 @@ -Subproject commit 1bd58ce00212cb0e6d313c927108119567ede261 +Subproject commit 7f71fcded7054888cd74fb205d2d718bbba0e5e7 diff --git a/src/base/JsonHelpers.hpp b/src/base/JsonHelpers.hpp index 38114f90..b238696b 100644 --- a/src/base/JsonHelpers.hpp +++ b/src/base/JsonHelpers.hpp @@ -7,42 +7,7 @@ #define CONCATENATE1(arg1, arg2) CONCATENATE2(arg1, arg2) #define CONCATENATE2(arg1, arg2) arg1##arg2 -#define EXPAND(x) x -#define FOR_EACH_1(what, x, ...) what(x) - -#define FOR_EACH_2(what, x, ...) \ - what(x); \ - EXPAND(FOR_EACH_1(what, __VA_ARGS__)) -#define FOR_EACH_3(what, x, ...) \ - what(x); \ - EXPAND(FOR_EACH_2(what, __VA_ARGS__)) -#define FOR_EACH_4(what, x, ...) \ - what(x); \ - EXPAND(FOR_EACH_3(what, __VA_ARGS__)) -#define FOR_EACH_5(what, x, ...) \ - what(x); \ - EXPAND(FOR_EACH_4(what, __VA_ARGS__)) -#define FOR_EACH_6(what, x, ...) \ - what(x); \ - EXPAND(FOR_EACH_5(what, __VA_ARGS__)) -#define FOR_EACH_7(what, x, ...) \ - what(x); \ - EXPAND(FOR_EACH_6(what, __VA_ARGS__)) -#define FOR_EACH_8(what, x, ...) \ - what(x); \ - EXPAND(FOR_EACH_7(what, __VA_ARGS__)) - -#define FOR_EACH_NARG(...) FOR_EACH_NARG_(__VA_ARGS__, FOR_EACH_RSEQ_N()) -#define FOR_EACH_NARG_(...) EXPAND(FOR_EACH_ARG_N(__VA_ARGS__)) -#define FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N -#define FOR_EACH_RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0 -#define CONCATENATE(x, y) x##y -#define FOR_EACH_(N, what, ...) EXPAND(CONCATENATE(FOR_EACH_, N)(what, __VA_ARGS__)) -#define FOR_EACH(what, ...) FOR_EACH_(FOR_EACH_NARG(__VA_ARGS__), what, __VA_ARGS__) -#define JADDEx_(jsonObj, field) jsonObj.insert(#field, field); -#define JADDEx(field) JADDEx_(root, field) - // Add key value pair into JSON named 'root' +#define JADDEx(field) root.insert(#field, field); #define JADD(...) FOR_EACH(JADDEx, __VA_ARGS__) - #define RROOT return root; diff --git a/src/base/models/CoreObjectModels.hpp b/src/base/models/CoreObjectModels.hpp index 3c4e5a68..990b117f 100644 --- a/src/base/models/CoreObjectModels.hpp +++ b/src/base/models/CoreObjectModels.hpp @@ -1,5 +1,5 @@ #pragma once -#include "3rdparty/x2struct/x2struct.hpp" +#include "libs/QJsonStruct/QJsonStruct.hpp" #include #include @@ -13,7 +13,7 @@ namespace Qv2ray::base::objects { QString user; QString pass; - XTOSTRUCT(O(user, pass)) + JSONSTRUCT_REGISTER(AccountObject, F(user, pass)) }; // // @@ -24,7 +24,7 @@ namespace Qv2ray::base::objects ApiObject() : tag("api"), services() { } - XTOSTRUCT(O(tag, services)) + JSONSTRUCT_REGISTER(ApiObject, F(tag, services)) }; // // @@ -35,7 +35,7 @@ namespace Qv2ray::base::objects SystemPolicyObject() : statsInboundUplink(), statsInboundDownlink() { } - XTOSTRUCT(O(statsInboundUplink, statsInboundDownlink)) + JSONSTRUCT_REGISTER(SystemPolicyObject, F(statsInboundUplink, statsInboundDownlink)) }; // // @@ -51,7 +51,7 @@ namespace Qv2ray::base::objects LevelPolicyObject() : handshake(), connIdle(), uplinkOnly(), downlinkOnly(), statsUserUplink(), statsUserDownlink(), bufferSize() { } - XTOSTRUCT(O(handshake, connIdle, uplinkOnly, downlinkOnly, statsUserUplink, statsUserDownlink, bufferSize)) + JSONSTRUCT_REGISTER(LevelPolicyObject, F(handshake, connIdle, uplinkOnly, downlinkOnly, statsUserUplink, statsUserDownlink, bufferSize)) }; // // @@ -62,7 +62,7 @@ namespace Qv2ray::base::objects PolicyObject() : level(), system() { } - XTOSTRUCT(O(level, system)) + JSONSTRUCT_REGISTER(PolicyObject, F(level, system)) }; // // @@ -90,8 +90,8 @@ namespace Qv2ray::base::objects port("1-65535"), network(""), source(), user(), inboundTag(), protocol(), attrs(), outboundTag(""), balancerTag("") { } - XTOSTRUCT(O(QV2RAY_RULE_ENABLED, QV2RAY_RULE_USE_BALANCER, QV2RAY_RULE_TAG, type, domain, ip, port, network, source, user, inboundTag, - protocol, attrs, outboundTag, balancerTag)) + JSONSTRUCT_REGISTER(RuleObject, F(QV2RAY_RULE_ENABLED, QV2RAY_RULE_USE_BALANCER, QV2RAY_RULE_TAG, type, domain, ip, port, network, + source, user, inboundTag, protocol, attrs, outboundTag, balancerTag)) }; // // @@ -102,7 +102,7 @@ namespace Qv2ray::base::objects BalancerObject() : tag(), selector() { } - XTOSTRUCT(O(tag, selector)) + JSONSTRUCT_REGISTER(BalancerObject, F(tag, selector)) }; // // @@ -117,7 +117,7 @@ namespace Qv2ray::base::objects HTTPRequestObject() : version("1.1"), method("GET"), path(), headers() { } - XTOSTRUCT(O(version, method, path, headers)) + JSONSTRUCT_REGISTER(HTTPRequestObject, F(version, method, path, headers)) }; // // @@ -130,7 +130,7 @@ namespace Qv2ray::base::objects HTTPResponseObject() : version("1.1"), status("200"), reason("OK"), headers() { } - XTOSTRUCT(O(version, status, reason, headers)) + JSONSTRUCT_REGISTER(HTTPResponseObject, F(version, status, reason, headers)) }; // // @@ -142,7 +142,7 @@ namespace Qv2ray::base::objects TCPHeader_M_Object() : type("none"), request(), response() { } - XTOSTRUCT(O(type, request, response)) + JSONSTRUCT_REGISTER(TCPHeader_M_Object, F(type, request, response)) }; // // @@ -152,7 +152,7 @@ namespace Qv2ray::base::objects HeaderObject() : type("none") { } - XTOSTRUCT(O(type)) + JSONSTRUCT_REGISTER(HeaderObject, F(type)) }; // // @@ -162,7 +162,7 @@ namespace Qv2ray::base::objects TCPObject() : header() { } - XTOSTRUCT(O(header)) + JSONSTRUCT_REGISTER(TCPObject, F(header)) }; // // @@ -179,7 +179,7 @@ namespace Qv2ray::base::objects KCPObject() : header() { } - XTOSTRUCT(O(mtu, tti, uplinkCapacity, downlinkCapacity, congestion, readBufferSize, writeBufferSize, header)) + JSONSTRUCT_REGISTER(KCPObject, F(mtu, tti, uplinkCapacity, downlinkCapacity, congestion, readBufferSize, writeBufferSize, header)) }; // // @@ -190,7 +190,7 @@ namespace Qv2ray::base::objects WebSocketObject() : path("/"), headers() { } - XTOSTRUCT(O(path, headers)) + JSONSTRUCT_REGISTER(WebSocketObject, F(path, headers)) }; // // @@ -201,7 +201,7 @@ namespace Qv2ray::base::objects HttpObject() : host(), path("/") { } - XTOSTRUCT(O(host, path)) + JSONSTRUCT_REGISTER(HttpObject, F(host, path)) }; // // @@ -211,7 +211,7 @@ namespace Qv2ray::base::objects DomainSocketObject() : path("/") { } - XTOSTRUCT(O(path)) + JSONSTRUCT_REGISTER(DomainSocketObject, F(path)) }; // // @@ -223,7 +223,7 @@ namespace Qv2ray::base::objects QuicObject() : security(""), key(""), header() { } - XTOSTRUCT(O(security, key, header)) + JSONSTRUCT_REGISTER(QuicObject, F(security, key, header)) }; // // @@ -235,7 +235,7 @@ namespace Qv2ray::base::objects SockoptObject() : mark(0), tcpFastOpen(false), tproxy("off") { } - XTOSTRUCT(O(mark, tcpFastOpen, tproxy)) + JSONSTRUCT_REGISTER(SockoptObject, F(mark, tcpFastOpen, tproxy)) }; // // @@ -249,7 +249,7 @@ namespace Qv2ray::base::objects CertificateObject() : usage(), certificateFile(), keyFile(), certificate(), key() { } - XTOSTRUCT(O(usage, certificateFile, keyFile, certificate, key)) + JSONSTRUCT_REGISTER(CertificateObject, F(usage, certificateFile, keyFile, certificate, key)) }; // // @@ -264,7 +264,7 @@ namespace Qv2ray::base::objects TLSObject() : serverName(), allowInsecure(), allowInsecureCiphers(), certificates(), disableSystemRoot() { } - XTOSTRUCT(O(serverName, allowInsecure, allowInsecureCiphers, alpn, certificates, disableSystemRoot)) + JSONSTRUCT_REGISTER(TLSObject, F(serverName, allowInsecure, allowInsecureCiphers, alpn, certificates, disableSystemRoot)) }; } // namespace transfer // @@ -276,7 +276,7 @@ namespace Qv2ray::base::objects SniffingObject() : enabled(), destOverride() { } - XTOSTRUCT(O(enabled, destOverride)) + JSONSTRUCT_REGISTER(SniffingObject, F(enabled, destOverride)) }; // // @@ -297,7 +297,8 @@ namespace Qv2ray::base::objects dsSettings(), quicSettings() { } - XTOSTRUCT(O(network, security, sockopt, tcpSettings, tlsSettings, kcpSettings, wsSettings, httpSettings, dsSettings, quicSettings)) + JSONSTRUCT_REGISTER(StreamSettingsObject, F(network, security, sockopt, tcpSettings, tlsSettings, kcpSettings, wsSettings, httpSettings, + dsSettings, quicSettings)) }; // // @@ -308,7 +309,7 @@ namespace Qv2ray::base::objects MuxObject() : enabled(), concurrency() { } - XTOSTRUCT(O(enabled, concurrency)) + JSONSTRUCT_REGISTER(MuxObject, F(enabled, concurrency)) }; // // Some protocols from: https://v2ray.com/chapter_02/02_protocols.html @@ -323,7 +324,7 @@ namespace Qv2ray::base::objects DNSOut() : network(""), address("0.0.0.0"), port(0) { } - XTOSTRUCT(O(network, address, port)) + JSONSTRUCT_REGISTER(DNSOut, F(network, address, port)) }; // // MTProto, InBound || OutBound @@ -337,10 +338,10 @@ namespace Qv2ray::base::objects UserObject() : email("user@domain.com"), level(0), secret("") { } - XTOSTRUCT(O(email, level, secret)) + JSONSTRUCT_REGISTER(UserObject, F(email, level, secret)) }; QList users; - XTOSTRUCT(O(users)) + JSONSTRUCT_REGISTER(MTProtoIn, F(users)) }; // // Socks, OutBound @@ -354,16 +355,14 @@ namespace Qv2ray::base::objects UserObject() : user(), pass(), level(0) { } - XTOSTRUCT(O(user, pass, level)) + JSONSTRUCT_REGISTER(UserObject, F(user, pass, level)) }; QString address; int port; QList users; - SocksServerObject() : address("0.0.0.0"), port(0), users() - { - } - XTOSTRUCT(O(address, port, users)) + SocksServerObject() : address("0.0.0.0"), port(0), users(){}; + JSONSTRUCT_REGISTER(SocksServerObject, F(address, port, users)) }; // // VMess Server @@ -378,7 +377,7 @@ namespace Qv2ray::base::objects UserObject() : id(""), alterId(64), security("auto"), level(0) { } - XTOSTRUCT(O(id, alterId, security, level)) + JSONSTRUCT_REGISTER(UserObject, F(id, alterId, security, level)) }; QString address; @@ -387,7 +386,7 @@ namespace Qv2ray::base::objects VMessServerObject() : address(""), port(0), users() { } - XTOSTRUCT(O(address, port, users)) + JSONSTRUCT_REGISTER(VMessServerObject, F(address, port, users)) }; // // ShadowSocks Server @@ -404,7 +403,7 @@ namespace Qv2ray::base::objects : email("user@domain.com"), address("0.0.0.0"), method("aes-256-cfb"), password(""), ota(false), level(0), port(0) { } - XTOSTRUCT(O(email, address, port, method, password, ota, level)) + JSONSTRUCT_REGISTER(ShadowSocksServerObject, F(email, address, port, method, password, ota, level)) }; } // namespace protocol } // namespace Qv2ray::base::objects diff --git a/src/base/models/QvConfigIdentifier.hpp b/src/base/models/QvConfigIdentifier.hpp index 4137c9d9..0708951f 100644 --- a/src/base/models/QvConfigIdentifier.hpp +++ b/src/base/models/QvConfigIdentifier.hpp @@ -1,5 +1,5 @@ #pragma once -#include "3rdparty/x2struct/x2struct.hpp" +#include "libs/QJsonStruct/QJsonStruct.hpp" #include #include @@ -13,39 +13,39 @@ namespace Qv2ray::base { QString displayName; QList connections; - int64_t importDate; + qint64 importDate; GroupObject_Config() : displayName(), connections(), importDate() { } - XTOSTRUCT(O(displayName, connections, importDate)) + JSONSTRUCT_REGISTER(GroupObject_Config, F(displayName, connections, importDate)) }; struct SubscriptionObject_Config : GroupObject_Config { // QString address; - int64_t lastUpdated; + qint64 lastUpdated; float updateInterval; SubscriptionObject_Config() : address(""), lastUpdated(system_clock::to_time_t(system_clock::now())), updateInterval(10) { } - XTOSTRUCT(O(lastUpdated, updateInterval, address, connections, displayName, importDate)) + JSONSTRUCT_REGISTER(SubscriptionObject_Config, F(lastUpdated, updateInterval, address), B(GroupObject_Config)) }; struct ConnectionObject_Config { QString displayName; - int64_t importDate; - int64_t lastConnected; - int64_t latency; - int64_t upLinkData; - int64_t downLinkData; + qint64 importDate; + qint64 lastConnected; + qint64 latency; + qint64 upLinkData; + qint64 downLinkData; ConnectionObject_Config() : displayName(), importDate(system_clock::to_time_t(system_clock::now())), lastConnected(), latency(QVTCPING_VALUE_NODATA), upLinkData(0), downLinkData(0) { } - XTOSTRUCT(O(displayName, importDate, lastConnected, latency, upLinkData, downLinkData)) + JSONSTRUCT_REGISTER(ConnectionObject_Config, F(displayName, importDate, lastConnected, latency, upLinkData, downLinkData)) }; } // namespace Qv2ray::base diff --git a/src/base/models/QvSettingsObject.hpp b/src/base/models/QvSettingsObject.hpp index c8ed20f8..c80227ea 100644 --- a/src/base/models/QvSettingsObject.hpp +++ b/src/base/models/QvSettingsObject.hpp @@ -1,7 +1,7 @@ #pragma once -#include "3rdparty/x2struct/x2struct.hpp" #include "base/models/CoreObjectModels.hpp" #include "base/models/QvConfigIdentifier.hpp" +#include "libs/QJsonStruct/QJsonStruct.hpp" #include @@ -22,7 +22,7 @@ namespace Qv2ray::base::config Message("") { } - XTOSTRUCT(O(Bold, Italic, ColorA, ColorR, ColorG, ColorB, Size, Family, Message, ContentType)) + JSONSTRUCT_REGISTER(QvBarLine, F(Bold, Italic, ColorA, ColorR, ColorG, ColorB, Size, Family, Message, ContentType)) }; struct QvBarPage @@ -32,13 +32,13 @@ namespace Qv2ray::base::config QvBarPage() : OffsetYpx(5) { } - XTOSTRUCT(O(OffsetYpx, Lines)) + JSONSTRUCT_REGISTER(QvBarPage, F(OffsetYpx, Lines)) }; struct Qv2rayToolBarConfig { QList Pages; - XTOSTRUCT(O(Pages)) + JSONSTRUCT_REGISTER(Qv2rayToolBarConfig, F(Pages)) }; struct Qv2rayForwardProxyConfig @@ -54,7 +54,7 @@ namespace Qv2ray::base::config : enableForwardProxy(false), type("http"), serverAddress("127.0.0.1"), port(8008), useAuth(false), username(), password() { } - XTOSTRUCT(O(enableForwardProxy, type, serverAddress, port, useAuth, username, password)) + JSONSTRUCT_REGISTER(Qv2rayForwardProxyConfig, F(enableForwardProxy, type, serverAddress, port, useAuth, username, password)) }; struct Qv2rayInboundsConfig @@ -96,9 +96,10 @@ namespace Qv2ray::base::config { } - XTOSTRUCT(O(setSystemProxy, listenip, useSocks, useHTTP, socks_port, socks_useAuth, socksAccount, socksSniffing, socksUDP, socksLocalIP, - http_port, http_useAuth, httpAccount, httpSniffing, useTPROXY, tproxy_ip, tproxy_port, tproxy_use_tcp, tproxy_use_udp, - tproxy_followRedirect, tproxy_mode, dnsIntercept)) + JSONSTRUCT_REGISTER(Qv2rayInboundsConfig, + F(setSystemProxy, listenip, useSocks, useHTTP, socks_port, socks_useAuth, socksAccount, socksSniffing, socksUDP, + socksLocalIP, http_port, http_useAuth, httpAccount, httpSniffing, useTPROXY), + F(tproxy_ip, tproxy_port, tproxy_use_tcp, tproxy_use_udp, tproxy_followRedirect, tproxy_mode, dnsIntercept)) }; struct Qv2rayOutboundsConfig @@ -107,7 +108,7 @@ namespace Qv2ray::base::config Qv2rayOutboundsConfig() : mark(255) { } - XTOSTRUCT(O(mark)) + JSONSTRUCT_REGISTER(Qv2rayOutboundsConfig, F(mark)) }; struct Qv2rayUIConfig @@ -124,7 +125,8 @@ namespace Qv2ray::base::config : theme("Fusion"), language("en_US"), useDarkTheme(false), useDarkTrayIcon(true), maximumLogLines(500), maxJumpListCount(20) { } - XTOSTRUCT(O(theme, language, quietMode, useDarkTheme, useDarkTrayIcon, maximumLogLines, maxJumpListCount, recentConnections)) + JSONSTRUCT_REGISTER(Qv2rayUIConfig, + F(theme, language, quietMode, useDarkTheme, useDarkTrayIcon, maximumLogLines, maxJumpListCount, recentConnections)) }; struct Qv2rayRouteConfig_Impl @@ -139,7 +141,7 @@ namespace Qv2ray::base::config } Qv2rayRouteConfig_Impl(const QList &_direct, const QList &_block, const QList &_proxy) : direct(_direct), block(_block), proxy(_proxy){}; - XTOSTRUCT(O(proxy, block, direct)) + JSONSTRUCT_REGISTER(Qv2rayRouteConfig_Impl, F(proxy, block, direct)) }; struct Qv2rayRouteConfig @@ -154,7 +156,7 @@ namespace Qv2ray::base::config Qv2rayRouteConfig(){}; Qv2rayRouteConfig(const Qv2rayRouteConfig_Impl &_domains, const Qv2rayRouteConfig_Impl &_ips, const QString &ds) : domainStrategy(ds), domains(_domains), ips(_ips){}; - XTOSTRUCT(O(domainStrategy, domains, ips)) + JSONSTRUCT_REGISTER(Qv2rayRouteConfig, F(domainStrategy, domains, ips)) }; struct Qv2rayPluginConfig @@ -163,7 +165,7 @@ namespace Qv2ray::base::config bool v2rayIntegration; int portAllocationStart; Qv2rayPluginConfig() : pluginStates(), v2rayIntegration(true), portAllocationStart(15000){}; - XTOSTRUCT(O(pluginStates, v2rayIntegration)) + JSONSTRUCT_REGISTER(Qv2rayPluginConfig, F(pluginStates, v2rayIntegration, portAllocationStart)) }; struct Qv2rayConnectionConfig @@ -181,7 +183,8 @@ namespace Qv2ray::base::config dnsList(QStringList{ "8.8.4.4", "1.1.1.1" }) { } - XTOSTRUCT(O(bypassCN, bypassBT, enableProxy, v2rayFreedomDNS, withLocalDNS, dnsList, forwardProxyConfig, routeConfig)) + JSONSTRUCT_REGISTER(Qv2rayConnectionConfig, + F(bypassCN, bypassBT, enableProxy, v2rayFreedomDNS, withLocalDNS, dnsList, forwardProxyConfig, routeConfig)) }; struct Qv2rayAPIConfig @@ -191,7 +194,7 @@ namespace Qv2ray::base::config Qv2rayAPIConfig() : enableAPI(true), statsPort(15490) { } - XTOSTRUCT(O(enableAPI, statsPort)) + JSONSTRUCT_REGISTER(Qv2rayAPIConfig, F(enableAPI, statsPort)) }; struct Qv2rayKernelConfig @@ -232,7 +235,8 @@ namespace Qv2ray::base::config #undef _VARNAME_VCOREPATH_ #undef _VARNAME_VASSETSPATH_ - XTOSTRUCT(O(v2CorePath_linux, v2AssetsPath_linux, v2CorePath_macx, v2AssetsPath_macx, v2CorePath_win, v2AssetsPath_win)) + JSONSTRUCT_REGISTER(Qv2rayKernelConfig, + F(v2CorePath_linux, v2AssetsPath_linux, v2CorePath_macx, v2AssetsPath_macx, v2CorePath_win, v2AssetsPath_win)) }; struct Qv2rayUpdateConfig @@ -243,7 +247,7 @@ namespace Qv2ray::base::config /// 0: Stable /// 1: Testing int updateChannel; - XTOSTRUCT(O(ignoredVersion, updateChannel)) + JSONSTRUCT_REGISTER(Qv2rayUpdateConfig, F(ignoredVersion, updateChannel)) }; struct Qv2rayAdvancedConfig @@ -251,16 +255,16 @@ namespace Qv2ray::base::config bool setAllowInsecure; bool setAllowInsecureCiphers; bool testLatencyPeriodcally; - XTOSTRUCT(O(setAllowInsecure, setAllowInsecureCiphers, testLatencyPeriodcally)) + JSONSTRUCT_REGISTER(Qv2rayAdvancedConfig, F(setAllowInsecure, setAllowInsecureCiphers, testLatencyPeriodcally)) }; struct Qv2rayNetworkConfig { - enum Qv2rayProxyType + enum Qv2rayProxyType : int { - QVPROXY_NONE, - QVPROXY_SYSTEM, - QVPROXY_CUSTOM + QVPROXY_NONE = 0, + QVPROXY_SYSTEM = 1, + QVPROXY_CUSTOM = 2 } proxyType; QString address; @@ -273,7 +277,7 @@ namespace Qv2ray::base::config type("http"), // port(8000), // userAgent("Qv2ray/$VERSION WebRequestHelper"){}; - XTOSTRUCT(O(proxyType, type, address, port, userAgent)) + JSONSTRUCT_REGISTER(Qv2rayNetworkConfig, F(proxyType, type, address, port, userAgent)) }; struct Qv2rayConfig @@ -324,24 +328,25 @@ namespace Qv2ray::base::config { } - XTOSTRUCT(O(config_version, // - tProxySupport, // - logLevel, // - uiConfig, // - pluginConfig, // - updateConfig, // - kernelConfig, // - networkConfig, // - groups, // - connections, // - subscriptions, // - autoStartId, // - inboundConfig, // - outboundConfig, // - connectionConfig, // - toolBarConfig, // - advancedConfig, // - apiConfig // - )) + JSONSTRUCT_REGISTER(Qv2rayConfig, + F(config_version, // + tProxySupport, // + logLevel, // + uiConfig, // + pluginConfig, // + updateConfig, // + kernelConfig, // + networkConfig, // + groups, // + connections, // + subscriptions, // + autoStartId, // + inboundConfig, // + outboundConfig, // + connectionConfig), + F(toolBarConfig, // + advancedConfig, // + apiConfig // + )) }; } // namespace Qv2ray::base::config diff --git a/src/common/QvHelpers.hpp b/src/common/QvHelpers.hpp index 14c61e30..584beb19 100644 --- a/src/common/QvHelpers.hpp +++ b/src/common/QvHelpers.hpp @@ -49,27 +49,6 @@ namespace Qv2ray::common return GenerateRandomString().toLower(); // return QUuid::createUuid().toString(QUuid::WithoutBraces); } - // - template - QString StructToJsonString(const TYPE &t) - { - return QString::fromStdString(x2struct::X::tojson(t, "", 4, ' ')); - } - // - template - TYPE StructFromJsonString(const QString &str) - { - TYPE v; - x2struct::X::loadjson(str.toStdString(), v, false); - return v; - } - // Misc - template - QJsonObject GetRootObject(const T &t) - { - auto json = StructToJsonString(t); - return JsonFromString(json); - } inline QString TruncateString(const QString &str, int limit = -1, const QString &suffix = "...") { diff --git a/src/components/plugins/toolbar/QvToolbar.cpp b/src/components/plugins/toolbar/QvToolbar.cpp index 967bfd22..bcee2f4e 100644 --- a/src/components/plugins/toolbar/QvToolbar.cpp +++ b/src/components/plugins/toolbar/QvToolbar.cpp @@ -172,7 +172,7 @@ namespace Qv2ray::components::plugins } } #undef CL - reply = StructToJsonString(BarConfig); + reply = JsonToString(BarConfig.toJson()); return reply; } } // namespace Toolbar diff --git a/src/components/route/RouteSchemeIO.hpp b/src/components/route/RouteSchemeIO.hpp index 3454061a..53267313 100644 --- a/src/components/route/RouteSchemeIO.hpp +++ b/src/components/route/RouteSchemeIO.hpp @@ -26,7 +26,7 @@ namespace Qv2ray::components::route QString description; // M: all these fields are mandatory - XTOSTRUCT(M(name, author, description, domainStrategy, domains, ips)); + JSONSTRUCT_REGISTER(Qv2rayRouteScheme, F(name, author, description, domainStrategy, domains, ips)); }; } // namespace Qv2ray::components::route diff --git a/src/core/CoreUtils.cpp b/src/core/CoreUtils.cpp index 282a0b7c..791b5af5 100644 --- a/src/core/CoreUtils.cpp +++ b/src/core/CoreUtils.cpp @@ -32,24 +32,21 @@ namespace Qv2ray::core if (*protocol == "vmess") { - auto Server = - StructFromJsonString(JsonToString(out["settings"].toObject()["vnext"].toArray().first().toObject())); + auto Server = VMessServerObject::fromJson(out["settings"].toObject()["vnext"].toArray().first().toObject()); *host = Server.address; *port = Server.port; return true; } else if (*protocol == "shadowsocks") { - auto x = JsonToString(out["settings"].toObject()["servers"].toArray().first().toObject()); - auto Server = StructFromJsonString(x); + auto Server = ShadowSocksServerObject::fromJson(out["settings"].toObject()["servers"].toArray().first().toObject()); *host = Server.address; *port = Server.port; return true; } else if (*protocol == "socks") { - auto x = JsonToString(out["settings"].toObject()["servers"].toArray().first().toObject()); - auto Server = StructFromJsonString(x); + auto Server = SocksServerObject::fromJson(out["settings"].toObject()["servers"].toArray().first().toObject()); *host = Server.address; *port = Server.port; return true; @@ -120,7 +117,7 @@ namespace Qv2ray::core int64_t GetConnectionLatency(const ConnectionId &id) { auto connection = ConnectionManager->GetConnectionMetaObject(id); - return max(connection.latency, (int64_t) 0); + return max(connection.latency, {}); } const QString GetConnectionProtocolString(const ConnectionId &id) diff --git a/src/core/connection/Generation.cpp b/src/core/connection/Generation.cpp index 4e234e2d..3406439c 100644 --- a/src/core/connection/Generation.cpp +++ b/src/core/connection/Generation.cpp @@ -163,7 +163,7 @@ namespace Qv2ray::core::connection continue; } - accounts.append(GetRootObject(account)); + accounts.append(account.toJson()); } if (!accounts.isEmpty()) @@ -212,7 +212,7 @@ namespace Qv2ray::core::connection continue; } - accounts.append(GetRootObject(acc)); + accounts.append(acc.toJson()); } if (!accounts.isEmpty()) diff --git a/src/core/connection/Serialization.cpp b/src/core/connection/Serialization.cpp index b9aa8908..56d2bc2c 100644 --- a/src/core/connection/Serialization.cpp +++ b/src/core/connection/Serialization.cpp @@ -89,13 +89,13 @@ namespace Qv2ray::core::connection QString sharelink = ""; if (type == "vmess") { - auto vmessServer = StructFromJsonString(JsonToString(settings["vnext"].toArray().first().toObject())); - auto transport = StructFromJsonString(JsonToString(outbound["streamSettings"].toObject())); + auto vmessServer = VMessServerObject::fromJson(settings["vnext"].toArray().first().toObject()); + auto transport = StreamSettingsObject::fromJson(outbound["streamSettings"].toObject()); sharelink = vmess::ConvertConfigToVMessString(transport, vmessServer, alias); } else if (type == "shadowsocks") { - auto ssServer = StructFromJsonString(JsonToString(settings["servers"].toArray().first().toObject())); + auto ssServer = ShadowSocksServerObject::fromJson(settings["servers"].toArray().first().toObject()); sharelink = ss::ConvertConfigToSSString(ssServer, alias, isSip002); } else diff --git a/src/core/connection/Serialization_vmess.cpp b/src/core/connection/Serialization_vmess.cpp index 8add3fa5..034f7247 100644 --- a/src/core/connection/Serialization_vmess.cpp +++ b/src/core/connection/Serialization_vmess.cpp @@ -225,7 +225,7 @@ namespace Qv2ray::core::connection // VMess root config OUTBOUNDSETTING vConf; QJsonArray vnextArray; - vnextArray.append(JsonFromString(StructToJsonString(serv))); + vnextArray.append(serv.toJson()); vConf["vnext"] = vnextArray; // // Stream Settings @@ -285,7 +285,7 @@ namespace Qv2ray::core::connection streaming.network = net; // // WARN Mux is missing here. - auto outbound = GenerateOutboundEntry("vmess", vConf, GetRootObject(streaming), QJsonObject(), "0.0.0.0", OUTBOUND_TAG_PROXY); + auto outbound = GenerateOutboundEntry("vmess", vConf, streaming.toJson(), {}, "0.0.0.0", OUTBOUND_TAG_PROXY); // root["outbounds"] = QJsonArray() << outbound; // If previous alias is empty, just the PS is needed, else, append a "_" diff --git a/src/core/settings/SettingsBackend.cpp b/src/core/settings/SettingsBackend.cpp index f03431cb..6cee454a 100644 --- a/src/core/settings/SettingsBackend.cpp +++ b/src/core/settings/SettingsBackend.cpp @@ -12,7 +12,7 @@ namespace Qv2ray::core::config void SaveGlobalSettings() { - QString str = StructToJsonString(GlobalConfig); + QString str = JsonToString(GlobalConfig.toJson()); StringToFile(str, QV2RAY_CONFIG_FILE); } diff --git a/src/core/settings/SettingsUpgrade.cpp b/src/core/settings/SettingsUpgrade.cpp index edbaad98..1cdd6df8 100644 --- a/src/core/settings/SettingsUpgrade.cpp +++ b/src/core/settings/SettingsUpgrade.cpp @@ -83,8 +83,7 @@ namespace Qv2ray _conf.address = item.value().toString(); _conf.lastUpdated = system_clock::to_time_t(system_clock::now()); _conf.updateInterval = 5; - auto value = GetRootObject(_conf); - newSubscriptions[key] = value; + newSubscriptions[key] = _conf.toJson(); } root["subscriptions"] = newSubscriptions; diff --git a/src/main.cpp b/src/main.cpp index 8055c6ed..8cc3dcdc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -312,7 +312,7 @@ int main(int argc, char *argv[]) } // Load config object from upgraded config QJsonObject - auto confObject = StructFromJsonString(JsonToString(conf)); + auto confObject = Qv2rayConfig::fromJson(conf); if (confObject.uiConfig.language.isEmpty()) { diff --git a/src/ui/editors/w_OutboundEditor.cpp b/src/ui/editors/w_OutboundEditor.cpp index 40b426fd..6beeaea5 100644 --- a/src/ui/editors/w_OutboundEditor.cpp +++ b/src/ui/editors/w_OutboundEditor.cpp @@ -80,7 +80,7 @@ QString OutboundEditor::GetFriendlyName() OUTBOUND OutboundEditor::GenerateConnectionJson() { OUTBOUNDSETTING settings; - auto streaming = GetRootObject(streamSettingsWidget->GetStreamSettings()); + auto streaming = streamSettingsWidget->GetStreamSettings().toJson(); if (outboundType == "vmess") { @@ -88,7 +88,7 @@ OUTBOUND OutboundEditor::GenerateConnectionJson() QJsonArray vnext; vmess.address = address; vmess.port = port; - vnext.append(GetRootObject(vmess)); + vnext.append(vmess.toJson()); settings.insert("vnext", vnext); } else if (outboundType == "shadowsocks") @@ -98,7 +98,7 @@ OUTBOUND OutboundEditor::GenerateConnectionJson() QJsonArray servers; shadowsocks.address = address; shadowsocks.port = port; - servers.append(GetRootObject(shadowsocks)); + servers.append(shadowsocks.toJson()); settings["servers"] = servers; } else if (outboundType == "socks") @@ -113,7 +113,7 @@ OUTBOUND OutboundEditor::GenerateConnectionJson() streaming = QJsonObject(); LOG(MODULE_CONNECTION, "Socks outbound does not need StreamSettings.") QJsonArray servers; - servers.append(GetRootObject(socks)); + servers.append(socks.toJson()); settings["servers"] = servers; } else @@ -149,7 +149,7 @@ void OutboundEditor::ReloadGUI() outboundType = originalConfig["protocol"].toString("vmess"); muxConfig = originalConfig["mux"].toObject(); useForwardProxy = originalConfig[QV2RAY_USE_FPROXY_KEY].toBool(false); - streamSettingsWidget->SetStreamObject(StructFromJsonString(JsonToString(originalConfig["streamSettings"].toObject()))); + streamSettingsWidget->SetStreamObject(StreamSettingsObject::fromJson(originalConfig["streamSettings"].toObject())); // useFPCB->setChecked(useForwardProxy); muxEnabledCB->setChecked(muxConfig["enabled"].toBool()); @@ -160,7 +160,7 @@ void OutboundEditor::ReloadGUI() if (outboundType == "vmess") { outBoundTypeCombo->setCurrentIndex(0); - vmess = StructFromJsonString(JsonToString(settings["vnext"].toArray().first().toObject())); + vmess = VMessServerObject::fromJson(settings["vnext"].toArray().first().toObject()); if (vmess.users.empty()) { vmess.users.push_back({}); @@ -174,7 +174,7 @@ void OutboundEditor::ReloadGUI() else if (outboundType == "shadowsocks") { outBoundTypeCombo->setCurrentIndex(1); - shadowsocks = StructFromJsonString(JsonToString(settings["servers"].toArray().first().toObject())); + shadowsocks = ShadowSocksServerObject::fromJson(settings["servers"].toArray().first().toObject()); address = shadowsocks.address; port = shadowsocks.port; // ShadowSocks Configs @@ -187,7 +187,7 @@ void OutboundEditor::ReloadGUI() else if (outboundType == "socks") { outBoundTypeCombo->setCurrentIndex(2); - socks = StructFromJsonString(JsonToString(settings["servers"].toArray().first().toObject())); + socks = SocksServerObject::fromJson(settings["servers"].toArray().first().toObject()); address = socks.address; port = socks.port; if (socks.users.empty()) diff --git a/src/ui/editors/w_RoutesEditor.cpp b/src/ui/editors/w_RoutesEditor.cpp index 14479a16..863d7da6 100644 --- a/src/ui/editors/w_RoutesEditor.cpp +++ b/src/ui/editors/w_RoutesEditor.cpp @@ -106,7 +106,7 @@ RouteEditor::RouteEditor(QJsonObject connection, QWidget *parent) : QDialog(pare for (auto item : root["routing"].toObject()["rules"].toArray()) { - AddRule(StructFromJsonString(JsonToString(item.toObject()))); + AddRule(RuleObject::fromJson(item.toObject())); } // Set default outboung combo text AFTER adding all outbounds. @@ -312,7 +312,7 @@ CONFIGROOT RouteEditor::OpenEditor() for (auto i = 0; i < ruleListWidget->count(); i++) { auto _rule = rules[ruleListWidget->item(i)->text()]; - auto ruleJsonObject = GetRootObject(_rule); + auto ruleJsonObject = _rule.toJson(); // Process balancer for a rule if (_rule.QV2RAY_RULE_USE_BALANCER) diff --git a/src/ui/widgets/RouteSettingsMatrix.cpp b/src/ui/widgets/RouteSettingsMatrix.cpp index 6742bea3..b39c2061 100644 --- a/src/ui/widgets/RouteSettingsMatrix.cpp +++ b/src/ui/widgets/RouteSettingsMatrix.cpp @@ -104,7 +104,7 @@ void RouteSettingsMatrixWidget::on_importSchemeBtn_clicked() // read the file and parse back to struct. // if error occurred on parsing, an exception will be thrown. auto content = StringFromFile(ACCESS_OPTIONAL_VALUE(filePath)); - auto scheme = StructFromJsonString(content); + auto scheme = Qv2rayRouteScheme::fromJson(JsonFromString(content)); // show the information of this scheme to user, // and ask user if he/she wants to import and apply this. @@ -174,7 +174,7 @@ void RouteSettingsMatrixWidget::on_exportSchemeBtn_clicked() scheme.domains = config.domains; // serialize and write out - auto content = StructToJsonString(scheme); + auto content = JsonToString(scheme.toJson()); StringToFile(content, ACCESS_OPTIONAL_VALUE(savePath)); // done diff --git a/src/ui/widgets/StreamSettingsWidget.cpp b/src/ui/widgets/StreamSettingsWidget.cpp index eea7fa3e..4fdd6312 100644 --- a/src/ui/widgets/StreamSettingsWidget.cpp +++ b/src/ui/widgets/StreamSettingsWidget.cpp @@ -40,8 +40,8 @@ void StreamSettingsWidget::SetStreamObject(const StreamSettingsObject &sso) alpnTxt->setPlainText(stream.tlsSettings.alpn.join(NEWLINE)); // TCP tcpHeaderTypeCB->setCurrentText(stream.tcpSettings.header.type); - tcpRequestTxt->setPlainText(StructToJsonString(stream.tcpSettings.header.request)); - tcpRespTxt->setPlainText(StructToJsonString(stream.tcpSettings.header.response)); + tcpRequestTxt->setPlainText(JsonToString(stream.tcpSettings.header.request.toJson())); + tcpRespTxt->setPlainText(JsonToString(stream.tcpSettings.header.response.toJson())); // HTTP httpHostTxt->setPlainText(stream.httpSettings.host.join(NEWLINE)); httpPathTxt->setText(stream.httpSettings.path); @@ -251,18 +251,18 @@ void StreamSettingsWidget::on_dsPathTxt_textEdited(const QString &arg1) void StreamSettingsWidget::on_tcpRequestEditBtn_clicked() { JsonEditor w(JsonFromString(tcpRequestTxt->toPlainText()), this); - auto rString = JsonToString(w.OpenEditor()); - tcpRequestTxt->setPlainText(rString); - auto tcpReqObject = StructFromJsonString(rString); + auto rJson = w.OpenEditor(); + tcpRequestTxt->setPlainText(JsonToString(rJson)); + auto tcpReqObject = HTTPRequestObject::fromJson(rJson); stream.tcpSettings.header.request = tcpReqObject; } void StreamSettingsWidget::on_tcpResponseEditBtn_clicked() { JsonEditor w(JsonFromString(tcpRespTxt->toPlainText()), this); - auto rString = JsonToString(w.OpenEditor()); - tcpRespTxt->setPlainText(rString); - auto tcpRspObject = StructFromJsonString(rString); + auto rJson = w.OpenEditor(); + tcpRespTxt->setPlainText(JsonToString(rJson)); + auto tcpRspObject = HTTPResponseObject::fromJson(rJson); stream.tcpSettings.header.response = tcpRspObject; }