Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get client ip? #230

Closed
xfxpositions opened this issue Sep 26, 2023 · 12 comments
Closed

How to get client ip? #230

xfxpositions opened this issue Sep 26, 2023 · 12 comments

Comments

@xfxpositions
Copy link

i'm trying to find something like this
from koajs

const client_ip =
ctx.req.ip ||
ctx.req.headers["x-forwarded-for"] ||
ctx.req.connection.remoteAddress;
@masfahru
Copy link

i think client ip should be available after this PR oven-sh/bun#6165 is done

@bogeychan
Copy link
Contributor

Hi 👋

since Bun v1.0.4 you can do something like this:

import { Elysia } from 'elysia';

const app = new Elysia();

app
  .derive((ctx) => ({
    remoteAddress: () => app.server!.requestIP(ctx.request)
  }))
  .get('/', (ctx) => ctx.remoteAddress())

  .listen(3000);

console.log(`Listening on http://${app.server!.hostname}:${app.server!.port}`);

Response:

{
    "address": "::1",
    "family": "IPv6",
    "port": 40908
}

@juancwu
Copy link

juancwu commented Oct 4, 2023

Is there a plan to add the IP to the context request object by default? If not, I am down to try to add it. I am relatively new here but I am sure it shouldn't be too hard.

Please let me know if having it in the context request object is desired or not :)

@gaurishhs
Copy link

You can use https://github.com/gaurishhs/elysia-ip

@cassinius
Copy link

Thank you for providing this library, it works well on the main Elysia app instance ;-)

However, if I try to use this in a plugin, I get

null is not an object (evaluating '<plugin>.server.requestIP')

Is there any way to make this work in a specific plugin only ?

@gaurishhs
Copy link

that means the server isn't up yet, Try to use the plugin inside an Elysia app instance and call the .listen method, It should work well. Currently, You don't seem to call .listen

@cassinius
Copy link

I am calling .listen on the main Elysia app instance, here is my code:

import { Elysia } from "elysia";
import { ip } from "elysia-ip";

const plugin = new Elysia()
  .use(ip())
  .get("/ip-from-plugin", ({ ip }) => ip);

const app = new Elysia()
  .use(ip())
  .get("/ip", ({ ip }) => ip)
  .use(plugin)
  .listen(3000);

Here, /ip works as expected, /ip-from-plugin throws the error.
If I call .listen(<different_port>) on the plugin as well, like

const plugin = new Elysia()
  .use(ip())
  .get("/ip-from-plugin", ({ ip }) => ip)
  .listen(3001);

it actually DOES work, but I doubt that's how it's meant to be used?

P.S. removing the ip-related lines from the main instance does not make a difference.

@gaurishhs
Copy link

gaurishhs commented Oct 5, 2023

I am facing the same, i don't really know the reason behind the same. I believe its somehow related to how plugins work in Elysia

@gaurishhs
Copy link

gaurishhs commented Oct 6, 2023

@cassinius Hi, i seem to have found a fix for the same. Not initalizing the ip in plugin seems to work.

import { Elysia } from "elysia";
import { ip } from "elysia-ip";

const plugin = new Elysia()
  // .use(ip())
  .get("/ip-from-plugin", ({ ip }) => ip);

const app = new Elysia()
  .use(ip())
  .get("/ip", ({ ip }) => ip)
  .use(plugin)
  .listen(3000);

The autocomplete is not there for some reason in plugin and there on the app instance which doesn't really make sense to me (It's probably related to how .derive works). I reached out to salty for further help.

@xfxpositions
Copy link
Author

// getting request ip
app.use(ip());
TypeError: undefined is not an object (evaluating 'request.headers')

not working

@gaurishhs
Copy link

gaurishhs commented Oct 6, 2023 via email

@SaltyAom
Copy link
Member

Close as elysia-ip plugin and server property is now implemented in Elysia 1.1

You can natively get IP from Elysia request like this, make sure server is running:

new Elysia()
	.get('/', ({ server, request }) => {
		server?.requestIP(request)
	})
        .listen(3000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants