TLS Encryption with JAVA Client

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

Moderators: Lapo, Bax

DeepakSharma
Posts: 4
Joined: 09 Oct 2020, 10:48

TLS Encryption with JAVA Client

Postby DeepakSharma » 09 Oct 2020, 11:13

Hi Lapo,
Good Evening.

I am trying to use TLS encryption for connecting java client with the server (SFS 2.16). I made all the necessary changes in server to allow encryption. Server also have a valid SSL certificate.
But my client code does not move ahead of sfs.initCrypto() method call.
SFSEvent.CRYPTO_INIT event is implemented in client code.

in smartfox.log I see session created and session removed statement.
below is my client config.

<SmartFoxConfig>
<!-- Mandatory Settings -->
<ip>gatewayskyline.adda52store.com</ip>
<udpIp>gatewayskyline.adda52store.com</udpIp>
<port>9933</port>
<udpPort>9933</udpPort>
<zone>PokerKing</zone>
<!-- End Mandatory Settings -->

<debug>true</debug>

<!-- For generic http port communication -->
<httpPort>8081</httpPort>
<httpsPort>8893</httpsPort>
<!-- BlueBox settings -->
<useBlueBox>true</useBlueBox>
<blueBoxPollingRate>700</blueBoxPollingRate>

</SmartFoxConfig>

Please suggest if i am missing something.
myjava client code works perfectly fine if I don't use TLS encryption.
User avatar
Lapo
Site Admin
Posts: 21723
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: TLS Encryption with JAVA Client

Postby Lapo » 09 Oct 2020, 14:12

Hi,
the first thing is to report both client and server side errors. If the initCrypto() fails to complete you should see errors both on server and client side.

Let us know.
Lapo
--
gotoAndPlay()
...addicted to flash games
DeepakSharma
Posts: 4
Joined: 09 Oct 2020, 10:48

Re: TLS Encryption with JAVA Client

Postby DeepakSharma » 10 Oct 2020, 11:23

Hi Lapo,
I didn't see any error at client side or server side.

At smartfox.log I see only these entries.

09 Oct 2020 | 10:11:30,360 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session created: { Id: 46, Type: DEFAULT, Logged: No, IP: 192.168.1.2:52114 } on Server port: 9933 <---> 52114
09 Oct 2020 | 10:11:30,757 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session removed: { Id: 46, Type: DEFAULT, Logged: No, IP: 192.168.1.2:52114 }

and at client side connection lost event is triggered.
User avatar
Lapo
Site Admin
Posts: 21723
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: TLS Encryption with JAVA Client

Postby Lapo » 11 Oct 2020, 16:33

Hi,
the first strange thing I notice is that the client IP address comes from a local network address, which seems very odd if you're testing connecting to an internet domain.

In any case I tried connecting to the domain you have mentioned with a Java client and did not find any problem. The Cryptography initialization is done correctly.

Can you please show me the code you're using for the client connection?

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
DeepakSharma
Posts: 4
Joined: 09 Oct 2020, 10:48

Re: TLS Encryption with JAVA Client

Postby DeepakSharma » 12 Oct 2020, 04:30

Hi Lapo,
Below is snippet for my client code .

Code: Select all

             sfs=new SmartFox();      
      //here the true flag assures that SmartFox will automatically connect to server on successful load config.
      try
      {
         
         sfs.loadConfig(getCfgPath(), true);   
      }
      catch (Exception e) {
         System.out.println("Unable to connect to host.");
      }
                 
      
 
      sfs.addEventListener(SFSEvent.CONNECTION, new IEventListener() {
           public void dispatch(BaseEvent evt) throws SFSException {
              System.out.println("Super Bot Connected with Id="+superBotId +" and password="+botAccountMap.get(superBotId).getPassword());
              //System.out.println("reconnecting before : "+reconnecting);
              if(reconnecting)
              {   
                 //System.out.println("SuperBot Reconnected successfully .Now doing Login.");
                 reconnectTimer.cancel();   
                 reconnectionAttempts=0;
                 //System.out.println("Setting reconnecting to FALSE.Connection");
                  reconnecting=false;
              }
              //System.out.println("reconnecting after: "+reconnecting);
//              ISFSObject obj = new SFSObject();
//             obj.putUtfString("appName", "JAVA");
             
             //TODO : add flag for encryption from property file
             
              System.out.println("Hello1");
             sfs.initCrypto();
             System.out.println("Hello2");
//              sfs.send(new LoginRequest(superBotId, getSHAofPass(botAccountMap.get(superBotId).getPassword()) , sfs.getCurrentZone(), obj));
           }
           });
      
            sfs.addEventListener(SFSEvent.CRYPTO_INIT, new IEventListener() {
               public void dispatch(BaseEvent evt) throws SFSException {
                  System.out.println("Attempting Encryption");
                  boolean success = (Boolean) evt.getArguments().get("success");

                  if (success) {
                     System.out.println("Encryption was successful!"+ success);

                     ISFSObject obj = new SFSObject();
                     obj.putUtfString("appName", "JAVA");

                     sfs.send(new LoginRequest(superBotId,
                           getSHAofPass(botAccountMap.get(superBotId).getPassword()), sfs.getCurrentZone(),
                           obj));
                  }
               }
            });
User avatar
Lapo
Site Admin
Posts: 21723
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: TLS Encryption with JAVA Client

Postby Lapo » 12 Oct 2020, 10:12

Hi,
in your code you're not managing the failure of the CRYPTO_INIT event, so that's likely the cause you're not seeing any error on client side.
You can extract the errorMsg property from the event to get more details.

