|
| 1 | +#include "handler/settings_snapshot.h" |
| 2 | + |
| 3 | +#include <string> |
| 4 | + |
| 5 | +#include <nlohmann/json.hpp> |
| 6 | + |
| 7 | +#include "handler/proxy_policy.h" |
| 8 | +#include "handler/settings.h" |
| 9 | +#include "utils/string.h" |
| 10 | + |
| 11 | +namespace { |
| 12 | + |
| 13 | +nlohmann::json sensitiveState(const std::string &value) { |
| 14 | + const std::string trimmed = trimWhitespace(value, true, true); |
| 15 | + return { |
| 16 | + {"configured", !trimmed.empty()}, |
| 17 | + {"length", trimmed.size()}, |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +std::string triState(const tribool &value) { |
| 22 | + if (value.is_undef()) |
| 23 | + return "inherit"; |
| 24 | + return value.get() ? "true" : "false"; |
| 25 | +} |
| 26 | + |
| 27 | +} // namespace |
| 28 | + |
| 29 | +std::string sanitizedSettingsSnapshot(const Settings &settings) { |
| 30 | + nlohmann::json snapshot = { |
| 31 | + {"schema", 1}, |
| 32 | + {"common", |
| 33 | + { |
| 34 | + {"base_path", settings.basePath}, |
| 35 | + {"default_urls", sensitiveState(settings.defaultUrls)}, |
| 36 | + {"insert_urls", sensitiveState(settings.insertUrls)}, |
| 37 | + {"default_external_config", |
| 38 | + sensitiveState(settings.defaultExtConfig)}, |
| 39 | + {"enable_insert", settings.enableInsert.get(false)}, |
| 40 | + {"prepend_insert", settings.prependInsert}, |
| 41 | + {"append_proxy_type", settings.appendType}, |
| 42 | + {"reload_conf_on_request", settings.reloadConfOnRequest}, |
| 43 | + {"exclude_remarks_count", settings.excludeRemarks.size()}, |
| 44 | + {"include_remarks_count", settings.includeRemarks.size()}, |
| 45 | + }}, |
| 46 | + {"node_pref", |
| 47 | + { |
| 48 | + {"udp", triState(settings.UDPFlag)}, |
| 49 | + {"tcp_fast_open", triState(settings.TFOFlag)}, |
| 50 | + {"skip_cert_verify", triState(settings.skipCertVerify)}, |
| 51 | + {"tls13", triState(settings.TLS13Flag)}, |
| 52 | + {"sort", settings.enableSort}, |
| 53 | + {"filter_deprecated", settings.filterDeprecated}, |
| 54 | + {"append_userinfo", settings.appendUserinfo}, |
| 55 | + {"clash_new_fields", settings.clashUseNewField}, |
| 56 | + {"clash_proxies_style", settings.clashProxiesStyle}, |
| 57 | + {"singbox_add_clash_modes", settings.singBoxAddClashModes}, |
| 58 | + }}, |
| 59 | + {"rules", |
| 60 | + { |
| 61 | + {"enabled", settings.enableRuleGen}, |
| 62 | + {"overwrite_original", settings.overwriteOriginalRules}, |
| 63 | + {"update_on_request", settings.updateRulesetOnRequest}, |
| 64 | + {"ruleset_count", settings.customRulesets.size()}, |
| 65 | + {"proxy_group_count", settings.customProxyGroups.size()}, |
| 66 | + }}, |
| 67 | + {"server", |
| 68 | + { |
| 69 | + {"listen", settings.listenAddress}, |
| 70 | + {"port", settings.listenPort}, |
| 71 | + {"serve_file_root", settings.serveFileRoot}, |
| 72 | + {"max_pending_connections", settings.maxPendingConns}, |
| 73 | + {"max_concurrent_threads", settings.maxConcurThreads}, |
| 74 | + {"max_server_threads", settings.maxServerThreads}, |
| 75 | + }}, |
| 76 | + {"advanced", |
| 77 | + { |
| 78 | + {"max_allowed_rulesets", settings.maxAllowedRulesets}, |
| 79 | + {"max_allowed_rules", settings.maxAllowedRules}, |
| 80 | + {"max_allowed_download_size", settings.maxAllowedDownloadSize}, |
| 81 | + {"cache_subscription", settings.cacheSubscription}, |
| 82 | + {"cache_config", settings.cacheConfig}, |
| 83 | + {"cache_ruleset", settings.cacheRuleset}, |
| 84 | + {"serve_cache_on_fetch_fail", settings.serveCacheOnFetchFail}, |
| 85 | + {"skip_failed_links", settings.skipFailedLinks}, |
| 86 | + {"request_coalescing", settings.enableRequestCoalescing}, |
| 87 | + {"coalesce_retry_on_5xx", settings.coalesceRetryOn5xx}, |
| 88 | + {"allow_insecure_tls", settings.allowInsecureTls}, |
| 89 | + {"response_cache_ttl", settings.responseCacheTtl}, |
| 90 | + }}, |
| 91 | + {"security", |
| 92 | + { |
| 93 | + {"profile", settings.securityProfile}, |
| 94 | + {"allow_public_upload", settings.allowPublicUpload}, |
| 95 | + }}, |
| 96 | + {"custom_openclash_rules", |
| 97 | + { |
| 98 | + {"fallback_enabled", settings.customOpenClashRulesFallback}, |
| 99 | + {"publish_enabled", settings.customOpenClashRulesPublish}, |
| 100 | + }}, |
| 101 | + {"proxies", |
| 102 | + { |
| 103 | + {"config", parseProxy(settings.proxyConfig).describe()}, |
| 104 | + {"ruleset", parseProxy(settings.proxyRuleset).describe()}, |
| 105 | + {"subscription", |
| 106 | + parseProxy(settings.proxySubscription).describe()}, |
| 107 | + }}, |
| 108 | + {"statistics", |
| 109 | + { |
| 110 | + {"enabled", settings.statisticsEnabled}, |
| 111 | + {"data_dir", settings.statisticsDataDir}, |
| 112 | + {"flush_interval", settings.statisticsFlushInterval}, |
| 113 | + {"geo_provider", settings.statisticsGeoProvider}, |
| 114 | + {"country_header_count", |
| 115 | + settings.statisticsCountryHeaders.size()}, |
| 116 | + {"china_region_header_count", |
| 117 | + settings.statisticsChinaRegionHeaders.size()}, |
| 118 | + {"dashboard_auth_enabled", settings.dashboardAuthEnabled}, |
| 119 | + {"dashboard_username_configured", |
| 120 | + !settings.dashboardAuthUsername.empty()}, |
| 121 | + {"dashboard_password_configured", |
| 122 | + !settings.dashboardAuthPassword.empty()}, |
| 123 | + {"dashboard_max_failures", settings.dashboardAuthMaxFailures}, |
| 124 | + {"dashboard_window_seconds", |
| 125 | + settings.dashboardAuthWindowSeconds}, |
| 126 | + {"dashboard_lock_seconds", settings.dashboardAuthLockSeconds}, |
| 127 | + }}, |
| 128 | + }; |
| 129 | + return snapshot.dump(2) + "\n"; |
| 130 | +} |
0 commit comments