Skip to content

Commit 2ef8952

Browse files
committed
feat: kick/ban by IP
1 parent fda39db commit 2ef8952

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

GhostServer/commands.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ std::string ssprintf(const char *fmt, ...) {
4343
# include <strings.h>
4444
#endif
4545

46+
// TODO: Add unban, and save bans/whitelist to disk
47+
4648
void handle_cmd(NetworkManager *network, char *line) {
4749
while (isspace(*line)) ++line;
4850

@@ -83,8 +85,10 @@ void handle_cmd(NetworkManager *network, char *line) {
8385
LINE(" countdown start a countdown");
8486
LINE(" disconnect disconnect a client by name");
8587
LINE(" disconnect_id disconnect a client by ID");
88+
LINE(" disconnect_ip disconnect a client by IP address");
8689
LINE(" ban ban connections from a certain IP by ghost name");
8790
LINE(" ban_id ban connections from a certain IP by ghost ID");
91+
LINE(" ban_ip ban connections from a certain IP by IP address");
8892
LINE(" accept_players start accepting connections from players");
8993
LINE(" refuse_players stop accepting connections from players");
9094
LINE(" accept_spectators start accepting connections from spectators");
@@ -151,6 +155,12 @@ void handle_cmd(NetworkManager *network, char *line) {
151155
return;
152156
}
153157

158+
if (!strcmp(line, "disconnect_ip")) {
159+
g_current_cmd = CMD_DISCONNECT_IP;
160+
LINE_NONL("IP of player to disconnect: ");
161+
return;
162+
}
163+
154164
if (!strcmp(line, "ban")) {
155165
g_current_cmd = CMD_BAN;
156166
LINE_NONL("Name of player to ban: ");
@@ -163,6 +173,12 @@ void handle_cmd(NetworkManager *network, char *line) {
163173
return;
164174
}
165175

176+
if (!strcmp(line, "ban_ip")) {
177+
g_current_cmd = CMD_BAN_IP;
178+
LINE_NONL("IP of player to ban: ");
179+
return;
180+
}
181+
166182
if (!strcmp(line, "accept_players")) {
167183
network->ScheduleServerThread([=]() {
168184
network->acceptingPlayers = true;
@@ -315,6 +331,24 @@ void handle_cmd(NetworkManager *network, char *line) {
315331
LINE("No player ID given; not disconnecting anyone.");
316332
}
317333
return;
334+
335+
case CMD_DISCONNECT_IP:
336+
g_current_cmd = CMD_NONE;
337+
if (len != 0) {
338+
sf::IpAddress ip(_line);
339+
if (ip == sf::IpAddress::None) {
340+
LINE("Invalid IP address: %s", line);
341+
return;
342+
}
343+
network->ScheduleServerThread([=]() {
344+
auto clients = network->GetClientByIP(ip);
345+
for (auto cl : clients) network->DisconnectPlayer(*cl, "kicked");
346+
});
347+
LINE("Disconnected player IP %s", line);
348+
} else {
349+
LINE("No player IP given; not disconnecting anyone.");
350+
}
351+
return;
318352

319353
case CMD_BAN:
320354
g_current_cmd = CMD_NONE;
@@ -343,6 +377,25 @@ void handle_cmd(NetworkManager *network, char *line) {
343377
}
344378
return;
345379

380+
case CMD_BAN_IP:
381+
g_current_cmd = CMD_NONE;
382+
if (len != 0) {
383+
sf::IpAddress ip(_line);
384+
if (ip == sf::IpAddress::None) {
385+
LINE("Invalid IP address: %s", line);
386+
return;
387+
}
388+
network->ScheduleServerThread([=]() {
389+
auto clients = network->GetClientByIP(ip);
390+
for (auto cl : clients) network->BanClientIP(*cl);
391+
});
392+
network->bannedIps.push_back(ip);
393+
LINE("Banned player IP %s", line);
394+
} else {
395+
LINE("No player IP given; not banning anyone.");
396+
}
397+
return;
398+
346399
case CMD_SERVER_MSG:
347400
g_current_cmd = CMD_NONE;
348401
network->ServerMessage(line);

GhostServer/commands.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ enum CommandType {
77
CMD_COUNTDOWN_SET,
88
CMD_DISCONNECT,
99
CMD_DISCONNECT_ID,
10+
CMD_DISCONNECT_IP,
1011
CMD_BAN,
1112
CMD_BAN_ID,
13+
CMD_BAN_IP,
1214
CMD_SERVER_MSG,
1315
CMD_WHITELIST_ENABLE,
1416
CMD_WHITELIST_ADD_NAME,

docs/commands.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
|countdown||start a countdown|
1212
|disconnect|name|disconnect a client by name|
1313
|disconnect_id|id|disconnect a client by ID|
14+
|disconnect_ip|ip|disconnect a client by IP address|
1415
|ban|name|ban connections from a certain IP by ghost name|
1516
|ban_id|id|ban connections from a certain IP by ghost ID|
17+
|ban_ip|ip|ban connections from a certain IP by IP address|
1618
|accept_players||start accepting connections from players|
1719
|refuse_players||stop accepting connections from players|
1820
|accept_spectators||start accepting connections from spectators|

0 commit comments

Comments
 (0)