From f8eeeaae8b62d0e256f08f4479ea82b2e011f6a3 Mon Sep 17 00:00:00 2001 From: linmeideli <2558527373@qq.com> Date: Tue, 18 Aug 2026 00:11:41 +0800 Subject: [PATCH] Enhanced Ban --- .../packages.lock.json | 3 +- src/Empostor.Api/Languages/LanguageService.cs | 106 +++++++++-- src/Empostor.Api/packages.lock.json | 3 +- src/Empostor.Server/Http/AdminController.cs | 8 +- .../Http/AdminTemplateDefaults.cs | 21 ++- src/Empostor.Server/Languages/de.json | 9 +- src/Empostor.Server/Languages/en.json | 10 +- src/Empostor.Server/Languages/es.json | 9 +- src/Empostor.Server/Languages/fil.json | 9 +- src/Empostor.Server/Languages/fr.json | 9 +- src/Empostor.Server/Languages/ga.json | 9 +- src/Empostor.Server/Languages/it.json | 9 +- src/Empostor.Server/Languages/ja.json | 9 +- src/Empostor.Server/Languages/ko.json | 9 +- src/Empostor.Server/Languages/nl.json | 9 +- src/Empostor.Server/Languages/pt.json | 9 +- src/Empostor.Server/Languages/pt_BR.json | 9 +- src/Empostor.Server/Languages/ru.json | 9 +- src/Empostor.Server/Languages/zh_CN.json | 9 +- src/Empostor.Server/Languages/zh_TW.json | 9 +- src/Empostor.Server/Pages/AdminStrings.json | 53 +----- .../Admin/Ban/BanEnforcementListener.cs | 30 +++- .../Service/Admin/Ban/BanStore.cs | 164 ++++++++++++++++-- 23 files changed, 403 insertions(+), 121 deletions(-) diff --git a/src/Empostor.Api.Innersloth.Generator/packages.lock.json b/src/Empostor.Api.Innersloth.Generator/packages.lock.json index fb68fad..5850ca3 100644 --- a/src/Empostor.Api.Innersloth.Generator/packages.lock.json +++ b/src/Empostor.Api.Innersloth.Generator/packages.lock.json @@ -150,6 +150,7 @@ "System.Runtime.CompilerServices.Unsafe": "4.5.3" } } - } + }, + ".NETStandard,Version=v2.0/win-x64": {} } } \ No newline at end of file diff --git a/src/Empostor.Api/Languages/LanguageService.cs b/src/Empostor.Api/Languages/LanguageService.cs index 9c70e57..dd5606a 100644 --- a/src/Empostor.Api/Languages/LanguageService.cs +++ b/src/Empostor.Api/Languages/LanguageService.cs @@ -166,7 +166,6 @@ public sealed class LanguageService "command.note.set": "Note set: {0}", "command.color.invalid": "Invalid color ID. Valid range: 0–17.", "command.color.set": "Color changed to {0} ({1}).", - "command.title.too_long": "Title too long. Max 12 characters.", "command.title.cleared": "Title removed.", "command.title.set": "Title set: {0}", @@ -205,7 +204,12 @@ public sealed class LanguageService "command.note.description": "Set or clear a lobby note (host only).", "command.note.usage": "note | note clear", "command.max.description": "Set max players for the lobby (host only).", - "command.max.usage": "max <1-15>" + "command.max.usage": "max <1-15>", + "ban.notice.header": "You are banned from this server.", + "ban.notice.reason": "Reason: {0}", + "ban.notice.unban": "Unban time: {0}", + "ban.notice.unban_permanent": "Unban time: Permanent", + "ban.notice.contact": "If you have any questions, please contact the administrator." } """; @@ -260,7 +264,12 @@ public sealed class LanguageService "command.note.description": "设置或清除房间备注(仅房主)。", "command.note.usage": "note <文字> | note clear", "command.max.description": "设置房间最大人数(仅房主)。", - "command.max.usage": "max <1-15>" + "command.max.usage": "max <1-15>", + "ban.notice.header": "您已被服务器封禁", + "ban.notice.reason": "理由:{0}", + "ban.notice.unban": "解封时间:{0}", + "ban.notice.unban_permanent": "解封时间:永久", + "ban.notice.contact": "如有问题请联系管理员" } """; @@ -315,7 +324,12 @@ public sealed class LanguageService "command.note.description": "設置或清除房間備註(僅房主)。", "command.note.usage": "note <文字> | note clear", "command.max.description": "設置房間最大人數(僅房主)。", - "command.max.usage": "max <1-15>" + "command.max.usage": "max <1-15>", + "ban.notice.header": "您已被伺服器封禁", + "ban.notice.reason": "理由:{0}", + "ban.notice.unban": "解封時間:{0}", + "ban.notice.unban_permanent": "解封時間:永久", + "ban.notice.contact": "如有問題請聯絡管理員" } """; @@ -356,7 +370,12 @@ public sealed class LanguageService "command.players.not_in_lobby": "이 명령어는 로비에서만 사용할 수 있습니다.", "command.players.header": "=== 플레이어 ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "내 핑: {0}ms" + "command.ping.result": "내 핑: {0}ms", + "ban.notice.header": "이 서버에서 차단되었습니다.", + "ban.notice.reason": "사유: {0}", + "ban.notice.unban": "차단 해제 시간: {0}", + "ban.notice.unban_permanent": "차단 해제 시간: 영구", + "ban.notice.contact": "문의 사항이 있으면 관리자에게 연락해 주세요." } """; @@ -397,7 +416,12 @@ public sealed class LanguageService "command.players.not_in_lobby": "Эта команда может использоваться только в лобби.", "command.players.header": "=== Игроки ({0}) ===", "command.players.entry": "{0} | {1} | {2}мс", - "command.ping.result": "Ваш пинг: {0}мс" + "command.ping.result": "Ваш пинг: {0}мс", + "ban.notice.header": "Вы забанены на этом сервере.", + "ban.notice.reason": "Причина: {0}", + "ban.notice.unban": "Время разбана: {0}", + "ban.notice.unban_permanent": "Время разбана: навсегда", + "ban.notice.contact": "Если у вас есть вопросы, обратитесь к администратору." } """; @@ -438,7 +462,12 @@ public sealed class LanguageService "command.players.not_in_lobby": "Dieser Befehl kann nur in der Lobby verwendet werden.", "command.players.header": "=== Spieler ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Dein Ping: {0}ms" + "command.ping.result": "Dein Ping: {0}ms", + "ban.notice.header": "Du wurdest von diesem Server gebannt.", + "ban.notice.reason": "Grund: {0}", + "ban.notice.unban": "Entbannungszeit: {0}", + "ban.notice.unban_permanent": "Entbannungszeit: Dauerhaft", + "ban.notice.contact": "Bei Fragen wende dich an den Administrator." } """; @@ -479,7 +508,12 @@ public sealed class LanguageService "command.players.not_in_lobby": "Cette commande ne peut être utilisée que dans le lobby.", "command.players.header": "=== Joueurs ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Votre ping : {0}ms" + "command.ping.result": "Votre ping : {0}ms", + "ban.notice.header": "Vous êtes banni de ce serveur.", + "ban.notice.reason": "Raison : {0}", + "ban.notice.unban": "Débannissement : {0}", + "ban.notice.unban_permanent": "Débannissement : Permanent", + "ban.notice.contact": "En cas de question, contactez un administrateur." } """; @@ -520,7 +554,12 @@ public sealed class LanguageService "command.players.not_in_lobby": "このコマンドはロビーでのみ使用できます。", "command.players.header": "=== プレイヤー ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "あなたのPing: {0}ms" + "command.ping.result": "あなたのPing: {0}ms", + "ban.notice.header": "このサーバーからBANされています。", + "ban.notice.reason": "理由:{0}", + "ban.notice.unban": "BAN解除日時:{0}", + "ban.notice.unban_permanent": "BAN解除日時:永久", + "ban.notice.contact": "ご不明な点があれば管理者にお問い合わせください。" } """; @@ -561,7 +600,12 @@ public sealed class LanguageService "command.players.not_in_lobby": "Este comando só pode ser usado no lobby.", "command.players.header": "=== Jogadores ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Seu ping: {0}ms" + "command.ping.result": "Seu ping: {0}ms", + "ban.notice.header": "Você foi banido deste servidor.", + "ban.notice.reason": "Motivo: {0}", + "ban.notice.unban": "Desbanimento: {0}", + "ban.notice.unban_permanent": "Desbanimento: Permanente", + "ban.notice.contact": "Em caso de dúvidas, contacte o administrador." } """; @@ -600,7 +644,12 @@ public sealed class LanguageService "command.players.not_in_lobby": "Este comando só pode ser usado no lobby.", "command.players.header": "=== Jogadores ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Seu ping: {0}ms" + "command.ping.result": "Seu ping: {0}ms", + "ban.notice.header": "Você foi banido deste servidor.", + "ban.notice.reason": "Motivo: {0}", + "ban.notice.unban": "Desbanimento: {0}", + "ban.notice.unban_permanent": "Desbanimento: Permanente", + "ban.notice.contact": "Em caso de dúvidas, contate o administrador." } """; @@ -639,7 +688,12 @@ public sealed class LanguageService "command.players.not_in_lobby": "Este comando solo puede usarse en el lobby.", "command.players.header": "=== Jugadores ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Tu ping: {0}ms" + "command.ping.result": "Tu ping: {0}ms", + "ban.notice.header": "Has sido baneado de este servidor.", + "ban.notice.reason": "Motivo: {0}", + "ban.notice.unban": "Fin del baneo: {0}", + "ban.notice.unban_permanent": "Fin del baneo: Permanente", + "ban.notice.contact": "Si tienes dudas, contacta con un administrador." } """; @@ -678,7 +732,12 @@ public sealed class LanguageService "command.players.not_in_lobby": "Questo comando può essere usato solo nella lobby.", "command.players.header": "=== Giocatori ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Il tuo ping: {0}ms" + "command.ping.result": "Il tuo ping: {0}ms", + "ban.notice.header": "Sei stato bannato da questo server.", + "ban.notice.reason": "Motivo: {0}", + "ban.notice.unban": "Sbanno: {0}", + "ban.notice.unban_permanent": "Sbanno: Permanente", + "ban.notice.contact": "Per qualsiasi domanda, contatta l'amministratore." } """; @@ -717,7 +776,12 @@ public sealed class LanguageService "command.players.not_in_lobby": "Dit commando kan alleen in de lobby worden gebruikt.", "command.players.header": "=== Spelers ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Jouw ping: {0}ms" + "command.ping.result": "Jouw ping: {0}ms", + "ban.notice.header": "Je bent geband van deze server.", + "ban.notice.reason": "Reden: {0}", + "ban.notice.unban": "Ontbantijd: {0}", + "ban.notice.unban_permanent": "Ontbantijd: Permanent", + "ban.notice.contact": "Neem bij vragen contact op met de beheerder." } """; @@ -756,7 +820,12 @@ public sealed class LanguageService "command.players.not_in_lobby": "Ang command na ito ay maaari lamang gamitin sa lobby.", "command.players.header": "=== Mga Manlalaro ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Ang iyong ping: {0}ms" + "command.ping.result": "Ang iyong ping: {0}ms", + "ban.notice.header": "Banned ka sa server na ito.", + "ban.notice.reason": "Dahilan: {0}", + "ban.notice.unban": "Oras ng pag-unban: {0}", + "ban.notice.unban_permanent": "Oras ng pag-unban: Permanente", + "ban.notice.contact": "Kung may tanong, makipag-ugnayan sa administrator." } """; @@ -795,7 +864,12 @@ public sealed class LanguageService "command.players.not_in_lobby": "Ní féidir an t-ordú seo a úsáid ach sa stocaireacht.", "command.players.header": "=== Imreoirí ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Do phing: {0}ms" + "command.ping.result": "Do phing: {0}ms", + "ban.notice.header": "Tá cosc ort ar an bhfreastalaí seo.", + "ban.notice.reason": "Cúis: {0}", + "ban.notice.unban": "Am dí-chosc: {0}", + "ban.notice.unban_permanent": "Am dí-chosc: Buan", + "ban.notice.contact": "Má tá ceist agat, déan teagmháil le riarthóir." } """; diff --git a/src/Empostor.Api/packages.lock.json b/src/Empostor.Api/packages.lock.json index 9d991b4..e851b84 100644 --- a/src/Empostor.Api/packages.lock.json +++ b/src/Empostor.Api/packages.lock.json @@ -139,6 +139,7 @@ "NextFast.Hazel.Abstractions": { "type": "Project" } - } + }, + "net8.0/win-x64": {} } } \ No newline at end of file diff --git a/src/Empostor.Server/Http/AdminController.cs b/src/Empostor.Server/Http/AdminController.cs index 6225bc4..ec2affc 100644 --- a/src/Empostor.Server/Http/AdminController.cs +++ b/src/Empostor.Server/Http/AdminController.cs @@ -487,7 +487,7 @@ namespace Empostor.Server.Http return BadRequest(Err("Invalid IP")); } - var entry = _bans.BanIp(ip, req.Reason ?? "Banned by admin"); + var entry = _bans.BanIp(ip, req.Reason ?? "Banned by admin", BanStore.ParseDuration(req.Duration)); var kicked = 0; foreach (var c in _clientManager.Clients.ToList()) { @@ -521,7 +521,7 @@ namespace Empostor.Server.Http return BadRequest(Err("FriendCode required")); } - var entry = _bans.BanFriendCode(req.FriendCode, req.Reason ?? "Banned by admin"); + var entry = _bans.BanFriendCode(req.FriendCode, req.Reason ?? "Banned by admin", BanStore.ParseDuration(req.Duration)); var kicked = 0; foreach (var c in _clientManager.Clients.ToList()) { @@ -901,9 +901,9 @@ namespace Empostor.Server.Http public sealed record ClientIdReq(int ClientId, string? Reason = null); - public sealed record BanIpReq(string Ip, string? Reason); + public sealed record BanIpReq(string Ip, string? Reason, string? Duration = null); - public sealed record BanFcReq(string FriendCode, string? Reason); + public sealed record BanFcReq(string FriendCode, string? Reason, string? Duration = null); public sealed record UnbanReq(string Value); diff --git a/src/Empostor.Server/Http/AdminTemplateDefaults.cs b/src/Empostor.Server/Http/AdminTemplateDefaults.cs index 44fcbc7..eb5687d 100644 --- a/src/Empostor.Server/Http/AdminTemplateDefaults.cs +++ b/src/Empostor.Server/Http/AdminTemplateDefaults.cs @@ -998,6 +998,7 @@ internal static class AdminTemplateDefaults

Ban IP

+
@@ -1005,6 +1006,7 @@ internal static class AdminTemplateDefaults

Ban Friend Code

+
@@ -1280,7 +1282,8 @@ internal static class AdminTemplateDefaults document.getElementById('bl-fc').innerHTML = d.friendCodes.length ? d.friendCodes.map(b => bi(b, 'fc')).join('') : '
' + _('ban.none', 'None') + '
'; } function bi(b, t) { - return `
${e(b.value)}
${e(b.reason)}
${new Date(b.bannedAt).toLocaleString()}
`; + const until = b.bannedUntil ? new Date(b.bannedUntil).toLocaleString() : _('ban.permanent', 'Permanent'); + return `
${e(b.value)}
${e(b.reason)}
${new Date(b.bannedAt).toLocaleString()}
${_('ban.until', 'Until')}: ${until}
`; } async function fGamesEnd() { @@ -1503,17 +1506,29 @@ internal static class AdminTemplateDefaults fKickList(); } + function banDuration(vId, uId) { + const u = document.getElementById(uId).value; + if (u === 'permanent') return 'permanent'; + let v = parseInt(document.getElementById(vId).value, 10); + if (isNaN(v) || v < 1) v = 1; + const max = { h: 24, d: 30, mo: 12, y: 10 }[u] || 24; + if (v > max) v = max; + return v + u; + } + async function doBanIp() { const v = document.getElementById('bi-v').value.trim(), r = document.getElementById('bi-r').value.trim(); if (!v) return msg('bi-msg', false, _('alert.ip_required', 'IP required')); - const { ok, data } = await api('POST', '/api/admin/ban/ip', { ip: v, reason: r }); + const duration = banDuration('bi-dv', 'bi-du'); + const { ok, data } = await api('POST', '/api/admin/ban/ip', { ip: v, reason: r, duration }); msg('bi-msg', ok, ok ? _('alert.banned', 'Banned {0} ({1} disconnected)').replace('{0}', data.banned).replace('{1}', data.disconnected) : (data.error ?? 'Error')); } async function doBanFc() { const v = document.getElementById('bf-v').value.trim(), r = document.getElementById('bf-r').value.trim(); if (!v) return msg('bf-msg', false, _('alert.fc_required', 'FC required')); - const { ok, data } = await api('POST', '/api/admin/ban/fc', { friendCode: v, reason: r }); + const duration = banDuration('bf-dv', 'bf-du'); + const { ok, data } = await api('POST', '/api/admin/ban/fc', { friendCode: v, reason: r, duration }); msg('bf-msg', ok, ok ? _('alert.banned', 'Banned {0} ({1} disconnected)').replace('{0}', data.banned).replace('{1}', data.disconnected) : (data.error ?? 'Error')); } diff --git a/src/Empostor.Server/Languages/de.json b/src/Empostor.Server/Languages/de.json index 79ec2da..67fe4a0 100644 --- a/src/Empostor.Server/Languages/de.json +++ b/src/Empostor.Server/Languages/de.json @@ -34,5 +34,10 @@ "command.players.not_in_lobby": "Dieser Befehl kann nur in der Lobby verwendet werden.", "command.players.header": "=== Spieler ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Dein Ping: {0}ms" -} \ No newline at end of file + "command.ping.result": "Dein Ping: {0}ms", + "ban.notice.header": "Du wurdest von diesem Server gebannt.", + "ban.notice.reason": "Grund: {0}", + "ban.notice.unban": "Entbannungszeit: {0}", + "ban.notice.unban_permanent": "Entbannungszeit: Dauerhaft", + "ban.notice.contact": "Bei Fragen wende dich an den Administrator." +} diff --git a/src/Empostor.Server/Languages/en.json b/src/Empostor.Server/Languages/en.json index 9115940..73e3325 100644 --- a/src/Empostor.Server/Languages/en.json +++ b/src/Empostor.Server/Languages/en.json @@ -10,7 +10,6 @@ "command.note.set": "Note set: {0}", "command.color.invalid": "Invalid color ID. Valid range: 0–17.", "command.color.set": "Color changed to {0} ({1}).", - "command.title.too_long": "Title too long. Max 12 characters.", "command.title.cleared": "Title removed.", "command.title.set": "Title set: {0}", @@ -66,5 +65,10 @@ "command.ban.cannot_ban_self": "You cannot ban yourself.", "command.ban.banned": "{0} has been banned. Reason: {1}", "command.ban.description": "Ban a player by friend code or name. Host only.", - "command.ban.usage_cmd": "ban [reason]" -} \ No newline at end of file + "command.ban.usage_cmd": "ban [reason]", + "ban.notice.header": "You are banned from this server.", + "ban.notice.reason": "Reason: {0}", + "ban.notice.unban": "Unban time: {0}", + "ban.notice.unban_permanent": "Unban time: Permanent", + "ban.notice.contact": "If you have any questions, please contact the administrator." +} diff --git a/src/Empostor.Server/Languages/es.json b/src/Empostor.Server/Languages/es.json index 618db7b..6015a27 100644 --- a/src/Empostor.Server/Languages/es.json +++ b/src/Empostor.Server/Languages/es.json @@ -32,5 +32,10 @@ "command.players.not_in_lobby": "Este comando solo puede usarse en el lobby.", "command.players.header": "=== Jugadores ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Tu ping: {0}ms" -} \ No newline at end of file + "command.ping.result": "Tu ping: {0}ms", + "ban.notice.header": "Has sido baneado de este servidor.", + "ban.notice.reason": "Motivo: {0}", + "ban.notice.unban": "Fin del baneo: {0}", + "ban.notice.unban_permanent": "Fin del baneo: Permanente", + "ban.notice.contact": "Si tienes dudas, contacta con un administrador." +} diff --git a/src/Empostor.Server/Languages/fil.json b/src/Empostor.Server/Languages/fil.json index 567025e..ad16ae7 100644 --- a/src/Empostor.Server/Languages/fil.json +++ b/src/Empostor.Server/Languages/fil.json @@ -32,5 +32,10 @@ "command.players.not_in_lobby": "Ang command na ito ay maaari lamang gamitin sa lobby.", "command.players.header": "=== Mga Manlalaro ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Ang iyong ping: {0}ms" -} \ No newline at end of file + "command.ping.result": "Ang iyong ping: {0}ms", + "ban.notice.header": "Banned ka sa server na ito.", + "ban.notice.reason": "Dahilan: {0}", + "ban.notice.unban": "Oras ng pag-unban: {0}", + "ban.notice.unban_permanent": "Oras ng pag-unban: Permanente", + "ban.notice.contact": "Kung may tanong, makipag-ugnayan sa administrator." +} diff --git a/src/Empostor.Server/Languages/fr.json b/src/Empostor.Server/Languages/fr.json index 9912c86..d2db85b 100644 --- a/src/Empostor.Server/Languages/fr.json +++ b/src/Empostor.Server/Languages/fr.json @@ -34,5 +34,10 @@ "command.players.not_in_lobby": "Cette commande ne peut être utilisée que dans le lobby.", "command.players.header": "=== Joueurs ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Votre ping : {0}ms" -} \ No newline at end of file + "command.ping.result": "Votre ping : {0}ms", + "ban.notice.header": "Vous êtes banni de ce serveur.", + "ban.notice.reason": "Raison : {0}", + "ban.notice.unban": "Débannissement : {0}", + "ban.notice.unban_permanent": "Débannissement : Permanent", + "ban.notice.contact": "En cas de question, contactez un administrateur." +} diff --git a/src/Empostor.Server/Languages/ga.json b/src/Empostor.Server/Languages/ga.json index fde99ad..d4297f1 100644 --- a/src/Empostor.Server/Languages/ga.json +++ b/src/Empostor.Server/Languages/ga.json @@ -32,5 +32,10 @@ "command.players.not_in_lobby": "Ní féidir an t-ordú seo a úsáid ach sa stocaireacht.", "command.players.header": "=== Imreoirí ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Do phing: {0}ms" -} \ No newline at end of file + "command.ping.result": "Do phing: {0}ms", + "ban.notice.header": "Tá cosc ort ar an bhfreastalaí seo.", + "ban.notice.reason": "Cúis: {0}", + "ban.notice.unban": "Am dí-chosc: {0}", + "ban.notice.unban_permanent": "Am dí-chosc: Buan", + "ban.notice.contact": "Má tá ceist agat, déan teagmháil le riarthóir." +} diff --git a/src/Empostor.Server/Languages/it.json b/src/Empostor.Server/Languages/it.json index a642121..48d0493 100644 --- a/src/Empostor.Server/Languages/it.json +++ b/src/Empostor.Server/Languages/it.json @@ -32,5 +32,10 @@ "command.players.not_in_lobby": "Questo comando può essere usato solo nella lobby.", "command.players.header": "=== Giocatori ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Il tuo ping: {0}ms" -} \ No newline at end of file + "command.ping.result": "Il tuo ping: {0}ms", + "ban.notice.header": "Sei stato bannato da questo server.", + "ban.notice.reason": "Motivo: {0}", + "ban.notice.unban": "Sbanno: {0}", + "ban.notice.unban_permanent": "Sbanno: Permanente", + "ban.notice.contact": "Per qualsiasi domanda, contatta l'amministratore." +} diff --git a/src/Empostor.Server/Languages/ja.json b/src/Empostor.Server/Languages/ja.json index 9958d08..04b47f4 100644 --- a/src/Empostor.Server/Languages/ja.json +++ b/src/Empostor.Server/Languages/ja.json @@ -34,5 +34,10 @@ "command.players.not_in_lobby": "このコマンドはロビーでのみ使用できます。", "command.players.header": "=== プレイヤー ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "あなたのPing: {0}ms" -} \ No newline at end of file + "command.ping.result": "あなたのPing: {0}ms", + "ban.notice.header": "このサーバーからBANされています。", + "ban.notice.reason": "理由:{0}", + "ban.notice.unban": "BAN解除日時:{0}", + "ban.notice.unban_permanent": "BAN解除日時:永久", + "ban.notice.contact": "ご不明な点があれば管理者にお問い合わせください。" +} diff --git a/src/Empostor.Server/Languages/ko.json b/src/Empostor.Server/Languages/ko.json index 708e732..6a406e7 100644 --- a/src/Empostor.Server/Languages/ko.json +++ b/src/Empostor.Server/Languages/ko.json @@ -34,5 +34,10 @@ "command.players.not_in_lobby": "이 명령어는 로비에서만 사용할 수 있습니다.", "command.players.header": "=== 플레이어 ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "내 핑: {0}ms" -} \ No newline at end of file + "command.ping.result": "내 핑: {0}ms", + "ban.notice.header": "이 서버에서 차단되었습니다.", + "ban.notice.reason": "사유: {0}", + "ban.notice.unban": "차단 해제 시간: {0}", + "ban.notice.unban_permanent": "차단 해제 시간: 영구", + "ban.notice.contact": "문의 사항이 있으면 관리자에게 연락해 주세요." +} diff --git a/src/Empostor.Server/Languages/nl.json b/src/Empostor.Server/Languages/nl.json index f1e94e0..be0dd76 100644 --- a/src/Empostor.Server/Languages/nl.json +++ b/src/Empostor.Server/Languages/nl.json @@ -32,5 +32,10 @@ "command.players.not_in_lobby": "Dit commando kan alleen in de lobby worden gebruikt.", "command.players.header": "=== Spelers ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Jouw ping: {0}ms" -} \ No newline at end of file + "command.ping.result": "Jouw ping: {0}ms", + "ban.notice.header": "Je bent geband van deze server.", + "ban.notice.reason": "Reden: {0}", + "ban.notice.unban": "Ontbantijd: {0}", + "ban.notice.unban_permanent": "Ontbantijd: Permanent", + "ban.notice.contact": "Neem bij vragen contact op met de beheerder." +} diff --git a/src/Empostor.Server/Languages/pt.json b/src/Empostor.Server/Languages/pt.json index 0ae4a1e..bbffae1 100644 --- a/src/Empostor.Server/Languages/pt.json +++ b/src/Empostor.Server/Languages/pt.json @@ -34,5 +34,10 @@ "command.players.not_in_lobby": "Este comando só pode ser usado no lobby.", "command.players.header": "=== Jogadores ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Seu ping: {0}ms" -} \ No newline at end of file + "command.ping.result": "Seu ping: {0}ms", + "ban.notice.header": "Você foi banido deste servidor.", + "ban.notice.reason": "Motivo: {0}", + "ban.notice.unban": "Desbanimento: {0}", + "ban.notice.unban_permanent": "Desbanimento: Permanente", + "ban.notice.contact": "Em caso de dúvidas, contacte o administrador." +} diff --git a/src/Empostor.Server/Languages/pt_BR.json b/src/Empostor.Server/Languages/pt_BR.json index ef395b5..5c6c21b 100644 --- a/src/Empostor.Server/Languages/pt_BR.json +++ b/src/Empostor.Server/Languages/pt_BR.json @@ -32,5 +32,10 @@ "command.players.not_in_lobby": "Este comando só pode ser usado no lobby.", "command.players.header": "=== Jogadores ({0}) ===", "command.players.entry": "{0} | {1} | {2}ms", - "command.ping.result": "Seu ping: {0}ms" -} \ No newline at end of file + "command.ping.result": "Seu ping: {0}ms", + "ban.notice.header": "Você foi banido deste servidor.", + "ban.notice.reason": "Motivo: {0}", + "ban.notice.unban": "Desbanimento: {0}", + "ban.notice.unban_permanent": "Desbanimento: Permanente", + "ban.notice.contact": "Em caso de dúvidas, contate o administrador." +} diff --git a/src/Empostor.Server/Languages/ru.json b/src/Empostor.Server/Languages/ru.json index 630e949..a1bc6e4 100644 --- a/src/Empostor.Server/Languages/ru.json +++ b/src/Empostor.Server/Languages/ru.json @@ -34,5 +34,10 @@ "command.players.not_in_lobby": "Эта команда может использоваться только в лобби.", "command.players.header": "=== Игроки ({0}) ===", "command.players.entry": "{0} | {1} | {2}мс", - "command.ping.result": "Ваш пинг: {0}мс" -} \ No newline at end of file + "command.ping.result": "Ваш пинг: {0}мс", + "ban.notice.header": "Вы забанены на этом сервере.", + "ban.notice.reason": "Причина: {0}", + "ban.notice.unban": "Время разбана: {0}", + "ban.notice.unban_permanent": "Время разбана: навсегда", + "ban.notice.contact": "Если у вас есть вопросы, обратитесь к администратору." +} diff --git a/src/Empostor.Server/Languages/zh_CN.json b/src/Empostor.Server/Languages/zh_CN.json index 19489f4..10df749 100644 --- a/src/Empostor.Server/Languages/zh_CN.json +++ b/src/Empostor.Server/Languages/zh_CN.json @@ -65,5 +65,10 @@ "command.ban.cannot_ban_self": "你不能封禁自己。", "command.ban.banned": "{0} 已被封禁。原因:{1}", "command.ban.description": "通过好友代码或名称封禁玩家(仅房主)。", - "command.ban.usage_cmd": "ban <玩家> [原因]" -} \ No newline at end of file + "command.ban.usage_cmd": "ban <玩家> [原因]", + "ban.notice.header": "您已被服务器封禁", + "ban.notice.reason": "理由:{0}", + "ban.notice.unban": "解封时间:{0}", + "ban.notice.unban_permanent": "解封时间:永久", + "ban.notice.contact": "如有问题请联系管理员" +} diff --git a/src/Empostor.Server/Languages/zh_TW.json b/src/Empostor.Server/Languages/zh_TW.json index e339cf6..64cf17b 100644 --- a/src/Empostor.Server/Languages/zh_TW.json +++ b/src/Empostor.Server/Languages/zh_TW.json @@ -48,5 +48,10 @@ "command.note.description": "設置或清除房間備註(僅房主)。", "command.note.usage": "note <文字> | note clear", "command.max.description": "設置房間最大人數(僅房主)。", - "command.max.usage": "max <1-15>" -} \ No newline at end of file + "command.max.usage": "max <1-15>", + "ban.notice.header": "您已被伺服器封禁", + "ban.notice.reason": "理由:{0}", + "ban.notice.unban": "解封時間:{0}", + "ban.notice.unban_permanent": "解封時間:永久", + "ban.notice.contact": "如有問題請聯絡管理員" +} diff --git a/src/Empostor.Server/Pages/AdminStrings.json b/src/Empostor.Server/Pages/AdminStrings.json index 659ad0f..7cfe6fa 100644 --- a/src/Empostor.Server/Pages/AdminStrings.json +++ b/src/Empostor.Server/Pages/AdminStrings.json @@ -27,9 +27,6 @@ "nav.extend": "Extend", "nav.system": "System", "nav.player_logs": "Player Logs", - "nav.statistics": "Statistics", - "nav.chat_filter": "Chat Filter", - "nav.discord": "Discord", "nav.hplp": "HPLP", "nav.reload_lang": "Reload Lang", @@ -85,6 +82,14 @@ "ban.banned_fcs": "Banned Friend Codes", "ban.none": "None", "ban.unban": "Unban", + "ban.duration": "Duration", + "ban.unit_hours": "Hours (1-24)", + "ban.unit_days": "Days (1-30)", + "ban.unit_months": "Months (1-12)", + "ban.unit_years": "Years (1-10)", + "ban.unit_permanent": "Permanent", + "ban.permanent": "Permanent", + "ban.until": "Until", "message.title": "Message to Game", "message.code_placeholder": "ABCDEF", @@ -185,48 +190,6 @@ "table.player": "Player", "table.detail": "Detail", - "stats.title": "Player Statistics", - "stats.refresh": "Refresh", - "stats.reset": "Reset All", - "stats.rank": "#", - "stats.games": "Games", - "stats.wins": "Wins", - "stats.losses": "Losses", - "stats.impostor_wins": "Imp. Wins", - "stats.kills": "Kills", - "stats.deaths": "Deaths", - "stats.tasks": "Tasks", - "stats.exiled": "Exiled", - "stats.empty": "Statistics not enabled in config.", - "stats.no_players": "No player stats recorded yet.", - "stats.confirm_reset": "Reset all player statistics? This cannot be undone.", - - "chatfilter.title": "Chat Filter", - "chatfilter.enabled": "Enable Filtering", - "chatfilter.block_message": "Block messages (uncheck = log only)", - "chatfilter.words_title": "Blocked Words", - "chatfilter.words_placeholder": "Add a word...", - "chatfilter.add_word": "Add", - "chatfilter.no_words": "No blocked words added.", - "chatfilter.spam_title": "Spam Protection", - "chatfilter.threshold": "Message threshold", - "chatfilter.window": "Window (seconds)", - "chatfilter.save": "Save Settings", - "chatfilter.saved": "Settings saved.", - "chatfilter.word_added": "Word added.", - "chatfilter.word_removed": "Word removed.", - - "discord.title": "Discord Webhook", - "discord.save": "Save Settings", - "discord.matchmaker_url": "Matchmaker Webhook URL", - "discord.matchmaker_url_placeholder": "https://discord.com/api/webhooks/...", - "discord.matchmaker_desc": "Game Created, Game Started, Game Ended, Game Destroyed", - "discord.admin_url": "Admin Webhook URL", - "discord.admin_url_placeholder": "https://discord.com/api/webhooks/...", - "discord.admin_desc": "Player Banned, Player Reported", - "discord.hint": "Leave a URL empty to disable its notifications. Leave both empty to disable all webhooks.", - "discord.saved": "Discord webhook settings saved.", - "hplp.title": "HPLP (Public Lobby List)", "hplp.save": "Save Settings", "hplp.enabled": "Enable HPLP endpoint (GET /x-api/games)", diff --git a/src/Empostor.Server/Service/Admin/Ban/BanEnforcementListener.cs b/src/Empostor.Server/Service/Admin/Ban/BanEnforcementListener.cs index 10b96b2..9ea1db6 100644 --- a/src/Empostor.Server/Service/Admin/Ban/BanEnforcementListener.cs +++ b/src/Empostor.Server/Service/Admin/Ban/BanEnforcementListener.cs @@ -1,6 +1,8 @@ +using System; using System.Threading.Tasks; using Empostor.Api.Events; using Empostor.Api.Events.Client; +using Empostor.Api.Languages; using Microsoft.Extensions.Logging; namespace Empostor.Server.Service.Admin.Ban; @@ -9,11 +11,13 @@ public sealed class BanEnforcementListener : IEventListener { private readonly ILogger _logger; private readonly BanStore _bans; + private readonly LanguageService _language; - public BanEnforcementListener(ILogger logger, BanStore bans) + public BanEnforcementListener(ILogger logger, BanStore bans, LanguageService language) { _logger = logger; _bans = bans; + _language = language; } [EventListener] @@ -23,18 +27,34 @@ public sealed class BanEnforcementListener : IEventListener var ip = client.Connection?.EndPoint?.Address; // IP 封禁检查 - if (ip != null && _bans.IsIpBanned(ip)) + if (ip != null && _bans.GetIpBan(ip) is { } ipBan) { _logger.LogWarning("BanRejecting banned IP {Ip} ({Name})", ip, client.Name); - await client.DisconnectAsync(DisconnectReason.Banned, "You are banned from this server."); + await client.DisconnectAsync(DisconnectReason.Custom, BuildMessage(client.Language, ipBan.Reason, ipBan.BannedUntil)); return; } // FriendCode 封禁检查 - if (_bans.IsFriendCodeBanned(client.FriendCode)) + if (_bans.GetFriendCodeBan(client.FriendCode) is { } fcBan) { _logger.LogWarning("BanRejecting banned FriendCode {FC} ({Name})", client.FriendCode, client.Name); - await client.DisconnectAsync(DisconnectReason.Banned, "You are banned from this server."); + await client.DisconnectAsync(DisconnectReason.Custom, BuildMessage(client.Language, fcBan.Reason, fcBan.BannedUntil)); } } + + private string BuildMessage(Language language, string reason, DateTime? bannedUntil) + { + var header = _language.Get("ban.notice.header", language).Get(); + var reasonLine = _language.Get("ban.notice.reason", language) + .Format(string.IsNullOrWhiteSpace(reason) ? "-" : reason).Get(); + var unbanLine = bannedUntil.HasValue + ? _language.Get("ban.notice.unban", language).Format(FormatExpiry(bannedUntil.Value)).Get() + : _language.Get("ban.notice.unban_permanent", language).Get(); + var contactLine = _language.Get("ban.notice.contact", language).Get(); + + return $"{header}\n{reasonLine}\n{unbanLine}\n{contactLine}"; + } + + private static string FormatExpiry(DateTime utc) + => utc.ToLocalTime().ToString("yyyy-MM-dd HH:mm"); } diff --git a/src/Empostor.Server/Service/Admin/Ban/BanStore.cs b/src/Empostor.Server/Service/Admin/Ban/BanStore.cs index 851a91a..8c25b94 100644 --- a/src/Empostor.Server/Service/Admin/Ban/BanStore.cs +++ b/src/Empostor.Server/Service/Admin/Ban/BanStore.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Text.Json.Serialization; +using System.Text.RegularExpressions; using Empostor.Api.Service; using Microsoft.Extensions.Logging; @@ -20,14 +21,49 @@ namespace Empostor.Server.Service.Admin.Ban Load(); } - public bool IsIpBanned(IPAddress ip) => _ips.ContainsKey(Normalize(ip)); + public bool IsIpBanned(IPAddress ip) => GetIpBan(ip) != null; - public bool IsFriendCodeBanned(string? fc) => fc != null && _friendCodes.ContainsKey(fc); + public bool IsFriendCodeBanned(string? fc) => GetFriendCodeBan(fc) != null; - public BanEntry BanIp(IPAddress ip, string reason) + public BanEntry? GetIpBan(IPAddress ip) { var key = Normalize(ip); - var entry = new BanEntry { Value = key, Reason = reason, BannedAt = DateTime.UtcNow }; + if (!_ips.TryGetValue(key, out var entry)) + { + return null; + } + + if (!entry.IsExpired) + { + return entry; + } + + _ips.TryRemove(key, out _); + SaveFireAndForget(); + return null; + } + + public BanEntry? GetFriendCodeBan(string? fc) + { + if (fc == null || !_friendCodes.TryGetValue(fc, out var entry)) + { + return null; + } + + if (!entry.IsExpired) + { + return entry; + } + + _friendCodes.TryRemove(fc, out _); + SaveFireAndForget(); + return null; + } + + public BanEntry BanIp(IPAddress ip, string reason, DateTime? bannedUntil = null) + { + var key = Normalize(ip); + var entry = new BanEntry { Value = key, Reason = reason, BannedAt = DateTime.UtcNow, BannedUntil = bannedUntil }; _ips[key] = entry; SaveFireAndForget(); return entry; @@ -44,9 +80,9 @@ namespace Empostor.Server.Service.Admin.Ban return r; } - public BanEntry BanFriendCode(string fc, string reason) + public BanEntry BanFriendCode(string fc, string reason, DateTime? bannedUntil = null) { - var entry = new BanEntry { Value = fc, Reason = reason, BannedAt = DateTime.UtcNow }; + var entry = new BanEntry { Value = fc, Reason = reason, BannedAt = DateTime.UtcNow, BannedUntil = bannedUntil }; _friendCodes[fc] = entry; SaveFireAndForget(); return entry; @@ -64,18 +100,85 @@ namespace Empostor.Server.Service.Admin.Ban } public IReadOnlyList AllIpBans() - => _ips.Values.OrderByDescending(b => b.BannedAt).ToList(); + { + if (PruneExpired()) + { + SaveFireAndForget(); + } + + return _ips.Values.OrderByDescending(b => b.BannedAt).ToList(); + } public IReadOnlyList AllFriendCodeBans() - => _friendCodes.Values.OrderByDescending(b => b.BannedAt).ToList(); - - public (int IpCount, int FcCount) Stats() => (_ips.Count, _friendCodes.Count); - - protected override BanData GetSnapshot() => new() { - Ips = new(_ips), - FriendCodes = new(_friendCodes), - }; + if (PruneExpired()) + { + SaveFireAndForget(); + } + + return _friendCodes.Values.OrderByDescending(b => b.BannedAt).ToList(); + } + + public (int IpCount, int FcCount) Stats() + { + if (PruneExpired()) + { + SaveFireAndForget(); + } + + return (_ips.Count, _friendCodes.Count); + } + + /// + /// Parses a ban duration string into a UTC expiry timestamp. + /// Supported formats: "permanent"/"forever" (or empty) for a permanent ban, + /// or "<number><unit>" where unit is h (hours), d (days), mo (months) or y (years). + /// Returns null for a permanent ban. + /// + public static DateTime? ParseDuration(string? duration) + { + if (string.IsNullOrWhiteSpace(duration)) + { + return null; + } + + var value = duration.Trim(); + if (value.Equals("permanent", StringComparison.OrdinalIgnoreCase) + || value.Equals("forever", StringComparison.OrdinalIgnoreCase) + || value.Equals("perm", StringComparison.OrdinalIgnoreCase)) + { + return null; + } + + var match = Regex.Match(value, @"^(\d+)(h|d|mo|y)$", RegexOptions.IgnoreCase); + if (!match.Success) + { + return null; + } + + var amount = int.Parse(match.Groups[1].Value); + var unit = match.Groups[2].Value.ToLowerInvariant(); + var now = DateTime.UtcNow; + + return unit switch + { + "h" => now.AddHours(amount), + "d" => now.AddDays(amount), + "mo" => now.AddMonths(amount), + "y" => now.AddYears(amount), + _ => null, + }; + } + + protected override BanData GetSnapshot() + { + PruneExpired(); + return new() + { + Ips = new(_ips), + FriendCodes = new(_friendCodes), + }; + } protected override void ApplySnapshot(BanData data) { @@ -83,6 +186,28 @@ namespace Empostor.Server.Service.Admin.Ban _friendCodes = new(data.FriendCodes ?? new()); } + private bool PruneExpired() + { + var changed = false; + foreach (var (key, entry) in _ips) + { + if (entry.IsExpired && _ips.TryRemove(key, out _)) + { + changed = true; + } + } + + foreach (var (key, entry) in _friendCodes) + { + if (entry.IsExpired && _friendCodes.TryRemove(key, out _)) + { + changed = true; + } + } + + return changed; + } + private static string Normalize(IPAddress ip) => ip.IsIPv4MappedToIPv6 ? ip.MapToIPv4().ToString() : ip.ToString(); @@ -106,5 +231,14 @@ namespace Empostor.Server.Service.Admin.Ban [JsonPropertyName("bannedAt")] public required DateTime BannedAt { get; init; } + + [JsonPropertyName("bannedUntil")] + public DateTime? BannedUntil { get; init; } + + [JsonIgnore] + public bool IsExpired => BannedUntil.HasValue && BannedUntil.Value <= DateTime.UtcNow; + + [JsonIgnore] + public bool IsPermanent => !BannedUntil.HasValue; } }