Get user's IP

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

NoN33d
Posts: 25
Joined: 23 Sep 2016, 12:07

Get user's IP

Postby NoN33d » 13 Sep 2019, 10:37

Hello,


We are using smartfox 2x version 2.13.6 for multiplayer functionality on our game.
We would like to find the client's IP in order to find his geo location.


This is the current code:

Code: Select all

public class UserHandler extends BaseClientRequestHandler {

    @Override
    public void handleClientRequest(User user, ISFSObject sfsObj) {

        String ipAdr = user.getIpAddress();
       
        IpGeoLocator geo = new IpGeoLocator();
        geo.init("data/", false);
        Country country = geo.locateIP(ipAdr);
       
        String country_name="Unknown";
        if (country != null) {
            country_name = country.getName();
        }
    }
}


The problem is that the user.getIpAddress() returns 10.2.1.2 which can not be used to track the geo location.


P.S. We have also tried enabling the "use geo location" option on the zone configuration section and called directly user.getCountry() but it returns null.


Thank you ,
Nikos
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Get user's IP

Postby Lapo » 14 Sep 2019, 07:06

Hi,
it sounds like your client connections are going through some kind of load balancer?
If that's the case there is no way to find out the original IP adddress.

One side note, in SFS2X 2.12 and higher there's a much simpler way to get the country of User:

Code: Select all

import com.smartfoxserver.v2.util.Country;
 
class MyHandler extends BaseClientRequestHandler
{
    void handleClientRequest(User sender, ISFSObject params)
    {
        Country ctry = sender.getCountry();
         
        if (ctry != null)
        {
            trace("User's country: " + ctry.getName());
            trace("Country ISO code: " + ctry.getIsoCode());
        }
    }
}

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
NoN33d
Posts: 25
Joined: 23 Sep 2016, 12:07

Re: Get user's IP

Postby NoN33d » 16 Sep 2019, 07:25

Hello Lapo,

Thank you for the reply.

We don't have a "load balancer", I am not even sure about what that is :) Can you give me more information to find out if I am using it and maybe how to disable it?

About your solution, as I mentioned on the first post I have tried it but it returns null.

What can I do to resolve this?

Thank you,
Nikos
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Get user's IP

Postby Lapo » 16 Sep 2019, 07:53

Maybe it's best to clarify one point first.
In your post you said you're getting "10.2.1.2" as the player's IP which corresponds to a local network IP.

Does this happen for every player? In other words if you check any player's IP address you always get the same local IP?
If so, it means that your server is behind a load balancer/proxy/firewall of some sort and what you see from the SmartFox side is the IP address of that device.

In other words it means that the player connection works like this:

Code: Select all

Player ---> Load Balancer ---> SmartFoxServer 2X

The player connects to the front facing load balancer/proxy which in turn opens a connection towards SFS2X and relays requests/responses.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
NoN33d
Posts: 25
Joined: 23 Sep 2016, 12:07

Re: Get user's IP

Postby NoN33d » 16 Sep 2019, 10:03

Hello Lapo and thank you for the update,

I have checked with other clients and they all return IP addresses like 10.2.1.1 or 10.2.1.2
Is there a way to find the user's IP then?

By the way, I am using Jelastic to host my game, if that helps. I haven't enabled the "Balancing option", as displayed on the attached screenshot.

Thank you,
Nikos
Attachments
Screenshot_2.png
(79.63 KiB) Not downloaded yet
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Get user's IP

Postby Lapo » 16 Sep 2019, 15:22

Hi,
NoN33d wrote:Hello Lapo and thank you for the update,

I have checked with other clients and they all return IP addresses like 10.2.1.1 or 10.2.1.2
Is there a way to find the user's IP then?

No because the client is not physically connected to the server, but rather to an intermediary which then connects to the server.

By the way, I am using Jelastic to host my game, if that helps. I haven't enabled the "Balancing option", as displayed on the attached screenshot.

