Server optimizations

This commit is contained in:
Bigjango13 2022-06-24 16:32:13 -04:00
parent 0e7a108a0a
commit 9b5292fe6b
1 changed files with 46 additions and 29 deletions

View File

@ -108,6 +108,7 @@ static void start_world(unsigned char *minecraft) {
static bool is_whitelist() {
return get_server_properties().get_bool("whitelist", DEFAULT_WHITELIST);
}
// Get Path Of Blacklist (Or Whitelist) File
static std::string get_blacklist_file() {
std::string file(home_get());
@ -119,10 +120,12 @@ static std::string get_blacklist_file() {
static std::vector<unsigned char *> get_players_in_level(unsigned char *level) {
return *(std::vector<unsigned char *> *) (level + Level_players_property_offset);
}
// Get Player's Username
static std::string *get_player_username(unsigned char *player) {
return (std::string *) (player + Player_username_property_offset);
}
// Get Level From Minecraft
static unsigned char *get_level(unsigned char *minecraft) {
return *(unsigned char **) (minecraft + Minecraft_level_property_offset);
@ -149,6 +152,30 @@ static void find_players(unsigned char *minecraft, std::string target_username,
}
}
std::vector<const char*> ips_in_list;
static void reload_lists(){
std::string blacklist_file_path = get_blacklist_file();
std::ifstream blacklist_file(blacklist_file_path);
if (blacklist_file) {
if (blacklist_file.good()) {
std::string line;
while (std::getline(blacklist_file, line)) {
// Check Line
if (line.length() > 0) {
if (line[0] != '#') {
ips_in_list.push_back(line.c_str());
}
}
}
}
if (blacklist_file.is_open()) {
blacklist_file.close();
}
} else {
ERR("Unable To Read Blacklist/Whitelist");
}
}
// Get RakNet Objects
static RakNet_RakNetGUID get_rak_net_guid(unsigned char *player) {
return *(RakNet_RakNetGUID *) (player + ServerPlayer_guid_property_offset);
@ -182,8 +209,7 @@ static void ban_callback(unsigned char *minecraft, std::string username, unsigne
// Get IP
char *ip = get_player_ip(minecraft, player);
// Ban Player
INFO("Banned: %s (%s)", username.c_str(), ip);
// Ban A Player
// Write To File
std::ofstream blacklist_output(get_blacklist_file(), std::ios_base::app);
if (blacklist_output) {
@ -194,6 +220,8 @@ static void ban_callback(unsigned char *minecraft, std::string username, unsigne
blacklist_output.close();
}
}
reload_lists();
INFO("Banned: %s (%s)", username.c_str(), ip);
}
// Kill Player
@ -293,6 +321,7 @@ __attribute__((destructor)) static void _free_stdin_buffer() {
// Handle Commands
static void handle_commands(unsigned char *minecraft) {
reload_lists();
// Check If Level Is Generated
if ((*Minecraft_isLevelGenerated)(minecraft) && stdin_buffer_complete) {
// Command Ready; Run It
@ -306,6 +335,7 @@ static void handle_commands(unsigned char *minecraft) {
static std::string kill_command("kill ");
static std::string list_command("list");
static std::string tps_command("tps");
static std::string reload_command("reload");
static std::string stop_command("stop");
static std::string help_command("help");
if (!is_whitelist() && data.rfind(ban_command, 0) == 0) {
@ -325,6 +355,8 @@ static void handle_commands(unsigned char *minecraft) {
// List Players
INFO("All Players:");
find_players(minecraft, "", list_callback, true);
} else if (data == reload_command) {
reload_lists();
} else if (data == tps_command) {
// Print TPS
INFO("TPS: %f", tps);
@ -340,6 +372,11 @@ static void handle_commands(unsigned char *minecraft) {
INFO(" say <Message> - Print Specified Message To Chat");
INFO(" list - List All Players");
INFO(" tps - Print TPS");
if (is_whitelist()) {
INFO(" reload - Reload The Whitelist");
} else {
INFO(" reload - Reload The Blacklist");
}
INFO(" stop - Stop Server");
INFO(" help - Print This Message");
} else {
@ -380,39 +417,19 @@ static void Minecraft_update_injection(unsigned char *minecraft) {
}
// Ban Players
static bool RakNet_RakPeer_IsBanned_injection(__attribute__((unused)) unsigned char *rakpeer, const char *ip) {
// Check banned-ips.txt
std::string blacklist_file_path = get_blacklist_file();
std::ifstream blacklist_file(blacklist_file_path);
if (blacklist_file) {
static bool RakNet_RakPeer_IsBanned_injection(__attribute__((unused)) unsigned char *rakpeer, const char *client_ip) {
bool ret = false;
if (blacklist_file.good()) {
std::string line;
while (std::getline(blacklist_file, line)) {
// Check Line
if (line.length() > 0) {
if (line[0] == '#') {
continue;
}
if (strcmp(line.c_str(), ip) == 0) {
// Is In File
ret = true;
break;
}
}
for (const char *ip : ips_in_list) {
if (strcmp(ip, client_ip) == 0){
ret = true;
break;
}
}
if (blacklist_file.is_open()) {
blacklist_file.close();
}
//INFO("%s, %i", client_ip, ret);
if (is_whitelist()) {
return !ret;
} else {
return ret;
}
} else {
ERR("Unable To Read Blacklist/Whitelist");
}
return ret;
}
// Log IPs