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)
336 // Enforce our new ban
338 FOR_EACH_REALCLIENT(e)
339 if(Ban_IsClientBanned(e, i))
342 reason = strcat(reason, ": affects ");
344 reason = strcat(reason, ", ");
345 reason = strcat(reason, e.netname);
346 s = strcat(s, "^1NOTE:^7 banned client ", e.netname, "^7 has to go\n");
353 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
354 OnlineBanList_SendBan(ip, bantime, reason);
357 for(i = 0; i < ban_count; ++i)
361 if(time + bantime > ban_expire[i])
363 ban_expire[i] = time + bantime;
364 print(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now\n");
369 // do we have a free slot?
370 for(i = 0; i < ban_count; ++i)
371 if(time > ban_expire[i])
373 // no free slot? Then look for the one who would get unbanned next
377 bestscore = ban_expire[i];
378 for(j = 1; j < ban_count; ++j)
380 if(ban_expire[j] < bestscore)
383 bestscore = ban_expire[i];
387 // if we replace someone, will we be banned longer than him (so long-term
388 // bans never get overridden by short-term bans)
389 if(ban_expire[i] > time + bantime)
391 // okay, insert our new victim as i
393 print(ip, " has been banned for ", ftos(bantime), " seconds\n");
394 ban_expire[i] = time + bantime;
395 ban_ip[i] = strzone(ip);
396 ban_count = max(ban_count, i + 1);
403 void Ban_KickBanClient(entity client, float bantime, float masksize, string reason)
405 if(!Ban_GetClientIP(client))
407 sprint(client, strcat("Kickbanned: ", reason, "\n"));
415 Ban_Insert(ban_ip1, bantime, reason, 1);
418 Ban_Insert(ban_ip2, bantime, reason, 1);
421 Ban_Insert(ban_ip3, bantime, reason, 1);
424 Ban_Insert(ban_ip4, bantime, reason, 1);
428 * not needed, as we enforce the ban in Ban_Insert anyway
430 sprint(client, strcat("Kickbanned: ", reason, "\n"));
435 float GameCommand_Ban(string command)
444 argc = tokenize_sane(command);
445 if(argv(0) == "help")
447 print(" kickban # n m p - kickban player n for m seconds, using mask size p (1 to 4)\n");
448 print(" ban ip m - ban an IP or range (incomplete IP, like 1.2.3) for m seconds\n");
449 print(" bans - list all existing bans\n");
450 print(" unban n - delete the entry #n from the bans list\n");
453 if(argv(0) == "kickban")
457 entno = stof(argv(2));
458 if(entno > maxclients || entno < 1)
460 client = edict_num(entno);
462 bantime = stof(argv(3));
464 bantime = cvar("g_ban_default_bantime");
466 masksize = stof(argv(4));
468 masksize = cvar("g_ban_default_masksize");
470 reason = substring(command, argv_start_index(5), strlen(command) - argv_start_index(5));
473 Ban_KickBanClient(client, bantime, masksize, reason);
477 else if(argv(0) == "ban")
484 bantime = stof(argv(2));
486 bantime = cvar("g_ban_default_bantime");
488 reason = substring(command, argv_start_index(3), strlen(command) - argv_start_index(3));
491 Ban_Insert(ip, bantime, reason, 1);
495 else if(argv(0) == "bans")
500 else if(argv(0) == "unban")