0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-25 04:35:18 +00:00
SmoOnlineServer/Server/Client.cs

134 lines
4.8 KiB
C#
Raw Normal View History

using System.Buffers;
using System.Collections.Concurrent;
2022-04-27 06:43:11 +00:00
using System.Diagnostics;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using Shared;
2022-02-10 08:42:35 +00:00
using Shared.Packet;
2022-02-08 22:46:12 +00:00
using Shared.Packet.Packets;
2021-11-29 04:04:34 +00:00
2022-02-10 01:44:50 +00:00
namespace Server;
2021-11-29 04:04:34 +00:00
public class Client : IDisposable {
2022-03-15 07:23:37 +00:00
public readonly ConcurrentDictionary<string, object?> Metadata = new ConcurrentDictionary<string, object?>(); // can be used to store any information about a player
2022-02-09 21:56:57 +00:00
public bool Connected = false;
Refactoring ban command (#48) * rename command: `ban ...` => `ban player ...` To enable adding other subcommands starting with `ban`. Moving ban list and crash related code into its own class to tidy the Program class up. Change Id values of the crash cmds, to fit into the 16 byte max length imposed by ChangeStagePacket.IdSize. * add command: `ban ip <ipv4-address>` To add an IPv4 address to the ban list. * add command: `ban profile <profile-id>` To add a profile ID to the ban list. * add command: `unban ip <ipv4-address>` To remove a banned IPv4 address from the ban list. * add command: `unban profile <profile-id>` To remove a banned profile ID from the ban list. * add commands: `ban enable` and `ban disable` To set the value of `BanList.Enabled` to `true` or `false` without editing the `settings.json` file. * add command: `ban list` To show the current ban list settings. * fix: actually working ban functionality Changes: - ignore new sockets from banned IP addresses way earlier. - ignore all packets by banned profiles. Intentionally keeping the connection open instead of d/c banned clients. This is to prevent endless server logs due to automatically reconnecting clients. Before: Reconnecting clients aren't entering `ClientJoined` and therefore the d/c is only working on first connections. Effectively banned clients got a d/c and then automatically reconnected again without getting a d/c again. Therefore allowing them to play normally. * use SortedSet instead of List for settings To enforce unique entries and maintain a stable order inside of the `settings.json`. * add commands: `ban stage <stage-name>` and `unban stage <stage-name>` To kick players from the server when they enter a banned stage. <stage-name> can also be a kingdom alias, which bans/unbans all stages in that kingdom. Because we aren't banning the player, d/c them would be no good, because of the client auto reconnect. Instead send them the crash and ignore all packets by them until they d/c on their own. This is an alternative solution for issue #43. * Update Server.cs --------- Co-authored-by: Sanae <32604996+Sanae6@users.noreply.github.com>
2023-09-05 23:14:54 +00:00
public bool Ignored = false;
public bool Banned = false;
public CostumePacket? CurrentCostume = null; // required for proper client sync
public string Name {
get => Logger.Name;
set => Logger.Name = value;
}
2022-02-10 01:44:50 +00:00
public Guid Id;
public Socket? Socket;
public Server Server { get; init; } = null!; //init'd in object initializer
public Logger Logger { get; }
2022-02-10 01:44:50 +00:00
public Client(Socket socket) {
Socket = socket;
Logger = new Logger("Unknown User");
}
// copy Client to use existing data for a new reconnected connection with a new socket
public Client(Client other, Socket socket) {
Metadata = other.Metadata;
Connected = other.Connected;
CurrentCostume = other.CurrentCostume;
Id = other.Id;
Socket = socket;
Server = other.Server;
Logger = other.Logger;
}
2022-02-10 01:44:50 +00:00
public void Dispose() {
if (Socket?.Connected is true) {
Socket.Disconnect(false);
}
2022-02-10 01:44:50 +00:00
}
2021-11-29 04:04:34 +00:00
2022-02-16 00:35:38 +00:00
public async Task Send<T>(T packet, Client? sender = null) where T : struct, IPacket {
IMemoryOwner<byte> memory = MemoryPool<byte>.Shared.RentZero(Constants.HeaderSize + packet.Size);
2022-04-27 05:48:13 +00:00
PacketAttribute packetAttribute = Constants.PacketMap[typeof(T)];
2022-04-27 20:09:50 +00:00
try {
// don't send most packets to ignored players
if (Ignored && packetAttribute.Type != PacketType.Init && packetAttribute.Type != PacketType.ChangeStage) {
memory.Dispose();
return;
}
2022-04-27 20:17:02 +00:00
Server.FillPacket(new PacketHeader {
Id = sender?.Id ?? Id,
Type = packetAttribute.Type,
2022-04-27 20:09:50 +00:00
PacketSize = packet.Size
2022-04-27 20:17:02 +00:00
}, packet, memory.Memory);
2022-04-27 20:09:50 +00:00
}
catch (Exception e) {
2022-04-28 03:32:52 +00:00
Logger.Error($"Failed to serialize {packetAttribute.Type}");
Logger.Error(e);
2022-04-27 20:09:50 +00:00
}
2022-04-27 19:43:03 +00:00
await Socket!.SendAsync(memory.Memory[..(Constants.HeaderSize + packet.Size)], SocketFlags.None);
memory.Dispose();
}
2022-04-27 20:02:26 +00:00
public async Task Send(Memory<byte> data, Client? sender) {
PacketHeader header = new PacketHeader();
header.Deserialize(data.Span);
if (!Connected && !Ignored && header.Type != PacketType.Connect) {
2022-04-27 05:58:43 +00:00
Server.Logger.Error($"Didn't send {header.Type} to {Id} because they weren't connected yet");
2022-02-10 08:42:35 +00:00
return;
}
// don't send most packets to ignored players
if (Ignored && header.Type != PacketType.Init && header.Type != PacketType.ChangeStage) {
return;
}
2022-04-27 05:58:43 +00:00
await Socket!.SendAsync(data[..(Constants.HeaderSize + header.PacketSize)], SocketFlags.None);
2021-11-29 04:04:34 +00:00
}
public void CleanMetadataOnNewConnection() {
object? tmp;
Metadata.TryRemove("time", out tmp);
Metadata.TryRemove("seeking", out tmp);
Metadata.TryRemove("lastCostumePacket", out tmp);
Metadata.TryRemove("lastCapturePacket", out tmp);
Metadata.TryRemove("lastGamePacket", out tmp);
Metadata.TryRemove("lastPlayerPacket", out tmp);
}
public TagPacket? GetTagPacket() {
var time = (Time?) (this.Metadata.ContainsKey("time") ? this.Metadata["time"] : null);
var seek = (bool?) (this.Metadata.ContainsKey("seeking") ? this.Metadata["seeking"] : null);
if (time == null && seek == null) { return null; }
return new TagPacket {
UpdateType = (seek != null ? TagPacket.TagUpdate.State : 0) | (time != null ? TagPacket.TagUpdate.Time: 0),
IsIt = seek ?? false,
Seconds = (byte) (time?.Seconds ?? 0),
Minutes = (ushort) (time?.Minutes ?? 0),
};
}
2022-02-10 01:44:50 +00:00
public static bool operator ==(Client? left, Client? right) {
return left is { } leftClient && right is { } rightClient && leftClient.Id == rightClient.Id;
2021-11-29 04:04:34 +00:00
}
2022-02-10 01:44:50 +00:00
public static bool operator !=(Client? left, Client? right) {
return !(left == right);
}
public override bool Equals(object? obj) {
if (obj is Client)
return this == (Client)obj;
else
return false;
}
public override int GetHashCode() {
return Id.GetHashCode(); //relies upon same info as == operator.
}
}