sharp-chat/SharpChat/PacketHandlers/AuthHandler.cs

165 lines
6.3 KiB
C#
Raw Normal View History

2023-02-16 21:16:06 +00:00
using SharpChat.Config;
using SharpChat.Misuzu;
using SharpChat.Packet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace SharpChat.PacketHandlers {
public class AuthHandler : IPacketHandler {
2023-02-16 21:16:06 +00:00
private readonly MisuzuClient Misuzu;
private readonly ChannelInfo DefaultChannel;
2023-02-16 21:16:06 +00:00
private readonly CachedValue<int> MaxMessageLength;
private readonly CachedValue<int> MaxConnections;
public AuthHandler(
MisuzuClient msz,
ChannelInfo defaultChannel,
2023-02-16 21:16:06 +00:00
CachedValue<int> maxMsgLength,
CachedValue<int> maxConns
) {
Misuzu = msz;
DefaultChannel = defaultChannel;
MaxMessageLength = maxMsgLength;
MaxConnections = maxConns;
2023-02-16 21:16:06 +00:00
}
public bool IsMatch(PacketHandlerContext ctx) {
2023-02-16 21:16:06 +00:00
return ctx.CheckPacketId("1");
}
public void Handle(PacketHandlerContext ctx) {
2023-02-16 21:16:06 +00:00
string[] args = ctx.SplitText(3);
2024-05-10 19:18:55 +00:00
string? authMethod = args.ElementAtOrDefault(1);
2023-02-16 21:16:06 +00:00
if(string.IsNullOrWhiteSpace(authMethod)) {
ctx.Connection.Send(new AuthFailPacket(AuthFailPacket.FailReason.AuthInvalid));
2023-02-16 21:25:41 +00:00
ctx.Connection.Dispose();
2023-02-16 21:16:06 +00:00
return;
}
2024-05-10 19:18:55 +00:00
string? authToken = args.ElementAtOrDefault(2);
2023-02-16 21:16:06 +00:00
if(string.IsNullOrWhiteSpace(authToken)) {
ctx.Connection.Send(new AuthFailPacket(AuthFailPacket.FailReason.AuthInvalid));
2023-02-16 21:25:41 +00:00
ctx.Connection.Dispose();
2023-02-16 21:16:06 +00:00
return;
}
if(authMethod.All(c => c is >= '0' and <= '9') && authToken.Contains(':')) {
string[] tokenParts = authToken.Split(':', 2);
authMethod = tokenParts[0];
authToken = tokenParts[1];
}
Task.Run(async () => {
2024-05-10 19:18:55 +00:00
MisuzuAuthInfo? fai;
2023-02-16 21:25:41 +00:00
string ipAddr = ctx.Connection.RemoteAddress.ToString();
2023-02-16 21:16:06 +00:00
try {
fai = await Misuzu.AuthVerifyAsync(authMethod, authToken, ipAddr);
} catch(Exception ex) {
2023-02-16 21:25:41 +00:00
Logger.Write($"<{ctx.Connection.Id}> Failed to authenticate: {ex}");
ctx.Connection.Send(new AuthFailPacket(AuthFailPacket.FailReason.AuthInvalid));
2023-02-16 21:25:41 +00:00
ctx.Connection.Dispose();
2023-02-16 21:16:06 +00:00
#if DEBUG
throw;
#else
return;
#endif
}
2024-05-10 19:18:55 +00:00
if(fai == null) {
Logger.Debug($"<{ctx.Connection.Id}> Auth fail: <null>");
ctx.Connection.Send(new AuthFailPacket(AuthFailPacket.FailReason.Null));
2024-05-10 19:18:55 +00:00
ctx.Connection.Dispose();
return;
}
2023-02-16 21:16:06 +00:00
if(!fai.Success) {
2023-02-16 21:25:41 +00:00
Logger.Debug($"<{ctx.Connection.Id}> Auth fail: {fai.Reason}");
ctx.Connection.Send(new AuthFailPacket(AuthFailPacket.FailReason.AuthInvalid));
2023-02-16 21:25:41 +00:00
ctx.Connection.Dispose();
2023-02-16 21:16:06 +00:00
return;
}
2024-05-10 19:18:55 +00:00
MisuzuBanInfo? fbi;
2023-02-16 21:16:06 +00:00
try {
fbi = await Misuzu.CheckBanAsync(fai.UserId.ToString(), ipAddr);
} catch(Exception ex) {
2023-02-16 21:25:41 +00:00
Logger.Write($"<{ctx.Connection.Id}> Failed auth ban check: {ex}");
ctx.Connection.Send(new AuthFailPacket(AuthFailPacket.FailReason.AuthInvalid));
2023-02-16 21:25:41 +00:00
ctx.Connection.Dispose();
2023-02-16 21:16:06 +00:00
#if DEBUG
throw;
#else
return;
2023-02-16 21:16:06 +00:00
#endif
}
2024-05-10 19:18:55 +00:00
if(fbi == null) {
Logger.Debug($"<{ctx.Connection.Id}> Ban check fail: <null>");
ctx.Connection.Send(new AuthFailPacket(AuthFailPacket.FailReason.Null));
2024-05-10 19:18:55 +00:00
ctx.Connection.Dispose();
return;
}
2023-02-16 21:16:06 +00:00
if(fbi.IsBanned && !fbi.HasExpired) {
2023-02-16 21:25:41 +00:00
Logger.Write($"<{ctx.Connection.Id}> User is banned.");
ctx.Connection.Send(new AuthFailPacket(fbi.ExpiresAt));
2023-02-16 21:25:41 +00:00
ctx.Connection.Dispose();
2023-02-16 21:16:06 +00:00
return;
}
await ctx.Chat.ContextAccess.WaitAsync();
try {
UserInfo? user = ctx.Chat.Users.Values.FirstOrDefault(u => u.UserId == fai.UserId);
if(user == null)
user = new UserInfo(
fai.UserId,
fai.UserName ?? string.Empty,
fai.Colour,
fai.Rank,
2023-11-07 14:49:12 +00:00
fai.Permissions,
isSuper: fai.IsSuper
);
else
ctx.Chat.UpdateUser(
user,
userName: fai.UserName,
colour: fai.Colour,
rank: fai.Rank,
2023-11-07 14:49:12 +00:00
perms: fai.Permissions,
isSuper: fai.IsSuper
);
// Enforce a maximum amount of connections per user
if(ctx.Chat.Connections.Count(conn => conn.User == user) >= MaxConnections) {
ctx.Connection.Send(new AuthFailPacket(AuthFailPacket.FailReason.MaxSessions));
ctx.Connection.Dispose();
return;
}
ctx.Connection.BumpPing();
ctx.Connection.User = user;
ctx.Connection.Send(new MOTDPacket($"Welcome to Flashii Chat, {user.UserName}!"));
if(File.Exists("welcome.txt")) {
IEnumerable<string> lines = File.ReadAllLines("welcome.txt").Where(x => !string.IsNullOrWhiteSpace(x));
2024-05-10 19:18:55 +00:00
string? line = lines.ElementAtOrDefault(RNG.Next(lines.Count()));
if(!string.IsNullOrWhiteSpace(line))
ctx.Connection.Send(new MOTDPacket(line));
}
ctx.Chat.HandleJoin(user, DefaultChannel, ctx.Connection, MaxMessageLength);
} finally {
ctx.Chat.ContextAccess.Release();
2023-02-16 21:16:06 +00:00
}
}).Wait();
}
}
}