Page 1 of 1

SSL error on Client

Posted: 22 Jun 2024, 10:45
by heroumi
Hello SFS Team,

I have used SFS Overcast successfully with SSL.
Currently for some reasons I am moving to a personal server. However, getting an error with SSL. I installed SFS2x on Google cloud Linux, added SSL and opened port for 8443. I was able to access the admin page domain.com/8443:admin. but cannot connect from SFS Unity Client.

Below is my configuration, please take a look and can you give me some guidance? Thank you,

Code: Select all

[Tooltip("IP address or domain name of the SmartFoxServer instance")]
   private string host = "domain.com";

   [Tooltip("TCP listening port of the SmartFoxServer instance, used for TCP socket connection in all builds except WebGL")]
   private int tcpPort = 8443;
   
   [Tooltip("Name of the SmartFoxServer Zone to join")]
   private string zone = "BasicExamples";

   [Tooltip("Display SmartFoxServer client debug messages")]
   private bool debug = false;

   //----------------------------------------------------------
   // UI elements
   //----------------------------------------------------------

   public InputField nameInput;
   public Button loginButton;
   public Text errorText;

   //----------------------------------------------------------
   // Private properties
   //----------------------------------------------------------

   private SmartFox sfs;

   //----------------------------------------------------------
   // Unity calback methods
   //----------------------------------------------------------

   private void Start()
   {
      // Focus on username input
      nameInput.Select();
      nameInput.ActivateInputField();

      // Reset buddy chat history
      BuddyChatHistoryManager.Init();

      // Show connection lost message, in case the disconnection occurred in another scene
      string connLostMsg = gm.ConnectionLostMessage;
      if (connLostMsg != null)
         errorText.text = connLostMsg;
   }

   //----------------------------------------------------------
   // UI event listeners
   //----------------------------------------------------------
   #region
   /**
    * On username input edit end, if the Enter key was pressed, connect to SmartFoxServer.
    */
   public void OnNameInputEndEdit()
   {
      if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
         Connect();
   }

   /**
    * On Login button click, connect to SmartFoxServer.
    */
   public void OnLoginButtonClick()
   {
      Connect();
   }
   #endregion

   //----------------------------------------------------------
   // Helper methods
   //----------------------------------------------------------
   #region
   /**
    * Enable/disable username input interaction.
    */
   private void EnableUI(bool enable)
   {
      nameInput.interactable = enable;
      loginButton.interactable = enable;
   }

   /**
    * Connect to SmartFoxServer.
    */
   private void Connect()
   {
      // Disable user interface
      EnableUI(false);

      // Clear any previour error message
      errorText.text = "";

      // Set connection parameters
      ConfigData cfg = new ConfigData();
      cfg.Host = host;
      cfg.Port = tcpPort;
      cfg.Zone = zone;
      cfg.Debug = debug;

#if UNITY_WEBGL
      cfg.Port = httpPort;
#endif

      // Initialize SmartFox client
      // The singleton class GlobalManager holds a reference to the SmartFox class instance,
      // so that it can be shared among all the scenes
#if !UNITY_WEBGL
      sfs = gm.CreateSfsClient();
#else
      sfs = gm.CreateSfsClient(UseWebSocket.WS_BIN);
#endif

      // Configure SmartFox internal logger
      sfs.Logger.EnableConsoleTrace = debug;

      // Add event listeners
      AddSmartFoxListeners();

      // Connect to SmartFoxServer
      sfs.Connect(cfg);
   }
   
   private void OnConnection(BaseEvent evt)
   {
      // Check if the conenction was established or not
      if ((bool)evt.Params["success"])
      {
         Debug.Log("SFS2X API version: " + sfs.Version);
         Debug.Log("Connection mode is: " + sfs.ConnectionMode);
         
         sfs.InitCrypto();

         // Login
         //sfs.Send(new LoginRequest(nameInput.text));
      }
      else
      {
         // Show error message
         errorText.text = "Connection failed; is the server running at all?";

         // Enable user interface
         EnableUI(true);
      }
   }
   
   private void OnCryptoInit(BaseEvent evt)
   {
      Debug.Log("Crypto initialized: " + evt.Params["success"]);
      if ((bool) evt.Params["success"])
      {
         // Login
         sfs.Send(new LoginRequest(nameInput.text));
      }
   }

   /**
    * Add all SmartFoxServer-related event listeners required by the scene.
    */
   private void AddSmartFoxListeners()
   {
      sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
      sfs.AddEventListener(SFSEvent.CRYPTO_INIT, OnCryptoInit);
      sfs.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
      sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
      sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
   }

Re: SSL error on Client

Posted: 22 Jun 2024, 15:29
by Lapo
Hi,
your client supports both socket and websockets connections. Are both failing?
If so can you provide the exact error you're getting and specify which connection type you're testing with?

Also, when using websockets the InitCrypto step should be skipped entirely as it's not needed.