I don't know exactly what might be going on. We have used Jelastic in the past for various tests and I don't recall seeing this kind of issues.
It would probably be best to speak with the Jelastic support people and ask them if they can do anything about it.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
NoN33d
Posts: 25
Joined: 23 Sep 2016, 12:07

Re: Get user's IP

Postby NoN33d » 17 Sep 2019, 09:54

Hello and thank you for the reply,

I will contact Jelastic support but in the meanwhile I would like to ask if there is another work-around.

For example, what about the code that you sent me before:

Code: Select all

import com.smartfoxserver.v2.util.Country;
 
class MyHandler extends BaseClientRequestHandler
{
    void handleClientRequest(User sender, ISFSObject params)
    {
        Country ctry = sender.getCountry();
         
        if (ctry != null)
        {
            trace("User's country: " + ctry.getName());
            trace("Country ISO code: " + ctry.getIsoCode());
        }
    }
}


Will that work in my setup or will it fail to identify the country since the user's IP corresponds to a local network IP?

Thank you,
Nikos
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Get user's IP

Postby Lapo » 17 Sep 2019, 14:31

Yes, it will fail because the IP address is pointing to a local network.
Cheers.
Lapo

--

gotoAndPlay()

...addicted to flash games
NoN33d
Posts: 25
Joined: 23 Sep 2016, 12:07

Re: Get user's IP

Postby NoN33d » 18 Sep 2019, 09:22

Hello Lapo and thank you for the reply,

Is there any work-around this? Maybe I can use client smartfox api to get the global IP and send it to the server via message?

Thank you,
Nikos
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Get user's IP

Postby Lapo » 18 Sep 2019, 09:50

Hi,
you could try with websocket, instead of regular sockets.
Because websocket is part of the HTTP protocol, most load-balancers and proxies recognize it and add a directive called "X-Forwarded-For" that allows the server to recognize the original IP address of the client. SFS2X supports this directive via Jetty, so if your proxy/balancer uses it you should be fine.

Of course the problem is if you can switch protocol, which in turn depends on the platform you're developing for. At the moment only HTML5 and Unity support websocket. Also if your game requires UDP, websocket won't be an option.

As regards the client sending their IP address, but there are several shortcomings:

1) the client can send you a fake IP address, you will have to trust them anyways
2) this won't work with the built-in functions in SFS2X such as banning system, IP filter, and similar security tools
3) in the log files you will still get the local addresses, so if you need to run any analytics they will fail at recognizing the players' IPs

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
NoN33d
Posts: 25
Joined: 23 Sep 2016, 12:07

Re: Get user's IP

Postby NoN33d » 18 Sep 2019, 09:59

Hello again and thank you for the quick reply,

The websocket solution seems promising I will look into it, thank you for that.

As regards the client sending their IP address, if I chose this approach which I think I will, how can I get the client's IP address from the client? Does smartfox have a build in function that does that?

By the way, the game is a mobile game, for android and ios devices.

Thank you,
Nikos
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Get user's IP

Postby Lapo » 18 Sep 2019, 15:37

NoN33d wrote:Hello again and thank you for the quick reply,

The websocket solution seems promising I will look into it, thank you for that.

It all depends on the proxy/balancer. If it transmits the original address no problem, otherwise you'll have the same issue.

As regards the client sending their IP address, if I chose this approach which I think I will, how can I get the client's IP address from the client? Does smartfox have a build in function that does that?

No. You need to find the public IP address of the client, so you need an external service that tells you that.
For example you could use this --> http://checkip.amazonaws.com
You just call the page and it gives you back your public address. That's the one you need to send.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
NoN33d
Posts: 25
Joined: 23 Sep 2016, 12:07

Re: Get user's IP

Postby NoN33d » 19 Sep 2019, 14:34

Hello Lapo,

Thank you for the support, you helped me a lot, I will consider all the options and try them out.

Thanks again,
Nikos

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 69 guests