2022-08-30 15:00:58 +00:00
|
|
|
|
using Fleck;
|
|
|
|
|
using SharpChat.Commands;
|
2023-02-08 23:53:42 +00:00
|
|
|
|
using SharpChat.Config;
|
2023-02-10 06:07:59 +00:00
|
|
|
|
using SharpChat.EventStorage;
|
2023-02-08 23:53:42 +00:00
|
|
|
|
using SharpChat.Misuzu;
|
2024-05-20 23:00:47 +00:00
|
|
|
|
using SharpChat.PacketsS2C;
|
|
|
|
|
using SharpChat.PacketsC2S;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2022-08-30 15:21:00 +00:00
|
|
|
|
using System.Net.Http;
|
2022-08-30 18:29:11 +00:00
|
|
|
|
using System.Threading;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
|
|
|
|
namespace SharpChat {
|
|
|
|
|
public class SockChatServer : IDisposable {
|
2023-02-08 23:53:42 +00:00
|
|
|
|
public const ushort DEFAULT_PORT = 6770;
|
|
|
|
|
public const int DEFAULT_MSG_LENGTH_MAX = 5000;
|
|
|
|
|
public const int DEFAULT_MAX_CONNECTIONS = 5;
|
|
|
|
|
public const int DEFAULT_FLOOD_KICK_LENGTH = 30;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
|
|
|
|
public IWebSocketServer Server { get; }
|
|
|
|
|
public ChatContext Context { get; }
|
|
|
|
|
|
2023-02-06 20:14:50 +00:00
|
|
|
|
private readonly HttpClient HttpClient;
|
2023-02-08 23:53:42 +00:00
|
|
|
|
private readonly MisuzuClient Misuzu;
|
|
|
|
|
|
|
|
|
|
private readonly CachedValue<int> MaxMessageLength;
|
|
|
|
|
private readonly CachedValue<int> MaxConnections;
|
|
|
|
|
private readonly CachedValue<int> FloodKickLength;
|
2022-08-30 15:21:00 +00:00
|
|
|
|
|
2024-05-20 23:00:47 +00:00
|
|
|
|
private readonly List<IC2SPacketHandler> GuestHandlers = new();
|
|
|
|
|
private readonly List<IC2SPacketHandler> AuthedHandlers = new();
|
|
|
|
|
private readonly SendMessageC2SPacketHandler SendMessageHandler;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2022-08-30 18:29:11 +00:00
|
|
|
|
private bool IsShuttingDown = false;
|
2024-05-20 16:16:32 +00:00
|
|
|
|
private bool IsRestarting = false;
|
2022-08-30 18:29:11 +00:00
|
|
|
|
|
2023-02-10 06:07:59 +00:00
|
|
|
|
public SockChatServer(HttpClient httpClient, MisuzuClient msz, IEventStorage evtStore, IConfig config) {
|
2023-02-08 23:53:42 +00:00
|
|
|
|
Logger.Write("Initialising Sock Chat server...");
|
|
|
|
|
|
2024-05-20 02:27:41 +00:00
|
|
|
|
DateTimeOffset started = DateTimeOffset.UtcNow;
|
2024-05-19 01:53:14 +00:00
|
|
|
|
HttpClient = httpClient;
|
|
|
|
|
Misuzu = msz;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2023-02-08 23:53:42 +00:00
|
|
|
|
MaxMessageLength = config.ReadCached("msgMaxLength", DEFAULT_MSG_LENGTH_MAX);
|
|
|
|
|
MaxConnections = config.ReadCached("connMaxCount", DEFAULT_MAX_CONNECTIONS);
|
|
|
|
|
FloodKickLength = config.ReadCached("floodKickLength", DEFAULT_FLOOD_KICK_LENGTH);
|
2022-08-30 18:29:11 +00:00
|
|
|
|
|
2023-02-10 06:07:59 +00:00
|
|
|
|
Context = new ChatContext(evtStore);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
string[]? channelNames = config.ReadValue("channels", new[] { "lounge" });
|
2023-02-08 23:53:42 +00:00
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
if(channelNames != null)
|
|
|
|
|
foreach(string channelName in channelNames) {
|
|
|
|
|
IConfig channelCfg = config.ScopeTo($"channels:{channelName}");
|
2023-02-08 23:53:42 +00:00
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
string? name = channelCfg.SafeReadValue("name", string.Empty);
|
|
|
|
|
if(string.IsNullOrWhiteSpace(name))
|
|
|
|
|
name = channelName;
|
2023-02-08 23:53:42 +00:00
|
|
|
|
|
2024-05-19 02:17:51 +00:00
|
|
|
|
ChannelInfo channelInfo = new(
|
2024-05-10 19:18:55 +00:00
|
|
|
|
name,
|
|
|
|
|
channelCfg.SafeReadValue("password", string.Empty),
|
|
|
|
|
rank: channelCfg.SafeReadValue("minRank", 0)
|
|
|
|
|
);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2024-05-19 21:02:17 +00:00
|
|
|
|
Context.Channels.Add(channelInfo);
|
2024-05-10 19:18:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-19 21:02:17 +00:00
|
|
|
|
if(Context.Channels.PublicCount < 1)
|
|
|
|
|
Context.Channels.Add(new ChannelInfo("Default"));
|
2023-02-08 23:53:42 +00:00
|
|
|
|
|
2024-05-20 23:00:47 +00:00
|
|
|
|
GuestHandlers.Add(new AuthC2SPacketHandler(
|
2024-05-20 02:27:41 +00:00
|
|
|
|
started,
|
|
|
|
|
Misuzu,
|
|
|
|
|
Context.Channels.MainChannel,
|
|
|
|
|
MaxMessageLength,
|
|
|
|
|
MaxConnections
|
|
|
|
|
));
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
2024-05-20 23:00:47 +00:00
|
|
|
|
AuthedHandlers.AddRange(new IC2SPacketHandler[] {
|
|
|
|
|
new PingC2SPacketHandler(Misuzu),
|
|
|
|
|
SendMessageHandler = new SendMessageC2SPacketHandler(MaxMessageLength),
|
2023-02-16 21:16:06 +00:00
|
|
|
|
});
|
|
|
|
|
|
2024-05-20 23:00:47 +00:00
|
|
|
|
SendMessageHandler.AddCommands(new ISockChatClientCommand[] {
|
2024-05-14 22:17:25 +00:00
|
|
|
|
new UserAFKCommand(),
|
|
|
|
|
new UserNickCommand(),
|
|
|
|
|
new MessageWhisperCommand(),
|
|
|
|
|
new MessageActionCommand(),
|
2023-02-16 20:34:59 +00:00
|
|
|
|
new WhoCommand(),
|
2024-05-14 22:17:25 +00:00
|
|
|
|
new ChannelJoinCommand(),
|
|
|
|
|
new ChannelCreateCommand(),
|
|
|
|
|
new ChannelDeleteCommand(),
|
|
|
|
|
new ChannelPasswordCommand(),
|
|
|
|
|
new ChannelRankCommand(),
|
|
|
|
|
new MessageBroadcastCommand(),
|
|
|
|
|
new MessageDeleteCommand(),
|
2023-02-16 20:34:59 +00:00
|
|
|
|
new KickBanCommand(msz),
|
|
|
|
|
new PardonUserCommand(msz),
|
|
|
|
|
new PardonAddressCommand(msz),
|
|
|
|
|
new BanListCommand(msz),
|
2024-05-14 19:11:09 +00:00
|
|
|
|
new WhoisCommand(),
|
2023-02-16 20:34:59 +00:00
|
|
|
|
});
|
|
|
|
|
|
2023-02-08 23:53:42 +00:00
|
|
|
|
ushort port = config.SafeReadValue("port", DEFAULT_PORT);
|
2023-02-08 03:17:07 +00:00
|
|
|
|
Server = new SharpChatWebSocketServer($"ws://0.0.0.0:{port}");
|
2023-02-06 20:14:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 20:34:59 +00:00
|
|
|
|
public void Listen(ManualResetEvent waitHandle) {
|
|
|
|
|
if(waitHandle != null)
|
2024-05-20 16:16:32 +00:00
|
|
|
|
SendMessageHandler.AddCommand(new ShutdownRestartCommand(
|
|
|
|
|
waitHandle,
|
|
|
|
|
() => IsShuttingDown,
|
|
|
|
|
restarting => {
|
|
|
|
|
IsShuttingDown = true;
|
|
|
|
|
IsRestarting = restarting;
|
|
|
|
|
}
|
|
|
|
|
));
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
|
|
|
|
Server.Start(sock => {
|
2023-02-19 22:27:08 +00:00
|
|
|
|
if(IsShuttingDown) {
|
2022-08-30 18:29:11 +00:00
|
|
|
|
sock.Close(1013);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-19 02:17:51 +00:00
|
|
|
|
ConnectionInfo conn = new(sock);
|
2023-02-19 22:27:08 +00:00
|
|
|
|
Context.Connections.Add(conn);
|
2023-02-17 22:17:24 +00:00
|
|
|
|
|
|
|
|
|
sock.OnOpen = () => OnOpen(conn);
|
|
|
|
|
sock.OnClose = () => OnClose(conn);
|
|
|
|
|
sock.OnError = err => OnError(conn, err);
|
|
|
|
|
sock.OnMessage = msg => OnMessage(conn, msg);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
});
|
2023-02-08 23:53:42 +00:00
|
|
|
|
|
|
|
|
|
Logger.Write("Listening...");
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-19 02:17:51 +00:00
|
|
|
|
private void OnOpen(ConnectionInfo conn) {
|
2024-05-20 16:16:32 +00:00
|
|
|
|
Logger.Write($"Connection opened from {conn.RemoteEndPoint}");
|
2023-02-19 22:27:08 +00:00
|
|
|
|
Context.SafeUpdate();
|
|
|
|
|
}
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2024-05-19 02:17:51 +00:00
|
|
|
|
private void OnError(ConnectionInfo conn, Exception ex) {
|
2024-05-20 16:16:32 +00:00
|
|
|
|
Logger.Write($"<{conn.RemoteEndPoint}> {ex}");
|
2023-02-19 22:27:08 +00:00
|
|
|
|
Context.SafeUpdate();
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-19 02:17:51 +00:00
|
|
|
|
private void OnClose(ConnectionInfo conn) {
|
2024-05-20 16:16:32 +00:00
|
|
|
|
Logger.Write($"Connection closed from {conn.RemoteEndPoint}");
|
2023-02-10 06:07:59 +00:00
|
|
|
|
|
2023-02-19 22:27:08 +00:00
|
|
|
|
Context.ContextAccess.Wait();
|
|
|
|
|
try {
|
2023-02-16 22:33:48 +00:00
|
|
|
|
Context.Connections.Remove(conn);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2024-05-20 16:16:32 +00:00
|
|
|
|
if(!Context.Connections.HasUser(conn.UserId)) {
|
|
|
|
|
UserInfo? userInfo = Context.Users.Get(conn.UserId);
|
|
|
|
|
if(userInfo != null)
|
|
|
|
|
Context.HandleDisconnect(userInfo);
|
|
|
|
|
}
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2023-02-19 22:27:08 +00:00
|
|
|
|
Context.Update();
|
|
|
|
|
} finally {
|
|
|
|
|
Context.ContextAccess.Release();
|
|
|
|
|
}
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-19 02:17:51 +00:00
|
|
|
|
private void OnMessage(ConnectionInfo conn, string msg) {
|
2023-02-19 22:27:08 +00:00
|
|
|
|
Context.SafeUpdate();
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2023-02-16 21:16:06 +00:00
|
|
|
|
// this doesn't affect non-authed connections?????
|
2024-05-20 16:16:32 +00:00
|
|
|
|
if(conn.UserId > 0) {
|
|
|
|
|
long banUserId = 0;
|
2023-02-19 22:27:08 +00:00
|
|
|
|
string banAddr = string.Empty;
|
|
|
|
|
TimeSpan banDuration = TimeSpan.MinValue;
|
|
|
|
|
|
|
|
|
|
Context.ContextAccess.Wait();
|
|
|
|
|
try {
|
2024-05-20 16:16:32 +00:00
|
|
|
|
if(!Context.UserRateLimiters.TryGetValue(conn.UserId, out RateLimiter? rateLimiter))
|
|
|
|
|
Context.UserRateLimiters.Add(conn.UserId, rateLimiter = new RateLimiter(
|
2024-05-19 02:17:51 +00:00
|
|
|
|
UserInfo.DEFAULT_SIZE,
|
|
|
|
|
UserInfo.DEFAULT_MINIMUM_DELAY,
|
|
|
|
|
UserInfo.DEFAULT_RISKY_OFFSET
|
2023-02-19 22:27:08 +00:00
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
rateLimiter.Update();
|
|
|
|
|
|
|
|
|
|
if(rateLimiter.IsExceeded) {
|
|
|
|
|
banDuration = TimeSpan.FromSeconds(FloodKickLength);
|
2024-05-20 16:16:32 +00:00
|
|
|
|
banUserId = conn.UserId;
|
|
|
|
|
banAddr = conn.RemoteAddress;
|
2023-02-19 22:27:08 +00:00
|
|
|
|
} else if(rateLimiter.IsRisky) {
|
2024-05-20 16:16:32 +00:00
|
|
|
|
banUserId = conn.UserId;
|
2023-02-19 22:27:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 16:16:32 +00:00
|
|
|
|
if(banUserId > 0) {
|
|
|
|
|
UserInfo? userInfo = Context.Users.Get(banUserId);
|
|
|
|
|
if(userInfo != null) {
|
|
|
|
|
if(banDuration == TimeSpan.MinValue) {
|
2024-05-20 23:00:47 +00:00
|
|
|
|
Context.SendTo(userInfo, new FloodWarningS2CPacket());
|
2024-05-20 16:16:32 +00:00
|
|
|
|
} else {
|
|
|
|
|
Context.BanUser(userInfo, banDuration, UserDisconnectReason.Flood);
|
|
|
|
|
|
|
|
|
|
if(banDuration > TimeSpan.Zero)
|
|
|
|
|
Misuzu.CreateBanAsync(
|
|
|
|
|
banUserId.ToString(), conn.RemoteAddress,
|
|
|
|
|
string.Empty, "::1",
|
|
|
|
|
banDuration,
|
|
|
|
|
"Kicked from chat for flood protection."
|
|
|
|
|
).Wait();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-19 22:27:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
Context.ContextAccess.Release();
|
|
|
|
|
}
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 23:00:47 +00:00
|
|
|
|
C2SPacketHandlerContext context = new(msg, Context, conn);
|
|
|
|
|
IC2SPacketHandler? handler = conn.UserId > 0
|
2024-05-20 16:16:32 +00:00
|
|
|
|
? AuthedHandlers.FirstOrDefault(h => h.IsMatch(context))
|
|
|
|
|
: GuestHandlers.FirstOrDefault(h => h.IsMatch(context));
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2023-02-19 22:47:53 +00:00
|
|
|
|
handler?.Handle(context);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 22:27:08 +00:00
|
|
|
|
private bool IsDisposed;
|
|
|
|
|
|
2023-02-06 20:14:50 +00:00
|
|
|
|
~SockChatServer() {
|
|
|
|
|
DoDispose();
|
|
|
|
|
}
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2023-02-06 20:14:50 +00:00
|
|
|
|
public void Dispose() {
|
|
|
|
|
DoDispose();
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2023-02-06 20:14:50 +00:00
|
|
|
|
private void DoDispose() {
|
2022-08-30 15:00:58 +00:00
|
|
|
|
if(IsDisposed)
|
|
|
|
|
return;
|
|
|
|
|
IsDisposed = true;
|
2023-02-19 22:27:08 +00:00
|
|
|
|
IsShuttingDown = true;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2024-05-20 16:16:32 +00:00
|
|
|
|
Context.Connections.WithAll(conn => conn.Close(IsRestarting ? 1012 : 1001));
|
2022-08-30 18:29:11 +00:00
|
|
|
|
|
2022-08-30 15:00:58 +00:00
|
|
|
|
Server?.Dispose();
|
2022-08-30 15:21:00 +00:00
|
|
|
|
HttpClient?.Dispose();
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|