Cheers

Re: SSL error on Client

Posted: 22 Jun 2024, 15:46
by heroumi

Code: Select all

Connection to SmartFoxServer lost; reason is: unknown



Hello, I got this error message.
Above code I took from SFS2x Demo matching on assetstore. and add encryption: InitCrypto.

Re: SSL error on Client

Posted: 22 Jun 2024, 15:48
by heroumi
In addition, I use TCP sockets to connect. Is the control panel configured to open port 8443? Even though I tried opening it, I couldn't connect from the client. going to the web console with SSL still succeeds.

Code: Select all

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Requests;
using Sfs2X.Util;

/**
 * Script attached to the Controller object in the Login scene.
 */
public class LoginSceneController : BaseSceneController
{
   //----------------------------------------------------------
   // Editor public properties
   //----------------------------------------------------------

   [Tooltip("IP address or domain name of the SmartFoxServer instance")]
   private string host = "domain.com";

   [Tooltip("TCP listening port of the SmartFoxServer instance, used for TCP socket connection in all builds except WebGL")]
   private int tcpPort = 8443;
   
   [Tooltip("Name of the SmartFoxServer Zone to join")]
   private string zone = "BasicExamples";

   [Tooltip("Display SmartFoxServer client debug messages")]
   private bool debug = false;

   //----------------------------------------------------------
   // UI elements
   //----------------------------------------------------------

   public InputField nameInput;
   public Button loginButton;
   public Text errorText;

   //----------------------------------------------------------
   // Private properties
   //----------------------------------------------------------

   private SmartFox sfs;

   //----------------------------------------------------------
   // Unity calback methods
   //----------------------------------------------------------

   private void Start()
   {
      // Focus on username input
      nameInput.Select();
      nameInput.ActivateInputField();

      // Reset buddy chat history
      BuddyChatHistoryManager.Init();

      // Show connection lost message, in case the disconnection occurred in another scene
      string connLostMsg = gm.ConnectionLostMessage;
      if (connLostMsg != null)
         errorText.text = connLostMsg;
   }

   //----------------------------------------------------------
   // UI event listeners
   //----------------------------------------------------------
   #region
   /**
    * On username input edit end, if the Enter key was pressed, connect to SmartFoxServer.
    */
   public void OnNameInputEndEdit()
   {
      if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
         Connect();
   }

   /**
    * On Login button click, connect to SmartFoxServer.
    */
   public void OnLoginButtonClick()
   {
      Connect();
   }
   #endregion

   //----------------------------------------------------------
   // Helper methods
   //----------------------------------------------------------
   #region
   /**
    * Enable/disable username input interaction.
    */
   private void EnableUI(bool enable)
   {
      nameInput.interactable = enable;
      loginButton.interactable = enable;
   }

   /**
    * Connect to SmartFoxServer.
    */
   private void Connect()
   {
      // Disable user interface
      EnableUI(false);

      // Clear any previour error message
      errorText.text = "";

      // Set connection parameters
      ConfigData cfg = new ConfigData();
      cfg.Host = host;
      cfg.Port = tcpPort;
      cfg.Zone = zone;
      cfg.Debug = debug;

#if UNITY_WEBGL
      cfg.Port = httpPort;
#endif

      // Initialize SmartFox client
      // The singleton class GlobalManager holds a reference to the SmartFox class instance,
      // so that it can be shared among all the scenes
#if !UNITY_WEBGL
      sfs = gm.CreateSfsClient();
#else
      sfs = gm.CreateSfsClient(UseWebSocket.WS_BIN);
#endif

      // Configure SmartFox internal logger
      sfs.Logger.EnableConsoleTrace = debug;

      // Add event listeners
      AddSmartFoxListeners();

      // Connect to SmartFoxServer
      sfs.Connect(cfg);
   }
   
   private void OnConnection(BaseEvent evt)
   {
      // Check if the conenction was established or not
      if ((bool)evt.Params["success"])
      {
         Debug.Log("SFS2X API version: " + sfs.Version);
         Debug.Log("Connection mode is: " + sfs.ConnectionMode);
         
         sfs.InitCrypto();

         // Login
         //sfs.Send(new LoginRequest(nameInput.text));
      }
      else
      {
         // Show error message
         errorText.text = "Connection failed; is the server running at all?";

         // Enable user interface
         EnableUI(true);
      }
   }
   
   private void OnCryptoInit(BaseEvent evt)
   {
      Debug.Log("Crypto initialized: " + evt.Params["success"]);
      if ((bool) evt.Params["success"])
      {
         // Login
         sfs.Send(new LoginRequest(nameInput.text));
      }
   }

   /**
    * Add all SmartFoxServer-related event listeners required by the scene.
    */
   private void AddSmartFoxListeners()
   {
      sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
      sfs.AddEventListener(SFSEvent.CRYPTO_INIT, OnCryptoInit);
      sfs.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
      sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
      sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
   }

