I'm using bun v0.1.7 (NOT nodejs), I'm trying the built-in basic http server without any other framework:
export default {
port: 3000,
fetch(request: Request) {
const remoteIp = request.headers.get('x-forwarded-for') || 'UNKNOWN';
return new Response(remoteIp);
},
};
Can I get the remote IP from the client when I'm not using any proxies ?
I've tried request.connenction.remoteAddress
, request.socket...
But nothing seems to be implemented.
Bun v1.0.4
release implements server.requestIP()
.
Quoting from the release notes:
With
#6165
, the IP address of a given Request can now be retrieved usingserver.requestIP()
.Bun.serve({ port: 3000, handler: (req, res) => { console.log(server.requestIP(req)); }, });
This does not read headers like
X-Forwarded-For
orX-Real-IP
. It simply returns the IP address of the socket, which may correspond to the IP address of a proxy.We've also added support to
node:http
for getting the socket address.net.Socket().address
now returns an object with address, port, and family properties.