Code: Select all

boolean success = (Boolean) evt.getArguments().get("success");

if (success)
{
   ...
}

else
{
   System.out.println("Error:" + evt.getArguments().get("errorMsg"));
}

Also you didn't include the external XML used for connection so I'd recommend double checking you're using the right host/port configuration.

Alternatively use the ConfigData object to pass setup the connection parameters in your code, as show in the Java example here:
http://docs2x.smartfoxserver.com/Gettin ... yptography

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
DeepakSharma
Posts: 4
Joined: 09 Oct 2020, 10:48

Re: TLS Encryption with JAVA Client

Postby DeepakSharma » 12 Oct 2020, 12:51

Hi Lapo,
Good Evening.
I shared in external config file in my first post.
Reposting it here.


<SmartFoxConfig>
<!-- Mandatory Settings -->
<ip>gatewayskyline.adda52store.com</ip>
<udpIp>gatewayskyline.adda52store.com</udpIp>
<port>9933</port>
<udpPort>9933</udpPort>
<zone>PokerKing</zone>
<!-- End Mandatory Settings -->

<debug>true</debug>

<!-- For generic http port communication -->
<httpPort>8081</httpPort>
<httpsPort>8893</httpsPort>
<!-- BlueBox settings -->
<useBlueBox>true</useBlueBox>
<blueBoxPollingRate>700</blueBoxPollingRate>
</SmartFoxConfig>



I handled the failure of the CRYPTO_INIT event as suggested by you but the fact is that I am not receiving this event at all.
Also after invoking initCrypto() method, next console line does not print (Hello2 does not print on console).
i.e.
System.out.println("Hello1");
try{
sfs.initCrypto();
System.out.println("Hello2");
}
catch(Exception e){
System.out.println("some error occurred "+ e);
}
User avatar
Lapo
Site Admin
Posts: 21723
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: TLS Encryption with JAVA Client

Postby Lapo » 12 Oct 2020, 14:27

Try this code instead:

Code: Select all

import sfs2x.client.SmartFox;
import sfs2x.client.core.BaseEvent;
import sfs2x.client.core.SFSEvent;
import sfs2x.client.requests.LoginRequest;
import sfs2x.client.util.ConfigData;

public class SFS2XConnector
{

    private SmartFox sfs;
    private ConfigData cfg;
    private boolean useCrypto = true;

    class MyHandler
    {
        public void testHandler (BaseEvent evt)
        {
            System.out.println("Okay we're connected. Cool");
        }
    }

    public SFS2XConnector()
    {
        init();
        MyHandler myh = new MyHandler();

        System.out.println("SFS2X API Version: " + sfs.getVersion());

        sfs.addEventListener(SFSEvent.CONNECTION, this::onConnection);
        sfs.addEventListener(SFSEvent.CONNECTION_LOST, this::onConnectionLost);
        sfs.addEventListener(SFSEvent.CRYPTO_INIT, this::onCrypto);
        sfs.addEventListener(SFSEvent.LOGIN, this::onLogin);
        sfs.addEventListener(SFSEvent.LOGIN_ERROR, this::onLoginError);
        sfs.addEventListener(SFSEvent.CONNECTION_LOST, (event) -> { System.out.println("We lost connection" ); });
        sfs.addEventListener(SFSEvent.UDP_INIT, this::onUdpInit);
        sfs.connect(cfg);
    }


    private void onConnection(BaseEvent evt)
    {
        Boolean success = (Boolean) evt.getArguments().get("success");

        if (success)
        {
            System.out.println("Connection event");
            if (useCrypto)
                sfs.initCrypto();
            else
                sendLogin();
        }

        else
            System.out.println("Connection failed!");
    }

    private void sendLogin()
    {
        sfs.send(new LoginRequest("", "", cfg.getZone()));
    }

    private void onConnectionLost(BaseEvent evt)
    {
        System.out.println("Connection was closed.");
    }

    private void onCrypto(BaseEvent evt)
    {
        boolean success = (Boolean) evt.getArguments().get("success");
        if (success)
            sendLogin();
        else
            System.out.println("Crypto Init Failed!");
    }

    private void onLogin(BaseEvent evt)
    {
        System.out.println("Logged in as: " + sfs.getMySelf().getName());
        //sfs.initUdp();
    }

    private void onLoginError(BaseEvent evt)
    {
        String errorMsg = (String) evt.getArguments().get("errorMessage");

        System.out.println("Login Error: " + errorMsg);
    }

    private void onUdpInit(BaseEvent evt)
    {
        Boolean success = (Boolean) evt.getArguments().get("success");
        if (success)
            System.out.println("UDP Success");
        else
            System.out.println("UDP Failed");
    }

    // ------------------------------------------------

    private void init()
    {
        sfs = new SmartFox();

        cfg = new ConfigData();

        cfg.setHost("gatewayskyline.adda52store.com");
        cfg.setPort(9933);
        cfg.setHttpPort(8081);
        cfg.setHttpsPort(8893);
        cfg.setUdpHost("gatewayskyline.adda52store.com");
        cfg.setUdpPort(9933);
        cfg.setUseBBox(true);
        cfg.setDebug(true);
        cfg.setZone("BasicExamples");
    }

    // -----------------------------------------------

    public static void main(String[] args)
    {
        new SFS2XConnector();
    }
}

This comes from our basic connector example (also found in the Java/Android example package) and I have no problems running it with your configuration. It just fails at login time because it is trying the default "BasicExamples" zone which is not available on your server.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Aldenea, DavinVicy, DerikFent, JerodSa, Kevengar and 22 guests