Connexion in restricted secured networks (unity web app)

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

wulei
Posts: 19
Joined: 19 Aug 2013, 17:42

Connexion in restricted secured networks (unity web app)

Postby wulei » 03 Feb 2014, 14:24

Hello everyone
I really require help or alternative about connecting a server from a Unity Web App in a restricted network

My situation
I work with Unity3D & SmartFoxServer2X and i export my Unity project to Unity Web Player application.
Our client has a restricted network.
In a NOT restricted network all the following solutions mentionned below works.

Attempt #1
First in the Unity app i use the normal standard socket of SFS2X in order to reach our server
Observation : Using the Unity Web App in this restricted network, there is no answer from the server so the connection seems to be impossible

Attempt #2
So i worked on the activation of the Bluebox solution in Unity3D in order to reach our server
Observation : Using the Unity Web App in this restricted network, it seems there is no answer from the server so the connection can't be established but the connection with bluebox freezes the Unity web app for a while (some seconds)

Attempt #3
So i made in unity a button to ping the server, but this failed to ping the server in the restricted network

Attempt #4
Then i made an html5/javascript connexion with SFS2X API. The idea was to allow unity web app to communicate with the server via an html5/javascript app in order to not be filtered.
Observation : Combining the Unity Web App (for 3D and GUI) and the Html5/ javascript app (for connexion) in this restricted network, a response from the server is received but the connection always failed with the error : is the server running at all ?

Attempt #5
Last try, I installed the flash example BattleFarm in our server. This flash example uses bluebox and normal socket. The client with a restricted network can connect after seconds and play with.

Conclusion
So i can conclude the following points :
- SFS2X is not the problem
- Our server seems to be well configured
- Perharps i improperly used the SFS2X API in Unity ?
- Perharps Unity apps are not authorized to connect out from the restricted network ?
- Perharps Flash apps are not filtered because frequently used in the web ?

Questions
Is someone have the same difficulties ?
Where are the wrong parts ?
Is someone have an example of unity web app using Smartfoxserver2X in Bluebox mode ?

Programs

My Unity Code + SFS2X API

public class TestConnexion : MonoBehaviour
{
#region Variables
//Config server
private SmartFox _smartFoxServer = null;
private string _IPServer = DataCenter.IPServer;
private int _IPPort = DataCenter.IPPort;
private string _Zone = DataCenter.Zone_Initial;
private string _Room = DataCenter.Room_Initial;
private string _scene_de_connexion = DataCenter.Connection_scene;
private string _sceneName = DataCenter.Scene_Principale;
//Server config && GUI display
private bool _allowConnection = true;
private bool _forceBlueBox = false;
private Ping p = null;
private string test1, test2 = string.Empty;
private string server_message_about_connexion = string.Empty;
private string server_message_about_login = string.Empty;
#endregion

#region Unity Calls
// Update is called once per frame
void FixedUpdate() { if (_smartFoxServer != null) { if (_smartFoxServer.IsConnected) { _smartFoxServer.ProcessEvents(); } } }

#region GUI
// Update is called once per frame
void OnGUI()
{
//window use
GUI.Window(0,
new Rect(
win_left, //Left
win_top, //Top
win_width, //Width
win_height), //Height
WindowConnection, //Function to call for GUI Display
"<size=25>" + "Connexion" + "</size>"); //Window's Title
}

void WindowConnection(int windowID)
{
scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, false);
{
GUILayout.Space(window_padding * 2);//Space
#region Set of tests about connectivity
if (GUILayout.Button("Test 1", GUILayout.Width(100))) { test1 = "Test 1 sock" + Security.PrefetchSocketPolicy(_IPServer, _IPPort); }
GUILayout.Label(test1);
GUILayout.Space(window_padding);//Space

if (GUILayout.Button("Test 2", GUILayout.Width(100))) { test2 = "Test 2 http" + Security.PrefetchSocketPolicy(_IPServer, DataCenter.HttpIPPort); }
GUILayout.Label(test2);
GUILayout.Space(window_padding);//Space

if (GUILayout.Button("Test 3", GUILayout.Width(100))) { StartCoroutine("Ping", _IPServer); }
if (p != null) GUILayout.Label("Test 3 ping" + p.ip + " " + p.isDone + " " + p.time);
GUILayout.Space(window_padding);//Space

GUILayout.Space(window_padding);//Space

_forceBlueBox = GUILayout.Toggle(_forceBlueBox, "Forcer la connection ?");
GUILayout.Space(window_padding);//Space
#endregion

#region Boutons Connexion

GUILayout.BeginHorizontal();
{
if (_allowConnection)
{
if (GUILayout.Button("<b>" + "Connecter 1" + "</b>", GUILayout.Width(100)))
{
InitiateConnectionToSFS2X_default();
}
}
}
GUILayout.EndHorizontal();
#endregion

GUILayout.Space(window_padding);//Space
GUILayout.Label(server_message_about_connexion);
GUILayout.Space(window_padding);//Space
GUILayout.Label(server_message_about_login);
}
GUILayout.EndScrollView();
}
#endregion
#endregion


