fix a lot of issues on psogc; add proxy module

- $ann implemented
- concurrency removed; server is now single-threaded, event-driven and much more stable
- rare seed is no longer the game id; ids are sequential from server startup so they weren't random at all before
- supports dropping privileges; now you can run it as root so it can open a sockets on low ports, then it will switch to the given user before serving any traffic
- newserv now behaves like a proxy if you run it with the --proxy-destination=<IP_OR_HOSTNAME> argument; there's also an (invisible) shell in this mode where you can inject commands to the server or client. e.g. it can always be christmas in the lobby if you do `sc DA 01 00 00`
- increased the mtu on PSODolphinConfig's tap0 configuration; this seems to make the connection more stable
- fixed some uninitialized memory bugs
- the shell is now event-driven and now uses libevent too; unfortunately this means readline doesn't work anymore (no history and vim-like shortcuts)
- made network command display consistent for input vs. output (the header appears in both cases now)
- fixed bugs in some subcommand handling (the BB logic was being applied to non-BB clients erroneously, causing most item drops not to work at all)
- fixed player tags in the short lobby data struct. unclear if this was actually a problem but it was inconsistent with other servers
- fixed "unused" field in game join command (actually it appears to be disable_udp and should be 1, not 0)
- cleaned up Server abstraction a bit
- rewrote some text functions; asan was complaining about the built-in ones for some reason
- added an optional welcome message
This commit is contained in:
Martin Michelsen
2020-02-16 15:03:47 -08:00
parent 76c810c1e6
commit 0d4b0b2279
39 changed files with 1487 additions and 975 deletions
+61 -151
View File
@@ -1,7 +1,6 @@
#include "Shell.hh"
#include <readline/history.h>
#include <readline/readline.h>
#include <event2/event.h>
#include <stdio.h>
#include <phosg/Strings.hh>
@@ -10,173 +9,84 @@ using namespace std;
class exit_shell : public runtime_error {
public:
exit_shell() : runtime_error("shell exited") { }
~exit_shell() = default;
};
Shell::exit_shell::exit_shell() : runtime_error("shell exited") { }
void execute_command(shared_ptr<ServerState> state, const string& command) {
// find the entry in the command table and run the command
size_t command_end = skip_non_whitespace(command, 0);
size_t args_begin = skip_whitespace(command, command_end);
string command_name = command.substr(0, command_end);
string command_args = command.substr(args_begin);
Shell::Shell(std::shared_ptr<struct event_base> base,
std::shared_ptr<ServerState> state) : base(base), state(state),
read_event(event_new(this->base.get(), 0, EV_READ | EV_PERSIST,
&Shell::dispatch_read_stdin, this), event_free),
prompt_event(event_new(this->base.get(), 0, EV_TIMEOUT,
&Shell::dispatch_print_prompt, this), event_free) {
event_add(this->read_event.get(), NULL);
if (command_name == "exit") {
throw exit_shell();
// schedule an event to print the prompt as soon as the event loop starts
// running. we do this so the prompt appears after any initialization
// messages that come after starting the shell
struct timeval tv = {0, 0};
event_add(this->prompt_event.get(), &tv);
} else if (command_name == "help") {
fprintf(stderr, "\
commands:\n\
help\n\
you\'re reading it now\n\
exit (or ctrl+d)\n\
shut down the server\n\
reload <item> ...\n\
reload data. <item> can be licenses, battle-params, level-table, or quests.\n\
add-license <parameters>\n\
add a license to the server. <parameters> is some subset of the following:\n\
username=<username> (bb username)\n\
bb-password=<password> (bb password)\n\
gc-password=<password> (gc password)\n\
access-key=<access-key> (gc/pc access key)\n\
serial=<serial-number> (gc/pc serial number; required for all licenses)\n\
privileges=<privilege-mask> (can be normal, mod, admin, root, or numeric)\n\
delete-license <serial-number>\n\
delete a license from the server\n\
list-licenses\n\
list all licenses registered on the server\n\
");
} else if (command_name == "reload") {
auto types = split(command_args, ' ');
for (const string& type : types) {
if (type == "licenses") {
shared_ptr<LicenseManager> lm(new LicenseManager("system/licenses.nsi"));
state->license_manager = lm;
} else if (type == "battle-params") {
shared_ptr<BattleParamTable> bpt(new BattleParamTable("system/blueburst/BattleParamEntry"));
state->battle_params = bpt;
} else if (type == "level-table") {
shared_ptr<LevelTable> lt(new LevelTable("system/blueburst/PlyLevelTbl.prs", true));
state->level_table = lt;
} else if (type == "quests") {
shared_ptr<QuestIndex> qi(new QuestIndex("system/quests"));
state->quest_index = qi;
} else {
throw invalid_argument("incorrect data type");
}
}
} else if (command_name == "add-license") {
shared_ptr<License> l(new License());
memset(l.get(), 0, sizeof(License));
for (const string& token : split(command_args, ' ')) {
if (starts_with(token, "username=")) {
if (token.size() >= 29) {
throw invalid_argument("username too long");
}
strcpy(l->username, token.c_str() + 9);
} else if (starts_with(token, "bb-password=")) {
if (token.size() >= 32) {
throw invalid_argument("bb-password too long");
}
strcpy(l->bb_password, token.c_str() + 12);
} else if (starts_with(token, "gc-password=")) {
if (token.size() > 20) {
throw invalid_argument("gc-password too long");
}
strcpy(l->gc_password, token.c_str() + 12);
} else if (starts_with(token, "access-key=")) {
if (token.size() > 23) {
throw invalid_argument("access-key is too long");
}
strcpy(l->access_key, token.c_str() + 11);
} else if (starts_with(token, "serial=")) {
l->serial_number = stoul(token.substr(7));
} else if (starts_with(token, "privileges=")) {
string mask = token.substr(11);
if (mask == "normal") {
l->privileges = 0;
} else if (mask == "mod") {
l->privileges = Privilege::Moderator;
} else if (mask == "admin") {
l->privileges = Privilege::Administrator;
} else if (mask == "root") {
l->privileges = Privilege::Root;
} else {
l->privileges = stoul(mask);
}
} else {
throw invalid_argument("incorrect field");
}
}
if (!l->serial_number) {
throw invalid_argument("license does not contain serial number");
}
state->license_manager->add(l);
fprintf(stderr, "license added\n");
} else if (command_name == "delete-license") {
uint32_t serial_number = stoul(command_args);
state->license_manager->remove(serial_number);
fprintf(stderr, "license deleted\n");
} else if (command_name == "list-licenses") {
for (const auto& l : state->license_manager->snapshot()) {
string s = l.str();
fprintf(stderr, "%s\n", s.c_str());
}
} else {
throw invalid_argument("unknown command; try \'help\'");
}
this->poll.add(0, POLLIN);
}
void run_shell(shared_ptr<ServerState> state) {
// initialize history
using_history();
// string history_filename = get_user_home_directory() + "/.newserv_history";
// read_history(history_filename.c_str());
// stifle_history(HISTORY_FILE_LENGTH);
void Shell::dispatch_print_prompt(evutil_socket_t fd, short events, void* ctx) {
reinterpret_cast<Shell*>(ctx)->print_prompt();
}
// read and execute commands
bool should_continue = true;
while (should_continue) {
void Shell::print_prompt() {
// default behavior: no prompt
}
// read the command
char* command = readline("newserv> ");
if (!command) {
fprintf(stderr, " -- exit\n");
void Shell::dispatch_read_stdin(evutil_socket_t fd, short events, void* ctx) {
reinterpret_cast<Shell*>(ctx)->read_stdin();
}
void Shell::read_stdin() {
bool any_command_read = false;
for (;;) {
auto poll_result = this->poll.poll();
short fd_events = 0;
try {
fd_events = poll_result.at(0);
} catch (const out_of_range&) { }
if (!(fd_events & POLLIN)) {
break;
}
// if there's a command, add it to the history
const char* command_to_execute = command + skip_whitespace(command, 0);
if (command_to_execute && *command_to_execute) {
add_history(command);
string command(2048, '\0');
if (!fgets(const_cast<char*>(command.data()), command.size(), stdin)) {
if (!any_command_read) {
// ctrl+d probably; we should exit
fputc('\n', stderr);
event_base_loopexit(this->base.get(), NULL);
return;
} else {
break; // probably not EOF; just no more commands for now
}
}
// dispatch the command
// trim the extra data off the string
size_t len = strlen(command.c_str());
if (len == 0) {
break;
}
if (command[len - 1] == '\n') {
len--;
}
command.resize(len);
any_command_read = true;
try {
execute_command(state, command_to_execute);
execute_command(command);
} catch (const exit_shell&) {
should_continue = false;
event_base_loopexit(this->base.get(), NULL);
return;
} catch (const exception& e) {
fprintf(stderr, "FAILED: %s\n", e.what());
}
free(command);
}
this->print_prompt();
}