2 * Protocol of online ban list:
5 * GET g_ban_sync_uri?action=ban&hostname=...&ip=xxx.xxx.xxx&duration=nnnn&reason=...................
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, "&reason=", 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(cvar("g_ban_sync_trusted_servers_verify"))
124 if((strstrofs(strcat(";", OnlineBanList_Servers, ";"), strcat(";", serverip, ";"), 0) == -1))
128 timeleft = min(syncinterval + 15, timeleft);
129 // 15 seconds for safety
130 // the ban will be prolonged on the next sync
131 Ban_Insert(ip, timeleft, strcat("ban synced from ", serverip), 0);
132 print("Ban list syncing: accepted ban of ", ip, " by ", serverip, ": ", reason, "\n");
139 void OnlineBanList_Think()
145 if(cvar_string("g_ban_sync_uri") == "")
147 if(cvar("g_ban_sync_interval") == 0) // < 0 is okay, it means "sync on level start only"
149 argc = tokenize_sane(cvar_string("g_ban_sync_trusted_servers"));
153 if(OnlineBanList_Requests == 0) // only if there is no ongoing request!
155 if(OnlineBanList_Servers)
156 strunzone(OnlineBanList_Servers);
157 OnlineBanList_Servers = argv(0);
158 for(i = 1; i < argc; ++i)
159 OnlineBanList_Servers = strcat(OnlineBanList_Servers, ";", argv(i));
160 OnlineBanList_Servers = strzone(OnlineBanList_Servers);
162 uri = strcat( "?action=list&hostname=", uri_escape(cvar_string("hostname")));
163 uri = strcat(uri, "&servers=", uri_escape(OnlineBanList_Servers));
165 OnlineBanList_Timeout = time + 10;
167 n = tokenize_sane(cvar_string("g_ban_sync_uri"));
168 for(i = 0; i < n; ++i)
170 ++OnlineBanList_Requests;
171 uri_get(strcat(argv(i), uri), 1); // 1 = "banlist" callback target
175 if(cvar("g_ban_sync_interval") > 0)
176 self.nextthink = time + max(60, cvar("g_ban_sync_interval") * 60);
181 string ban_ip[BAN_MAX];
182 float ban_expire[BAN_MAX];
200 for(i = 0; i < ban_count; ++i)
202 if(time > ban_expire[i])
204 out = strcat(out, " ", ban_ip[i]);
205 out = strcat(out, " ", ftos(ban_expire[i] - time));
207 if(strlen(out) <= 1) // no real entries
208 cvar_set("g_banned_list", "");
210 cvar_set("g_banned_list", out);
213 float Ban_Delete(float i)
219 if(ban_expire[i] == 0)
221 if(ban_expire[i] > 0)
223 OnlineBanList_SendUnban(ban_ip[i]);
224 strunzone(ban_ip[i]);
235 for(i = 0; i < ban_count; ++i)
239 n = tokenize_sane(cvar_string("g_banned_list"));
240 if(stof(argv(0)) == 1)
242 ban_count = (n - 1) / 2;
243 for(i = 0; i < ban_count; ++i)
245 ban_ip[i] = strzone(argv(2*i+1));
246 ban_expire[i] = time + stof(argv(2*i+2));
252 e.classname = "bansyncer";
253 e.think = OnlineBanList_Think;
254 e.nextthink = time + 1;
261 for(i = 0; i < ban_count; ++i)
263 if(time > ban_expire[i])
265 msg = strcat("#", ftos(i), ": ");
266 msg = strcat(msg, ban_ip[i], " is still banned for ");
267 msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
272 float Ban_GetClientIP(entity client)
275 n = tokenizebyseparator(client.netaddress, ".");
278 ban_ip1 = strcat1(argv(0));
279 ban_ip2 = strcat(ban_ip1, ".", argv(1));
280 ban_ip3 = strcat(ban_ip2, ".", argv(2));
281 ban_ip4 = strcat(ban_ip3, ".", argv(3));
285 float Ban_IsClientBanned(entity client, float idx)
290 if(!Ban_GetClientIP(client))
302 for(i = b; i < e; ++i)
305 if(time > ban_expire[i])
308 if(ban_ip1 == s) return TRUE;
309 if(ban_ip2 == s) return TRUE;
310 if(ban_ip3 == s) return TRUE;
311 if(ban_ip4 == s) return TRUE;
316 float Ban_MaybeEnforceBan(entity client)
318 if(Ban_IsClientBanned(client, -1))
321 s = strcat("^1NOTE:^7 banned client ", client.netaddress, " just tried to enter\n");
329 float Ban_Insert(string ip, float bantime, string reason, float dosync)
338 for(i = 0; i < ban_count; ++i)
342 if(time + bantime > ban_expire[i])
344 ban_expire[i] = time + bantime;
345 print(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now\n");
351 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
352 OnlineBanList_SendBan(ip, bantime, reason);
356 // do we have a free slot?
357 for(i = 0; i < ban_count; ++i)
358 if(time > ban_expire[i])
360 // no free slot? Then look for the one who would get unbanned next
364 bestscore = ban_expire[i];
365 for(j = 1; j < ban_count; ++j)
367 if(ban_expire[j] < bestscore)
370 bestscore = ban_expire[i];
374 // if we replace someone, will we be banned longer than him (so long-term
375 // bans never get overridden by short-term bans)
376 if(ban_expire[i] > time + bantime)
378 // okay, insert our new victim as i
380 print(ip, " has been banned for ", ftos(bantime), " seconds\n");
381 ban_expire[i] = time + bantime;
382 ban_ip[i] = strzone(ip);
383 ban_count = max(ban_count, i + 1);
387 // Enforce our new ban
389 FOR_EACH_REALCLIENT(e)
390 if(Ban_IsClientBanned(e, i))
395 reason = strcat(reason, ": affects ");
397 reason = strcat(reason, ", ");
398 reason = strcat(reason, e.netname);
400 s = strcat(s, "^1NOTE:^7 banned client ", e.netname, "^7 has to go\n");
408 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
409 OnlineBanList_SendBan(ip, bantime, reason);
414 void Ban_KickBanClient(entity client, float bantime, float masksize, string reason)
416 if(!Ban_GetClientIP(client))
418 sprint(client, strcat("Kickbanned: ", reason, "\n"));
426 Ban_Insert(ban_ip1, bantime, reason, 1);
429 Ban_Insert(ban_ip2, bantime, reason, 1);
432 Ban_Insert(ban_ip3, bantime, reason, 1);
435 Ban_Insert(ban_ip4, bantime, reason, 1);
439 * not needed, as we enforce the ban in Ban_Insert anyway
441 sprint(client, strcat("Kickbanned: ", reason, "\n"));
446 float GameCommand_Ban(string command)
456 argc = tokenize_sane(command);
457 if(argv(0) == "help")
459 print(" kickban # n m p reason - kickban player n for m seconds, using mask size p (1 to 4)\n");
460 print(" ban ip m reason - ban an IP or range (incomplete IP, like 1.2.3) for m seconds\n");
461 print(" bans - list all existing bans\n");
462 print(" unban n - delete the entry #n from the bans list\n");
465 if(argv(0) == "kickban")
467 #define INITARG(c) reasonarg = c
468 #define GETARG(v,d) if((argc > reasonarg) && ((v = stof(argv(reasonarg))) != 0)) ++reasonarg; else v = d
469 #define RESTARG(v) if(argc > reasonarg) v = substring(command, argv_start_index(reasonarg), strlen(command) - argv_start_index(reasonarg)); else v = ""
472 entno = stof(argv(2));
473 if(entno > maxclients || entno < 1)
475 client = edict_num(entno);
478 GETARG(bantime, cvar("g_ban_default_bantime"));
479 GETARG(masksize, cvar("g_ban_default_masksize"));
482 Ban_KickBanClient(client, bantime, masksize, reason);
486 else if(argv(0) == "ban")
494 GETARG(bantime, cvar("g_ban_default_bantime"));
497 Ban_Insert(ip, bantime, reason, 1);
504 else if(argv(0) == "bans")
509 else if(argv(0) == "unban")