Allow sending chat message and command in console

This commit is contained in:
Sardelka
2022-07-22 19:43:48 +08:00
parent cb53e8a664
commit e52135c343
7 changed files with 63 additions and 45 deletions

View File

@ -91,7 +91,7 @@ namespace RageCoop.Client
PedID = Main.LocalPlayerID,
Username =username,
ModVersion = Main.CurrentVersion,
PassHashEncrypted=Security.Encrypt(password.GetHash())
PassHashEncrypted=Security.Encrypt(password.GetSHA256Hash())
};
Security.GetSymmetricKeysCrypted(out handshake.AesKeyCrypted, out handshake.AesIVCrypted);

View File

@ -230,11 +230,6 @@ namespace RageCoop.Client
{
Function.Call(Hash.SET_RADIO_TO_STATION_INDEX, index);
}
public static byte[] GetHash(this string inputString)
{
using (HashAlgorithm algorithm = SHA256.Create())
return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
}
#region WIN32

View File

@ -111,13 +111,6 @@ namespace RageCoop.Core
bytes.AddInt(toadd.Length);
bytes.AddRange(toadd);
}
public static int GetHash(string s)
{
MD5 md5Hasher = MD5.Create();
var hashed = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(s));
return BitConverter.ToInt32(hashed, 0);
}
public static byte[] GetBytes(this string s)
{
return Encoding.UTF8.GetBytes(s);
@ -284,4 +277,21 @@ namespace RageCoop.Core
return false;
}
}
/// <summary>
/// Some extension methods provided by RageCoop
/// </summary>
public static class PublicExtensions
{
/// <summary>
/// Get a hex-encoded string of the input string hashed by SHA256 algorithmn, internally used to hash password at client side.
/// </summary>
/// <param name="inputString"></param>
/// <returns></returns>
public static byte[] GetSHA256Hash(this string inputString)
{
using (HashAlgorithm algorithm = SHA256.Create())
return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
}
}
}

View File

@ -134,13 +134,7 @@ namespace RageCoop.Server
{
try
{
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetID);
if (userConnection == null)
{
return;
}
Server.SendChatMessage(from, message, userConnection);
Server.SendChatMessage(from, message, this);
}
catch (Exception e)
{

View File

@ -39,6 +39,7 @@ namespace RageCoop.Server
mainLogger.Info("Server stopped.");
mainLogger.Dispose();
Thread.Sleep(3000);
Environment.Exit(0);
}
else
{
@ -49,6 +50,15 @@ namespace RageCoop.Server
};
server.Start();
mainLogger?.Info("Please use CTRL + C if you want to stop the server!");
while (true)
{
mainLogger?.Info("Type here to send chat message: ");
var s=Console.ReadLine();
if (!Stopping && s!=null)
{
server.ChatMessageReceived("Server", s, null);
}
}
}
catch (Exception e)
{

View File

@ -95,7 +95,7 @@ namespace RageCoop.Server.Scripting
else
{
Server.SendChatMessage("Server", "Command not found!", sender.Connection);
Server.SendChatMessage("Server", "Command not found!", sender);
}
}
@ -211,7 +211,7 @@ namespace RageCoop.Server.Scripting
targets ??= new(Server.Clients.Values);
foreach(Client client in targets)
{
Server.SendChatMessage(username, message, client.Connection);
Server.SendChatMessage(username, message, client);
}
}
catch (Exception e)

View File

@ -221,6 +221,7 @@ namespace RageCoop.Server
Resources.LoadAll();
_listenerThread=new Thread(() => Listen());
_listenerThread.Start();
Logger?.Info("Listening for clients");
}
/// <summary>
/// Terminate threads and stop the server
@ -239,7 +240,6 @@ namespace RageCoop.Server
private void Listen()
{
NetIncomingMessage msg=null;
Logger?.Info("Listening for clients");
while (!_stopping)
{
try
@ -481,7 +481,7 @@ namespace RageCoop.Server
packet.Unpack(data);
_worker.QueueJob(() => API.Events.InvokeOnChatMessage(packet, sender));
SendChatMessage(packet, sender);
ChatMessageReceived(packet.Username,packet.Message, sender);
}
break;
case PacketType.CustomEvent:
@ -666,7 +666,7 @@ namespace RageCoop.Server
if (!string.IsNullOrEmpty(Settings.WelcomeMessage))
{
SendChatMessage(new Packets.ChatMessage() { Username = "Server", Message = Settings.WelcomeMessage }, null,new List<NetConnection>() { newClient.Connection });
ChatMessageReceived("Server",Settings.WelcomeMessage , null);
}
}
@ -773,35 +773,44 @@ namespace RageCoop.Server
#endregion
// Send a message to targets or all players
private void SendChatMessage(Packets.ChatMessage packet,Client sender=null, List<NetConnection> targets = null)
internal void ChatMessageReceived(string name, string message,Client sender=null)
{
if (packet.Message.StartsWith('/'))
if (message.StartsWith('/'))
{
string[] cmdArgs = packet.Message.Split(" ");
string[] cmdArgs = message.Split(" ");
string cmdName = cmdArgs[0].Remove(0, 1);
_worker.QueueJob(()=>API.Events.InvokeOnCommandReceived(cmdName, cmdArgs, sender));
return;
}
packet.Message = packet.Message.Replace("~", "");
SendChatMessage(packet.Username, packet.Message, targets);
message = message.Replace("~", "");
Logger?.Info(packet.Username + ": " + packet.Message);
var msg=MainNetServer.CreateMessage();
new Packets.ChatMessage()
{
Username=name,
Message=message
}.Pack(msg);
if (sender==null)
{
// Sent by server
MainNetServer.SendToAll(msg,NetDeliveryMethod.ReliableOrdered,(int)ConnectionChannel.Chat);
}
else
{
MainNetServer.SendToAll(msg,sender.Connection, NetDeliveryMethod.ReliableOrdered, (int)ConnectionChannel.Chat);
}
Logger?.Info(name + ": " + message);
}
internal void SendChatMessage(string username, string message, List<NetConnection> targets = null)
internal void SendChatMessage(string name, string message, Client target)
{
if (MainNetServer.Connections.Count==0) { return; }
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
new Packets.ChatMessage() { Username = username, Message = message }.Pack(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, targets ?? MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Chat);
}
internal void SendChatMessage(string username, string message, NetConnection target)
{
SendChatMessage(username, message, new List<NetConnection>() { target });
var msg = MainNetServer.CreateMessage();
new Packets.ChatMessage()
{
Username= name,
Message=message,
}.Pack(msg);
MainNetServer.SendMessage(msg, target.Connection, NetDeliveryMethod.ReliableOrdered, (int)ConnectionChannel.Chat);
}
#endregion