From 86f970455d280f4e01f49bc5940222a805875425 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Sat, 22 Jul 2023 16:08:54 +0200 Subject: [PATCH] net: refactor bans --- src/game/g_game.pas | 65 ++++++---------------------------- src/game/g_language.pas | 3 ++ src/game/g_net.pas | 77 ++++++++++++++++++++++++++++------------- src/game/g_netmsg.pas | 8 ++--- 4 files changed, 71 insertions(+), 82 deletions(-) diff --git a/src/game/g_game.pas b/src/game/g_game.pas index 71eb609..2c158c4 100644 --- a/src/game/g_game.pas +++ b/src/game/g_game.pas @@ -5194,7 +5194,7 @@ begin end; end; - g_Net_UnbanNonPermHosts(); + g_Net_UnbanNonPerm(); end; if gLastMap then @@ -6702,14 +6702,8 @@ begin pl := g_Net_Client_ByName(P[1]); if (pl <> nil) then - begin - s := g_Net_ClientName_ByID(pl^.ID); - g_Net_BanHost(pl^.Peer^.address.host, False); - g_Net_Host_Kick(pl^.ID, NET_DISC_TEMPBAN); - g_Console_Add(Format(_lc[I_PLAYER_BAN], [s])); - MH_SEND_GameEvent(NET_EV_PLAYER_BAN, 0, s); - g_Net_Slist_ServerPlayerLeaves(); - end else + g_Net_Host_Ban(pl, False) + else g_Console_Add(Format(_lc[I_NET_ERR_NAME404], [P[1]])); end else g_Console_Add(_lc[I_MSG_SERVERONLY]); @@ -6732,14 +6726,7 @@ begin a := StrToIntDef(P[1], 0); if (NetClients <> nil) and (a <= High(NetClients)) then if NetClients[a].Used and (NetClients[a].Peer <> nil) then - begin - s := g_Net_ClientName_ByID(NetClients[a].ID); - g_Net_BanHost(NetClients[a].Peer^.address.host, False); - g_Net_Host_Kick(NetClients[a].ID, NET_DISC_TEMPBAN); - g_Console_Add(Format(_lc[I_PLAYER_BAN], [s])); - MH_SEND_GameEvent(NET_EV_PLAYER_BAN, 0, s); - g_Net_Slist_ServerPlayerLeaves(); - end; + g_Net_Host_Ban(pl, False); end else g_Console_Add(_lc[I_MSG_SERVERONLY]); end @@ -6761,14 +6748,7 @@ begin a := StrToIntDef(P[1], 0); pl := g_Net_Client_ByPlayer(a); if (pl <> nil) and pl^.Used and (pl^.Peer <> nil) then - begin - s := g_Net_ClientName_ByID(pl^.ID); - g_Net_BanHost(pl^.Peer^.address.host, False); - g_Net_Host_Kick(pl^.ID, NET_DISC_TEMPBAN); - g_Console_Add(Format(_lc[I_PLAYER_BAN], [s])); - MH_SEND_GameEvent(NET_EV_PLAYER_BAN, 0, s); - g_Net_Slist_ServerPlayerLeaves(); - end; + g_Net_Host_Ban(pl, False); end else g_Console_Add(_lc[I_MSG_SERVERONLY]); end @@ -6789,15 +6769,8 @@ begin pl := g_Net_Client_ByName(P[1]); if (pl <> nil) then - begin - s := g_Net_ClientName_ByID(pl^.ID); - g_Net_BanHost(pl^.Peer^.address.host); - g_Net_Host_Kick(pl^.ID, NET_DISC_BAN); - g_Net_SaveBanList(); - g_Console_Add(Format(_lc[I_PLAYER_BAN], [s])); - MH_SEND_GameEvent(NET_EV_PLAYER_BAN, 0, s); - g_Net_Slist_ServerPlayerLeaves(); - end else + g_Net_Host_Ban(pl, True) + else g_Console_Add(Format(_lc[I_NET_ERR_NAME404], [P[1]])); end else g_Console_Add(_lc[I_MSG_SERVERONLY]); @@ -6820,15 +6793,7 @@ begin a := StrToIntDef(P[1], 0); if (NetClients <> nil) and (a <= High(NetClients)) then if NetClients[a].Used and (NetClients[a].Peer <> nil) then - begin - s := g_Net_ClientName_ByID(NetClients[a].ID); - g_Net_BanHost(NetClients[a].Peer^.address.host); - g_Net_Host_Kick(NetClients[a].ID, NET_DISC_BAN); - g_Net_SaveBanList(); - g_Console_Add(Format(_lc[I_PLAYER_BAN], [s])); - MH_SEND_GameEvent(NET_EV_PLAYER_BAN, 0, s); - g_Net_Slist_ServerPlayerLeaves(); - end; + g_Net_Host_Ban(@NetClients[a], True); end else g_Console_Add(_lc[I_MSG_SERVERONLY]); end @@ -6850,15 +6815,7 @@ begin a := StrToIntDef(P[1], 0); pl := g_Net_Client_ByPlayer(a); if (pl <> nil) and pl^.Used and (pl^.Peer <> nil) then - begin - s := g_Net_ClientName_ByID(pl^.ID); - g_Net_BanHost(pl^.Peer^.address.host); - g_Net_Host_Kick(pl^.ID, NET_DISC_TEMPBAN); - g_Net_SaveBanList(); - g_Console_Add(Format(_lc[I_PLAYER_BAN], [s])); - MH_SEND_GameEvent(NET_EV_PLAYER_BAN, 0, s); - g_Net_Slist_ServerPlayerLeaves(); - end; + g_Net_Host_Ban(pl, True); end else g_Console_Add(_lc[I_MSG_SERVERONLY]); end @@ -6877,7 +6834,7 @@ begin Exit; end; - g_Net_BanHost(P[1]); + g_Net_BanAddress(P[1]); g_Net_SaveBanList(); g_Console_Add(Format(_lc[I_PLAYER_BAN], [P[1]])); end else @@ -6898,7 +6855,7 @@ begin Exit; end; - if g_Net_UnbanHost(P[1]) then + if g_Net_UnbanAddress(P[1]) then begin g_Console_Add(Format(_lc[I_MSG_UNBAN_OK], [P[1]])); g_Net_SaveBanList(); diff --git a/src/game/g_language.pas b/src/game/g_language.pas index a75db11..e7e3a42 100644 --- a/src/game/g_language.pas +++ b/src/game/g_language.pas @@ -404,6 +404,7 @@ type I_NET_DISC_PASSWORD, I_NET_DISC_TEMPBAN, I_NET_DISC_BAN, + I_NET_DISC_BADMSG, I_NET_SLIST, I_NET_SLIST_NOSERVERS, @@ -1418,6 +1419,8 @@ const 'Âû çàáàíåíû äî êîíöà ðàóíäà.'), ('NET DISC BAN', 'You are banned. Contact the server administrator.', 'Âû çàáàíåíû. Ñâÿæèòåñü ñ àäìèíèñòðàòîðîì ñåðâåðà.'), + ('NET DISC BADMSG' 'Malformed net message.', + 'Íåâåðíûé ôîðìàò ïàêåòà.'), ('NET SLIST', 'Server list', 'Ñïèñîê ñåðâåðîâ'), diff --git a/src/game/g_net.pas b/src/game/g_net.pas index d97d4bd..292f919 100644 --- a/src/game/g_net.pas +++ b/src/game/g_net.pas @@ -204,6 +204,8 @@ procedure g_Net_Host_Die(); procedure g_Net_Host_Send(ID: Integer; Reliable: Boolean); procedure g_Net_Host_Update(); procedure g_Net_Host_Kick(ID: Integer; Reason: enet_uint32); +procedure g_Net_Host_Ban(ID: Integer; Perm: Boolean); +procedure g_Net_Host_Ban(C: pTNetClient; Perm: Boolean); function g_Net_Connect(IP: string; Port: enet_uint16): Boolean; procedure g_Net_Disconnect(Forced: Boolean = False); @@ -217,12 +219,12 @@ function g_Net_ClientName_ByID(ID: Integer): string; function IpToStr(IP: LongWord): string; function StrToIp(IPstr: string; var IP: LongWord): Boolean; -function g_Net_IsHostBanned(IP: LongWord; Perm: Boolean = False): Boolean; -procedure g_Net_BanHost(IP: LongWord; Perm: Boolean = True); overload; -procedure g_Net_BanHost(IP: string; Perm: Boolean = True); overload; -function g_Net_UnbanHost(IP: string): Boolean; overload; -function g_Net_UnbanHost(IP: LongWord): Boolean; overload; -procedure g_Net_UnbanNonPermHosts(); +function g_Net_IsAddressBanned(IP: LongWord; Perm: Boolean = False): Boolean; +procedure g_Net_BanAddress(IP: LongWord; Perm: Boolean = True); overload; +procedure g_Net_BanAddress(IP: string; Perm: Boolean = True); overload; +function g_Net_UnbanAddress(IP: string): Boolean; overload; +function g_Net_UnbanAddress(IP: LongWord): Boolean; overload; +procedure g_Net_UnbanNonPerm(); procedure g_Net_SaveBanList(); procedure g_Net_Penalize(C: pTNetClient; Reason: string); @@ -1363,7 +1365,7 @@ begin begin Readln(F, IPstr); if StrToIp(IPstr, IP) then - g_Net_BanHost(IP); + g_Net_BanAddress(IP); end; CloseFile(F); g_Net_SaveBanList(); @@ -1788,7 +1790,7 @@ begin Exit; end; - if g_Net_IsHostBanned(NetEvent.Peer^.address.host) then + if g_Net_IsAddressBanned(NetEvent.Peer^.address.host) then begin g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] + _lc[I_NET_DISC_BAN]); @@ -2128,10 +2130,12 @@ begin pl := g_Player_Get(NetClients[a].Player); if pl = nil then Exit; Result := pl.Name; + Exit; end; -end; + Result := 'Client #' + IntToStr(ID); + end; -function g_Net_IsHostBanned(IP: LongWord; Perm: Boolean = False): Boolean; +function g_Net_IsAddressBanned(IP: LongWord; Perm: Boolean = False): Boolean; var I: Integer; begin @@ -2146,13 +2150,13 @@ begin end; end; -procedure g_Net_BanHost(IP: LongWord; Perm: Boolean = True); overload; +procedure g_Net_BanAddress(IP: LongWord; Perm: Boolean = True); overload; var I, P: Integer; begin if IP = 0 then Exit; - if g_Net_IsHostBanned(IP, Perm) then + if g_Net_IsAddressBanned(IP, Perm) then Exit; P := -1; @@ -2173,17 +2177,17 @@ begin NetBannedHosts[P].Perm := Perm; end; -procedure g_Net_BanHost(IP: string; Perm: Boolean = True); overload; +procedure g_Net_BanAddress(IP: string; Perm: Boolean = True); overload; var a: LongWord; b: Boolean; begin b := StrToIp(IP, a); if b then - g_Net_BanHost(a, Perm); + g_Net_BanAddress(a, Perm); end; -procedure g_Net_UnbanNonPermHosts(); +procedure g_Net_UnbanNonPerm(); var I: Integer; begin @@ -2197,16 +2201,16 @@ begin end; end; -function g_Net_UnbanHost(IP: string): Boolean; overload; +function g_Net_UnbanAddress(IP: string): Boolean; overload; var a: LongWord; begin Result := StrToIp(IP, a); if Result then - Result := g_Net_UnbanHost(a); + Result := g_Net_UnbanAddress(a); end; -function g_Net_UnbanHost(IP: LongWord): Boolean; overload; +function g_Net_UnbanAddress(IP: LongWord): Boolean; overload; var I: Integer; begin @@ -2245,6 +2249,36 @@ begin end end; +procedure g_Net_Host_Ban(C: pTNetClient; Perm: Boolean); +var + KickReason: enet_uint32; + Name: string; +begin + if (not C^.Used) then + exit; + + if Perm then + KickReason := NET_DISC_BAN + else + KickReason := NET_DISC_TEMPBAN; + + Name := g_Net_ClientName_ByID(C^.ID); + + g_Net_BanAddress(C^.Peer^.address.host, Perm); + g_Net_Host_Kick(C^.ID, KickReason); + g_Console_Add(Format(_lc[I_PLAYER_BAN], [Name])); + MH_SEND_GameEvent(NET_EV_PLAYER_BAN, 0, Name); + g_Net_Slist_ServerPlayerLeaves(); + g_Net_SaveBanList(); +end; + +procedure g_Net_Host_Ban(ID: Integer; Perm: Boolean); +begin + if (ID < 0) or (ID > High(NetClients)) then + exit; + g_Net_Host_Ban(@NetClients[ID], Perm); +end; + procedure g_Net_Penalize(C: pTNetClient; Reason: string); var s: string; @@ -2270,12 +2304,7 @@ begin if (C^.Crimes >= NetAutoBanLimit) then begin - s := '#' + IntToStr(C^.ID); // can't be arsed - g_Net_BanHost(C^.Peer^.address.host, NetAutoBanPerm); - g_Net_Host_Kick(C^.ID, NET_DISC_BAN); - g_Console_Add(Format(_lc[I_PLAYER_BAN], [s])); - MH_SEND_GameEvent(NET_EV_PLAYER_BAN, 0, s); - g_Net_Slist_ServerPlayerLeaves(); + end; end; diff --git a/src/game/g_netmsg.pas b/src/game/g_netmsg.pas index 1eb3683..b090aa3 100644 --- a/src/game/g_netmsg.pas +++ b/src/game/g_netmsg.pas @@ -340,8 +340,8 @@ end; procedure MH_MalformedPacket(C: pTNetClient); begin g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] + - _lc[I_NET_DISC_PROTOCOL]); - g_Net_Host_Kick(C^.ID, NET_DISC_PROTOCOL); + _lc[I_NET_DISC_BADMSG]); + g_Net_Host_Ban(C, True); end; procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg); @@ -418,9 +418,9 @@ begin Exit; end; - if g_Net_IsHostBanned(C^.Peer^.address.host) then + if g_Net_IsAddressBanned(C^.Peer^.address.host) then begin - if g_Net_IsHostBanned(C^.Peer^.address.host, True) then + if g_Net_IsAddressBanned(C^.Peer^.address.host, True) then begin g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] + _lc[I_NET_DISC_BAN]); -- 2.29.2