2022-06-11 01:59:57 +00:00
|
|
|
// Config Needs To Load First
|
|
|
|
#include <libreborn/libreborn.h>
|
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
#ifndef MCPI_SERVER_MODE
|
|
|
|
#error "Server Code Requires Server Mode"
|
|
|
|
#endif
|
|
|
|
|
2020-10-10 23:02:13 +00:00
|
|
|
#include <string>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <ctime>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <fstream>
|
2020-11-03 22:39:55 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2021-11-21 04:09:25 +00:00
|
|
|
#include <sys/ioctl.h>
|
2020-10-31 01:31:55 +00:00
|
|
|
#include <pthread.h>
|
2020-10-10 23:02:13 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
#include <SDL/SDL.h>
|
2020-10-10 23:02:13 +00:00
|
|
|
|
2021-11-14 04:29:48 +00:00
|
|
|
#include <symbols/minecraft.h>
|
2020-10-10 23:02:13 +00:00
|
|
|
|
2022-06-25 21:30:08 +00:00
|
|
|
#include <mods/server/server_properties.h>
|
2020-10-10 23:02:13 +00:00
|
|
|
|
2022-06-25 21:30:08 +00:00
|
|
|
#include <mods/feature/feature.h>
|
|
|
|
#include <mods/init/init.h>
|
|
|
|
#include <mods/home/home.h>
|
|
|
|
#include <mods/compat/compat.h>
|
|
|
|
#include <mods/misc/misc.h>
|
2020-11-21 21:52:27 +00:00
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// --only-generate: Ony Generate World And Then Exit
|
|
|
|
static bool only_generate = false;
|
|
|
|
__attribute__((constructor)) static void _init_only_generate(int argc, char *argv[]) {
|
|
|
|
// Iterate Arguments
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
// Check Argument
|
|
|
|
if (strcmp(argv[i], "--only-generate") == 0) {
|
|
|
|
// Enabled
|
|
|
|
only_generate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 23:02:13 +00:00
|
|
|
// Server Properties
|
|
|
|
static ServerProperties &get_server_properties() {
|
|
|
|
static ServerProperties properties;
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default Server Properties
|
2022-04-13 00:38:44 +00:00
|
|
|
#define DEFAULT_MOTD "Minecraft Server"
|
2020-11-10 20:16:42 +00:00
|
|
|
#define DEFAULT_SHOW_MINECON_BADGE "false"
|
2020-10-10 23:02:13 +00:00
|
|
|
#define DEFAULT_GAME_MODE "0"
|
|
|
|
#define DEFAULT_PORT "19132"
|
|
|
|
#define DEFAULT_SEED ""
|
2021-07-04 23:02:45 +00:00
|
|
|
#define DEFAULT_FORCE_MOB_SPAWNING "false"
|
2022-05-11 22:24:03 +00:00
|
|
|
#define DEFAULT_PEACEFUL_MODE "false"
|
2020-10-10 23:02:13 +00:00
|
|
|
#define DEFAULT_WORLD_NAME "world"
|
2020-10-11 19:38:48 +00:00
|
|
|
#define DEFAULT_MAX_PLAYERS "4"
|
2021-03-06 20:56:05 +00:00
|
|
|
#define DEFAULT_WHITELIST "false"
|
2022-04-10 00:06:44 +00:00
|
|
|
#define DEFAULT_DEATH_MESSAGES "false"
|
2020-10-10 23:02:13 +00:00
|
|
|
|
2020-11-06 04:05:37 +00:00
|
|
|
// Get World Name
|
2021-06-17 21:32:24 +00:00
|
|
|
static std::string get_world_name() {
|
2020-11-06 04:05:37 +00:00
|
|
|
return get_server_properties().get_string("world-name", DEFAULT_WORLD_NAME);
|
|
|
|
}
|
|
|
|
|
2020-11-03 22:39:55 +00:00
|
|
|
// Create/Start World
|
|
|
|
static void start_world(unsigned char *minecraft) {
|
2021-11-14 04:29:48 +00:00
|
|
|
// Get World Name
|
|
|
|
std::string world_name = get_world_name();
|
|
|
|
|
|
|
|
// Log
|
|
|
|
INFO("Loading World: %s", world_name.c_str());
|
2020-10-31 01:31:55 +00:00
|
|
|
|
2022-05-11 22:24:03 +00:00
|
|
|
// Peaceful Mode
|
|
|
|
unsigned char *options = minecraft + Minecraft_options_property_offset;
|
|
|
|
*(int32_t *) (options + Options_game_difficulty_property_offset) = get_server_properties().get_bool("peaceful-mode", DEFAULT_PEACEFUL_MODE) ? 0 : 2;
|
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// Specify Level Settings
|
2020-11-03 22:39:55 +00:00
|
|
|
LevelSettings settings;
|
2021-11-14 04:29:48 +00:00
|
|
|
settings.game_type = get_server_properties().get_int("game-mode", DEFAULT_GAME_MODE);
|
2020-11-03 22:39:55 +00:00
|
|
|
std::string seed_str = get_server_properties().get_string("seed", DEFAULT_SEED);
|
|
|
|
int32_t seed = seed_str.length() > 0 ? std::stoi(seed_str) : time(NULL);
|
|
|
|
settings.seed = seed;
|
2020-10-10 23:02:13 +00:00
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// Select Level
|
2020-11-03 22:39:55 +00:00
|
|
|
(*Minecraft_selectLevel)(minecraft, world_name, world_name, settings);
|
2020-10-10 23:02:13 +00:00
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// Don't Open Port When Using --only-generate
|
|
|
|
if (!only_generate) {
|
|
|
|
// Open Port
|
|
|
|
int port = get_server_properties().get_int("port", DEFAULT_PORT);
|
|
|
|
INFO("Listening On: %i", port);
|
2021-07-19 01:08:51 +00:00
|
|
|
(*Minecraft_hostMultiplayer)(minecraft, port);
|
2021-06-17 21:32:24 +00:00
|
|
|
}
|
2020-10-10 23:02:13 +00:00
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// Open ProgressScreen
|
2022-04-28 03:38:30 +00:00
|
|
|
unsigned char *screen = (unsigned char *) ::operator new(PROGRESS_SCREEN_SIZE);
|
2021-02-16 17:26:40 +00:00
|
|
|
ALLOC_CHECK(screen);
|
2022-04-28 03:38:30 +00:00
|
|
|
screen = (*ProgressScreen)(screen);
|
|
|
|
(*Minecraft_setScreen)(minecraft, screen);
|
2020-11-03 22:39:55 +00:00
|
|
|
}
|
2020-10-10 23:02:13 +00:00
|
|
|
|
2021-03-06 20:56:05 +00:00
|
|
|
// Check If Running In Whitelist Mode
|
|
|
|
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() {
|
2021-06-28 02:16:37 +00:00
|
|
|
std::string file(home_get());
|
2021-06-17 21:32:24 +00:00
|
|
|
file.append(is_whitelist() ? "/whitelist.txt" : "/blacklist.txt");
|
2020-11-03 22:39:55 +00:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2020-11-06 04:05:37 +00:00
|
|
|
// Get Vector Of Players In Level
|
2021-06-17 21:32:24 +00:00
|
|
|
static std::vector<unsigned char *> get_players_in_level(unsigned char *level) {
|
2020-12-18 03:22:56 +00:00
|
|
|
return *(std::vector<unsigned char *> *) (level + Level_players_property_offset);
|
2020-11-06 04:05:37 +00:00
|
|
|
}
|
|
|
|
// Get Player's Username
|
2021-12-01 02:54:43 +00:00
|
|
|
static std::string *get_player_username(unsigned char *player) {
|
|
|
|
return (std::string *) (player + Player_username_property_offset);
|
2020-11-06 04:05:37 +00:00
|
|
|
}
|
|
|
|
// Get Level From Minecraft
|
2021-06-17 21:32:24 +00:00
|
|
|
static unsigned char *get_level(unsigned char *minecraft) {
|
2020-12-18 03:22:56 +00:00
|
|
|
return *(unsigned char **) (minecraft + Minecraft_level_property_offset);
|
2020-11-06 04:05:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 22:39:55 +00:00
|
|
|
// Find Players With Username And Run Callback
|
2021-06-17 21:32:24 +00:00
|
|
|
typedef void (*player_callback_t)(unsigned char *minecraft, std::string username, unsigned char *player);
|
2020-11-04 00:31:27 +00:00
|
|
|
static void find_players(unsigned char *minecraft, std::string target_username, player_callback_t callback, bool all_players) {
|
2021-06-17 21:32:24 +00:00
|
|
|
unsigned char *level = get_level(minecraft);
|
|
|
|
std::vector<unsigned char *> players = get_players_in_level(level);
|
2020-11-03 22:39:55 +00:00
|
|
|
bool found_player = false;
|
|
|
|
for (std::size_t i = 0; i < players.size(); i++) {
|
|
|
|
// Iterate Players
|
|
|
|
unsigned char *player = players[i];
|
2021-12-01 02:54:43 +00:00
|
|
|
std::string username = *get_player_username(player);
|
2020-11-04 00:31:27 +00:00
|
|
|
if (all_players || username == target_username) {
|
2020-11-03 22:39:55 +00:00
|
|
|
// Run Callback
|
|
|
|
(*callback)(minecraft, username, player);
|
|
|
|
found_player = true;
|
|
|
|
}
|
|
|
|
}
|
2020-11-04 00:31:27 +00:00
|
|
|
if (!all_players && !found_player) {
|
2020-11-03 22:39:55 +00:00
|
|
|
INFO("Invalid Player: %s", target_username.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// Get RakNet Objects
|
2020-12-18 03:22:56 +00:00
|
|
|
static RakNet_RakNetGUID get_rak_net_guid(unsigned char *player) {
|
|
|
|
return *(RakNet_RakNetGUID *) (player + ServerPlayer_guid_property_offset);
|
2020-11-04 21:05:31 +00:00
|
|
|
}
|
|
|
|
static RakNet_SystemAddress get_system_address(unsigned char *rak_peer, RakNet_RakNetGUID guid) {
|
2020-11-03 22:39:55 +00:00
|
|
|
unsigned char *rak_peer_vtable = *(unsigned char **) rak_peer;
|
2020-12-18 03:22:56 +00:00
|
|
|
RakNet_RakPeer_GetSystemAddressFromGuid_t RakNet_RakPeer_GetSystemAddressFromGuid = *(RakNet_RakPeer_GetSystemAddressFromGuid_t *) (rak_peer_vtable + RakNet_RakPeer_GetSystemAddressFromGuid_vtable_offset);
|
2020-11-03 22:39:55 +00:00
|
|
|
// Get SystemAddress
|
2020-11-04 21:05:31 +00:00
|
|
|
return (*RakNet_RakPeer_GetSystemAddressFromGuid)(rak_peer, guid);
|
|
|
|
}
|
|
|
|
static unsigned char *get_rak_peer(unsigned char *minecraft) {
|
2020-12-18 03:22:56 +00:00
|
|
|
unsigned char *rak_net_instance = *(unsigned char **) (minecraft + Minecraft_rak_net_instance_property_offset);
|
|
|
|
return *(unsigned char **) (rak_net_instance + RakNetInstance_peer_property_offset);
|
2020-11-04 21:05:31 +00:00
|
|
|
}
|
2021-09-28 18:04:05 +00:00
|
|
|
static char *get_rak_net_guid_ip(unsigned char *rak_peer, RakNet_RakNetGUID guid) {
|
|
|
|
RakNet_SystemAddress address = get_system_address(rak_peer, guid);
|
|
|
|
// Get IP
|
|
|
|
return (*RakNet_SystemAddress_ToString)(&address, false, '|');
|
|
|
|
}
|
2020-11-04 21:05:31 +00:00
|
|
|
|
|
|
|
// Get IP From Player
|
|
|
|
static char *get_player_ip(unsigned char *minecraft, unsigned char *player) {
|
|
|
|
unsigned char *rak_peer = get_rak_peer(minecraft);
|
2021-09-28 18:04:05 +00:00
|
|
|
RakNet_RakNetGUID guid = get_rak_net_guid(player);
|
|
|
|
// Return
|
|
|
|
return get_rak_net_guid_ip(rak_peer,guid);
|
2020-11-03 22:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ban Player
|
2022-06-26 03:32:31 +00:00
|
|
|
static bool is_ip_in_blacklist(const char *ip);
|
2020-11-03 22:39:55 +00:00
|
|
|
static void ban_callback(unsigned char *minecraft, std::string username, unsigned char *player) {
|
|
|
|
// Get IP
|
|
|
|
char *ip = get_player_ip(minecraft, player);
|
|
|
|
|
|
|
|
// Ban Player
|
|
|
|
INFO("Banned: %s (%s)", username.c_str(), ip);
|
|
|
|
// Write To File
|
2021-03-06 20:56:05 +00:00
|
|
|
std::ofstream blacklist_output(get_blacklist_file(), std::ios_base::app);
|
|
|
|
if (blacklist_output) {
|
|
|
|
if (blacklist_output.good()) {
|
|
|
|
blacklist_output << "# " << username << '\n' << ip << '\n';
|
2020-11-03 22:39:55 +00:00
|
|
|
}
|
2021-03-06 20:56:05 +00:00
|
|
|
if (blacklist_output.is_open()) {
|
|
|
|
blacklist_output.close();
|
2020-10-31 01:31:55 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-26 03:32:31 +00:00
|
|
|
// Reload
|
|
|
|
is_ip_in_blacklist(NULL);
|
2020-10-10 23:02:13 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 22:39:55 +00:00
|
|
|
// Kill Player
|
|
|
|
static void kill_callback(__attribute__((unused)) unsigned char *minecraft, __attribute__((unused)) std::string username, unsigned char *player) {
|
|
|
|
unsigned char *player_vtable = *(unsigned char **) player;
|
2021-07-05 01:23:12 +00:00
|
|
|
Mob_actuallyHurt_t Player_actuallyHurt = *(Mob_actuallyHurt_t *) (player_vtable + Mob_actuallyHurt_vtable_offset);
|
|
|
|
(*Player_actuallyHurt)(player, INT32_MAX);
|
2020-11-03 22:39:55 +00:00
|
|
|
INFO("Killed: %s", username.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// List Player
|
|
|
|
static void list_callback(unsigned char *minecraft, std::string username, unsigned char *player) {
|
|
|
|
INFO(" - %s (%s)", username.c_str(), get_player_ip(minecraft, player));
|
|
|
|
}
|
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// Handle Server Stop
|
2020-11-04 21:05:31 +00:00
|
|
|
static void handle_server_stop(unsigned char *minecraft) {
|
2021-06-17 21:32:24 +00:00
|
|
|
if (compat_check_exit_requested()) {
|
2022-04-15 01:12:42 +00:00
|
|
|
INFO("Stopping Server");
|
2020-11-04 21:05:31 +00:00
|
|
|
// Save And Exit
|
2021-06-17 21:32:24 +00:00
|
|
|
unsigned char *level = get_level(minecraft);
|
2020-11-10 02:31:02 +00:00
|
|
|
if (level != NULL) {
|
|
|
|
Level_saveLevelData_injection(level);
|
|
|
|
}
|
2020-11-04 21:05:31 +00:00
|
|
|
(*Minecraft_leaveGame)(minecraft, false);
|
|
|
|
// Stop Game
|
|
|
|
SDL_Event event;
|
|
|
|
event.type = SDL_QUIT;
|
|
|
|
SDL_PushEvent(&event);
|
2020-11-03 22:39:55 +00:00
|
|
|
}
|
2020-11-04 21:05:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-29 22:44:27 +00:00
|
|
|
// Track TPS
|
|
|
|
#define NANOSECONDS_IN_SECOND 1000000000ll
|
|
|
|
static long long int get_time() {
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
|
|
|
|
long long int a = (long long int) ts.tv_nsec;
|
|
|
|
long long int b = ((long long int) ts.tv_sec) * NANOSECONDS_IN_SECOND;
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
static bool is_last_tick_time_set = false;
|
|
|
|
static long long int last_tick_time;
|
|
|
|
static double tps = 0;
|
|
|
|
static void Minecraft_tick_injection(__attribute__((unused)) unsigned char *minecraft) {
|
|
|
|
long long int time = get_time();
|
|
|
|
if (is_last_tick_time_set) {
|
|
|
|
long long int tick_time = time - last_tick_time;
|
|
|
|
tps = ((double) NANOSECONDS_IN_SECOND) / ((double) tick_time);
|
|
|
|
} else {
|
|
|
|
is_last_tick_time_set = true;
|
|
|
|
}
|
|
|
|
last_tick_time = time;
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:05:31 +00:00
|
|
|
// Get ServerSideNetworkHandler From Minecraft
|
|
|
|
static unsigned char *get_server_side_network_handler(unsigned char *minecraft) {
|
2021-02-17 16:31:01 +00:00
|
|
|
return *(unsigned char **) (minecraft + Minecraft_network_handler_property_offset);
|
2020-11-03 22:39:55 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// Read STDIN Thread
|
|
|
|
static volatile bool stdin_buffer_complete = false;
|
|
|
|
static volatile char *stdin_buffer = NULL;
|
|
|
|
static void *read_stdin_thread(__attribute__((unused)) void *data) {
|
2022-05-29 22:44:27 +00:00
|
|
|
// Loop
|
|
|
|
while (1) {
|
|
|
|
int bytes_available;
|
|
|
|
if (ioctl(fileno(stdin), FIONREAD, &bytes_available) == -1) {
|
|
|
|
bytes_available = 0;
|
|
|
|
}
|
|
|
|
char buffer[bytes_available];
|
|
|
|
bytes_available = read(fileno(stdin), (void *) buffer, bytes_available);
|
|
|
|
for (int i = 0; i < bytes_available; i++) {
|
|
|
|
if (!stdin_buffer_complete) {
|
|
|
|
// Read Data
|
|
|
|
char x = buffer[i];
|
|
|
|
if (x == '\n') {
|
|
|
|
if (stdin_buffer == NULL) {
|
|
|
|
stdin_buffer = (volatile char *) malloc(1);
|
|
|
|
stdin_buffer[0] = '\0';
|
2021-06-17 21:32:24 +00:00
|
|
|
}
|
2022-05-29 22:44:27 +00:00
|
|
|
stdin_buffer_complete = true;
|
|
|
|
} else {
|
|
|
|
string_append((char **) &stdin_buffer, "%c", (char) x);
|
2021-06-17 21:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-28 18:04:05 +00:00
|
|
|
return NULL;
|
2021-06-17 21:32:24 +00:00
|
|
|
}
|
|
|
|
__attribute__((destructor)) static void _free_stdin_buffer() {
|
2021-09-28 18:04:05 +00:00
|
|
|
if (stdin_buffer != NULL) {
|
|
|
|
free((void *) stdin_buffer);
|
|
|
|
stdin_buffer = NULL;
|
|
|
|
}
|
2021-06-17 21:32:24 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 22:39:55 +00:00
|
|
|
// Handle Commands
|
|
|
|
static void handle_commands(unsigned char *minecraft) {
|
2021-06-17 21:32:24 +00:00
|
|
|
// Check If Level Is Generated
|
2020-11-04 21:05:31 +00:00
|
|
|
if ((*Minecraft_isLevelGenerated)(minecraft) && stdin_buffer_complete) {
|
2021-06-17 21:32:24 +00:00
|
|
|
// Command Ready; Run It
|
2020-11-03 22:39:55 +00:00
|
|
|
if (stdin_buffer != NULL) {
|
2020-11-04 21:05:31 +00:00
|
|
|
unsigned char *server_side_network_handler = get_server_side_network_handler(minecraft);
|
2020-11-03 22:39:55 +00:00
|
|
|
if (server_side_network_handler != NULL) {
|
|
|
|
std::string data((char *) stdin_buffer);
|
|
|
|
|
|
|
|
static std::string ban_command("ban ");
|
|
|
|
static std::string say_command("say ");
|
|
|
|
static std::string kill_command("kill ");
|
|
|
|
static std::string list_command("list");
|
2022-06-26 03:32:31 +00:00
|
|
|
static std::string reload_command("reload");
|
2022-05-29 22:44:27 +00:00
|
|
|
static std::string tps_command("tps");
|
2020-11-03 22:39:55 +00:00
|
|
|
static std::string stop_command("stop");
|
|
|
|
static std::string help_command("help");
|
2021-03-06 20:56:05 +00:00
|
|
|
if (!is_whitelist() && data.rfind(ban_command, 0) == 0) {
|
2020-11-03 22:39:55 +00:00
|
|
|
// IP-Ban Target Username
|
|
|
|
std::string ban_username = data.substr(ban_command.length());
|
2020-11-04 00:31:27 +00:00
|
|
|
find_players(minecraft, ban_username, ban_callback, false);
|
2022-06-26 03:32:31 +00:00
|
|
|
} else if (data == reload_command) {
|
|
|
|
INFO("Reloading %s", is_whitelist() ? "Whitelist" : "Blacklist");
|
|
|
|
is_ip_in_blacklist(NULL);
|
2020-11-03 22:39:55 +00:00
|
|
|
} else if (data.rfind(kill_command, 0) == 0) {
|
|
|
|
// Kill Target Username
|
|
|
|
std::string kill_username = data.substr(kill_command.length());
|
2020-11-04 00:31:27 +00:00
|
|
|
find_players(minecraft, kill_username, kill_callback, false);
|
2020-11-03 22:39:55 +00:00
|
|
|
} else if (data.rfind(say_command, 0) == 0) {
|
|
|
|
// Format Message
|
|
|
|
std::string message = "[Server] " + data.substr(say_command.length());
|
|
|
|
// Post Message To Chat
|
|
|
|
(*ServerSideNetworkHandler_displayGameMessage)(server_side_network_handler, message);
|
|
|
|
} else if (data == list_command) {
|
|
|
|
// List Players
|
2022-04-15 01:12:42 +00:00
|
|
|
INFO("All Players:");
|
2020-11-04 00:31:27 +00:00
|
|
|
find_players(minecraft, "", list_callback, true);
|
2022-05-29 22:44:27 +00:00
|
|
|
} else if (data == tps_command) {
|
|
|
|
// Print TPS
|
|
|
|
INFO("TPS: %f", tps);
|
2020-11-03 22:39:55 +00:00
|
|
|
} else if (data == stop_command) {
|
|
|
|
// Stop Server
|
2021-06-17 21:32:24 +00:00
|
|
|
compat_request_exit();
|
2020-11-03 22:39:55 +00:00
|
|
|
} else if (data == help_command) {
|
2022-04-15 01:12:42 +00:00
|
|
|
INFO("All Commands:");
|
2021-03-06 20:56:05 +00:00
|
|
|
if (!is_whitelist()) {
|
2022-04-15 01:12:42 +00:00
|
|
|
INFO(" ban <Username> - IP-Ban All Players With Specifed Username");
|
2021-03-06 20:56:05 +00:00
|
|
|
}
|
2022-06-26 03:32:31 +00:00
|
|
|
INFO(" reload - Reload The %s", is_whitelist() ? "Whitelist" : "Blacklist");
|
2022-04-15 01:12:42 +00:00
|
|
|
INFO(" kill <Username> - Kill All Players With Specifed Username");
|
|
|
|
INFO(" say <Message> - Print Specified Message To Chat");
|
|
|
|
INFO(" list - List All Players");
|
2022-05-29 22:44:27 +00:00
|
|
|
INFO(" tps - Print TPS");
|
2022-04-15 01:12:42 +00:00
|
|
|
INFO(" stop - Stop Server");
|
|
|
|
INFO(" help - Print This Message");
|
2020-11-03 22:39:55 +00:00
|
|
|
} else {
|
|
|
|
INFO("Invalid Command: %s", data.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// Free
|
2020-11-03 22:39:55 +00:00
|
|
|
free((void *) stdin_buffer);
|
|
|
|
stdin_buffer = NULL;
|
|
|
|
}
|
|
|
|
stdin_buffer_complete = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runs Every Tick
|
|
|
|
static bool loaded = false;
|
|
|
|
static void Minecraft_update_injection(unsigned char *minecraft) {
|
|
|
|
// Create/Start World
|
|
|
|
if (!loaded) {
|
|
|
|
start_world(minecraft);
|
|
|
|
loaded = true;
|
|
|
|
}
|
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// Handle --only-generate
|
|
|
|
if (only_generate && (*Minecraft_isLevelGenerated)(minecraft)) {
|
|
|
|
// Request Exit
|
|
|
|
compat_request_exit();
|
|
|
|
// Disable Special Behavior After Requesting Exit
|
|
|
|
only_generate = false;
|
|
|
|
}
|
|
|
|
|
2020-11-03 22:39:55 +00:00
|
|
|
// Handle Commands
|
|
|
|
handle_commands(minecraft);
|
2020-11-04 21:05:31 +00:00
|
|
|
|
|
|
|
// Server Stop
|
|
|
|
handle_server_stop(minecraft);
|
2020-11-03 22:39:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-26 03:32:31 +00:00
|
|
|
// Check Blacklist/Whitelist
|
|
|
|
static bool is_ip_in_blacklist(const char *ip) {
|
|
|
|
static std::vector<std::string> ips;
|
|
|
|
if (ip == NULL) {
|
|
|
|
// Reload
|
|
|
|
ips.clear();
|
|
|
|
// Check banned-ips.txt
|
|
|
|
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 && line[0] != '#') {
|
|
|
|
ips.push_back(line);
|
2020-11-03 22:39:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-26 03:32:31 +00:00
|
|
|
if (blacklist_file.is_open()) {
|
|
|
|
blacklist_file.close();
|
|
|
|
}
|
2021-03-06 20:56:05 +00:00
|
|
|
} else {
|
2022-06-26 03:32:31 +00:00
|
|
|
ERR("Unable To Read Blacklist/Whitelist");
|
2020-11-10 00:49:41 +00:00
|
|
|
}
|
2022-06-26 03:32:31 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// Check List
|
|
|
|
for (std::string x : ips) {
|
|
|
|
if (x.compare(ip) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ban Players
|
|
|
|
static bool RakNet_RakPeer_IsBanned_injection(__attribute__((unused)) unsigned char *rakpeer, const char *ip) {
|
|
|
|
// Check List
|
|
|
|
bool ret = is_ip_in_blacklist(ip);
|
|
|
|
if (is_whitelist()) {
|
|
|
|
return !ret;
|
2020-11-10 00:49:41 +00:00
|
|
|
} else {
|
2022-06-26 03:32:31 +00:00
|
|
|
return ret;
|
2020-10-10 23:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 18:04:05 +00:00
|
|
|
// Log IPs
|
|
|
|
static unsigned char *ServerSideNetworkHandler_onReady_ClientGeneration_ServerSideNetworkHandler_popPendingPlayer_injection(unsigned char *server_side_network_handler, RakNet_RakNetGUID *guid) {
|
|
|
|
// Call Original Method
|
|
|
|
unsigned char *player = (*ServerSideNetworkHandler_popPendingPlayer)(server_side_network_handler, guid);
|
|
|
|
|
|
|
|
// Check If Player Is Null
|
|
|
|
if (player != NULL) {
|
|
|
|
// Get Data
|
2021-12-01 02:54:43 +00:00
|
|
|
std::string *username = (std::string *) (player + Player_username_property_offset);
|
2021-09-28 18:04:05 +00:00
|
|
|
unsigned char *minecraft = *(unsigned char **) (server_side_network_handler + ServerSideNetworkHandler_minecraft_property_offset);
|
|
|
|
unsigned char *rak_peer = get_rak_peer(minecraft);
|
|
|
|
char *ip = get_rak_net_guid_ip(rak_peer, *guid);
|
|
|
|
|
|
|
|
// Log
|
2021-12-01 02:54:43 +00:00
|
|
|
INFO("%s Has Joined (IP: %s)", username->c_str(), ip);
|
2021-09-28 18:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
2022-04-13 00:38:44 +00:00
|
|
|
// Get MOTD
|
|
|
|
static std::string get_motd() {
|
|
|
|
std::string motd(get_server_properties().get_string("motd", DEFAULT_MOTD));
|
|
|
|
return motd;
|
|
|
|
}
|
|
|
|
|
2021-03-06 20:56:05 +00:00
|
|
|
// Get Feature Flags
|
2020-12-01 17:02:48 +00:00
|
|
|
static bool loaded_features = false;
|
2020-12-02 23:18:49 +00:00
|
|
|
static const char *get_features() {
|
2020-12-01 17:02:48 +00:00
|
|
|
static std::string features;
|
|
|
|
if (!loaded_features) {
|
|
|
|
loaded_features = true;
|
|
|
|
|
2021-07-04 23:02:45 +00:00
|
|
|
features.clear();
|
2021-07-05 01:23:12 +00:00
|
|
|
if (get_server_properties().get_bool("force-mob-spawning", DEFAULT_FORCE_MOB_SPAWNING)) {
|
|
|
|
features += "Force Mob Spawning|";
|
|
|
|
}
|
2022-04-10 00:06:44 +00:00
|
|
|
if (get_server_properties().get_bool("death-messages", DEFAULT_DEATH_MESSAGES)) {
|
|
|
|
features += "Implement Death Messages|";
|
|
|
|
}
|
2020-12-01 17:02:48 +00:00
|
|
|
}
|
|
|
|
return features.c_str();
|
2020-10-10 23:02:13 +00:00
|
|
|
}
|
2020-12-01 17:02:48 +00:00
|
|
|
|
2021-03-06 20:56:05 +00:00
|
|
|
// Get Max Players
|
2020-12-01 17:02:48 +00:00
|
|
|
static unsigned char get_max_players() {
|
2020-10-11 19:38:48 +00:00
|
|
|
int val = get_server_properties().get_int("max-players", DEFAULT_MAX_PLAYERS);
|
|
|
|
if (val < 0) {
|
|
|
|
val = 0;
|
|
|
|
}
|
|
|
|
if (val > 255) {
|
|
|
|
val = 255;
|
|
|
|
}
|
|
|
|
return (unsigned char) val;
|
|
|
|
}
|
2020-10-10 23:02:13 +00:00
|
|
|
|
2020-12-02 23:18:49 +00:00
|
|
|
static void server_init() {
|
2020-10-10 23:02:13 +00:00
|
|
|
// Open Properties File
|
2021-06-28 02:16:37 +00:00
|
|
|
std::string file(home_get());
|
2021-06-17 21:32:24 +00:00
|
|
|
file.append("/server.properties");
|
2020-10-10 23:02:13 +00:00
|
|
|
std::ifstream properties_file(file);
|
|
|
|
|
2021-11-14 04:29:48 +00:00
|
|
|
// Check Properties File
|
2021-06-28 02:16:37 +00:00
|
|
|
if (!properties_file.good()) {
|
2020-10-10 23:02:13 +00:00
|
|
|
// Write Defaults
|
|
|
|
std::ofstream properties_file_output(file);
|
2022-04-13 00:38:44 +00:00
|
|
|
properties_file_output << "# Message Of The Day\n";
|
|
|
|
properties_file_output << "motd=" DEFAULT_MOTD "\n";
|
2020-11-10 20:16:42 +00:00
|
|
|
properties_file_output << "# Show The MineCon Badge Next To MOTD In Server List\n";
|
|
|
|
properties_file_output << "show-minecon-badge=" DEFAULT_SHOW_MINECON_BADGE "\n";
|
2020-10-14 17:40:32 +00:00
|
|
|
properties_file_output << "# Game Mode (0 = Survival, 1 = Creative)\n";
|
2020-10-10 23:02:13 +00:00
|
|
|
properties_file_output << "game-mode=" DEFAULT_GAME_MODE "\n";
|
2020-10-14 17:40:32 +00:00
|
|
|
properties_file_output << "# Port\n";
|
2020-10-10 23:02:13 +00:00
|
|
|
properties_file_output << "port=" DEFAULT_PORT "\n";
|
2020-10-14 17:40:32 +00:00
|
|
|
properties_file_output << "# World Seed (Blank = Random Seed)\n";
|
2020-10-10 23:02:13 +00:00
|
|
|
properties_file_output << "seed=" DEFAULT_SEED "\n";
|
2021-07-04 23:02:45 +00:00
|
|
|
properties_file_output << "# Force Mob Spawning (false = Disabled, true = Enabled)\n";
|
|
|
|
properties_file_output << "force-mob-spawning=" DEFAULT_FORCE_MOB_SPAWNING "\n";
|
2022-05-11 22:24:03 +00:00
|
|
|
properties_file_output << "# Peaceful Mode (false = Disabled, true = Enabled)\n";
|
|
|
|
properties_file_output << "peaceful-mode=" DEFAULT_PEACEFUL_MODE "\n";
|
2020-10-14 17:40:32 +00:00
|
|
|
properties_file_output << "# World To Select\n";
|
2020-10-10 23:02:13 +00:00
|
|
|
properties_file_output << "world-name=" DEFAULT_WORLD_NAME "\n";
|
2020-10-15 03:23:31 +00:00
|
|
|
properties_file_output << "# Maximum Player Count\n";
|
2020-10-11 19:38:48 +00:00
|
|
|
properties_file_output << "max-players=" DEFAULT_MAX_PLAYERS "\n";
|
2021-03-06 20:56:05 +00:00
|
|
|
properties_file_output << "# Enable Whitelist\n";
|
|
|
|
properties_file_output << "whitelist=" DEFAULT_WHITELIST "\n";
|
2022-04-10 00:06:44 +00:00
|
|
|
properties_file_output << "# Enable Death Messages\n";
|
|
|
|
properties_file_output << "death-messages=" DEFAULT_DEATH_MESSAGES "\n";
|
2020-10-10 23:02:13 +00:00
|
|
|
properties_file_output.close();
|
|
|
|
// Re-Open File
|
|
|
|
properties_file = std::ifstream(file);
|
|
|
|
}
|
|
|
|
|
2021-06-28 02:16:37 +00:00
|
|
|
// Check Properties File
|
2020-10-10 23:02:13 +00:00
|
|
|
if (!properties_file.is_open()) {
|
2021-06-28 02:16:37 +00:00
|
|
|
ERR("Unable To Open %s", file.c_str());
|
2020-10-10 23:02:13 +00:00
|
|
|
}
|
|
|
|
// Load Properties
|
|
|
|
get_server_properties().load(properties_file);
|
2021-03-06 20:56:05 +00:00
|
|
|
// Close Properties File
|
2020-10-10 23:02:13 +00:00
|
|
|
properties_file.close();
|
|
|
|
|
2021-03-06 20:56:05 +00:00
|
|
|
// Create Empty Blacklist/Whitelist File
|
|
|
|
std::string blacklist_file_path = get_blacklist_file();
|
|
|
|
std::ifstream blacklist_file(blacklist_file_path);
|
2021-06-28 02:16:37 +00:00
|
|
|
if (!blacklist_file.good()) {
|
2020-11-03 22:39:55 +00:00
|
|
|
// Write Default
|
2021-03-06 20:56:05 +00:00
|
|
|
std::ofstream blacklist_output(blacklist_file_path);
|
|
|
|
blacklist_output << "# Blacklist/Whitelist; Each Line Is One IP Address\n";
|
|
|
|
blacklist_output.close();
|
2020-11-03 22:39:55 +00:00
|
|
|
}
|
2021-03-06 20:56:05 +00:00
|
|
|
if (blacklist_file.is_open()) {
|
|
|
|
blacklist_file.close();
|
2020-11-03 22:39:55 +00:00
|
|
|
}
|
2022-06-26 03:32:31 +00:00
|
|
|
// Load Blacklist/Whitelist
|
|
|
|
is_ip_in_blacklist(NULL);
|
2020-11-03 22:39:55 +00:00
|
|
|
|
2020-10-10 23:02:13 +00:00
|
|
|
// Prevent Main Player From Loading
|
2021-03-05 00:27:24 +00:00
|
|
|
unsigned char player_patch[4] = {0x00, 0x20, 0xa0, 0xe3}; // "mov r2, #0x0"
|
2020-10-10 23:02:13 +00:00
|
|
|
patch((void *) 0x1685c, player_patch);
|
|
|
|
// Start World On Launch
|
2021-11-14 04:29:48 +00:00
|
|
|
misc_run_on_update(Minecraft_update_injection);
|
2020-10-11 19:38:48 +00:00
|
|
|
// Set Max Players
|
2021-03-05 00:27:24 +00:00
|
|
|
unsigned char max_players_patch[4] = {get_max_players(), 0x30, 0xa0, 0xe3}; // "mov r3, #MAX_PLAYERS"
|
2020-10-11 19:38:48 +00:00
|
|
|
patch((void *) 0x166d0, max_players_patch);
|
2020-11-03 22:39:55 +00:00
|
|
|
// Custom Banned IP List
|
2020-11-10 00:49:41 +00:00
|
|
|
overwrite((void *) RakNet_RakPeer_IsBanned, (void *) RakNet_RakPeer_IsBanned_injection);
|
2020-10-31 01:31:55 +00:00
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// Show The MineCon Icon Next To MOTD In Server List
|
2020-11-10 20:16:42 +00:00
|
|
|
if (get_server_properties().get_bool("show-minecon-badge", DEFAULT_SHOW_MINECON_BADGE)) {
|
2021-03-05 00:27:24 +00:00
|
|
|
unsigned char minecon_badge_patch[4] = {0x04, 0x1a, 0x9f, 0xe5}; // "ldr r1, [0x741f0]"
|
2020-11-10 20:16:42 +00:00
|
|
|
patch((void *) 0x737e4, minecon_badge_patch);
|
2020-11-05 01:12:48 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 18:04:05 +00:00
|
|
|
// Log IPs
|
|
|
|
overwrite_call((void *) 0x75e54, (void *) ServerSideNetworkHandler_onReady_ClientGeneration_ServerSideNetworkHandler_popPendingPlayer_injection);
|
|
|
|
|
2022-05-29 22:44:27 +00:00
|
|
|
// Track TPS
|
|
|
|
misc_run_on_tick(Minecraft_tick_injection);
|
|
|
|
|
2020-10-31 01:31:55 +00:00
|
|
|
// Start Reading STDIN
|
|
|
|
pthread_t read_stdin_thread_obj;
|
|
|
|
pthread_create(&read_stdin_thread_obj, NULL, read_stdin_thread, NULL);
|
2020-10-10 23:02:13 +00:00
|
|
|
}
|
2020-12-02 23:18:49 +00:00
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// Init Server
|
2020-12-02 23:18:49 +00:00
|
|
|
void init_server() {
|
2021-06-17 21:32:24 +00:00
|
|
|
server_init();
|
|
|
|
setenv("MCPI_FEATURE_FLAGS", get_features(), 1);
|
2022-07-09 02:40:56 +00:00
|
|
|
setenv("MCPI_RENDER_DISTANCE", "Tiny", 1);
|
2022-04-13 00:38:44 +00:00
|
|
|
setenv("MCPI_USERNAME", get_motd().c_str(), 1);
|
2021-06-17 21:32:24 +00:00
|
|
|
}
|