support CDP proxy override

This commit is contained in:
Karl Seguin
2025-08-06 11:49:57 +08:00
parent 1e612e4166
commit c96fb3c2f2
4 changed files with 60 additions and 15 deletions

View File

@@ -338,10 +338,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
inspector: Inspector,
isolated_world: ?IsolatedWorld,
// Used to restore the proxy after the CDP session ends. If CDP never over-wrote it, it won't restore it (the first null).
// If the CDP is restoring it, but the original value was null, that's the 2nd null.
// If you only have 1 null it would be ambiguous, does null mean it shouldn't be restored, or should it be restored to null?
http_proxy_before: ??std.Uri = null,
http_proxy_changed: bool = false,
const Self = @This();
@@ -397,7 +394,13 @@ pub fn BrowserContext(comptime CDP_T: type) type {
self.node_search_list.deinit();
self.cdp.browser.notification.unregisterAll(self);
if (self.http_proxy_before) |prev_proxy| self.cdp.browser.http_client.http_proxy = prev_proxy;
if (self.http_proxy_changed) {
// has to be called after browser.closeSession, since it won't
// work if there are active connections.
self.cdp.browser.http_client.restoreOriginalProxy() catch |err| {
log.warn(.http, "restoreOriginalProxy", .{ .err = err });
};
}
}
pub fn reset(self: *Self) void {

View File

@@ -68,7 +68,7 @@ fn getBrowserContexts(cmd: anytype) !void {
fn createBrowserContext(cmd: anytype) !void {
const params = try cmd.params(struct {
disposeOnDetach: bool = false,
proxyServer: ?[]const u8 = null,
proxyServer: ?[:0]const u8 = null,
proxyBypassList: ?[]const u8 = null,
originsWithUniversalNetworkAccess: ?[]const []const u8 = null,
});
@@ -84,9 +84,8 @@ fn createBrowserContext(cmd: anytype) !void {
if (params) |p| {
if (p.proxyServer) |proxy| {
// For now the http client is not in the browser context so we assume there is just 1.
bc.http_proxy_before = cmd.cdp.browser.http_client.http_proxy;
const proxy_cp = try cmd.cdp.browser.http_client.allocator.dupe(u8, proxy);
cmd.cdp.browser.http_client.http_proxy = try std.Uri.parse(proxy_cp);
try cmd.cdp.browser.http_client.changeProxy(proxy);
bc.http_proxy_changed = true;
}
}