Skip to content

Truly independent web browser

RequestServer+LibRequests: Look up hosts for others that connect

Ali Mohammad Pura043bd394a8f
diff --git a/Libraries/LibRequests/RequestClient.cpp b/Libraries/LibRequests/RequestClient.cpp
index 338247d43d6..ec94ef226b4 100644
--- a/Libraries/LibRequests/RequestClient.cpp
+++ b/Libraries/LibRequests/RequestClient.cpp
@@ -60,6 +60,10 @@ void RequestClient::die()
m_requests.clear();
m_websockets.clear();
+ auto host_lookups = move(m_host_lookups);
+ for (auto& [id, on_resolved] : host_lookups)
+ on_resolved({});
+
for (auto& [id, websocket] : websockets) {
auto ready_state = websocket->ready_state();
websocket->detach_from_client({});
@@ -182,6 +186,19 @@ void RequestClient::ensure_connection(URL::URL const& url, RequestServer::CacheL
async_ensure_connection(request_id, url, cache_level);
}
+void RequestClient::resolve_host(ByteString const& host, Function<void(Vector<ByteString>)> on_resolved)
+{
+ auto lookup_id = m_next_host_lookup_id++;
+ m_host_lookups.set(lookup_id, move(on_resolved));
+ async_resolve_host(lookup_id, host);
+}
+
+void RequestClient::host_resolved(u64 lookup_id, Vector<ByteString> addresses)
+{
+ if (auto on_resolved = m_host_lookups.take(lookup_id); on_resolved.has_value())
+ (*on_resolved)(move(addresses));
+}
+
void RequestClient::request_requires_network(u64 request_id)
{
if (auto request = m_requests.get(request_id); request.has_value()) {
diff --git a/Libraries/LibRequests/RequestClient.h b/Libraries/LibRequests/RequestClient.h
index 3c8217040df..3f383254ae1 100644
--- a/Libraries/LibRequests/RequestClient.h
+++ b/Libraries/LibRequests/RequestClient.h
@@ -62,6 +62,8 @@ public:
void release_request_transfer_lease(Badge<Request>, Request&, RequestTransferLeaseKey);
void release_request_transfer_lease(RequestTransferLeaseKey);
void ensure_connection(URL::URL const&, RequestServer::CacheLevel);
+ // Calls back with no addresses when the lookup fails or a proxy is configured.
+ void resolve_host(ByteString const& host, Function<void(Vector<ByteString>)> on_resolved);
int request_server_client_id() const { return m_request_server_client_id; }
RefPtr<WebSocket> websocket_connect(URL::URL const&, Optional<HTTP::NetworkIsolationKey> const&, ByteString const& origin, Vector<ByteString> const& protocols, Vector<ByteString> const& extensions, HTTP::HeaderList const& request_headers);
@@ -83,6 +85,7 @@ private:
virtual void request_finished(u64 request_id, u64, RequestTimingInfo, Optional<NetworkError>) override;
virtual void headers_became_available(u64 request_id, Vector<HTTP::Header>, Optional<u32>, Optional<String>, Optional<IPC::File>, u64 javascript_bytecode_size, Optional<u64>, CacheState, ByteString http_version) override;
virtual void request_transferred(u64 request_id) override;
+ virtual void host_resolved(u64 lookup_id, Vector<ByteString> addresses) override;
virtual void websocket_connected(u64 websocket_id) override;
virtual void websocket_received(u64 websocket_id, bool, ByteBuffer) override;
@@ -98,6 +101,9 @@ private:
HashMap<u64, NonnullRefPtr<WebSocket>> m_websockets;
u64 m_next_websocket_id { 0 };
+
+ HashMap<u64, Function<void(Vector<ByteString>)>> m_host_lookups;
+ u64 m_next_host_lookup_id { 0 };
};
}
diff --git a/Services/RequestServer/ConnectionFromClient.cpp b/Services/RequestServer/ConnectionFromClient.cpp
index 5b5d1fce24c..bdd829975ab 100644
--- a/Services/RequestServer/ConnectionFromClient.cpp
+++ b/Services/RequestServer/ConnectionFromClient.cpp
@@ -800,6 +800,32 @@ void ConnectionFromClient::import_request(u64 request_id, Requests::ExportedRequ
m_active_requests.set(request_id, move(request));
}
+void ConnectionFromClient::resolve_host(u64 lookup_id, ByteString host)
+{
+ // Whatever connects to the host would bypass a proxy, and a proxy that resolves names would never see this lookup,
+ // so nothing is looked up while one is configured.
+ if (!proxy_configuration().is_empty() || host.is_empty() || host.length() > 253) {
+ async_host_resolved(lookup_id, {});
+ return;
+ }
+
+ auto weak_self = make_weak_ptr<ConnectionFromClient>();
+ m_resolver->dns.lookup(host, DNS::Messages::Class::IN, { DNS::Messages::ResourceType::A, DNS::Messages::ResourceType::AAAA }, { .validate_dnssec_locally = DNSInfo::the().validate_dnssec_locally })
+ ->when_rejected([weak_self, lookup_id](auto const&) {
+ if (auto self = weak_self.strong_ref())
+ self->async_host_resolved(lookup_id, {});
+ })
+ .when_resolved([weak_self, lookup_id](auto const& dns_result) {
+ auto self = weak_self.strong_ref();
+ if (!self)
+ return;
+ Vector<ByteString> addresses;
+ for (auto const& address : dns_result->cached_addresses())
+ addresses.append(address.visit([](auto const& address) { return ByteString { MUST(address.to_string()) }; }));
+ self->async_host_resolved(lookup_id, move(addresses));
+ });
+}
+
void ConnectionFromClient::ensure_connection(u64 request_id, URL::URL url, ::RequestServer::CacheLevel cache_level)
{
if (is_live_request_id(request_id)) {
diff --git a/Services/RequestServer/ConnectionFromClient.h b/Services/RequestServer/ConnectionFromClient.h
index 675be55f1a3..f176441328c 100644
--- a/Services/RequestServer/ConnectionFromClient.h
+++ b/Services/RequestServer/ConnectionFromClient.h
@@ -93,6 +93,7 @@ private:
virtual Messages::RequestServer::ExportRequestResponse export_request(u64 request_id) override;
virtual void import_request(u64 request_id, Requests::ExportedRequest, bool create_transfer_lease) override;
virtual void ensure_connection(u64 request_id, URL::URL url, ::RequestServer::CacheLevel cache_level) override;
+ virtual void resolve_host(u64 lookup_id, ByteString host) override;
virtual Messages::RequestServer::StoreCacheAssociatedDataResponse store_cache_associated_data(Optional<HTTP::NetworkIsolationKey>, URL::URL, ByteString method, Vector<HTTP::Header> request_headers, Optional<u64> vary_key, HTTP::CacheEntryAssociatedData, Core::AnonymousBuffer) override;
virtual Messages::RequestServer::RetrieveCacheAssociatedDataResponse retrieve_cache_associated_data(Optional<HTTP::NetworkIsolationKey>, URL::URL, ByteString method, Vector<HTTP::Header> request_headers, Optional<u64> vary_key, HTTP::CacheEntryAssociatedData) override;
diff --git a/Services/RequestServer/RequestClient.ipc b/Services/RequestServer/RequestClient.ipc
index 088004dcd2e..0b89380d0ef 100644
--- a/Services/RequestServer/RequestClient.ipc
+++ b/Services/RequestServer/RequestClient.ipc
@@ -15,6 +15,7 @@ endpoint RequestClient
request_finished(u64 request_id, u64 total_size, Requests::RequestTimingInfo timing_info, Optional<Requests::NetworkError> network_error) =|
headers_became_available(u64 request_id, Vector<HTTP::Header> response_headers, Optional<u32> status_code, Optional<String> reason_phrase, Optional<IPC::File> javascript_bytecode, u64 javascript_bytecode_size, Optional<u64> javascript_bytecode_cache_vary_key, Requests::CacheState cache_state, ByteString http_version) =|
request_transferred(u64 request_id) =|
+ host_resolved(u64 lookup_id, Vector<ByteString> addresses) =|
// Websocket API
// FIXME: See if this can be merged with the regular APIs
diff --git a/Services/RequestServer/RequestServer.ipc b/Services/RequestServer/RequestServer.ipc
index b7e3d7825a5..c66176ed836 100644
--- a/Services/RequestServer/RequestServer.ipc
+++ b/Services/RequestServer/RequestServer.ipc
@@ -29,6 +29,10 @@ endpoint RequestServer
ensure_connection(u64 request_id, URL::URL url, ::RequestServer::CacheLevel cache_level) =|
+ // Looks up a host that something other than a request connects to, such as a WebRTC ICE server, so that the
+ // browser's DNS settings apply to it too.
+ resolve_host(u64 lookup_id, ByteString host) =|
+
store_cache_associated_data(Optional<HTTP::NetworkIsolationKey> network_isolation_key, URL::URL url, ByteString method, Vector<HTTP::Header> request_headers, Optional<u64> vary_key, HTTP::CacheEntryAssociatedData associated_data, Core::AnonymousBuffer data) => (Optional<IPC::File> file, u64 size)
retrieve_cache_associated_data(Optional<HTTP::NetworkIsolationKey> network_isolation_key, URL::URL url, ByteString method, Vector<HTTP::Header> request_headers, Optional<u64> vary_key, HTTP::CacheEntryAssociatedData associated_data) => (Optional<Core::AnonymousBuffer> data)