From 6afa78120ac0512bc55d00a8a1562d8f0eafa2c2 Mon Sep 17 00:00:00 2001 From: dave caruso Date: Fri, 29 Sep 2023 03:39:26 -0700 Subject: feat(runtime): implement `server.requestIp` + node:http `socket.address()` (#6165) * [server] requestIp and AnyRequestContext Changed Request.uws_request to the new AnyRequestContext. This allows grabbing the IP from a Request. Unfinished. * [server] basic `requestIp` implementation Currently using uws's requestIpAsText, which always returns a ipv6 string. We should return a `SocketAddress` object to the user instead, which will contain the formatted address string and what type it is. We'll have to use requestIpAsBinary and parse that ourselves. * TypeScript docs, use `bun.String`, return `undefined` instead of `null` if we can't get the ip. * binary address formatting * uws getRemoteAddress binding * remove dead code * working * final touches:sparkles: * I will abide by the results of this poll. --------- Co-authored-by: Parzival-3141 <29632054+Parzival-3141@users.noreply.github.com> --- packages/bun-usockets/src/socket.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'packages/bun-usockets/src/socket.c') diff --git a/packages/bun-usockets/src/socket.c b/packages/bun-usockets/src/socket.c index ce5203ccb..78185e681 100644 --- a/packages/bun-usockets/src/socket.c +++ b/packages/bun-usockets/src/socket.c @@ -280,3 +280,25 @@ int us_socket_raw_write(int ssl, struct us_socket_t *s, const char *data, int le // non-TLS is always raw return us_socket_write(ssl, s, data, length, msg_more); } + +unsigned int us_get_remote_address_info(char *buf, struct us_socket_t *s, const char **dest, int *port, int *is_ipv6) +{ + // This function is manual inlining + modification of + // us_socket_remote_address + // AsyncSocket::getRemoteAddress + // To get { ip, port, is_ipv6 } for Bun.serve().requestIP() + struct bsd_addr_t addr; + if (bsd_remote_addr(us_poll_fd(&s->p), &addr)) { + return 0; + } + + int length = bsd_addr_get_ip_length(&addr); + if (!length) { + return 0; + } + + memcpy(buf, bsd_addr_get_ip(&addr), length); + *port = bsd_addr_get_port(&addr); + + return length; +} \ No newline at end of file -- cgit v1.2.3