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 connectinfo(remoteAddress) #379

Closed
biluohc opened this issue Jul 14, 2022 · 3 comments
Closed

how to get connectinfo(remoteAddress) #379

biluohc opened this issue Jul 14, 2022 · 3 comments

Comments

@biluohc
Copy link

biluohc commented Jul 14, 2022

No description provided.

@biluohc biluohc changed the title how to get connectinfo(remote) how to get connectinfo(remoteAddress) Jul 14, 2022
@yusukebe
Copy link
Member

Hi @biluohc

If your script run on Cloudflare Workers, you can get it by x-real-ip Request header.

app.get('/', (c) => {
  console.log(c.req.header('x-real-ip'))
  return c.text('foo')
})

On Deno and Bun, we don't have any way to get the client remote address.

@biluohc
Copy link
Author

biluohc commented Jul 15, 2022

Thanks.

The second parameter of the Handler is ConnInfo in the the standard library of Deno, consider supporting it?

https://deno.land/x/std@0.148.0/http/server.ts?code#L35

@biluohc
Copy link
Author

biluohc commented Jul 16, 2022

Solved.

export function sa(da: Deno.Addr) {
  if (["tcp", "udp"].includes(da.transport)) {
    const a = (da as Deno.NetAddr);
    return `${a.hostname}:${a.port}`;
  } else {
    const a = (da as Deno.UnixAddr);
    return a.path;
  }
}

serve(async (request: Request, ci: ConnInfo) => {
  const env = { sa: sa(ci.remoteAddr) };
  return app.fetch(request, env)
})

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

2 participants