Skip to content

Commit

Permalink
Merge branch 'upstream' into json-api
Browse files Browse the repository at this point in the history
Conflicts:
	Server/Client.cs
	Server/Server.cs
	Server/Settings.cs
  • Loading branch information
Istador committed Apr 27, 2024
2 parents ecc1337 + 497b5b4 commit a1db092
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 73 deletions.
14 changes: 6 additions & 8 deletions Server/BanLists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,30 +148,27 @@ private static void Save() {

public static void Crash(
Client user,
bool permanent = false,
bool dispose_user = true,
int delay_ms = 0
int delay_ms = 0
) {
user.Ignored = true;
Task.Run(async () => {
if (delay_ms > 0) {
await Task.Delay(delay_ms);
}
bool permanent = user.Banned;
await user.Send(new ChangeStagePacket {
Id = (permanent ? "$agogus/ban4lyfe" : "$among$us/cr4sh%"),
Stage = (permanent ? "$ejected" : "$agogusStage"),
Scenario = (sbyte) (permanent ? 69 : 21),
SubScenarioType = (byte) (permanent ? 21 : 69),
});
if (dispose_user) {
user.Dispose();
}
});
}

private static void CrashMultiple(string[] args, MUCH much) {
foreach (Client user in much(args).toActUpon) {
Crash(user, true);
user.Banned = true;
Crash(user);
}
}

Expand Down Expand Up @@ -245,8 +242,9 @@ public static string HandleBanCommand(string[] args, MUCH much) {
}

foreach (Client user in res.toActUpon) {
user.Banned = true;
BanClient(user);
Crash(user, true);
Crash(user);
}

Save();
Expand Down
25 changes: 19 additions & 6 deletions Server/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Client : IDisposable {
public readonly ConcurrentDictionary<string, object?> Metadata = new ConcurrentDictionary<string, object?>(); // can be used to store any information about a player
public bool Connected = false;
public bool Ignored = false;
public bool Banned = false;
public CostumePacket? CurrentCostume = null; // required for proper client sync
public string Name {
get => Logger.Name;
Expand Down Expand Up @@ -41,8 +42,9 @@ public Client(Client other, Socket socket) {
}

public void Dispose() {
if (Socket?.Connected is true)
if (Socket?.Connected is true) {
Socket.Disconnect(false);
}
}


Expand All @@ -51,9 +53,14 @@ public async Task Send<T>(T packet, Client? sender = null) where T : struct, IPa

PacketAttribute packetAttribute = Constants.PacketMap[typeof(T)];
try {
// don't send most packets to ignored players
if (Ignored && packetAttribute.Type != PacketType.Init && packetAttribute.Type != PacketType.ChangeStage) {
memory.Dispose();
return;
}
Server.FillPacket(new PacketHeader {
Id = sender?.Id ?? Id,
Type = packetAttribute.Type,
Id = sender?.Id ?? Id,
Type = packetAttribute.Type,
PacketSize = packet.Size
}, packet, memory.Memory);
}
Expand All @@ -69,11 +76,17 @@ public async Task Send<T>(T packet, Client? sender = null) where T : struct, IPa
public async Task Send(Memory<byte> data, Client? sender) {
PacketHeader header = new PacketHeader();
header.Deserialize(data.Span);
if (!Connected && header.Type is not PacketType.Connect) {

if (!Connected && !Ignored && header.Type != PacketType.Connect) {
Server.Logger.Error($"Didn't send {header.Type} to {Id} because they weren't connected yet");
return;
}

// don't send most packets to ignored players
if (Ignored && header.Type != PacketType.Init && header.Type != PacketType.ChangeStage) {
return;
}

await Socket!.SendAsync(data[..(Constants.HeaderSize + header.PacketSize)], SocketFlags.None);
}

Expand All @@ -88,8 +101,8 @@ public void CleanMetadataOnNewConnection() {
}

public TagPacket? GetTagPacket() {
var time = (Time?) this.Metadata?["time"];
var seek = (bool?) this.Metadata?["seeking"];
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),
Expand Down
19 changes: 17 additions & 2 deletions Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,20 @@ void logError(Task x) {
server.PacketHandler = (c, p) => {
switch (p) {
case GamePacket gamePacket: {
// crash ignored player
if (c.Ignored) {
c.Logger.Info($"Crashing ignored player after entering stage {gamePacket.Stage}.");
BanLists.Crash(c, 500);
return false;
}

// crash player entering a banned stage
if (BanLists.Enabled && BanLists.IsStageBanned(gamePacket.Stage)) {
c.Logger.Warn($"Crashing player for entering banned stage {gamePacket.Stage}.");
BanLists.Crash(c, false, false, 500);
BanLists.Crash(c, 500);
return false;
}

c.Logger.Info($"Got game packet {gamePacket.Stage}->{gamePacket.ScenarioNum}");

// reset lastPlayerPacket on stage changes
Expand Down Expand Up @@ -177,6 +186,11 @@ void logError(Task x) {
break;
}

// ignore all other packets from ignored players
case IPacket pack when c.Ignored: {
return false;
}

case TagPacket tagPacket: {
// c.Logger.Info($"Got tag packet: {tagPacket.IsIt}");
if ((tagPacket.UpdateType & TagPacket.TagUpdate.State) != 0) c.Metadata["seeking"] = tagPacket.IsIt;
Expand All @@ -191,7 +205,7 @@ void logError(Task x) {
break;
}

case CostumePacket costumePacket:
case CostumePacket costumePacket: {
c.Logger.Info($"Got costume packet: {costumePacket.BodyName}, {costumePacket.CapName}");
c.Metadata["lastCostumePacket"] = costumePacket;
c.CurrentCostume = costumePacket;
Expand All @@ -200,6 +214,7 @@ void logError(Task x) {
#pragma warning restore CS4014
c.Metadata["loadedSave"] = true;
break;
}

case ShinePacket shinePacket: {
if (!Settings.Instance.Shines.Enabled) return false;
Expand Down
Loading

0 comments on commit a1db092

Please sign in to comment.