- Ban durations (TODO: add a way to set and view the durations through the menus).

- Separated ban & kick methods.
- Fixed compilation errors in DebugConsole when building the server.
This commit is contained in:
Joonas Rikkonen
2017-07-02 18:58:07 +03:00
parent da71b6bf9c
commit df0bdb64d6
9 changed files with 255 additions and 83 deletions
@@ -11,6 +11,7 @@ namespace Barotrauma.Networking
public string Name;
public string IP;
public string Reason;
public DateTime? ExpirationTime;
public bool CompareTo(string ipCompare)
{
@@ -26,11 +27,12 @@ namespace Barotrauma.Networking
}
}
public BannedPlayer(string name, string ip, string reason)
public BannedPlayer(string name, string ip, string reason, DateTime? expirationTime)
{
this.Name = name;
this.IP = ip;
this.Reason = reason;
this.ExpirationTime = expirationTime;
}
}
@@ -64,14 +66,26 @@ namespace Barotrauma.Networking
string name = separatedLine[0];
string ip = separatedLine[1];
string reason = separatedLine.Length > 2 ? string.Join(",", separatedLine.Skip(2)) : "";
bannedPlayers.Add(new BannedPlayer(name, ip,reason));
DateTime? expirationTime = DateTime.Now;
if (separatedLine.Length > 2)
{
DateTime parsedTime;
if (DateTime.TryParse(separatedLine[2], out parsedTime))
{
expirationTime = parsedTime;
}
}
string reason = separatedLine.Length > 3 ? string.Join(",", separatedLine.Skip(3)) : "";
if (expirationTime.HasValue && expirationTime.Value > DateTime.Now) continue;
bannedPlayers.Add(new BannedPlayer(name, ip, reason, expirationTime));
}
}
}
public void BanPlayer(string name, string ip, string reason)
public void BanPlayer(string name, string ip, string reason, TimeSpan? duration)
{
if (bannedPlayers.Any(bp => bp.IP == ip)) return;
@@ -79,12 +93,19 @@ namespace Barotrauma.Networking
DebugConsole.Log("Banned " + name);
bannedPlayers.Add(new BannedPlayer(name, ip, reason));
DateTime? expirationTime = null;
if (duration.HasValue)
{
expirationTime = DateTime.Now + duration.Value;
}
bannedPlayers.Add(new BannedPlayer(name, ip, reason, expirationTime));
Save();
}
public bool IsBanned(string IP)
{
bannedPlayers.RemoveAll(bp => bp.ExpirationTime.HasValue && DateTime.Now > bp.ExpirationTime.Value);
return bannedPlayers.Any(bp => bp.CompareTo(IP));
}
@@ -131,12 +152,15 @@ namespace Barotrauma.Networking
{
GameServer.Log("Saving banlist", ServerLog.MessageType.ServerMessage);
List<string> lines = new List<string>();
bannedPlayers.RemoveAll(bp => bp.ExpirationTime.HasValue && DateTime.Now > bp.ExpirationTime.Value);
List<string> lines = new List<string>();
foreach (BannedPlayer banned in bannedPlayers)
{
string line = banned.Name + "," + banned.IP;
if (banned.ExpirationTime.HasValue) line += "," + banned.ExpirationTime.Value.ToString();
if (!string.IsNullOrWhiteSpace(banned.Reason)) line += "," + banned.Reason;
lines.Add(line);
}
@@ -531,7 +531,7 @@ namespace Barotrauma.Networking
{
if (banList.IsBanned(inc.SenderEndPoint.Address.ToString()))
{
KickClient(inc.SenderConnection, "You have been banned from the server.", true);
KickClient(inc.SenderConnection, "You have been banned from the server.");
return;
}
@@ -759,7 +759,7 @@ namespace Barotrauma.Networking
if (kickedClient != null)
{
Log("Client \"" + sender.name + "\" kicked \"" + kickedClient.name + "\".", ServerLog.MessageType.ServerMessage);
KickClient(kickedClient, "Kicked by " + sender.name, false, false);
KickClient(kickedClient, "Kicked by " + sender.name);
}
break;
case ClientPermissions.Ban:
@@ -768,7 +768,7 @@ namespace Barotrauma.Networking
if (bannedClient != null)
{
Log("Client \"" + sender.name + "\" banned \"" + bannedClient.name + "\".", ServerLog.MessageType.ServerMessage);
KickClient(bannedClient, "Banned by " + sender.name, true, false);
BanClient(bannedClient, "Banned by " + sender.name, false);
}
break;
case ClientPermissions.EndRound:
@@ -809,6 +809,11 @@ namespace Barotrauma.Networking
/// </summary>
private void ClientWriteInitial(Client c, NetBuffer outmsg)
{
if (GameSettings.VerboseLogging)
{
DebugConsole.NewMessage("Sending initial lobby update", Color.Gray);
}
outmsg.Write(c.ID);
var subList = GameMain.NetLobbyScreen.GetSubList();
@@ -1416,7 +1421,7 @@ namespace Barotrauma.Networking
yield return CoroutineStatus.Success;
}
public override void KickPlayer(string playerName, string reason, bool ban, bool range = false)
public override void KickPlayer(string playerName, string reason)
{
playerName = playerName.ToLowerInvariant();
@@ -1424,48 +1429,68 @@ namespace Barotrauma.Networking
c.name.ToLowerInvariant() == playerName ||
(c.Character != null && c.Character.Name.ToLowerInvariant() == playerName));
KickClient(client, reason, ban, range);
KickClient(client, reason);
}
public void KickClient(NetConnection conn, string reason, bool ban = false, bool range = false)
public void KickClient(NetConnection conn, string reason)
{
Client client = connectedClients.Find(c => c.Connection == conn);
KickClient(client, reason);
}
public void KickClient(Client client, string reason)
{
if (client == null) return;
string msg = "You have been kicked from the server.";
if (!string.IsNullOrWhiteSpace(reason)) msg += "\nReason: " + reason;
DisconnectClient(client, client.name + " has been kicked from the server.", msg);
}
public override void BanPlayer(string playerName, string reason, bool range = false, TimeSpan? duration = null)
{
playerName = playerName.ToLowerInvariant();
Client client = connectedClients.Find(c =>
c.name.ToLowerInvariant() == playerName ||
(c.Character != null && c.Character.Name.ToLowerInvariant() == playerName));
if (client == null)
{
DebugConsole.ThrowError("Client \"" + playerName + "\" not found.");
return;
}
BanClient(client, reason, range, duration);
}
public void BanClient(NetConnection conn, string reason, bool range = false, TimeSpan? duration = null)
{
Client client = connectedClients.Find(c => c.Connection == conn);
if (client == null)
{
conn.Disconnect(ban ? "You have been banned from the server" : "You have been kicked from the server");
if (ban)
conn.Disconnect("You have been banned from the server");
if (!banList.IsBanned(conn.RemoteEndPoint.Address.ToString()))
{
if (!banList.IsBanned(conn.RemoteEndPoint.Address.ToString()))
{
banList.BanPlayer("Unnamed", conn.RemoteEndPoint.Address.ToString(), reason);
}
}
banList.BanPlayer("Unnamed", conn.RemoteEndPoint.Address.ToString(), reason, duration);
}
}
else
{
KickClient(client, reason, ban, range);
BanClient(client, reason, range);
}
}
public void KickClient(Client client, string reason, bool ban = false, bool range = false)
public void BanClient(Client client, string reason, bool range = false, TimeSpan? duration = null)
{
if (client == null) return;
if (ban)
{
string msg = "You have been banned from the server.";
if (!string.IsNullOrWhiteSpace(reason)) msg += "\nReason: " + reason;
DisconnectClient(client, client.name + " has been banned from the server.", msg);
string ip = client.Connection.RemoteEndPoint.Address.ToString();
if (range) { ip = banList.ToRange(ip); }
banList.BanPlayer(client.name, ip, reason);
}
else
{
string msg = "You have been kicked from the server.";
if (!string.IsNullOrWhiteSpace(reason)) msg += "\nReason: " + reason;
DisconnectClient(client, client.name + " has been kicked from the server.", msg);
}
string msg = "You have been banned from the server.";
if (!string.IsNullOrWhiteSpace(reason)) msg += "\nReason: " + reason;
DisconnectClient(client, client.name + " has been banned from the server.", msg);
string ip = client.Connection.RemoteEndPoint.Address.ToString();
if (range) { ip = banList.ToRange(ip); }
banList.BanPlayer(client.name, ip, reason, duration);
}
public void DisconnectClient(NetConnection senderConnection, string msg = "", string targetmsg = "")
@@ -102,7 +102,7 @@ namespace Barotrauma.Networking
if (unauthClient.failedAttempts > 3)
{
//disconnect and ban after too many failed attempts
banList.BanPlayer("Unnamed", unauthClient.Connection.RemoteEndPoint.Address.ToString(), "Too many failed login attempts.");
banList.BanPlayer("Unnamed", unauthClient.Connection.RemoteEndPoint.Address.ToString(), "Too many failed login attempts.", null);
DisconnectUnauthClient(inc, unauthClient, "Too many failed login attempts. You have been automatically banned from the server.");
Log(inc.SenderConnection.RemoteEndPoint.Address.ToString() + " has been banned from the server (too many wrong passwords)", ServerLog.MessageType.Error);
@@ -191,7 +191,9 @@ namespace Barotrauma.Networking
#endif
}
public virtual void KickPlayer(string kickedName, string reason, bool ban, bool range = false) { }
public virtual void KickPlayer(string kickedName, string reason) { }
public virtual void BanPlayer(string kickedName, string reason, bool range = false, TimeSpan? duration = null) { }
public virtual void Update(float deltaTime)
{