Merge pull request #1955 from lightpanda-io/advertise_host
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled

Add --advertise_host option to serve command
This commit is contained in:
Karl Seguin
2026-03-23 20:00:42 +08:00
committed by GitHub
2 changed files with 54 additions and 20 deletions

View File

@@ -163,6 +163,20 @@ pub fn cdpTimeout(self: *const Config) usize {
}; };
} }
pub fn port(self: *const Config) u16 {
return switch (self.mode) {
.serve => |opts| opts.port,
else => unreachable,
};
}
pub fn advertiseHost(self: *const Config) []const u8 {
return switch (self.mode) {
.serve => |opts| opts.advertise_host orelse opts.host,
else => unreachable,
};
}
pub fn webBotAuth(self: *const Config) ?WebBotAuthConfig { pub fn webBotAuth(self: *const Config) ?WebBotAuthConfig {
return switch (self.mode) { return switch (self.mode) {
inline .serve, .fetch, .mcp => |opts| WebBotAuthConfig{ inline .serve, .fetch, .mcp => |opts| WebBotAuthConfig{
@@ -199,6 +213,7 @@ pub const Mode = union(RunMode) {
pub const Serve = struct { pub const Serve = struct {
host: []const u8 = "127.0.0.1", host: []const u8 = "127.0.0.1",
port: u16 = 9222, port: u16 = 9222,
advertise_host: ?[]const u8 = null,
timeout: u31 = 10, timeout: u31 = 10,
cdp_max_connections: u16 = 16, cdp_max_connections: u16 = 16,
cdp_max_pending_connections: u16 = 128, cdp_max_pending_connections: u16 = 128,
@@ -416,6 +431,11 @@ pub fn printUsageAndExit(self: *const Config, success: bool) void {
\\--port Port of the CDP server \\--port Port of the CDP server
\\ Defaults to 9222 \\ Defaults to 9222
\\ \\
\\--advertise_host
\\ The host to advertise, e.g. in the /json/version response.
\\ Useful, for example, when --host is 0.0.0.0.
\\ Defaults to --host value
\\
\\--timeout Inactivity timeout in seconds before disconnecting clients \\--timeout Inactivity timeout in seconds before disconnecting clients
\\ Defaults to 10 (seconds). Limited to 604800 (1 week). \\ Defaults to 10 (seconds). Limited to 604800 (1 week).
\\ \\
@@ -557,6 +577,15 @@ fn parseServeArgs(
continue; continue;
} }
if (std.mem.eql(u8, "--advertise_host", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--advertise_host" });
return error.InvalidArgument;
};
serve.advertise_host = try allocator.dupe(u8, str);
continue;
}
if (std.mem.eql(u8, "--timeout", opt)) { if (std.mem.eql(u8, "--timeout", opt)) {
const str = args.next() orelse { const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--timeout" }); log.fatal(.app, "missing argument value", .{ .arg = "--timeout" });

View File

@@ -45,7 +45,7 @@ clients_pool: std.heap.MemoryPool(Client),
pub fn init(app: *App, address: net.Address) !*Server { pub fn init(app: *App, address: net.Address) !*Server {
const allocator = app.allocator; const allocator = app.allocator;
const json_version_response = try buildJSONVersionResponse(allocator, address); const json_version_response = try buildJSONVersionResponse(app);
errdefer allocator.free(json_version_response); errdefer allocator.free(json_version_response);
const self = try allocator.create(Server); const self = try allocator.create(Server);
@@ -484,11 +484,17 @@ pub const Client = struct {
// -------- // --------
fn buildJSONVersionResponse( fn buildJSONVersionResponse(
allocator: Allocator, app: *const App,
address: net.Address,
) ![]const u8 { ) ![]const u8 {
const body_format = "{{\"webSocketDebuggerUrl\": \"ws://{f}/\"}}"; const port = app.config.port();
const body_len = std.fmt.count(body_format, .{address}); const host = app.config.advertiseHost();
if (std.mem.eql(u8, host, "0.0.0.0")) {
log.info(.cdp, "unreachable advertised host", .{
.message = "when --host is set to 0.0.0.0 consider setting --advertise_host to a reachable address",
});
}
const body_format = "{{\"webSocketDebuggerUrl\": \"ws://{s}:{d}/\"}}";
const body_len = std.fmt.count(body_format, .{ host, port });
// We send a Connection: Close (and actually close the connection) // We send a Connection: Close (and actually close the connection)
// because chromedp (Go driver) sends a request to /json/version and then // because chromedp (Go driver) sends a request to /json/version and then
@@ -502,23 +508,22 @@ fn buildJSONVersionResponse(
"Connection: Close\r\n" ++ "Connection: Close\r\n" ++
"Content-Type: application/json; charset=UTF-8\r\n\r\n" ++ "Content-Type: application/json; charset=UTF-8\r\n\r\n" ++
body_format; body_format;
return try std.fmt.allocPrint(allocator, response_format, .{ body_len, address }); return try std.fmt.allocPrint(app.allocator, response_format, .{ body_len, host, port });
} }
pub const timestamp = @import("datetime.zig").timestamp; pub const timestamp = @import("datetime.zig").timestamp;
pub const milliTimestamp = @import("datetime.zig").milliTimestamp; pub const milliTimestamp = @import("datetime.zig").milliTimestamp;
const testing = std.testing; const testing = @import("testing.zig");
test "server: buildJSONVersionResponse" { test "server: buildJSONVersionResponse" {
const address = try net.Address.parseIp4("127.0.0.1", 9001); const res = try buildJSONVersionResponse(testing.test_app);
const res = try buildJSONVersionResponse(testing.allocator, address); defer testing.test_app.allocator.free(res);
defer testing.allocator.free(res);
try testing.expectEqualStrings("HTTP/1.1 200 OK\r\n" ++ try testing.expectEqual("HTTP/1.1 200 OK\r\n" ++
"Content-Length: 48\r\n" ++ "Content-Length: 48\r\n" ++
"Connection: Close\r\n" ++ "Connection: Close\r\n" ++
"Content-Type: application/json; charset=UTF-8\r\n\r\n" ++ "Content-Type: application/json; charset=UTF-8\r\n\r\n" ++
"{\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9001/\"}", res); "{\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9222/\"}", res);
} }
test "Client: http invalid request" { test "Client: http invalid request" {
@@ -526,7 +531,7 @@ test "Client: http invalid request" {
defer c.deinit(); defer c.deinit();
const res = try c.httpRequest("GET /over/9000 HTTP/1.1\r\n" ++ "Header: " ++ ("a" ** 4100) ++ "\r\n\r\n"); const res = try c.httpRequest("GET /over/9000 HTTP/1.1\r\n" ++ "Header: " ++ ("a" ** 4100) ++ "\r\n\r\n");
try testing.expectEqualStrings("HTTP/1.1 413 \r\n" ++ try testing.expectEqual("HTTP/1.1 413 \r\n" ++
"Connection: Close\r\n" ++ "Connection: Close\r\n" ++
"Content-Length: 17\r\n\r\n" ++ "Content-Length: 17\r\n\r\n" ++
"Request too large", res); "Request too large", res);
@@ -595,7 +600,7 @@ test "Client: http valid handshake" {
"Custom: Header-Value\r\n\r\n"; "Custom: Header-Value\r\n\r\n";
const res = try c.httpRequest(request); const res = try c.httpRequest(request);
try testing.expectEqualStrings("HTTP/1.1 101 Switching Protocols\r\n" ++ try testing.expectEqual("HTTP/1.1 101 Switching Protocols\r\n" ++
"Upgrade: websocket\r\n" ++ "Upgrade: websocket\r\n" ++
"Connection: upgrade\r\n" ++ "Connection: upgrade\r\n" ++
"Sec-Websocket-Accept: flzHu2DevQ2dSCSVqKSii5e9C2o=\r\n\r\n", res); "Sec-Websocket-Accept: flzHu2DevQ2dSCSVqKSii5e9C2o=\r\n\r\n", res);
@@ -723,7 +728,7 @@ test "server: 404" {
defer c.deinit(); defer c.deinit();
const res = try c.httpRequest("GET /unknown HTTP/1.1\r\n\r\n"); const res = try c.httpRequest("GET /unknown HTTP/1.1\r\n\r\n");
try testing.expectEqualStrings("HTTP/1.1 404 \r\n" ++ try testing.expectEqual("HTTP/1.1 404 \r\n" ++
"Connection: Close\r\n" ++ "Connection: Close\r\n" ++
"Content-Length: 9\r\n\r\n" ++ "Content-Length: 9\r\n\r\n" ++
"Not found", res); "Not found", res);
@@ -735,7 +740,7 @@ test "server: get /json/version" {
"Content-Length: 48\r\n" ++ "Content-Length: 48\r\n" ++
"Connection: Close\r\n" ++ "Connection: Close\r\n" ++
"Content-Type: application/json; charset=UTF-8\r\n\r\n" ++ "Content-Type: application/json; charset=UTF-8\r\n\r\n" ++
"{\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9583/\"}"; "{\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9222/\"}";
{ {
// twice on the same connection // twice on the same connection
@@ -743,7 +748,7 @@ test "server: get /json/version" {
defer c.deinit(); defer c.deinit();
const res1 = try c.httpRequest("GET /json/version HTTP/1.1\r\n\r\n"); const res1 = try c.httpRequest("GET /json/version HTTP/1.1\r\n\r\n");
try testing.expectEqualStrings(expected_response, res1); try testing.expectEqual(expected_response, res1);
} }
{ {
@@ -752,7 +757,7 @@ test "server: get /json/version" {
defer c.deinit(); defer c.deinit();
const res1 = try c.httpRequest("GET /json/version HTTP/1.1\r\n\r\n"); const res1 = try c.httpRequest("GET /json/version HTTP/1.1\r\n\r\n");
try testing.expectEqualStrings(expected_response, res1); try testing.expectEqual(expected_response, res1);
} }
} }
@@ -770,7 +775,7 @@ fn assertHTTPError(
.{ expected_status, expected_body.len, expected_body }, .{ expected_status, expected_body.len, expected_body },
); );
try testing.expectEqualStrings(expected_response, res); try testing.expectEqual(expected_response, res);
} }
fn assertWebSocketError(close_code: u16, input: []const u8) !void { fn assertWebSocketError(close_code: u16, input: []const u8) !void {
@@ -914,7 +919,7 @@ const TestClient = struct {
"Custom: Header-Value\r\n\r\n"; "Custom: Header-Value\r\n\r\n";
const res = try self.httpRequest(request); const res = try self.httpRequest(request);
try testing.expectEqualStrings("HTTP/1.1 101 Switching Protocols\r\n" ++ try testing.expectEqual("HTTP/1.1 101 Switching Protocols\r\n" ++
"Upgrade: websocket\r\n" ++ "Upgrade: websocket\r\n" ++
"Connection: upgrade\r\n" ++ "Connection: upgrade\r\n" ++
"Sec-Websocket-Accept: flzHu2DevQ2dSCSVqKSii5e9C2o=\r\n\r\n", res); "Sec-Websocket-Accept: flzHu2DevQ2dSCSVqKSii5e9C2o=\r\n\r\n", res);