#region About connection's phases
void InitiateConnectionToSFS2X_default()
{
#region Intance and Events
//Add to session SmartFox'Events
_smartFoxServer = SmartFoxInstance.Instance;

_smartFoxServer.AddEventListener(SFSEvent.CONNECTION, OnConnection);
_smartFoxServer.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
_smartFoxServer.AddEventListener(SFSEvent.LOGIN, OnLogin);
_smartFoxServer.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
_smartFoxServer.AddEventListener(SFSEvent.LOGOUT, OnLogout);
_smartFoxServer.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
#endregion

#region Connection To SFS2X Configuration

ConfigData cd = new ConfigData();
cd.Host = _IPServer;
cd.Port = _IPPort;
cd.Zone = _Zone;
//bluebox
cd.HttpPort = DataCenter.HttpIPPort;
cd.BlueBoxPollingRate = 750;
cd.UseBlueBox = _forceBlueBox;
_smartFoxServer.BitSwarm.ForceBlueBox(_forceBlueBox);

//Necessary for the web
if (Application.isWebPlayer || Application.isEditor)
{
if (_forceBlueBox)
Security.PrefetchSocketPolicy(_IPServer, DataCenter.HttpIPPort);
else
Security.PrefetchSocketPolicy(_IPServer, _IPPort);
}
#endregion

_smartFoxServer.Connect(cd);//Connect to your server instance
}

void EndConnectionPhase()
{
if (Application.loadedLevelName != _sceneName && Application.CanStreamedLevelBeLoaded(_sceneName))
{
Object.DontDestroyOnLoad(this);
Application.LoadLevel(_sceneName);
}
//Desactivate connection GUI
_allowConnection = false;
}

IEnumerator Ping(string val)
{
p = new Ping(val);
while (!p.isDone)
{
yield return new WaitForEndOfFrame();
}
}
#endregion

#region About Connection

//Alerts your console on the success of the connection
public void OnConnection(BaseEvent evt)
{
server_message_about_connexion = "Etat de la connexion ";
server_message_about_connexion += ((bool)evt.Params["success"]) ? "SUCCES" : "ECHEC";
server_message_about_connexion += ((bool)evt.Params["success"]) ? "" : " Erreur : <" + (string)evt.Params["errorMessage"] + ">)";

//Once connected (use event listeners), we want to Login to a SmartFoxServer Zone:
_smartFoxServer.Send(new LoginRequest("", "", _Zone));
}
//Will give you the reason why you were disconnected
void OnConnectionLost(BaseEvent evt)
{
server_message_about_connexion = "Connection lost" + " " + (string)evt.Params["Reason"];
OnLogout(null);
}
#endregion

#region About Login

//When you log in, this tells smart fox to initialize your buddy list
void OnLogin(BaseEvent evt)
{
try
{
if (evt.Params.Contains("success") && !(bool)evt.Params["success"])//if login phase failed ...
{
string loginErrorMessage = (string)evt.Params["errorMessage"];
server_message_about_login = "Login error : " + loginErrorMessage;
}
else
{
server_message_about_login = "Logged in successfully as " + ((User)evt.Params["user"]).Name;
//Once logged in to the zone (again, use event listeners), we can join a room:
_smartFoxServer.Send(new JoinRoomRequest(_Room));
}
}
catch (System.Exception ex)
{
Debug.Log("Exception handling login request: " + ex.Message + " " + ex.StackTrace);
}
}
#endregion

#region About Joining Space

//Runs when you join a room and notifies you in your chat log
void OnJoinRoom(BaseEvent evt)
{
Debug.Log("Room " + ((Room)evt.Params["room"]).Name + " joined successfully");
EndConnectionPhase();
}
#endregion
}

My Html5/Javascript code + SFS2X API

