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 : 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)) {
|
2023-02-16 21:25:41 +00:00
|
|
|
|
ctx.Connection.Send(new AuthFailPacket(AuthFailReason.AuthInvalid));
|
|
|
|
|
ctx.Connection.Dispose();
|
2023-02-16 21:16:06 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string authToken = args.ElementAtOrDefault(2);
|
|
|
|
|
if(string.IsNullOrWhiteSpace(authToken)) {
|
2023-02-16 21:25:41 +00:00
|
|
|
|
ctx.Connection.Send(new AuthFailPacket(AuthFailReason.AuthInvalid));
|
|
|
|
|
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 () => {
|
|
|
|
|
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(AuthFailReason.AuthInvalid));
|
|
|
|
|
ctx.Connection.Dispose();
|
2023-02-16 21:16:06 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
throw;
|
|
|
|
|
#else
|
|
|
|
|
return;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(AuthFailReason.AuthInvalid));
|
|
|
|
|
ctx.Connection.Dispose();
|
2023-02-16 21:16:06 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MisuzuBanInfo fbi;
|
|
|
|
|
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(AuthFailReason.AuthInvalid));
|
|
|
|
|
ctx.Connection.Dispose();
|
2023-02-16 21:16:06 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
throw;
|
|
|
|
|
#else
|
|
|
|
|
return;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(AuthFailReason.Banned, fbi));
|
|
|
|
|
ctx.Connection.Dispose();
|
2023-02-16 21:16:06 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lock(ctx.Chat.UsersAccess) {
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ChatUser user = ctx.Chat.Users.FirstOrDefault(u => u.UserId == fai.UserId);
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
2023-02-16 22:33:48 +00:00
|
|
|
|
if(user == null)
|
|
|
|
|
user = new ChatUser(fai);
|
2023-02-16 21:16:06 +00:00
|
|
|
|
else {
|
2023-02-16 22:33:48 +00:00
|
|
|
|
user.ApplyAuth(fai);
|
2023-02-17 19:02:35 +00:00
|
|
|
|
if(user.CurrentChannel != null)
|
|
|
|
|
ctx.Chat.SendTo(user.CurrentChannel, new UserUpdatePacket(user));
|
2023-02-16 21:16:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Enforce a maximum amount of connections per user
|
2023-02-16 22:33:48 +00:00
|
|
|
|
lock(ctx.Chat.ConnectionsAccess)
|
|
|
|
|
if(ctx.Chat.Connections.Count(conn => conn.User == user) >= MaxConnections) {
|
|
|
|
|
ctx.Connection.Send(new AuthFailPacket(AuthFailReason.MaxSessions));
|
|
|
|
|
ctx.Connection.Dispose();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
2023-02-16 21:25:41 +00:00
|
|
|
|
ctx.Connection.BumpPing();
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Connection.User = user;
|
|
|
|
|
ctx.Connection.Send(new LegacyCommandResponse(LCR.WELCOME, false, $"Welcome to Flashii Chat, {user.Username}!"));
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
|
|
|
|
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))
|
2023-02-16 21:25:41 +00:00
|
|
|
|
ctx.Connection.Send(new LegacyCommandResponse(LCR.WELCOME, false, line));
|
2023-02-16 21:16:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.HandleJoin(user, DefaultChannel, ctx.Connection, MaxMessageLength);
|
2023-02-16 21:16:06 +00:00
|
|
|
|
}
|
|
|
|
|
}).Wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|