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 if(n >= MAX_IPBAN_URIS)
37 for(i = 0; i < n; ++i)
38 uri_get(strcat(argv(i), uri), URI_GET_DISCARD); // 0 = "discard" callback target
41 void OnlineBanList_SendUnban(string ip)
46 uri = strcat( "?action=unban&hostname=", uri_escape(cvar_string("hostname")));
47 uri = strcat(uri, "&ip=", uri_escape(ip));
49 n = tokenize_sane(cvar_string("g_ban_sync_uri"));
50 if(n >= MAX_IPBAN_URIS)
52 for(i = 0; i < n; ++i)
53 uri_get(strcat(argv(i), uri), URI_GET_DISCARD); // 0 = "discard" callback target
56 string OnlineBanList_Servers;
57 float OnlineBanList_Timeout;
58 float OnlineBanList_RequestWaiting[MAX_IPBAN_URIS];
60 void OnlineBanList_URI_Get_Callback(float id, float status, string data)
72 if(id >= MAX_IPBAN_URIS)
74 print("Received ban list for invalid ID\n");
78 tokenize_sane(cvar_string("g_ban_sync_uri"));
81 print("Received ban list from ", uri, ": ");
83 if(OnlineBanList_RequestWaiting[id] == 0)
85 print("rejected (unexpected)\n");
89 OnlineBanList_RequestWaiting[id] = 0;
91 if(time > OnlineBanList_Timeout)
93 print("rejected (too late)\n");
97 syncinterval = cvar("g_ban_sync_interval");
100 print("rejected (syncing disabled)\n");
108 print("error: status is ", ftos(status), "\n");
112 if(substring(data, 0, 1) == "<")
114 print("error: received HTML instead of a ban list: ");
118 if(strstrofs(data, "\r", 0) != -1)
120 print("error: received carriage returns: ");
127 n = tokenizebyseparator(data, "\n");
131 print("error: received invalid item count: ", ftos(n), "\n");
135 print("OK, ", ftos(n), " items\n");
137 for(i = 0; i < n; i += 4)
140 timeleft = stof(argv(i + 1));
141 reason = argv(i + 2);
142 serverip = argv(i + 3);
144 dprint("received ban list item ", ftos(i), ": ip=", ip);
145 dprint(" timeleft=", ftos(timeleft), " reason=", reason);
146 dprint(" serverip=", serverip, "\n");
148 timeleft -= 1.5 * cvar("g_ban_sync_timeout");
153 for(j = 0; j < l; ++j)
154 if(strstrofs("0123456789.", substring(ip, j, 1), 0) == -1)
156 print("Invalid character ", substring(ip, j, 1), " in IP address ", ip, ". Skipping this ban.\n");
160 if(cvar("g_ban_sync_trusted_servers_verify"))
161 if((strstrofs(strcat(";", OnlineBanList_Servers, ";"), strcat(";", serverip, ";"), 0) == -1))
165 timeleft = min(syncinterval + (OnlineBanList_Timeout - time) + 5, timeleft);
166 // the ban will be prolonged on the next sync
167 // or expire 5 seconds after the next timeout
168 Ban_Insert(ip, timeleft, strcat("ban synced from ", serverip, " at ", uri), 0);
169 print("Ban list syncing: accepted ban of ", ip, " by ", serverip, " at ", uri, ": ");
176 void OnlineBanList_Think()
182 if(cvar_string("g_ban_sync_uri") == "")
184 if(cvar("g_ban_sync_interval") == 0) // < 0 is okay, it means "sync on level start only"
186 argc = tokenize_sane(cvar_string("g_ban_sync_trusted_servers"));
190 if(OnlineBanList_Servers)
191 strunzone(OnlineBanList_Servers);
192 OnlineBanList_Servers = argv(0);
193 for(i = 1; i < argc; ++i)
194 OnlineBanList_Servers = strcat(OnlineBanList_Servers, ";", argv(i));
195 OnlineBanList_Servers = strzone(OnlineBanList_Servers);
197 uri = strcat( "?action=list&hostname=", uri_escape(cvar_string("hostname")));
198 uri = strcat(uri, "&servers=", uri_escape(OnlineBanList_Servers));
200 OnlineBanList_Timeout = time + cvar("g_ban_sync_timeout");
202 n = tokenize_sane(cvar_string("g_ban_sync_uri"));
203 if(n >= MAX_IPBAN_URIS)
205 for(i = 0; i < n; ++i)
207 if(OnlineBanList_RequestWaiting[i])
209 OnlineBanList_RequestWaiting[i] = 1;
210 uri_get(strcat(argv(i), uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
213 if(cvar("g_ban_sync_interval") > 0)
214 self.nextthink = time + max(60, cvar("g_ban_sync_interval") * 60);
219 string ban_ip[BAN_MAX];
220 float ban_expire[BAN_MAX];
238 for(i = 0; i < ban_count; ++i)
240 if(time > ban_expire[i])
242 out = strcat(out, " ", ban_ip[i]);
243 out = strcat(out, " ", ftos(ban_expire[i] - time));
245 if(strlen(out) <= 1) // no real entries
246 cvar_set("g_banned_list", "");
248 cvar_set("g_banned_list", out);
251 float Ban_Delete(float i)
257 if(ban_expire[i] == 0)
259 if(ban_expire[i] > 0)
261 OnlineBanList_SendUnban(ban_ip[i]);
262 strunzone(ban_ip[i]);
273 for(i = 0; i < ban_count; ++i)
277 n = tokenize_sane(cvar_string("g_banned_list"));
278 if(stof(argv(0)) == 1)
280 ban_count = (n - 1) / 2;
281 for(i = 0; i < ban_count; ++i)
283 ban_ip[i] = strzone(argv(2*i+1));
284 ban_expire[i] = time + stof(argv(2*i+2));
290 e.classname = "bansyncer";
291 e.think = OnlineBanList_Think;
292 e.nextthink = time + 1;
299 for(i = 0; i < ban_count; ++i)
301 if(time > ban_expire[i])
303 msg = strcat("#", ftos(i), ": ");
304 msg = strcat(msg, ban_ip[i], " is still banned for ");
305 msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
310 float Ban_GetClientIP(entity client)
313 n = tokenizebyseparator(client.netaddress, ".");
316 ban_ip1 = strcat1(argv(0));
317 ban_ip2 = strcat(ban_ip1, ".", argv(1));
318 ban_ip3 = strcat(ban_ip2, ".", argv(2));
319 ban_ip4 = strcat(ban_ip3, ".", argv(3));
323 float Ban_IsClientBanned(entity client, float idx)
328 if(!Ban_GetClientIP(client))
340 for(i = b; i < e; ++i)
343 if(time > ban_expire[i])
346 if(ban_ip1 == s) return TRUE;
347 if(ban_ip2 == s) return TRUE;
348 if(ban_ip3 == s) return TRUE;
349 if(ban_ip4 == s) return TRUE;
354 float Ban_MaybeEnforceBan(entity client)
356 if(Ban_IsClientBanned(client, -1))
359 s = strcat("^1NOTE:^7 banned client ", client.netaddress, " just tried to enter\n");
367 string Ban_Enforce(float i, string reason)
372 // Enforce our new ban
374 FOR_EACH_REALCLIENT(e)
375 if(Ban_IsClientBanned(e, i))
380 reason = strcat(reason, ": affects ");
382 reason = strcat(reason, ", ");
383 reason = strcat(reason, e.netname);
385 s = strcat(s, "^1NOTE:^7 banned client ", e.netname, "^7 has to go\n");
393 float Ban_Insert(string ip, float bantime, string reason, float dosync)
400 for(i = 0; i < ban_count; ++i)
404 if(time + bantime > ban_expire[i])
406 ban_expire[i] = time + bantime;
407 print(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now\n");
411 reason = Ban_Enforce(i, reason);
416 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
417 OnlineBanList_SendBan(ip, bantime, reason);
422 // do we have a free slot?
423 for(i = 0; i < ban_count; ++i)
424 if(time > ban_expire[i])
426 // no free slot? Then look for the one who would get unbanned next
430 bestscore = ban_expire[i];
431 for(j = 1; j < ban_count; ++j)
433 if(ban_expire[j] < bestscore)
436 bestscore = ban_expire[i];
440 // if we replace someone, will we be banned longer than him (so long-term
441 // bans never get overridden by short-term bans)
442 if(ban_expire[i] > time + bantime)
444 // okay, insert our new victim as i
446 print(ip, " has been banned for ", ftos(bantime), " seconds\n");
447 ban_expire[i] = time + bantime;
448 ban_ip[i] = strzone(ip);
449 ban_count = max(ban_count, i + 1);
453 reason = Ban_Enforce(i, reason);
458 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
459 OnlineBanList_SendBan(ip, bantime, reason);
464 void Ban_KickBanClient(entity client, float bantime, float masksize, string reason)
466 if(!Ban_GetClientIP(client))
468 sprint(client, strcat("Kickbanned: ", reason, "\n"));
476 Ban_Insert(ban_ip1, bantime, reason, 1);
479 Ban_Insert(ban_ip2, bantime, reason, 1);
482 Ban_Insert(ban_ip3, bantime, reason, 1);
485 Ban_Insert(ban_ip4, bantime, reason, 1);
489 * not needed, as we enforce the ban in Ban_Insert anyway
491 sprint(client, strcat("Kickbanned: ", reason, "\n"));
496 float GameCommand_Ban(string command)
506 argc = tokenize_sane(command);
507 if(argv(0) == "help")
509 print(" kickban # n m p reason - kickban player n for m seconds, using mask size p (1 to 4)\n");
510 print(" ban ip m reason - ban an IP or range (incomplete IP, like 1.2.3) for m seconds\n");
511 print(" bans - list all existing bans\n");
512 print(" unban n - delete the entry #n from the bans list\n");
515 if(argv(0) == "kickban")
517 #define INITARG(c) reasonarg = c
518 #define GETARG(v,d) if((argc > reasonarg) && ((v = stof(argv(reasonarg))) != 0)) ++reasonarg; else v = d
519 #define RESTARG(v) if(argc > reasonarg) v = substring(command, argv_start_index(reasonarg), strlen(command) - argv_start_index(reasonarg)); else v = ""
522 entno = stof(argv(2));
523 if(entno > maxclients || entno < 1)
525 client = edict_num(entno);
528 GETARG(bantime, cvar("g_ban_default_bantime"));
529 GETARG(masksize, cvar("g_ban_default_masksize"));
532 Ban_KickBanClient(client, bantime, masksize, reason);
536 else if(argv(0) == "ban")
544 GETARG(bantime, cvar("g_ban_default_bantime"));
547 Ban_Insert(ip, bantime, reason, 1);
554 else if(argv(0) == "bans")
559 else if(argv(0) == "unban")