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

Possible to get client ipv4 address on HttpRequest or HttpResponse? #460

Closed
Wyzix33 opened this issue Feb 18, 2021 · 2 comments
Closed

Possible to get client ipv4 address on HttpRequest or HttpResponse? #460

Wyzix33 opened this issue Feb 18, 2021 · 2 comments

Comments

@Wyzix33
Copy link

Wyzix33 commented Feb 18, 2021

Hi,
I need to get the client ip address before the websocket connection and i have this:

  const app = uWS
  .SSLApp({ key_file_name: './privkey1.pem', cert_file_name: './cert1.pem' })
  .ws('/*', require('./socket/connect'))
  .any('/*', (res,req){
 res.cork(() => {
   res.write('<html><h1>');
   res.write('Your proxied IP is: ' + Buffer.from(res.getProxiedRemoteAddressAsText()).toString());
   res.write('</h1><h1>');
   res.write('Your IP as seen by the origin server is: ' + Buffer.from(res.getRemoteAddressAsText()).toString());
   res.end('</h1></html>');
  });
})
  .listen(443, (token) => {
   if (token) {
    console.log('Listening to port 443');
   } else {
    console.log('Failed to listen to port 443');
   }
  });

but I'm getting:

Your proxied IP is:
Your IP as seen by the origin server is: 0000:0000:0000:0000:0000:ffff:bc1b:f640

This seems my ipv6 address but if i check on https://www.whatismyip.com/ i get

 My Public IPv4 is: 188.27.111.21
 My Public IPv6 is: Not Detected

Is is safe to use this address to identify connections coming from the same IP?
Is there a way to get the ipv4 address?
Thanks

@hst-m
Copy link
Contributor

hst-m commented Feb 20, 2021

You dont need the getProxiedRemoteAddress unless you have a reverse proxy set up, see here. The 0000:0000:0000:0000:0000:ffff:bc1b:f640 is the ip6 format which converts to/is the same thing as your ip4 188.27.111.21. All ip4 can convert to ip6 but not all ip6 can convert to ip4. So you should probably leave it in ip6 format, but you can convert to ip4 if needed https://stackoverflow.com/q/2786632

@Wyzix33
Copy link
Author

Wyzix33 commented Feb 20, 2021

OK, Thanks

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