2025-01-12 00:43:01 -08:00
/* Copyright (C) 2025 Wildfire Games.
2023-12-02 16:30:12 -08:00
* This file is part of 0 A . D .
2017-09-11 17:29:10 -07:00
*
2023-12-02 16:30:12 -08:00
* 0 A . D . is free software : you can redistribute it and / or modify
2017-09-11 17:29:10 -07:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 2 of the License , or
* ( at your option ) any later version .
*
2023-12-02 16:30:12 -08:00
* 0 A . D . is distributed in the hope that it will be useful ,
2017-09-11 17:29:10 -07:00
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2023-12-02 16:30:12 -08:00
* along with 0 A . D . If not , see < http : //www.gnu.org/licenses/>.
2017-09-11 17:29:10 -07:00
*/
# include "precompiled.h"
2018-04-27 09:48:44 -07:00
# include "JSInterface_Network.h"
2017-09-11 17:29:10 -07:00
# include "lib/external_libraries/enet.h"
# include "lib/external_libraries/libsdl.h"
2018-08-25 07:34:30 -07:00
# include "lib/types.h"
2017-09-11 17:29:10 -07:00
# include "lobby/IXmppClient.h"
# include "network/NetClient.h"
# include "network/NetMessage.h"
# include "network/NetServer.h"
# include "network/StunClient.h"
# include "ps/CLogger.h"
2021-05-18 07:47:36 -07:00
# include "ps/CStr.h"
2017-09-11 17:29:10 -07:00
# include "ps/Game.h"
2021-02-27 09:44:59 -08:00
# include "ps/GUID.h"
2021-05-18 07:47:36 -07:00
# include "ps/Hashing.h"
# include "ps/Pyrogenesis.h"
2024-08-31 01:54:56 -07:00
# include "ps/SavedGame.h"
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 10:31:39 -08:00
# include "ps/Util.h"
2021-03-02 12:01:14 -08:00
# include "scriptinterface/FunctionWrapper.h"
2021-05-10 04:51:32 -07:00
# include "scriptinterface/StructuredClone.h"
2021-05-14 03:18:03 -07:00
# include "scriptinterface/JSON.h"
2017-09-11 17:29:10 -07:00
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 10:31:39 -08:00
# include "third_party/encryption/pkcs5_pbkdf2.h"
2024-11-30 02:18:24 -08:00
# include <optional>
2021-03-02 12:01:14 -08:00
namespace JSI_Network
{
u16 GetDefaultPort ( )
2017-09-11 17:29:10 -07:00
{
return PS_DEFAULT_PORT ;
}
2021-03-02 12:01:14 -08:00
bool IsNetController ( )
2021-02-27 09:44:59 -08:00
{
return ! ! g_NetClient & & g_NetClient - > IsController ( ) ;
}
2021-03-02 12:01:14 -08:00
bool HasNetServer ( )
2018-04-04 05:43:25 -07:00
{
2020-11-26 14:28:50 -08:00
return ! ! g_NetServer ;
2018-04-04 05:43:25 -07:00
}
2021-03-02 12:01:14 -08:00
bool HasNetClient ( )
2018-04-04 05:20:34 -07:00
{
2020-11-26 14:28:50 -08:00
return ! ! g_NetClient ;
2018-04-04 05:20:34 -07:00
}
2025-01-12 00:43:01 -08:00
void StartNetworkHost ( const ScriptRequest & rq , const CStrW & playerName , const u16 serverPort ,
2024-08-31 01:54:56 -07:00
const CStr & password , const bool continueSavedGame , bool storeReplay )
2017-09-11 17:29:10 -07:00
{
ENSURE ( ! g_NetClient ) ;
ENSURE ( ! g_NetServer ) ;
ENSURE ( ! g_Game ) ;
2018-08-25 07:34:30 -07:00
// Always use lobby authentication for lobby matches to prevent impersonation and smurfing, in particular through mods that implemented an UI for arbitrary or other players nicknames.
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 10:31:39 -08:00
bool hasLobby = ! ! g_XmppClient ;
2024-08-31 01:54:56 -07:00
g_NetServer = new CNetServer ( continueSavedGame , hasLobby ) ;
2021-05-17 08:14:10 -07:00
if ( ! g_NetServer - > SetupConnection ( serverPort ) )
{
ScriptException : : Raise ( rq , " Failed to start server " ) ;
SAFE_DELETE ( g_NetServer ) ;
return ;
}
// In lobby, we send our public ip and port on request to the players who want to connect.
2025-01-12 00:43:01 -08:00
// Thus we need to know our public IP and use STUN to get it.
if ( hasLobby & & ! g_NetServer - > SetConnectionData ( ) )
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 10:31:39 -08:00
{
2025-01-12 00:43:01 -08:00
ScriptException : : Raise ( rq , " Failed to resolve public IP-address. " ) ;
SAFE_DELETE ( g_NetServer ) ;
return ;
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 10:31:39 -08:00
}
2021-02-27 09:44:59 -08:00
// Generate a secret to identify the host client.
std : : string secret = ps_generate_guid ( ) ;
g_NetServer - > SetControllerSecret ( secret ) ;
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 10:31:39 -08:00
2022-03-06 13:12:16 -08:00
g_Game = new CGame ( storeReplay ) ;
2021-02-27 09:44:59 -08:00
g_NetClient = new CNetClient ( g_Game ) ;
2017-09-11 17:29:10 -07:00
g_NetClient - > SetUserName ( playerName ) ;
2021-05-18 07:47:36 -07:00
2021-05-09 05:51:32 -07:00
if ( hasLobby )
2021-05-18 07:47:36 -07:00
{
CStr hostJID = g_XmppClient - > GetJID ( ) ;
/**
2023-12-02 16:30:12 -08:00
* Password security - we want 0 A . D . to protect players from malicious hosts . We assume that clients
2021-05-18 07:47:36 -07:00
* might mistakenly send a personal password instead of the game password ( e . g . enter their mail account ' s password on autopilot ) .
* Malicious dedicated servers might be set up to farm these failed logins and possibly obtain user credentials .
* Therefore , we hash the passwords on the client side before sending them to the server .
* This still makes the passwords potentially recoverable , but makes it much harder at scale .
* To prevent the creation of rainbow tables , hash with :
* - the host name
* - the client name ( this makes rainbow tables completely unworkable unless a specific user is targeted ,
* but that would require both computing the matching rainbow table _and_ for that specific user to mistype a personal password ,
* at which point we assume the attacker would / could probably just rather use another means of obtaining the password ) .
* - the password itself
* - the engine version ( so that the hashes change periodically )
* TODO : it should be possible to implement SRP or something along those lines to completely protect from this ,
* but the cost / benefit ratio is probably not worth it .
*/
CStr hashedPass = HashCryptographically ( password , hostJID + password + engine_version ) ;
g_NetServer - > SetPassword ( hashedPass ) ;
g_NetClient - > SetHostJID ( hostJID ) ;
g_NetClient - > SetGamePassword ( hashedPass ) ;
}
2025-01-12 00:43:01 -08:00
g_NetClient - > SetupServerData ( " 127.0.0.1 " , serverPort ) ;
2021-02-27 09:44:59 -08:00
g_NetClient - > SetControllerSecret ( secret ) ;
2017-09-11 17:29:10 -07:00
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 10:31:39 -08:00
if ( ! g_NetClient - > SetupConnection ( nullptr ) )
2017-09-11 17:29:10 -07:00
{
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
ScriptException : : Raise ( rq , " Failed to connect to server " ) ;
2017-09-11 17:29:10 -07:00
SAFE_DELETE ( g_NetClient ) ;
SAFE_DELETE ( g_Game ) ;
}
}
2022-03-06 13:12:16 -08:00
void StartNetworkJoin ( const ScriptRequest & rq , const CStrW & playerName , const CStr & serverAddress , u16 serverPort , bool storeReplay )
2017-09-11 17:29:10 -07:00
{
ENSURE ( ! g_NetClient ) ;
ENSURE ( ! g_NetServer ) ;
ENSURE ( ! g_Game ) ;
2022-03-06 13:12:16 -08:00
g_Game = new CGame ( storeReplay ) ;
2021-02-27 09:44:59 -08:00
g_NetClient = new CNetClient ( g_Game ) ;
2017-09-11 17:29:10 -07:00
g_NetClient - > SetUserName ( playerName ) ;
2025-01-12 00:43:01 -08:00
g_NetClient - > SetupServerData ( serverAddress , serverPort ) ;
2017-09-11 17:29:10 -07:00
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 10:31:39 -08:00
if ( ! g_NetClient - > SetupConnection ( nullptr ) )
2017-09-11 17:29:10 -07:00
{
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
ScriptException : : Raise ( rq , " Failed to connect to server " ) ;
2017-09-11 17:29:10 -07:00
SAFE_DELETE ( g_NetClient ) ;
SAFE_DELETE ( g_Game ) ;
}
}
2021-03-02 12:01:14 -08:00
/**
* Requires XmppClient to send iq request to the server to get server ' s ip and port based on passed password .
* This is needed to not force server to share it ' s public ip with all potential clients in the lobby .
* XmppClient will also handle logic after receiving the answer .
*/
void StartNetworkJoinLobby ( const CStrW & playerName , const CStr & hostJID , const CStr & password )
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 10:31:39 -08:00
{
ENSURE ( ! ! g_XmppClient ) ;
ENSURE ( ! g_NetClient ) ;
ENSURE ( ! g_NetServer ) ;
ENSURE ( ! g_Game ) ;
2021-05-18 07:47:36 -07:00
CStr hashedPass = HashCryptographically ( password , hostJID + password + engine_version ) ;
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 10:31:39 -08:00
g_Game = new CGame ( true ) ;
2021-02-27 09:44:59 -08:00
g_NetClient = new CNetClient ( g_Game ) ;
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 10:31:39 -08:00
g_NetClient - > SetUserName ( playerName ) ;
2021-05-09 05:51:32 -07:00
g_NetClient - > SetHostJID ( hostJID ) ;
2021-01-23 10:04:36 -08:00
g_NetClient - > SetGamePassword ( hashedPass ) ;
2021-05-18 07:47:36 -07:00
g_NetClient - > SetupConnectionViaLobby ( ) ;
Hide ip and port from users until they want to join, add optional password
Current issue with the lobby, is that we make ips of hosts public for
anyone to read. This patch consists of 3 parts.
1.) Removing ips and ports from lobby javascript
2.) Removing need of script on the server to attach public ips to game
stanza by asking the host using xmppclient as proxy.
3.) Implementing password protected matches, to deny this information to
not trusted players.
Further description:
Do not send ports and stunip to the bots.
Removed from stanza.
Do not send ip to the lobby.
Removed from mapping gamelist from backend to gui (still on the backend
side, because it is done by script on 0ad server).
Get ip and ports on request when trying to connect.
On the host side, ask stun server what is host's public ip and remember
it.
On the client side, send iq through xmppclient to the hosting player and
ask for ip, port and if Stun is used, then if answer is success,
continue
with connecting, else fail.
Add optional password for matches.
Add password required identifier to the stanza.
Allow host to setup password for the match. Hash it on the host side and
store inside Netserver. If no password is given, matches will behave
as it is not required.
On the client side, if password for the match is required, show
additional window before trying to connect and ask for password, then
hash it
and send with iq request for ip, port and stun.
Server will answer with ip, port and stun only if passwords matches,
else will asnwer with error string.
Some security:
Passwords are hashed before sending, so it is not easy to guess what
users typed. (per wraitii)
Hashes are using different salt as lobby hashing and not using usernames
as salt (as that is not doable), so they are different even typing the
same password as for the lobby account.
Client remembers which user was asked for connection data and iq's id of
request. If answer doesn't match these things, it is ignored. (thnx
user1)
Every request for connection data is logged with hostname of the
requester to the mainlog file (no ips).
If user gets iq to send connection data and is not hosting the match,
will respond with error string "not_server".
If server gets iq::result with connection data, request is ignored.
Differential revision: D3184
Reviewed by: @wraitii
Comments by: @Stan, @bb, @Imarok, @vladislavbelov
Tested in lobby
This was SVN commit r24728.
2021-01-20 10:31:39 -08:00
}
2021-03-02 12:01:14 -08:00
void DisconnectNetworkGame ( )
2017-09-11 17:29:10 -07:00
{
// TODO: we ought to do async reliable disconnections
SAFE_DELETE ( g_NetServer ) ;
SAFE_DELETE ( g_NetClient ) ;
SAFE_DELETE ( g_Game ) ;
}
2021-03-02 12:01:14 -08:00
CStr GetPlayerGUID ( )
2017-09-11 17:29:10 -07:00
{
if ( ! g_NetClient )
return " local " ;
return g_NetClient - > GetGUID ( ) ;
}
2021-05-10 04:51:32 -07:00
JS : : Value PollNetworkClient ( const ScriptInterface & guiInterface )
2017-09-11 17:29:10 -07:00
{
if ( ! g_NetClient )
return JS : : UndefinedValue ( ) ;
// Convert from net client context to GUI script context
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
ScriptRequest rqNet ( g_NetClient - > GetScriptInterface ( ) ) ;
2020-11-13 05:18:22 -08:00
JS : : RootedValue pollNet ( rqNet . cx ) ;
2017-09-11 17:29:10 -07:00
g_NetClient - > GuiPoll ( & pollNet ) ;
2021-05-10 04:51:32 -07:00
return Script : : CloneValueFromOtherCompartment ( guiInterface , g_NetClient - > GetScriptInterface ( ) , pollNet ) ;
2017-09-11 17:29:10 -07:00
}
2021-03-22 03:13:27 -07:00
void SendGameSetupMessage ( const ScriptInterface & scriptInterface , JS : : HandleValue attribs1 )
2017-09-11 17:29:10 -07:00
{
ENSURE ( g_NetClient ) ;
// TODO: This is a workaround because we need to pass a MutableHandle to a JSAPI functions somewhere (with no obvious reason).
2021-03-02 12:01:14 -08:00
ScriptRequest rq ( scriptInterface ) ;
2020-11-13 05:18:22 -08:00
JS : : RootedValue attribs ( rq . cx , attribs1 ) ;
2017-09-11 17:29:10 -07:00
2021-03-02 12:01:14 -08:00
g_NetClient - > SendGameSetupMessage ( & attribs , scriptInterface ) ;
2017-09-11 17:29:10 -07:00
}
2021-03-02 12:01:14 -08:00
void AssignNetworkPlayer ( int playerID , const CStr & guid )
2017-09-11 17:29:10 -07:00
{
ENSURE ( g_NetClient ) ;
g_NetClient - > SendAssignPlayerMessage ( playerID , guid ) ;
}
2025-02-04 17:39:05 -08:00
void KickPlayer ( const ScriptRequest & rq , const CStrW & playerName , bool ban )
2017-09-11 17:29:10 -07:00
{
2025-02-04 17:39:05 -08:00
if ( ! g_NetClient )
{
ScriptException : : Raise ( rq , " g_NetClient is null. " ) ;
return ;
}
2017-09-11 17:29:10 -07:00
g_NetClient - > SendKickPlayerMessage ( playerName , ban ) ;
}
2024-11-30 02:18:24 -08:00
void SendNetworkChat ( const ScriptRequest & rq , const CStrW & message , JS : : HandleValue handle )
2017-09-11 17:29:10 -07:00
{
ENSURE ( g_NetClient ) ;
2024-11-30 02:18:24 -08:00
if ( handle . isNullOrUndefined ( ) )
{
g_NetClient - > SendChatMessage ( message , std : : nullopt ) ;
return ;
}
auto receivers = std : : make_optional < std : : vector < std : : string > > ( ) ;
if ( ! Script : : FromJSVal ( rq , handle , * receivers ) )
{
ScriptException : : Raise ( rq , " The second argument to `SendNetworkChat` has to be either an Array "
" or a nullish value. " ) ;
return ;
}
g_NetClient - > SendChatMessage ( message , std : : move ( receivers ) ) ;
2017-09-11 17:29:10 -07:00
}
2021-03-02 12:01:14 -08:00
void SendNetworkReady ( int message )
2017-09-11 17:29:10 -07:00
{
ENSURE ( g_NetClient ) ;
g_NetClient - > SendReadyMessage ( message ) ;
}
2021-03-02 12:01:14 -08:00
void ClearAllPlayerReady ( )
2017-09-11 17:29:10 -07:00
{
ENSURE ( g_NetClient ) ;
g_NetClient - > SendClearAllReadyMessage ( ) ;
}
2024-08-31 01:54:56 -07:00
void StartNetworkGame ( const ScriptInterface & scriptInterface , JS : : HandleValue savegame , JS : : HandleValue attribs1 )
2017-09-11 17:29:10 -07:00
{
ENSURE ( g_NetClient ) ;
2021-03-22 03:13:27 -07:00
ScriptRequest rq ( scriptInterface ) ;
2024-08-31 01:54:56 -07:00
2021-03-22 03:13:27 -07:00
JS : : RootedValue attribs ( rq . cx , attribs1 ) ;
2024-08-31 01:54:56 -07:00
std : : string attributesAsString { Script : : StringifyJSON ( rq , & attribs ) } ;
2024-12-31 03:54:21 -08:00
if ( savegame . isUndefined ( ) )
2024-08-31 01:54:56 -07:00
{
g_NetClient - > SendStartGameMessage ( attributesAsString ) ;
return ;
}
std : : wstring savegameID ;
Script : : FromJSVal ( rq , savegame , savegameID ) ;
const std : : optional < SavedGames : : LoadResult > loadResult { SavedGames : : Load ( scriptInterface , savegameID ) } ;
if ( loadResult )
g_NetClient - > SendStartSavedGameMessage ( attributesAsString , loadResult - > savedState ) ;
else
ScriptException : : Raise ( rq , " Failed to load the saved game: \" %ls \" " , savegameID . c_str ( ) ) ;
2017-09-11 17:29:10 -07:00
}
2021-03-02 12:01:14 -08:00
void SetTurnLength ( int length )
2017-09-11 17:29:10 -07:00
{
if ( g_NetServer )
g_NetServer - > SetTurnLength ( length ) ;
else
LOGERROR ( " Only network host can change turn length " ) ;
}
2024-12-03 12:24:55 -08:00
void SendNetworkFlare ( JS : : HandleValue position )
{
ENSURE ( g_NetClient ) ;
ScriptRequest rq ( g_NetClient - > GetScriptInterface ( ) ) ;
ENSURE ( position . isObject ( ) ) ;
JS : : RootedObject positionObj ( rq . cx , & position . toObject ( ) ) ;
JS : : RootedValue positionX ( rq . cx ) ;
JS : : RootedValue positionY ( rq . cx ) ;
JS : : RootedValue positionZ ( rq . cx ) ;
ENSURE ( JS_GetProperty ( rq . cx , positionObj , " x " , & positionX ) ) ;
ENSURE ( JS_GetProperty ( rq . cx , positionObj , " y " , & positionY ) ) ;
ENSURE ( JS_GetProperty ( rq . cx , positionObj , " z " , & positionZ ) ) ;
// (TODO?): Converting the doubles into strings here is a workaround because direct (de)serialisation of floating point numbers is not supported.
// It causes somewhat awkward message handling, but the resulting efficiency losses are negligible.
g_NetClient - > SendFlareMessage (
CStr : : FromDouble ( positionX . toNumber ( ) ) ,
CStr : : FromDouble ( positionY . toNumber ( ) ) ,
CStr : : FromDouble ( positionZ . toNumber ( ) )
) ;
}
2021-03-02 12:01:14 -08:00
void RegisterScriptFunctions ( const ScriptRequest & rq )
2017-09-11 17:29:10 -07:00
{
2021-03-02 12:01:14 -08:00
ScriptFunction : : Register < & GetDefaultPort > ( rq , " GetDefaultPort " ) ;
ScriptFunction : : Register < & IsNetController > ( rq , " IsNetController " ) ;
ScriptFunction : : Register < & HasNetServer > ( rq , " HasNetServer " ) ;
ScriptFunction : : Register < & HasNetClient > ( rq , " HasNetClient " ) ;
ScriptFunction : : Register < & StartNetworkHost > ( rq , " StartNetworkHost " ) ;
ScriptFunction : : Register < & StartNetworkJoin > ( rq , " StartNetworkJoin " ) ;
ScriptFunction : : Register < & StartNetworkJoinLobby > ( rq , " StartNetworkJoinLobby " ) ;
ScriptFunction : : Register < & DisconnectNetworkGame > ( rq , " DisconnectNetworkGame " ) ;
ScriptFunction : : Register < & GetPlayerGUID > ( rq , " GetPlayerGUID " ) ;
ScriptFunction : : Register < & PollNetworkClient > ( rq , " PollNetworkClient " ) ;
2021-03-22 03:13:27 -07:00
ScriptFunction : : Register < & SendGameSetupMessage > ( rq , " SendGameSetupMessage " ) ;
2021-03-02 12:01:14 -08:00
ScriptFunction : : Register < & AssignNetworkPlayer > ( rq , " AssignNetworkPlayer " ) ;
ScriptFunction : : Register < & KickPlayer > ( rq , " KickPlayer " ) ;
ScriptFunction : : Register < & SendNetworkChat > ( rq , " SendNetworkChat " ) ;
ScriptFunction : : Register < & SendNetworkReady > ( rq , " SendNetworkReady " ) ;
ScriptFunction : : Register < & ClearAllPlayerReady > ( rq , " ClearAllPlayerReady " ) ;
ScriptFunction : : Register < & StartNetworkGame > ( rq , " StartNetworkGame " ) ;
ScriptFunction : : Register < & SetTurnLength > ( rq , " SetTurnLength " ) ;
2024-12-03 12:24:55 -08:00
ScriptFunction : : Register < & SendNetworkFlare > ( rq , " SendNetworkFlare " ) ;
2021-03-02 12:01:14 -08:00
}
2017-09-11 17:29:10 -07:00
}