Binary support

This commit is contained in:
twoischnig committed 2023-12-01 22:58:44 +01:00
1 parent 840218f119
commit 97df572f1e
21 files changed
+310 -45

No files matched your search

+33 -8
View File
@@ -5,8 +5,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@@ -20,6 +18,7 @@ namespace Lobbies
AutoResetEvent waitForExternalIp = new AutoResetEvent(false);
private Dictionary<Guid, LobbyInfo> lobbyInformation = new Dictionary<Guid, LobbyInfo>();
private string? host;
private int port;
private int connectionId;
@@ -74,8 +73,15 @@ namespace Lobbies
}
}
public void HostLobby(Guid gameId, string name, int gameMode, int maxPlayerCount,string? password, string? ip, int port)
public void HostLobby(Guid gameId, string name, int gameMode, int maxPlayerCount, string? password, string? ip, int port)
{
byte[]? hash = null, salt = null;
if(!string.IsNullOrEmpty(password))
{
(hash, salt) = PasswordHash.Hash(password);
}
var lobbyCreate = new LobbyCreate()
{
GameId = gameId,
@@ -83,7 +89,8 @@ namespace Lobbies
GameMode = gameMode,
MaxPlayerCount = maxPlayerCount,
PlayerCount = 0,
PasswordHash = string.IsNullOrEmpty(password) ? null : SHA256.HashData(Encoding.UTF8.GetBytes(password)),
PasswordHash = hash,
PasswordSalt = salt,
HostIp = ip,
HostPort = port
};
@@ -95,10 +102,14 @@ namespace Lobbies
public void RequestLobbyHostInfo(Guid lobbyId, string? password)
{
byte[]? passwordHash = null;
if(!string.IsNullOrEmpty(password) && lobbyInformation.ContainsKey(lobbyId) && lobbyInformation[lobbyId].PasswordSalt != null)
passwordHash = PasswordHash.Hash(password, lobbyInformation[lobbyId].PasswordSalt!);
var lobbyCreate = new LobbyRequestHostInfo()
{
LobbyId = lobbyId,
PasswordHash = string.IsNullOrEmpty(password) ? null : SHA256.HashData(Encoding.UTF8.GetBytes(password)),
PasswordHash = passwordHash,
};
byte[] messageData = bufferRental.Rent();
@@ -110,13 +121,17 @@ namespace Lobbies
public delegate void SendUdpMessageCallback(IPEndPoint remoteEndpoint, byte[] messageBuffer, int messageLength);
public void RequestLobbyNatPunch(Guid lobbyId, string? password, SendUdpMessageCallback sendUdpCallback)
{
byte[]? passwordHash = null;
if (!string.IsNullOrEmpty(password) && lobbyInformation.ContainsKey(lobbyId) && lobbyInformation[lobbyId].PasswordSalt != null)
passwordHash = PasswordHash.Hash(password, lobbyInformation[lobbyId].PasswordSalt!);
Task.Run(() =>
{
QueryExternalIpAndPort(sendUdpCallback);
var lobbyRequestNatPunch = new LobbyRequestNatPunch()
{
LobbyId = lobbyId,
PasswordHash = string.IsNullOrEmpty(password) ? null : SHA256.HashData(Encoding.UTF8.GetBytes(password)),
PasswordHash = passwordHash,
ClientIp = externalIp,
ClientPort = externalPort
};
@@ -129,13 +144,21 @@ namespace Lobbies
public void UpdateLobby(string name, int gameMode, int maxPlayerCount, int playerCount, string? password, string? ip, int port)
{
byte[]? hash = null, salt = null;
if (!string.IsNullOrEmpty(password))
{
(hash, salt) = PasswordHash.Hash(password);
}
var lobbyUpdate = new LobbyUpdate()
{
Name = name,
GameMode = gameMode,
MaxPlayerCount = maxPlayerCount,
PlayerCount = playerCount,
PasswordHash = string.IsNullOrEmpty(password) ? null : SHA256.HashData(Encoding.UTF8.GetBytes(password)),
PasswordHash = hash,
PasswordSalt = salt,
HostIp = ip,
HostPort = port
};
@@ -257,7 +280,7 @@ namespace Lobbies
private void TcpClient_DataReceived(int dataLength, Memory<byte> data)
{
try
{
{
if (dataLength > 0)
{
switch (LobbyMessageIdentifier.ReadLobbyMessageIdentifier(data.Span))
@@ -277,6 +300,7 @@ namespace Lobbies
var lobbyInfo = LobbyInfo.Deserialize(data.Span);
if (lobbyInfo != null)
{
lobbyInformation[lobbyInfo.Id] = lobbyInfo;
events.Enqueue(new LobbyClientEvent { EventType = LobbyClientEventTypes.LobbyUpdate, EventData = lobbyInfo });
}
}
@@ -286,6 +310,7 @@ namespace Lobbies
var lobbyDelete = LobbyDelete.Deserialize(data.Span);
if (lobbyDelete != null)
{
lobbyInformation.Remove(lobbyDelete.Id);
events.Enqueue(new LobbyClientEvent { EventType = LobbyClientEventTypes.LobbyDelete, EventData = lobbyDelete });
}
}
+3 -2
View File
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<OutputPath>..\Assets\NetworkLobbyClient\Runtime</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\LobbyServerDto\LobbyServerDto.csproj" />
</ItemGroup>
</Project>
+37
View File
@@ -0,0 +1,37 @@
using System.Security.Cryptography;
namespace Lobbies
{
internal class PasswordHash
{
const int keySize = 64;
const int saltSize = 16;
const int iterations = 350000;
static HashAlgorithmName hashAlgorithm = HashAlgorithmName.SHA512;
static internal (byte[] hash, byte[] salt) Hash(string text)
{
using (var pbkdf2 = new Rfc2898DeriveBytes(
text,
saltSize,
iterations,
hashAlgorithm))
{
return (pbkdf2.GetBytes(keySize), pbkdf2.Salt);
}
}
static internal byte[] Hash(string text, byte[] salt)
{
using (var pbkdf2 = new Rfc2898DeriveBytes(
text,
salt,
iterations,
hashAlgorithm))
{
return pbkdf2.GetBytes(keySize);
}
}
}
}
+7 -2
View File
@@ -29,9 +29,14 @@ namespace Lobbies
try
{
cancellationTokenSource!.Token.ThrowIfCancellationRequested();
running = true;
tcpClient = new TcpClient();
await tcpClient.ConnectAsync(host, port, cancellationTokenSource!.Token);
tcpClient = new TcpClient();
using (cancellationTokenSource!.Token.Register(() => { tcpClient.Close(); }))
{
await tcpClient.ConnectAsync(host, port);
}
networkStream = tcpClient.GetStream();
Memory<byte> buffer = new byte[4096];