<script type="text/javascript">
<!--
var sfs = null;
function Connect( arg ){
var config = {};
config.host = "xx.xx.xx.xx";
config.port = 8888;
config.zone = "zintao";
config.debug = true;

// Create SmartFox client instance
sfs = new SmartFox(config);

// Add event listeners
sfs.addEventListener(SFS2X.SFSEvent.CONNECTION, onConnection, this);
sfs.addEventListener(SFS2X.SFSEvent.CONNECTION_LOST, onConnectionLost, this);
sfs.addEventListener(SFS2X.SFSEvent.LOGIN_ERROR, onLoginError, this);
sfs.addEventListener(SFS2X.SFSEvent.LOGIN, onLogin, this);
sfs.addEventListener(SFS2X.SFSEvent.LOGOUT, onLogout, this);
sfs.addEventListener(SFS2X.SFSEvent.ROOM_JOIN_ERROR, onRoomJoinError, this);
sfs.addEventListener(SFS2X.SFSEvent.ROOM_JOIN, onRoomJoin, this);

sfs.connect();
//trace("SmartFox API version: " + sfs.version, true);
}

//------------------------------------
// SFS EVENT HANDLERS
//------------------------------------


function onConnection(event)
{
if (event.success)
{
trace("Server Connected to SmartFoxServer 2X!", true);
sfs.send(new SFS2X.Requests.System.LoginRequest("GuestName"));
}
else
{
var error = "Connection failed: " + (event.errorMessage ? event.errorMessage + " (code " + event.errorCode + ")" : "Is the server running at all?");
trace(error, true);
}
}

function onConnectionLost(event)
{
// Show disconnection reason
if (event.reason != SFS2X.Utils.ClientDisconnectionReason.MANUAL && event.reason != SFS2X.Utils.ClientDisconnectionReason.UNKNOWN)
{
trace("You have been disconnected");
var error = "You have been disconnected; reason is: " + event.reason;
showError(error);
}
else
trace("You have been disconnected; reason is: " + event.reason, true);
}

function onLoginError(event)
{
// Show error
var error = "Login error: " + event.errorMessage + " (code " + event.errorCode + ")";
showError(error);
}

function onLogin(event)
{
trace("Login successful!" +
"\n\tZone: " + event.zone +
"\n\tUser: " + event.user +
"\n\tData: " + event.data, true);

// Join lobby room
sfs.send(new SFS2X.Requests.System.JoinRoomRequest("test_de_connexion"));
}

function onLogout(event)
{
trace("Logout from zone " + event.zone + " performed!");
}

function onRoomJoinError(event)
{
trace("Room join error: " + event.errorMessage + " (code: " + event.errorCode + ")", true);
}

function onRoomJoin(event)
{
trace("Room joined: " + event.room);
}

function showError(text)
{
trace(text);
$("#errorLb").html("<b>ATTENTION</b><br/>" + text);
$("#errorLb").toggle();
}
function trace(txt, showAlert)
{
console.log(txt);

if (showAlert)
alert(txt);
}
-->
</script>
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Connexion in restricted secured networks (unity web app)

Postby Lapo » 03 Feb 2014, 18:17

Hi,
can you explain in details what kind of restrictions there are in the network you are using? (Blocked ports? Proxies? ...)

Also when you say that the Flash game Battle Farm worked ok for you I think it probably worked using the HTTP / BlueBox connection. Can you confirm this?

What ports are you using for sockets and BlueBox on the server side?

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
wulei
Posts: 19
Joined: 19 Aug 2013, 17:42

Re: Connexion in restricted secured networks (unity web app)

Postby wulei » 04 Feb 2014, 11:28

Hi Lapo
Information
For the moment I am trying to get in touch with people in charge of the restricted network. As soon as i got related information i will inform you.
Because i did my own html5 test, I will try to set up the "html5 examples" on our server and do tests in restricted network and inform you as well.

Answers
Otherwise i got answers to other questions this morning :
Also when you say that the Flash game Battle Farm worked ok for you I think it probably worked using the HTTP / BlueBox connection. Can you confirm this?

Yes, the Flash game Battle Farm worked only using the HTTP / BlueBox connection.
What ports are you using for sockets and BlueBox on the server side?

Listening Sockets:
{ 127.0.0.1:9934, (Tcp) }
{ 127.0.0.1:9934, (Udp) }
{ 91.121.76.190:9934, (Tcp) }
{ 91.121.76.190:9934, (Udp) }
{ 91.121.76.190:9933, (Tcp) }
{ 91.121.76.190:9933, (Udp) }
{ 91.121.76.190:8888 (WebSocket) }
Hoping this can help.

Request
Do you have a unity web application using the Http/Bluebox connection ?

Question
Did you see something wrong in my code ?

Thank you very much Lapo for your assistance !
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Connexion in restricted secured networks (unity web app)

Postby Lapo » 04 Feb 2014, 11:50

Request
Do you have a unity web application using the Http/Bluebox connection ?

All examples work with the BlueBox. To force the connection via http just make sure to point the client to a wrong TCP port, e.g. 9934 instead of 9933. The socket connection will fail and the BlueBox will be used instead.

