138 lines
5.3 KiB
C#
138 lines
5.3 KiB
C#
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 : IChatPacketHandler {
|
|
private readonly MisuzuClient Misuzu;
|
|
private readonly ChatChannel DefaultChannel;
|
|
private readonly CachedValue<int> MaxMessageLength;
|
|
private readonly CachedValue<int> MaxConnections;
|
|
|
|
public AuthHandler(
|
|
MisuzuClient msz,
|
|
ChatChannel defaultChannel,
|
|
CachedValue<int> maxMsgLength,
|
|
CachedValue<int> maxConns
|
|
) {
|
|
Misuzu = msz ?? throw new ArgumentNullException(nameof(msz));
|
|
DefaultChannel = defaultChannel ?? throw new ArgumentNullException(nameof(defaultChannel));
|
|
MaxMessageLength = maxMsgLength ?? throw new ArgumentNullException(nameof(maxMsgLength));
|
|
MaxConnections = maxConns ?? throw new ArgumentNullException(nameof(maxConns));
|
|
}
|
|
|
|
public bool IsMatch(ChatPacketHandlerContext ctx) {
|
|
return ctx.CheckPacketId("1");
|
|
}
|
|
|
|
public void Handle(ChatPacketHandlerContext ctx) {
|
|
string[] args = ctx.SplitText(3);
|
|
|
|
string authMethod = args.ElementAtOrDefault(1);
|
|
if(string.IsNullOrWhiteSpace(authMethod)) {
|
|
ctx.Session.Send(new AuthFailPacket(AuthFailReason.AuthInvalid));
|
|
ctx.Session.Dispose();
|
|
return;
|
|
}
|
|
|
|
string authToken = args.ElementAtOrDefault(2);
|
|
if(string.IsNullOrWhiteSpace(authToken)) {
|
|
ctx.Session.Send(new AuthFailPacket(AuthFailReason.AuthInvalid));
|
|
ctx.Session.Dispose();
|
|
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 () => {
|
|
MisuzuAuthInfo fai;
|
|
string ipAddr = ctx.Session.RemoteAddress.ToString();
|
|
|
|
try {
|
|
fai = await Misuzu.AuthVerifyAsync(authMethod, authToken, ipAddr);
|
|
} catch(Exception ex) {
|
|
Logger.Write($"<{ctx.Session.Id}> Failed to authenticate: {ex}");
|
|
ctx.Session.Send(new AuthFailPacket(AuthFailReason.AuthInvalid));
|
|
ctx.Session.Dispose();
|
|
#if DEBUG
|
|
throw;
|
|
#else
|
|
return;
|
|
#endif
|
|
}
|
|
|
|
if(!fai.Success) {
|
|
Logger.Debug($"<{ctx.Session.Id}> Auth fail: {fai.Reason}");
|
|
ctx.Session.Send(new AuthFailPacket(AuthFailReason.AuthInvalid));
|
|
ctx.Session.Dispose();
|
|
return;
|
|
}
|
|
|
|
MisuzuBanInfo fbi;
|
|
try {
|
|
fbi = await Misuzu.CheckBanAsync(fai.UserId.ToString(), ipAddr);
|
|
} catch(Exception ex) {
|
|
Logger.Write($"<{ctx.Session.Id}> Failed auth ban check: {ex}");
|
|
ctx.Session.Send(new AuthFailPacket(AuthFailReason.AuthInvalid));
|
|
ctx.Session.Dispose();
|
|
#if DEBUG
|
|
throw;
|
|
#else
|
|
return;
|
|
#endif
|
|
}
|
|
|
|
if(fbi.IsBanned && !fbi.HasExpired) {
|
|
Logger.Write($"<{ctx.Session.Id}> User is banned.");
|
|
ctx.Session.Send(new AuthFailPacket(AuthFailReason.Banned, fbi));
|
|
ctx.Session.Dispose();
|
|
return;
|
|
}
|
|
|
|
lock(ctx.Chat.UsersAccess) {
|
|
ChatUser aUser = ctx.Chat.Users.FirstOrDefault(u => u.UserId == fai.UserId);
|
|
|
|
if(aUser == null)
|
|
aUser = new ChatUser(fai);
|
|
else {
|
|
aUser.ApplyAuth(fai);
|
|
aUser.Channel?.Send(new UserUpdatePacket(aUser));
|
|
}
|
|
|
|
// Enforce a maximum amount of connections per user
|
|
if(aUser.SessionCount >= MaxConnections) {
|
|
ctx.Session.Send(new AuthFailPacket(AuthFailReason.MaxSessions));
|
|
ctx.Session.Dispose();
|
|
return;
|
|
}
|
|
|
|
// Bumping the ping to prevent upgrading
|
|
ctx.Session.BumpPing();
|
|
|
|
aUser.AddSession(ctx.Session);
|
|
|
|
ctx.Session.Send(new LegacyCommandResponse(LCR.WELCOME, false, $"Welcome to Flashii Chat, {aUser.Username}!"));
|
|
|
|
if(File.Exists("welcome.txt")) {
|
|
IEnumerable<string> lines = File.ReadAllLines("welcome.txt").Where(x => !string.IsNullOrWhiteSpace(x));
|
|
string line = lines.ElementAtOrDefault(RNG.Next(lines.Count()));
|
|
|
|
if(!string.IsNullOrWhiteSpace(line))
|
|
ctx.Session.Send(new LegacyCommandResponse(LCR.WELCOME, false, line));
|
|
}
|
|
|
|
ctx.Chat.HandleJoin(aUser, DefaultChannel, ctx.Session, MaxMessageLength);
|
|
}
|
|
}).Wait();
|
|
}
|
|
}
|
|
}
|