   /**
    * Remove all SmartFoxServer-related event listeners added by the scene.
    * This method is called by the parent BaseSceneController.OnDestroy method when the scene is destroyed.
    */
   override protected void RemoveSmartFoxListeners()
   {
      // NOTE
      // If this scene is stopped before a connection is established, the SmartFox client instance
        // could still be null, causing an error when trying to remove its listeners

      if (sfs != null)
      {
         sfs.RemoveEventListener(SFSEvent.CONNECTION, OnConnection);
         sfs.RemoveEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
         sfs.RemoveEventListener(SFSEvent.LOGIN, OnLogin);
         sfs.RemoveEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
      }
   }

   /**
    * Hide all modal panels.
    */
   override protected void HideModals()
   {
      // No modals used by this scene
   }
   #endregion

   //----------------------------------------------------------
   // SmartFoxServer event listeners
   //----------------------------------------------------------
   #region

   private void OnConnectionLost(BaseEvent evt)
   {
      // Remove SFS listeners
      RemoveSmartFoxListeners();

      // Show error message
      string reason = (string)evt.Params["reason"];
      
      if (reason != ClientDisconnectionReason.MANUAL)
         errorText.text = "Connection lost; reason is: " + reason;

      // Enable user interface
      EnableUI(true);
   }

   private void OnLogin(BaseEvent evt)
   {
      // Load lobby scene
      SceneManager.LoadScene("Lobby");
   }

   private void OnLoginError(BaseEvent evt)
   {
      // Disconnect
      // NOTE: this causes a CONNECTION_LOST event with reason "manual", which in turn removes all SFS listeners
      sfs.Disconnect();

      // Show error message
      errorText.text = "Login failed due to the following error:\n" + (string)evt.Params["errorMessage"];

      // Enable user interface
      EnableUI(true);
   }
   #endregion
}


Re: SSL error on Client

Posted: 22 Jun 2024, 15:51
by heroumi
Just finished, I tried connecting using: private int tcpPort = 9933; and execute "InitCrypto". Successful connection result. So is this an SSL connection made or is it not using SSL? I really wonder. if you use port 8443, you will not be able to connect.

Re: SSL error on Client

Posted: 23 Jun 2024, 14:58
by Lapo
Is the control panel configured to open port 8443? Even though I tried opening it, I couldn't connect from the client. going to the web console with SSL still succeeds.

Port 8443 is open by default (and it looks active from the screenshots posted here)
You can point your browser to https://<your-server-address>:8443 and you should see the usual welcome page with the link to the AdminTool. If you don't have a valid SSL certificate deployed you will see a browser warning saying that it could be "dangerous" to proceed.

Does this work, at least?

Thanks

Re: SSL error on Client

Posted: 24 Jun 2024, 02:18
by heroumi
Hi, I can access admin with ssl for port 8443. Only problem with unity client.
- If I choose InitCrypto and use port 9933 tcp the connection is successful.
- If not InitCrypto and using port 9933 will fail.
- If use port 8443 and InitCrypto it will fail

Re: SSL error on Client

Posted: 24 Jun 2024, 13:43
by Lapo
heroumi wrote:Hi, I can access admin with ssl for port 8443. Only problem with unity client.
- If I choose InitCrypto and use port 9933 tcp the connection is successful.
- If not InitCrypto and using port 9933 will fail.
- If use port 8443 and InitCrypto it will fail


This is correct. SmartFoxServer communicates on TCP port 9933. You don''t need to change port in order to use SSL

- If not InitCrypto and using port 9933 will fail.

Correct. Your Zone is configured to accept encrypted connections only, so non-encrypted clients won't be able to access the Zone.

- If use port 8443 and InitCrypto it will fail

Correct. Port 9933 is the used for TCP connections and communication.
Port 8443 is only needed to initialize SSL cryptography.

Cheers

Re: SSL error on Client

Posted: 24 Jun 2024, 15:50
by heroumi
Lapo wrote:
heroumi wrote:Hi, I can access admin with ssl for port 8443. Only problem with unity client.
- If I choose InitCrypto and use port 9933 tcp the connection is successful.
- If not InitCrypto and using port 9933 will fail.
- If use port 8443 and InitCrypto it will fail


This is correct. SmartFoxServer communicates on TCP port 9933. You don''t need to change port in order to use SSL

- If not InitCrypto and using port 9933 will fail.

Correct. Your Zone is configured to accept encrypted connections only, so non-encrypted clients won't be able to access the Zone.

- If use port 8443 and InitCrypto it will fail

Correct. Port 9933 is the used for TCP connections and communication.
Port 8443 is only needed to initialize SSL cryptography.

Cheers



Thanks for your information. Very helpful.
I would like to ask, Will the upcoming update of Smartfox still take place on SFS2X or will there be a new and better version?