Remote Unbanning

This commit is contained in:
ChocoTaco 2022-04-11 17:22:00 -04:00
parent 3d876eb0de
commit 4d7cc2cc55
2 changed files with 220 additions and 43 deletions

228
Classic/scripts/autoexec/dtBanSystem.cs Executable file → Normal file
View file

@ -11,12 +11,12 @@
//$dtBanList::GUID3555379 = "4\t2021\t18\t31\t518400"; //$dtBanList::GUID3555379 = "4\t2021\t18\t31\t518400";
//TO UNBAN SOMEONE WITHOUT RESTARTING THE SERVER //TO UNBAN SOMEONE WITHOUT RESTARTING THE SERVER
//unban(%guid,%ip); in console //banList();in console
//unbanIndex(%index) %index is the number next to the players name from listBans();
//Example: unban(555555,"22.222.222.222"); put ip in quotes //Example: unban(555555,"22.222.222.222"); put ip in quotes
package dtBan package dtBan
{ {
function ClassicLoadBanlist() function ClassicLoadBanlist()
{ {
$ClassicPermaBans = 0; $ClassicPermaBans = 0;
@ -30,21 +30,35 @@ function BanList::add(%guid, %ipAddress, %time){
if(%time > 999999){ if(%time > 999999){
%time = "BAN"; %time = "BAN";
} }
%name = getClientBanName(%guid, %ipAddress);
if (%guid > 0){ if (%guid > 0){
$dtBanList::GUID[%guid] = dtBanMark() TAB %time; $dtBanList::GUID[%guid] = dtBanMark() TAB %time TAB %name;
} }
if (getSubStr(%ipAddress, 0, 3) $= "IP:"){ if (getSubStr(%ipAddress, 0, 3) $= "IP:"){
// add IP ban
%bareIP = getSubStr(%ipAddress, 3, strLen(%ipAddress)); %bareIP = getSubStr(%ipAddress, 3, strLen(%ipAddress));
%bareIP = getSubStr(%bareIP, 0, strstr(%bareIP, ":")); %bareIP = getSubStr(%bareIP, 0, strstr(%bareIP, ":"));
%bareIP = strReplace(%bareIP, ".", "_"); // variable access bug workaround %bareIP = strReplace(%bareIP, ".", "_"); // variable access bug workaround
// add IP ban
$dtBanList::IP[%bareIP] = dtBanMark() TAB %time; $dtBanList::IP[%bareIP] = dtBanMark() TAB %time TAB %name;
//error("ban" SPC %bareIP SPC $dtBanList::IP[%bareIP]); }
%found = 0;
for (%i = 0; %i < $dtBanList::NameListCount; %i++){
if(getField($dtBanList::NameList[%i], 0) $= %name){
%found =1;
if(%guid > 0)
$dtBanList::NameList[%i] = setField($dtBanList::NameList[%i], 1, %guid);
if(getSubStr(%ipAddress, 0, 3) $= "IP:")
$dtBanList::NameList[%i] = setField($dtBanList::NameList[%i], 2, %bareIP);
break;
}
}
if(!%found){
$dtBanList::NameListCount = ($dtBanList::NameListCount > 0) ? $dtBanList::NameListCount : 0;
$dtBanList::NameList[$dtBanList::NameListCount] = %name TAB %guid TAB %bareIP;
$dtBanList::NameListCount++;
} }
// write out the updated bans to the file // write out the updated bans to the file
export("$dtBanList*", $Host::dtBanlist); saveBanList();
} }
function banList_checkIP(%client){ function banList_checkIP(%client){
@ -56,14 +70,19 @@ function banList_checkIP(%client){
%time = $dtBanList::IP[%ip]; %time = $dtBanList::IP[%ip];
if(%time $= "BAN") if(%time $= "BAN")
return 1; return 1;
if (%time !$= "" && %time != 0){ if (getFieldCount(%time) > 0){
%delta = getBanCount(getField(%time,0), getField(%time,1),getField(%time,2),getField(%time,3)); %delta = getBanCount(getField(%time,0), getField(%time,1),getField(%time,2),getField(%time,3));
if (%delta < getField(%time,4)) if (%delta < getField(%time,4))
return 1; return 1;
else{ else{
for (%i = 0; %i < $dtBanList::NameListCount; %i++){
if(getField($dtBanList::NameList[%i], 2) $= %ip){
unbanIndex(%i);
break;
}
}
$dtBanList::IP[%ip] = ""; $dtBanList::IP[%ip] = "";
schedule(1000,0,"export","$dtBanList*", $Host::dtBanlist); saveBanList();
//export("$dtBanList*", "prefs/dtBanlist.cs");
} }
} }
return 0; return 0;
@ -73,14 +92,19 @@ function banList_checkGUID(%guid){
%time = $dtBanList::GUID[%guid]; %time = $dtBanList::GUID[%guid];
if(%time $= "BAN") if(%time $= "BAN")
return 1; return 1;
if (%time !$= "" && %time != 0){ if (getFieldCount(%time) > 0){
%delta = getBanCount(getField(%time,0), getField(%time,1),getField(%time,2),getField(%time,3)); %delta = getBanCount(getField(%time,0), getField(%time,1),getField(%time,2),getField(%time,3));
if (%delta < getField(%time,4)) if (%delta < getField(%time,4))
return 1; return 1;
else{ else{
$dtBanList::GUID[%guid] = ""; for (%i = 0; %i < $dtBanList::NameListCount; %i++){
schedule(500,0,"export","$dtBanList*", $Host::dtBanlist); if(getField($dtBanList::NameList[%i], 1) $= %guid){
//export("$dtBanList*", "prefs/dtBanlist.cs"); unbanIndex(%i);
break;
}
}
$dtBanList::GUID[%guid] = "";
saveBanList();
} }
} }
return 0; return 0;
@ -88,31 +112,63 @@ function banList_checkGUID(%guid){
}; };
if (!isActivePackage(dtBan)) if (!isActivePackage(dtBan)){
activatePackage(dtBan); activatePackage(dtBan);
}
function saveBanList(){
if(!isEventPending($banEvent))
$banEvent = schedule(1000,0,"export","$dtBanList*", $Host::dtBanlist);
}
function getClientBanName(%guid, %ip){
%found = 0;
for (%i = 0; %i < ClientGroup.getCount(); %i++){
%client = ClientGroup.getObject(%i);
if(%guid > 0 && %client.guid $= %guid){
%found = 1;
break;
}
else if(%client.getAddress() $= %ip){
%found = 1;
break;
}
}
if(%found){
%authInfo = %client.getAuthInfo();
%realName = getField( %authInfo, 0 );
if(%realName !$= "")
%name = %realName;
else
%name = stripChars( detag( getTaggedString( %client.name ) ), "\cp\co\c6\c7\c8\c9\c0" );
return trim(%name);
}
return 0;
}
function getBanCount(%d, %year, %h, %n){ function getBanCount(%d, %year, %h, %n){
%dif = formattimestring("yy") - %year; if(%d && %year && %h && %n){
%days += 365 * (%dif-1); %dif = formattimestring("yy") - %year;
%days += 365 - %d; %days += 365 * (%dif-1);
%days += dtBanDay(); %days += 365 - %d;
%ht = %nt = 0; %days += dtBanDay();
if(formattimestring("H") > %h){ %ht = %nt = 0;
%ht = formattimestring("H") - %h; if(formattimestring("H") > %h){
%ht = formattimestring("H") - %h;
}
else if(formattimestring("H") < %h){
%ht = 24 - %h;
%ht = formattimestring("H")+ %ht;
}
if(formattimestring("n") > %n){
%nt = formattimestring("n") - %n;
}
else if(formattimestring("n") < %n){
%nt = 60 - %n;
%nt = formattimestring("n") + %nt;
}
return mfloor((%days * 1440) + (%ht*60) + %nt);
} }
else if(formattimestring("H") < %h){ return 0;
%ht = 24 - %h;
%ht = formattimestring("H")+ %ht;
}
if(formattimestring("n") > %n){
%nt = formattimestring("n") - %n;
}
else if(formattimestring("n") < %n){
%nt = 60 - %n;
%nt = formattimestring("n") + %nt;
}
return mfloor((%days * 1440) + (%ht*60) + %nt);
//return mfloor((%days * 1440) + (%ht*60) + %nt) TAB (%days * 1440) TAB (%ht*60) TAB %nt; //return mfloor((%days * 1440) + (%ht*60) + %nt) TAB (%days * 1440) TAB (%ht*60) TAB %nt;
} }
@ -146,15 +202,109 @@ function dtBanMark(){
return %count + %d TAB formattimestring("yy") TAB formattimestring("H") TAB formattimestring("n"); return %count + %d TAB formattimestring("yy") TAB formattimestring("H") TAB formattimestring("n");
} }
function unban(%guid,%ip){ function banList(){
$dtBanList::NameListCount = ($dtBanList::NameListCount > 0) ? $dtBanList::NameListCount : 0;
if(!$dtBanList::NameListCount){
error("No bans, see" SPC $Host::dtBanlist SPC "for older system entries");
}
else{
for (%i = 0; %i < $dtBanList::NameListCount; %i++){
%fieldList = $dtBanList::NameList[%i];
error("index:" @ %i SPC "Name:" @ getField(%fieldList,0) SPC "GUID:" @ getField(%fieldList,1) SPC "IP:" @ getField(%fieldList,2));
}
if($dtBanList::NameListCount > 0){
error("Use unbanIndex(%index); to unban user from the list ");
}
}
}
function unbanIndex(%index){
%fieldList = $dtBanList::NameList[%index];
%name = getField(%fieldList, 0);
if(getFieldCount(%fieldList) > 1){
for(%i = %index; %i < $dtBanList::NameListCount -1; %i++) {
$dtBanList::NameList[%i] =$dtBanList::NameList[%i+1];
}
$dtBanList::NameList[%i] = "";
$dtBanList::NameListCount--;
error("Name" SPC getField(%fieldList,0) SPC "UNBANNED");
%guid = getField(%fieldList,1);
if($dtBanList::GUID[%guid] !$= ""){
$dtBanList::GUID[%guid] = "";
error("GUID" SPC %guid SPC "UNBANNED");
}
%ip = getField(%fieldList,2);
if($dtBanList::IP[%ip] !$= ""){
$dtBanList::IP[%ip] = "";
error("IP" SPC %ip SPC "UNBANNED");
}
}
else{
for(%i = %index; %i < $dtBanList::NameListCount -1; %i++) {
$dtBanList::NameList[%i] =$dtBanList::NameList[%i+1];
}
$dtBanList::NameList[%i] = "";
$dtBanList::NameListCount--;
error("Error removing invalid index");
}
saveBanList();
return %name;
}
function banListClean(){
%count = 0;
for(%i = 0; %i < $dtBanList::NameListCount; %i++) {
%ban = 1;
%banField = $dtBanList::NameList[%i];
%guid = getField(%banField,1);
%ip = getField(%banField,2);
%time = $dtBanList::IP[%ip];
if(%time !$= "BAN" && getFieldCount(%time) > 0){
%delta = getBanCount(getField(%time,0), getField(%time,1),getField(%time,2),getField(%time,3));
if (%delta > getField(%time,4)){
$dtBanList::IP[%ip] = "";
%ban = 0;
}
}
%time = $dtBanList::GUID[%guid];
if(%time !$= "BAN" && getFieldCount(%time) > 0){
%delta = getBanCount(getField(%time,0), getField(%time,1),getField(%time,2),getField(%time,3));
if (%delta > getField(%time,4)){
$dtBanList::GUID[%guid] = "";
%ban = 0;
}
}
if(%banField !$= "" && %ban){
%tempList[%count] = %banField;
%count++;
}
}
for(%i = 0; %i < %count; %i++) {
$dtBanList::NameList[%i] = %tempList[%i];
}
$dtBanList::NameListCount = %count;
}
//old method
function unbanold(%guid,%ip){
%ip = strReplace(%ip, ".", "_");
for (%i = 0; %i < $dtBanList::NameListCount; %i++){
if(getField($dtBanList::NameList[%i], 2) $= %ip || getField($dtBanList::NameList[%i], 1) $= %guid){
unbanIndex(%i);
return;
}
}
if($dtBanList::GUID[%guid] !$= ""){ if($dtBanList::GUID[%guid] !$= ""){
$dtBanList::GUID[%guid] = ""; $dtBanList::GUID[%guid] = "";
error("GUID" SPC %guid SPC "UNBANNED"); error("GUID" SPC %guid SPC "UNBANNED");
} }
%ip = strReplace(%ip, ".", "_");
if($dtBanList::IP[%ip] !$= ""){ if($dtBanList::IP[%ip] !$= ""){
$dtBanList::IP[%ip] = ""; $dtBanList::IP[%ip] = "";
error("IP" SPC %ip SPC "UNBANNED"); error("IP" SPC %ip SPC "UNBANNED");
} }
export("$dtBanList*", $Host::dtBanlist); saveBanList();
} }

View file

@ -5,7 +5,7 @@
//exec("scripts/autoexec/zzDiscordBot.cs"); //exec("scripts/autoexec/zzDiscordBot.cs");
//ip of the bot //ip of the bot
$discordBot::IP = ""; $discordBot::IP = "127.0.0.1:28003";
$discordBot::reconnectTimeout = 3 * 60000; $discordBot::reconnectTimeout = 3 * 60000;
//auto connect on start //auto connect on start
$discordBot::autoStart = 0; $discordBot::autoStart = 0;
@ -203,7 +203,6 @@ function discordBotProcess(%type, %var1, %var2, %var3, %var4, %var5, %var6)
sendToDiscordEmote(%msg, $discordBot::serverFeed); sendToDiscordEmote(%msg, $discordBot::serverFeed);
} }
} }
function sendToDiscord(%msg,%channel) function sendToDiscord(%msg,%channel)
{ {
if(isObject(discord) && %msg !$= "") if(isObject(discord) && %msg !$= "")
@ -353,6 +352,33 @@ function discord::onLine(%this, %line){
discord.schedule((%i+1)*32,"send","PROCSTACK" @ $discordBot::cmdSplit @ ($discordBot::monitorChannel) @ $discordBot::cmdSplit @ "msgList" @ "\r\n"); discord.schedule((%i+1)*32,"send","PROCSTACK" @ $discordBot::cmdSplit @ ($discordBot::monitorChannel) @ $discordBot::cmdSplit @ "msgList" @ "\r\n");
} }
} }
case "BANLIST":
if($dtBanList::NameListCount){
for (%i = 0; %i < $dtBanList::NameListCount; %i++){
%fieldList = $dtBanList::NameList[%i];
%msg = "Index:" @ %i SPC "Name:" @ getField(%fieldList,0) SPC "GUID:" @ getField(%fieldList,1) SPC "IP:" @ getField(%fieldList,2);
discord.schedule(%i*32,"send","MSGSTACK" @ $discordBot::cmdSplit @ ($discordBot::monitorChannel) @ $discordBot::cmdSplit @ %msg @ "\r\n");
}
discord.schedule((%i+1)*32,"send","PROCSTACK" @ $discordBot::cmdSplit @ ($discordBot::monitorChannel) @ $discordBot::cmdSplit @ "banList" @ "\r\n");
}
else{
sendToDiscord("No active bans, see ban file for manual/older entries", $discordBot::monitorChannel);
}
case "UNBANINDEX":
%var = getWord(%lineStrip,1);
if(%var < $dtBanList::NameListCount){
%name = unbanIndex(%var);
if(%name !$= ""){
sendToDiscord("User:" @ %name SPC "has been unbanned", $discordBot::monitorChannel);
}
else{
sendToDiscord("Index Removed", $discordBot::monitorChannel);
}
}
else{
sendToDiscord("Invalid Index", $discordBot::monitorChannel);
}
default: default:
error("Discord Bad Command" SPC %line); error("Discord Bad Command" SPC %line);
} }
@ -483,3 +509,4 @@ function pathMapData(){ //loop to collect player position data
sendPrx(0); sendPrx(0);
} }
} }