NOTE: Make sure the useBlueBox flag is turned on in the ConfigData class.

Question
Did you see something wrong in my code ?

It looks good.
Lapo

--

gotoAndPlay()

...addicted to flash games
wulei
Posts: 19
Joined: 19 Aug 2013, 17:42

Re: Connexion in restricted secured networks (unity web app)

Postby wulei » 04 Feb 2014, 15:13

Hello Lapo
Just a new element about my problem
I installed on my server the html5 examples (Tris)
:arrow: In a non restricted network it's ok, it's works. :)

:arrow: But my client which have a restricted network got a "Failed Connection : is Server running at all ?". :twisted:
This means he got a server answer but can not established a connexion.

If you have any idea...

I am still trying to get in touch with people in charge of the security of the network. I will let you know as soon i got further information.
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Connexion in restricted secured networks (unity web app)

Postby Lapo » 04 Feb 2014, 15:17

Something as simple as a firewall can cause that :)
But without the exact details it's difficult to understand. For websockets you may try to use a port like 80 or 443 in order to avoid the firewall. Typically those ports aren't filtered.

This means he got a server answer but can not established a connexion.

No, it's more likely there wasn't any answer or connections, simply the other side does not respond at all.
Lapo

--

gotoAndPlay()

...addicted to flash games
wulei
Posts: 19
Joined: 19 Aug 2013, 17:42

Re: Connexion in restricted secured networks (unity web app)

Postby wulei » 04 Feb 2014, 16:41

Hi Lapo

Attempts#1
:arrow: Html 5 example (Tris) works well with 443 port even in restricted network (That's good)
:arrow: But not with port 80 because Apache is installed on the server and when i type the ip of the server i get "It works ! ... [etc.]"
I think the 80 port is already used by Apache and perharps that's why the connexion failed with 80 port.

Attempts#2
In unity I changed the socket port to 443 on client and server side :
:arrow: works well in non restricted network
:arrow: Failed in restricted network

Attempts#3
In unity I changed the http / bluebox port to 443 on client side :
:arrow: Failed in non restricted network

Question
:?: So if i want SFS2X listening the 80 port, must i change the listening port of Apache and set the listening port of SFS2X to 80 ?
:?: Is there a way to configure the server SFS2X in order to set bluebox port to 443 ?
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Connexion in restricted secured networks (unity web app)

Postby Lapo » 04 Feb 2014, 17:18

wulei wrote:Hi Lapo

Attempts#1
:arrow: Html 5 example (Tris) works well with 443 port even in restricted network (That's good)
:arrow: But not with port 80 because Apache is installed on the server and when i type the ip of the server i get "It works ! ... [etc.]"

Correct, only one service can use a specific port. So Apache wins, if it's already running.

Attempts#2
In unity I changed the socket port to 443 on client and server side :
:arrow: works well in non restricted network
:arrow: Failed in restricted network

Attempts#3
In unity I changed the http / bluebox port to 443 on client side :
:arrow: Failed in non restricted network

Problem is Unity also looks for a crossdomain policy file for security reasons, unless you are running an executable (not webplayer)
I would have expected port 443 via socket to work. It should be the same for any socket connection regardless of the client type.

:?: So if i want SFS2X listening the 80 port, must i change the listening port of Apache and set the listening port of SFS2X to 80 ?

Yes. If you have two network cards you can assign each service to each NIC and run both on port 80

:?: Is there a way to configure the server SFS2X in order to set bluebox port to 443 ?

Yes, you will need to edit the config/jetty/jetty.xml file and change from 8080 to 443
Lapo

--

gotoAndPlay()

...addicted to flash games
wulei
Posts: 19
Joined: 19 Aug 2013, 17:42

Re: Connexion in restricted secured networks (unity web app)

Postby wulei » 06 Feb 2014, 11:25

Hello Lapo, and thank you
I changed the listening port of apache from 80 to another

In SFS2X
Server configurator : General
I added in my sockect addresses under Server configurator the following line "[server ip adress] : 443 : TCP"
Thus i can connect with unity in SFS2X connection mode via the port 443 (HTTPS)
Remote admin
My administration TCP port is still 9933
Html5 websockets
Html5 websockets listener adress is [server ip adress] which is different from 127.0.0.1
Html5 websockets listener port is 443
Unity bluebox connection in 80 port
To set SF2X on the listening port 80
I changed in "SFS2X/config/jetty/jetty.xml" the port "8080" to "80"
Thus like this, unity bluebox connection mode works via the port 80 (HTTP)

News
All connections with Unity works in a non restricted network for 443 and 80
I will try in a restricted network.
No news about security for moment.
Keep you informed

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 23 guests