2 * Protocol of online ban list:
5 * GET g_ban_sync_uri?action=ban&hostname=...&ip=xxx.xxx.xxx&duration=nnnn&why=...................
6 * (IP 1, 2, 3, or 4 octets, 3 octets for example is a /24 mask)
8 * GET g_ban_sync_uri?action=unban&hostname=...&ip=xxx.xxx.xxx
9 * - Querying the ban list
10 * GET g_ban_sync_uri?action=list&hostname=...&servers=xxx.xxx.xxx.xxx:xxx.xxx.xxx.xxx:...
12 * shows the bans from the listed servers, and possibly others.
13 * Format of a ban is ASCII plain text, four lines per ban, delimited by
14 * newline ONLY (no carriage return):
16 * IP address (also 1, 2, 3, or 4 octets, delimited by dot)
17 * time left in seconds
19 * server IP that registered the ban
22 float Ban_Insert(string ip, float bantime, string reason, float dosync);
24 void OnlineBanList_SendBan(string ip, float bantime, string reason)
29 uri = strcat( "?action=ban&hostname=", uri_escape(cvar_string("hostname")));
30 uri = strcat(uri, "&ip=", uri_escape(ip));
31 uri = strcat(uri, "&duration=", ftos(bantime));
32 uri = strcat(uri, "&why=", uri_escape(reason));
34 n = tokenize_sane(cvar_string("g_ban_sync_uri"));
35 for(i = 0; i < n; ++i)
36 uri_get(strcat(argv(i), uri), 0); // 0 = "discard" callback target
39 void OnlineBanList_SendUnban(string ip)
44 uri = strcat( "?action=unban&hostname=", uri_escape(cvar_string("hostname")));
45 uri = strcat(uri, "&ip=", uri_escape(ip));
47 n = tokenize_sane(cvar_string("g_ban_sync_uri"));
48 for(i = 0; i < n; ++i)
49 uri_get(strcat(argv(i), uri), 0); // 0 = "discard" callback target
52 string OnlineBanList_Servers;
53 float OnlineBanList_Timeout;
54 float OnlineBanList_Requests;
56 void OnlineBanList_URI_Get_Callback(float status, string data)
65 if(OnlineBanList_Requests <= 0)
68 --OnlineBanList_Requests;
70 if(time > OnlineBanList_Timeout)
73 syncinterval = cvar("g_ban_sync_interval");
81 print("Error receiving the online ban list: Status is ", ftos(status), "\n");
85 if(substring(data, 0, 1) == "<")
87 print("Error receiving the online ban list: Received HTML instead of a ban list: ");
91 if(strstrofs(data, "\r", 0) != -1)
93 print("Error receiving the online ban list: Received carriage returns: ");
97 n = tokenizebyseparator(data, "\n");
100 print("Error receiving the online ban list: Received invalid item count: ");
104 for(i = 0; i < n; i += 4)
107 timeleft = stof(argv(i + 1));
108 reason = argv(i + 2);
109 serverip = argv(i + 3);
116 for(j = 0; j < l; ++j)
117 if(strstrofs("0123456789.", substring(ip, j, 1), 0) == -1)
119 print("Invalid character ", substring(ip, j, 1), " in IP address ", ip, ". Skipping this ban.\n");
123 if(strstrofs(strcat(":", OnlineBanList_Servers, ":"), strcat(":", serverip, ":"), 0) != -1)
126 timeleft = min(syncinterval + 15, timeleft);
127 // 15 seconds for safety
128 // the ban will be prolonged on the next sync
129 Ban_Insert(ip, timeleft, strcat("ban synced from ", serverip), 0);
130 print("Ban list syncing: accepted ban of ", ip, " by ", serverip, ": ", reason, "\n");
138 void OnlineBanList_Think()
144 if(cvar_string("g_ban_sync_uri") == "")
146 if(cvar("g_ban_sync_interval") == 0) // < 0 is okay, it means "sync on level start only"
148 argc = tokenize_sane(cvar_string("g_ban_sync_trusted_servers"));
152 if(OnlineBanList_Requests == 0) // only if there is no ongoing request!
154 if(OnlineBanList_Servers)
155 strunzone(OnlineBanList_Servers);
156 OnlineBanList_Servers = argv(0);
157 for(i = 1; i < argc; ++i)
158 OnlineBanList_Servers = strcat(OnlineBanList_Servers, ":", argv(i));
159 OnlineBanList_Servers = strzone(OnlineBanList_Servers);
161 uri = strcat( "?action=list&hostname=", uri_escape(cvar_string("hostname")));
162 uri = strcat(uri, "&servers=", uri_escape(OnlineBanList_Servers));
164 OnlineBanList_Timeout = time + 10;
166 n = tokenize_sane(cvar_string("g_ban_sync_uri"));
167 for(i = 0; i < n; ++i)
169 ++OnlineBanList_Requests;
170 uri_get(strcat(argv(i), uri), 1); // 1 = "banlist" callback target
174 if(cvar("g_ban_sync_interval") > 0)
175 self.nextthink = time + max(60, cvar("g_ban_sync_interval") * 60);
180 string ban_ip[BAN_MAX];
181 float ban_expire[BAN_MAX];
199 for(i = 0; i < ban_count; ++i)
201 if(time > ban_expire[i])
203 out = strcat(out, " ", ban_ip[i]);
204 out = strcat(out, " ", ftos(ban_expire[i] - time));
206 if(strlen(out) <= 1) // no real entries
207 cvar_set("g_banned_list", "");
209 cvar_set("g_banned_list", out);
212 float Ban_Delete(float i)
218 if(ban_expire[i] == 0)
220 if(ban_expire[i] > 0)
222 OnlineBanList_SendUnban(ban_ip[i]);
223 strunzone(ban_ip[i]);
234 for(i = 0; i < ban_count; ++i)
238 n = tokenize_sane(cvar_string("g_banned_list"));
239 if(stof(argv(0)) == 1)
241 ban_count = (n - 1) / 2;
242 for(i = 0; i < ban_count; ++i)
244 ban_ip[i] = strzone(argv(2*i+1));
245 ban_expire[i] = time + stof(argv(2*i+2));
251 e.classname = "bansyncer";
252 e.think = OnlineBanList_Think;
253 e.nextthink = time + 1;
260 for(i = 0; i < ban_count; ++i)
262 if(time > ban_expire[i])
264 msg = strcat("#", ftos(i), ": ");
265 msg = strcat(msg, ban_ip[i], " is still banned for ");
266 msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
271 float Ban_GetClientIP(entity client)
274 n = tokenizebyseparator(client.netaddress, ".");
277 ban_ip1 = strcat1(argv(0));
278 ban_ip2 = strcat(ban_ip1, ".", argv(1));
279 ban_ip3 = strcat(ban_ip2, ".", argv(2));
280 ban_ip4 = strcat(ban_ip3, ".", argv(3));
284 float Ban_IsClientBanned(entity client, float idx)
289 if(!Ban_GetClientIP(client))
301 for(i = b; i < e; ++i)
304 if(time > ban_expire[i])
307 if(ban_ip1 == s) return TRUE;
308 if(ban_ip2 == s) return TRUE;
309 if(ban_ip3 == s) return TRUE;
310 if(ban_ip4 == s) return TRUE;
315 float Ban_MaybeEnforceBan(entity client)
317 if(Ban_IsClientBanned(client, -1))
320 s = strcat("^1NOTE:^7 banned client ", client.netaddress, " just tried to enter\n");
328 float Ban_Insert(string ip, float bantime, string reason, float dosync)
337 for(i = 0; i < ban_count; ++i)
341 if(time + bantime > ban_expire[i])
343 ban_expire[i] = time + bantime;
344 print(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now\n");
350 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
351 OnlineBanList_SendBan(ip, bantime, reason);
355 // do we have a free slot?
356 for(i = 0; i < ban_count; ++i)
357 if(time > ban_expire[i])
359 // no free slot? Then look for the one who would get unbanned next
363 bestscore = ban_expire[i];
364 for(j = 1; j < ban_count; ++j)
366 if(ban_expire[j] < bestscore)
369 bestscore = ban_expire[i];
373 // if we replace someone, will we be banned longer than him (so long-term
374 // bans never get overridden by short-term bans)
375 if(ban_expire[i] > time + bantime)
377 // okay, insert our new victim as i
379 print(ip, " has been banned for ", ftos(bantime), " seconds\n");
380 ban_expire[i] = time + bantime;
381 ban_ip[i] = strzone(ip);
382 ban_count = max(ban_count, i + 1);
386 // Enforce our new ban
388 FOR_EACH_REALCLIENT(e)
389 if(Ban_IsClientBanned(e, i))
394 reason = strcat(reason, ": affects ");
396 reason = strcat(reason, ", ");
397 reason = strcat(reason, e.netname);
399 s = strcat(s, "^1NOTE:^7 banned client ", e.netname, "^7 has to go\n");
407 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
408 OnlineBanList_SendBan(ip, bantime, reason);
413 void Ban_KickBanClient(entity client, float bantime, float masksize, string reason)
415 if(!Ban_GetClientIP(client))
417 sprint(client, strcat("Kickbanned: ", reason, "\n"));
425 Ban_Insert(ban_ip1, bantime, reason, 1);
428 Ban_Insert(ban_ip2, bantime, reason, 1);
431 Ban_Insert(ban_ip3, bantime, reason, 1);
434 Ban_Insert(ban_ip4, bantime, reason, 1);
438 * not needed, as we enforce the ban in Ban_Insert anyway
440 sprint(client, strcat("Kickbanned: ", reason, "\n"));
445 float GameCommand_Ban(string command)
455 argc = tokenize_sane(command);
456 if(argv(0) == "help")
458 print(" kickban # n m p reason - kickban player n for m seconds, using mask size p (1 to 4)\n");
459 print(" ban ip m reason - ban an IP or range (incomplete IP, like 1.2.3) for m seconds\n");
460 print(" bans - list all existing bans\n");
461 print(" unban n - delete the entry #n from the bans list\n");
464 if(argv(0) == "kickban")
466 #define INITARG(c) reasonarg = c
467 #define GETARG(v,d) if((argc > reasonarg) && ((v = stof(argv(reasonarg))) != 0)) ++reasonarg; else v = d
468 #define RESTARG(v) if(argc > reasonarg) v = substring(command, argv_start_index(reasonarg), strlen(command) - argv_start_index(reasonarg)); else v = ""
471 entno = stof(argv(2));
472 if(entno > maxclients || entno < 1)
474 client = edict_num(entno);
477 GETARG(bantime, cvar("g_ban_default_bantime"));
478 GETARG(masksize, cvar("g_ban_default_masksize"));
481 Ban_KickBanClient(client, bantime, masksize, reason);
485 else if(argv(0) == "ban")
493 GETARG(bantime, cvar("g_ban_default_bantime"));
496 Ban_Insert(ip, bantime, reason, 1);
503 else if(argv(0) == "bans")
508 else if(argv(0) == "unban")