LibWebView+RequestServer: Add some UI for DNS settings

This commit is contained in:
Ali Mohammad Pur 2025-04-08 03:56:35 +02:00 committed by Tim Flynn
parent e1369aa7d6
commit 2c13504bfc
Notes: github-actions[bot] 2025-04-22 22:06:21 +00:00
12 changed files with 284 additions and 14 deletions

View file

@ -41,6 +41,8 @@ static constexpr auto autoplay_key = "autoplay"sv;
static constexpr auto do_not_track_key = "doNotTrack"sv;
static constexpr auto dns_settings_key = "dnsSettings"sv;
static ErrorOr<JsonObject> read_settings_file(StringView settings_path)
{
auto settings_file = Core::File::open(settings_path, Core::File::OpenMode::Read);
@ -134,9 +136,40 @@ Settings Settings::create(Badge<Application>)
if (auto do_not_track = settings_json.value().get_bool(do_not_track_key); do_not_track.has_value())
settings.m_do_not_track = *do_not_track ? DoNotTrack::Yes : DoNotTrack::No;
if (auto dns_settings = settings_json.value().get(dns_settings_key); dns_settings.has_value())
settings.m_dns_settings = parse_dns_settings(*dns_settings);
return settings;
}
DNSSettings Settings::parse_dns_settings(JsonValue const& dns_settings)
{
if (!dns_settings.is_object())
return SystemDNS {};
auto& dns_settings_object = dns_settings.as_object();
auto mode = dns_settings_object.get_string("mode"sv);
if (mode.has_value()) {
if (*mode == "system"sv)
return SystemDNS {};
if (*mode == "custom"sv) {
auto server = dns_settings_object.get_string("server"sv);
auto port = dns_settings_object.get_u16("port"sv);
auto type = dns_settings_object.get_string("type"sv);
if (server.has_value() && port.has_value() && type.has_value()) {
if (*type == "tls"sv)
return DNSOverTLS { .server_address = server->to_byte_string(), .port = *port };
if (*type == "udp"sv)
return DNSOverUDP { .server_address = server->to_byte_string(), .port = *port };
}
}
}
dbgln("Invalid DNS settings in parse_dns_settings, falling back to system DNS");
return SystemDNS {};
}
Settings::Settings(ByteString settings_path)
: m_settings_path(move(settings_path))
, m_new_tab_page_url(URL::about_newtab())
@ -202,6 +235,28 @@ JsonValue Settings::serialize_json() const
settings.set(do_not_track_key, m_do_not_track == DoNotTrack::Yes);
// dnsSettings :: { mode: "system" } | { mode: "custom", server: string, port: u16, type: "udp" | "tls", forciblyEnabled: bool }
JsonObject dns_settings;
m_dns_settings.visit(
[&](SystemDNS) {
dns_settings.set("mode"sv, "system"sv);
},
[&](DNSOverTLS const& dot) {
dns_settings.set("mode"sv, "custom"sv);
dns_settings.set("server"sv, dot.server_address.view());
dns_settings.set("port"sv, dot.port);
dns_settings.set("type"sv, "tls"sv);
dns_settings.set("forciblyEnabled"sv, m_dns_override_by_command_line);
},
[&](DNSOverUDP const& dns) {
dns_settings.set("mode"sv, "custom"sv);
dns_settings.set("server"sv, dns.server_address.view());
dns_settings.set("port"sv, dns.port);
dns_settings.set("type"sv, "udp"sv);
dns_settings.set("forciblyEnabled"sv, m_dns_override_by_command_line);
});
settings.set(dns_settings_key, move(dns_settings));
return settings;
}
@ -214,6 +269,7 @@ void Settings::restore_defaults()
m_autocomplete_engine.clear();
m_autoplay = SiteSetting {};
m_do_not_track = DoNotTrack::No;
m_dns_settings = SystemDNS {};
persist_settings();
@ -224,6 +280,7 @@ void Settings::restore_defaults()
observer.autocomplete_engine_changed();
observer.autoplay_settings_changed();
observer.do_not_track_changed();
observer.dns_settings_changed();
}
}
@ -395,6 +452,18 @@ void Settings::set_do_not_track(DoNotTrack do_not_track)
observer.do_not_track_changed();
}
void Settings::set_dns_settings(DNSSettings const& dns_settings, bool override_by_command_line)
{
m_dns_settings = dns_settings;
m_dns_override_by_command_line = override_by_command_line;
if (!override_by_command_line)
persist_settings();
for (auto& observer : m_observers)
observer.dns_settings_changed();
}
void Settings::persist_settings()
{
auto settings = serialize_json();
@ -416,13 +485,29 @@ void Settings::remove_observer(Badge<SettingsObserver>, SettingsObserver& observ
VERIFY(was_removed);
}
SettingsObserver::SettingsObserver()
SettingsObserver::SettingsObserver(AddToObservers add_to_observers)
{
Settings::add_observer({}, *this);
if (add_to_observers == AddToObservers::Yes)
Settings::add_observer({}, *this);
else
m_registration_was_delayed = true;
}
SettingsObserver::~SettingsObserver()
{
if (!m_registration_was_delayed)
Settings::remove_observer({}, *this);
}
void SettingsObserver::complete_delayed_registration()
{
VERIFY(m_registration_was_delayed);
Settings::add_observer({}, *this);
}
void SettingsObserver::complete_delayed_unregistration()
{
VERIFY(m_registration_was_delayed);
Settings::remove_observer({}, *this);
}