Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Version 2.3.1.0 #1

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
• Fixed issues shipment client.
• Update doc.
  • Loading branch information
treviasxk committed Jan 24, 2024
commit beffd7d6118a8f9d0114f7a231b578a9b6b6f484
6 changes: 3 additions & 3 deletions Nethostfire.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<OutputType>Library</OutputType>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>latest</LangVersion>
<Version>2.3.0.0</Version>
<AssemblyVersion>2.3.0.0</AssemblyVersion>
<FileVersion>2.3.0.0</FileVersion>
<Version>2.3.1.0</Version>
<AssemblyVersion>2.3.1.0</AssemblyVersion>
<FileVersion>2.3.1.0</FileVersion>
<Company>treviasxk</Company>
<Authors>Trevias Xk</Authors>
<Product>Nethostfire</Product>
Expand Down
6 changes: 4 additions & 2 deletions UDP/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,14 @@ public static void DisconnectServer(){
if(Status == ClientStatusConnection.Connected || Status == ClientStatusConnection.Connecting){
if(Status == ClientStatusConnection.Connected)
ChangeStatus(ClientStatusConnection.Disconnecting);
Utility.SendPing(Socket, new byte[]{0});
Utility.SendPing(Socket, [0]);
Socket.Close();
Socket = null;
SendOnlineThread = null;
ClientReceiveUDPThread = null;
publicKeyRSA = null;
privateKeyAES = null;
Utility.listIndex.Clear();
Utility.listHoldConnectionClient.Clear();
if(Status == ClientStatusConnection.Disconnecting)
ChangeStatus(ClientStatusConnection.Disconnected);
Expand Down Expand Up @@ -289,7 +290,7 @@ static void SendOnline(){
// Enviando byte 1 para o server, para dizer que está online
if(Status == ClientStatusConnection.Connected){
pingTmp = Environment.TickCount;
Utility.SendPing(Socket, new byte[]{1});
Utility.SendPing(Socket, [1]);
}
// Verificando se o ultimo ping com o server é de 3000ms
if(Environment.TickCount - timeLastPacket > 3000 && Status == ClientStatusConnection.Connected)
Expand Down Expand Up @@ -331,6 +332,7 @@ static void ChangeStatus(ClientStatusConnection _status){
case ClientStatusConnection.Connecting:
publicKeyRSA = null;
privateKeyAES = null;
Utility.listIndex.Clear();
Utility.listHoldConnectionClient.Clear();
timeLastPacket = Environment.TickCount;
Send(Encoding.ASCII.GetBytes(Utility.PublicKeyRSAClient), 0, TypeShipping.RSA, TypeHoldConnection.NotEnqueue);
Expand Down
4 changes: 2 additions & 2 deletions UDP/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,8 @@ _Encryption/Decryprion Speed may vary depending on your machine's performance._
### TypeHoldConnection

• TypeHoldConnection.None - Value default. (No effect)
• TypeHoldConnection.NotEnqueue - With NotEnqueue, shipments are sent to their destination without packet loss, shipments will not be sent in a queue to improve performance. (PS: it is possible that the same byte is received twice, increase the value of ReceiveAndSendTimeOut if this happens)
• TypeHoldConnection.Enqueue - With Enqueue, bytes are sent to their destination without packet loss, shipments will be sent in a queue, this feature is not recommended to be used for high demand for shipments, each package can vary between 1ms and 1000ms. (PS: it is possible that the same byte is received twice, increase the value of ReceiveAndSendTimeOut if this happens)
• TypeHoldConnection.NotEnqueue - With NotEnqueue, shipments are sent to their destination without packet loss, shipments will not be sent in a queue to improve performance.
• TypeHoldConnection.Enqueue - With Enqueue, bytes are sent to their destination without packet loss, shipments will be sent in a queue, this feature is not recommended to be used for high demand for shipments, each package can vary between 1ms and 1000ms.

-----

Expand Down
18 changes: 9 additions & 9 deletions UDP/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,15 @@ public static void SendAll(float _number, int _groupID, TypeHoldConnection _type
/// </summary>
public static void DisconnectClient(DataClient _dataClient){
if(Status == ServerStatusConnection.Running)
Utility.SendPing(Socket, new byte[]{0},_dataClient);
Utility.SendPing(Socket, [0],_dataClient);
}
/// <summary>
/// To disconnect a group clients from server, it is necessary to inform the List DataClient.
/// </summary>
public static void DisconnectClientGroup(ConcurrentQueue<DataClient> _dataClients){
Parallel.ForEach(_dataClients, dataClient => {
if(Status == ServerStatusConnection.Running)
Utility.SendPing(Socket, new byte[]{0}, dataClient);
Utility.SendPing(Socket, [0], dataClient);
});
}
/// <summary>
Expand All @@ -387,7 +387,7 @@ public static void DisconnectClientGroup(ConcurrentQueue<DataClient> _dataClient
public static void DisconnectClientAll(){
Parallel.ForEach(DataClients, item => {
if(Status == ServerStatusConnection.Running)
Utility.SendPing(Socket, new byte[]{0}, item.Value);
Utility.SendPing(Socket, [0], item.Value);
});
DataClients.Clear();
WaitDataClients.Clear();
Expand All @@ -405,7 +405,7 @@ public static void ChangeBlockIP(IPEndPoint _ip, int _time){
ListBlockedIPs.TryRemove(_ip, out _);
}else{
ListBlockedIPs.TryAdd(_ip, _time + Environment.TickCount);
Utility.SendPing(Socket, new byte[]{2}, new DataClient(){IP = _ip});
Utility.SendPing(Socket, [2], new DataClient(){IP = _ip});
}
}

Expand All @@ -418,10 +418,10 @@ static bool CheckBlockerIP(IPEndPoint _ip){
}
else{
if(DataClients.TryRemove(_ip, out var _dataClient))
Utility.SendPing(Socket, new byte[]{2}, _dataClient);
Utility.SendPing(Socket, [2], _dataClient);

if(WaitDataClients.TryRemove(_ip, out var _waitDataClient))
Utility.SendPing(Socket, new byte[]{2}, _waitDataClient);
Utility.SendPing(Socket, [2], _waitDataClient);

return true;
}
Expand Down Expand Up @@ -499,14 +499,14 @@ static async void ServerReceiveUDP(){
case 1:
_dataClient.Ping = Environment.TickCount - _dataClient.Time - 1000;
_dataClient.Time = Environment.TickCount;
Utility.SendPing(Socket, new byte[]{1}, _dataClient);
Utility.SendPing(Socket, [1], _dataClient);
break;
}
}
}else{
if(DataClients.Count >= maxClients){
Utility.SendPing(Socket, new byte[]{3}, new DataClient(){IP = _ip});
data = new byte[]{};
Utility.SendPing(Socket, [3], new DataClient(){IP = _ip});
data = [];
}
}

Expand Down
71 changes: 35 additions & 36 deletions UDP/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class DataClient{
/// Private AES key
/// </summary>
public byte[] PrivateKeyAES = null;
public readonly List<int> ListIndex = new();
public readonly ConcurrentDictionary<int, HoldConnection> ListHoldConnection = new();
public readonly ConcurrentDictionary<int, HoldConnection> ListHoldConnectionQueue = new();
}
Expand All @@ -56,11 +57,11 @@ public class DataClient{
public enum TypeHoldConnection {
None = 0,
/// <summary>
/// With NotEnqueue, bytes are sent to their destination without packet loss, shipments will not be queued to improve performance. (PS: it is possible that the same byte is received twice, increase the value of ReceiveAndSendTimeOut if this happens)
/// With NotEnqueue, bytes are sent to their destination without packet loss, shipments will not be queued to improve performance.
/// </summary>
NotEnqueue = 1,
/// <summary>
/// With Enqueue, bytes are sent to their destination without packet loss, shipments will be sent in a queue, this feature is not recommended to be used for high demand for shipments, each package can vary between 1ms and 1000ms. (PS: it is possible that the same byte is received twice, increase the value of ReceiveAndSendTimeOut if this happens)
/// With Enqueue, bytes are sent to their destination without packet loss, shipments will be sent in a queue, this feature is not recommended to be used for high demand for shipments, each package can vary between 1ms and 1000ms.
/// </summary>
Enqueue = 2,
}
Expand Down Expand Up @@ -156,9 +157,9 @@ class Utility {
public static int receiveAndSendTimeOut = 1000;
public static bool RunningInUnity = false, ShowDebugConsole = true, UnityBatchMode = false, UnityEditorMode = false;
public static ConcurrentQueue<Action> ListRunOnMainThread = new();
public static List<int> listIndex = new();
public static ConcurrentDictionary<int, HoldConnection> listHoldConnectionClient = new();
public static ConcurrentDictionary<int, HoldConnection> listHoldConnectionQueueClient = new();
public static List<int> listIndex = new();
public static int IndexShipping = 1;
static Aes AES;
public static string GetVersion {get {return UnityEngine.Application.version;}}
Expand Down Expand Up @@ -255,7 +256,7 @@ public static (byte[], int, TypeContent, TypeShipping, int) ByteToReceive(byte[]
UDP.Server.Send(_lhcq.Bytes, _lhcq.GroupID, _dataClient, _lhcq.TypeShipping, TypeHoldConnection.NotEnqueue);
}

return (new byte[]{}, 0, TypeContent.Background, TypeShipping.None, _indexID);
return ([], 0, TypeContent.Background, TypeShipping.None, _indexID);
}

type = new byte[_byte[3]];
Expand All @@ -272,7 +273,7 @@ public static (byte[], int, TypeContent, TypeShipping, int) ByteToReceive(byte[]
_TypeShipping = (TypeShipping)_byte[2];
_typeContent = (TypeContent)_byte[1];
}catch{
return (new byte[]{}, 0, TypeContent.Background, TypeShipping.None, 0);
return ([], 0, TypeContent.Background, TypeShipping.None, 0);
}

if(_typeContent == TypeContent.Background)
Expand Down Expand Up @@ -321,15 +322,20 @@ public static (byte[], int, TypeContent, TypeShipping, int) ByteToReceive(byte[]
type.CopyTo(data2, 2); // IndexID
SendPing(_udpClient, data2, _dataClient);
}
if(!listIndex.Contains(_indexID)){
listIndex.Add(_indexID);
return (data.Length > 1 ? data : new byte[]{}, _groupID, _typeContent, _TypeShipping, _indexID);

if(_dataClient != null){
if(!_dataClient.ListIndex.Contains(_indexID)){
_dataClient.ListIndex.Add(_indexID);
return (data.Length > 1 ? data : [], _groupID, _typeContent, _TypeShipping, _indexID);
}
}else{
return (new byte[]{}, 0, TypeContent.Background, TypeShipping.None, 0);
if(!listIndex.Contains(_indexID)){
listIndex.Add(_indexID);
return (data.Length > 1 ? data : [], _groupID, _typeContent, _TypeShipping, _indexID);
}
}
}catch{
return (new byte[]{}, 0, TypeContent.Background, TypeShipping.None, 0);
}
}catch{}
return ([], 0, TypeContent.Background, TypeShipping.None, 0);
}

public static byte[] ByteToSend(byte[] _byte, int _groupID, TypeShipping _TypeShipping, TypeHoldConnection _typeHoldConnection, TypeContent _typeContent, int _indexID, DataClient _dataClient = null){
Expand All @@ -352,14 +358,14 @@ public static byte[] ByteToSend(byte[] _byte, int _groupID, TypeShipping _TypeSh
_byte = EncryptRSA(_byte, _dataClient != null ? _dataClient.PublicKeyRSA : UDP.Client.PublicKeyRSA);
break;
case TypeShipping.Base64:
_byte = EncryptBase64(_byte) == "" ? new byte[]{} : System.Text.Encoding.ASCII.GetBytes(EncryptBase64(_byte));
_byte = EncryptBase64(_byte) == "" ? [] : System.Text.Encoding.ASCII.GetBytes(EncryptBase64(_byte));
break;
case TypeShipping.Compress:
_byte = Compress(_byte);
break;
case TypeShipping.OnlyBase64:
if(_dataClient == null)
_byte = EncryptBase64(_byte) == "" ? new byte[]{} : System.Text.Encoding.ASCII.GetBytes(EncryptBase64(_byte));
_byte = EncryptBase64(_byte) == "" ? [] : System.Text.Encoding.ASCII.GetBytes(EncryptBase64(_byte));
break;
case TypeShipping.OnlyCompress:
if(_dataClient == null)
Expand All @@ -368,7 +374,7 @@ public static byte[] ByteToSend(byte[] _byte, int _groupID, TypeShipping _TypeSh
}

if(_TypeShipping != TypeShipping.None && _byte.Length == 0)
return new byte[]{};
return [];

try{
byte[] groupID = BitConverter.GetBytes(_groupID);
Expand All @@ -388,7 +394,7 @@ public static byte[] ByteToSend(byte[] _byte, int _groupID, TypeShipping _TypeSh
return data;
}
catch{
return new byte[]{};
return [];
}
}

Expand Down Expand Up @@ -450,32 +456,26 @@ public static bool SendPing(UdpClient _udpClient, byte[] _byte, DataClient _data
}
private static byte[] EncryptRSA(byte[] _byte, string _publicKeyRSA){
using var RSA = new RSACryptoServiceProvider();
try
{
try{
RSA.FromXmlString(_publicKeyRSA);
return RSA.Encrypt(_byte, true);
}
catch
{
}catch{
var b = ((RSA.KeySize - 384) / 8) + 6;
if (b < _byte.Length)
ShowLog("The key size defined in KeySizeBytesRSA, can only encrypt at most " + b + " bytes.");
return new byte[]{};
return [];
}
}
private static byte[] DecryptRSA(byte[] _byte, string _privateKeyRSA){
using var RSA = new RSACryptoServiceProvider();
try
{
try{
RSA.FromXmlString(_privateKeyRSA);
return RSA.Decrypt(_byte, true);
}
catch
{
}catch{
var b = ((RSA.KeySize - 384) / 8) + 6;
if (b < _byte.Length)
ShowLog("The key size defined in KeySizeBytesRSA, can only decrypt at most " + b + " bytes.");
return new byte[]{};
return [];
}
}
private static byte[] EncryptAES(byte[] _byte, byte[] _privateKeyAES){
Expand All @@ -484,7 +484,7 @@ private static byte[] EncryptAES(byte[] _byte, byte[] _privateKeyAES){
return encryptor.TransformFinalBlock(_byte, 0, _byte.Length);
}
catch{
return new byte[]{};
return [];
}
}
private static byte[] DecryptAES(byte[] _byte, byte[] _privateKeyAES){
Expand All @@ -493,7 +493,7 @@ private static byte[] DecryptAES(byte[] _byte, byte[] _privateKeyAES){
return encryptor.TransformFinalBlock(_byte, 0, _byte.Length);
}
catch{
return new byte[]{};
return [];
}
}
private static string EncryptBase64(byte[] _byte){
Expand All @@ -507,7 +507,7 @@ private static byte[] DecryptBase64(string _text){
try{
return Convert.FromBase64String(_text);
}catch{
return new byte[]{};
return [];
}
}
private static byte[] Compress(byte[] _byte){
Expand All @@ -518,27 +518,26 @@ private static byte[] Compress(byte[] _byte){
}
return output.ToArray();
}catch{
return new byte[]{};
return [];
}
}
private static byte[] Decompress(byte[] data){
try{
MemoryStream input = new(data);
MemoryStream output = new();
using (DeflateStream dstream = new(input, CompressionMode.Decompress)){
using(DeflateStream dstream = new(input, CompressionMode.Decompress))
dstream.CopyTo(output);
}
return output.ToArray();
}catch{
return new byte[]{};
return [];
}
}
private static byte[] GetHashMD5(byte[] _byte){
try{
MD5 md5 = MD5.Create();
return md5.ComputeHash(_byte);
}catch{
return new byte[]{};
return [];
}
}
public static string ShowLog(string Message){
Expand Down
1 change: 1 addition & 0 deletions Unity/NethostfireService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ static void Init(){
UDP.Server.OnReceivedBytes = null;
UDP.Server.OnServerStatus = null;
Utility.ShowDebugConsole = ShowDebug;
Utility.listHoldConnectionQueueClient.Clear();
Utility.listHoldConnectionClient.Clear();
Utility.ListRunOnMainThread.Clear();
}
Expand Down