add interactive shell

This commit is contained in:
Martin Michelsen
2019-01-29 00:17:04 -08:00
parent d0408246d3
commit f3842b49f3
10 changed files with 274 additions and 15 deletions
+53 -6
View File
@@ -1,3 +1,4 @@
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
@@ -10,13 +11,44 @@ using namespace std;
LicenseManager::LicenseManager(const std::string& filename) : filename(filename) {
auto licenses = load_vector_file<License>(this->filename);
string License::str() const {
string ret = string_printf("License(serial_number=%" PRIu32, this->serial_number);
if (this->username[0]) {
ret += ", username=";
ret += this->username;
}
if (this->bb_password[0]) {
ret += ", bb-password=";
ret += this->bb_password;
}
if (this->access_key[0]) {
ret += ", access-key=";
ret += this->access_key;
}
if (this->gc_password[0]) {
ret += ", gc-password=";
ret += this->gc_password;
}
ret += string_printf(", privileges=%" PRIu32, this->privileges);
if (this->ban_end_time) {
ret += string_printf(", banned-until=%" PRIu64, this->ban_end_time);
}
return ret + ")";
}
for (const auto& read_license : licenses) {
shared_ptr<License> license(new License(read_license));
this->bb_username_to_license.emplace(license->username, license);
this->serial_number_to_license.emplace(license->serial_number, license);
LicenseManager::LicenseManager(const std::string& filename) : filename(filename) {
try {
auto licenses = load_vector_file<License>(this->filename);
for (const auto& read_license : licenses) {
shared_ptr<License> license(new License(read_license));
this->bb_username_to_license.emplace(license->username, license);
this->serial_number_to_license.emplace(license->serial_number, license);
}
} catch (const cannot_open_file&) {
log(WARNING, "%s does not exist; no licenses are registered", this->filename.c_str());
}
}
@@ -78,6 +110,11 @@ shared_ptr<const License> LicenseManager::verify_bb(const char* username,
return license;
}
size_t LicenseManager::count() const {
rw_guard g(this->lock, false);
return this->serial_number_to_license.size();
}
void LicenseManager::ban_until(uint32_t serial_number, uint64_t end_time) {
rw_guard g(this->lock, false);
this->serial_number_to_license.at(serial_number)->ban_end_time = end_time;
@@ -110,3 +147,13 @@ void LicenseManager::remove(uint32_t serial_number) {
rw_guard g(this->lock, false);
this->save_locked();
}
vector<License> LicenseManager::snapshot() const {
vector<License> ret;
rw_guard g(this->lock, false);
for (auto it : this->serial_number_to_license) {
ret.emplace_back(*it.second);
}
return ret;
}
+5
View File
@@ -34,6 +34,8 @@ struct License {
char gc_password[12]; // GC password
uint32_t privileges; // privilege level
uint64_t ban_end_time; // end time of ban (zero = not banned)
std::string str() const;
} __attribute__((packed));
class LicenseManager {
@@ -49,8 +51,11 @@ public:
const char* password) const;
void ban_until(uint32_t serial_number, uint64_t seconds);
size_t count() const;
void add(std::shared_ptr<License> l);
void remove(uint32_t serial_number);
std::vector<License> snapshot() const;
protected:
void save_locked() const;
+17 -5
View File
@@ -15,6 +15,7 @@
#include "Server.hh"
#include "FileContentsCache.hh"
#include "Text.hh"
#include "Shell.hh"
using namespace std;
@@ -112,6 +113,12 @@ void populate_state_from_config(shared_ptr<ServerState> s,
} catch (const JSONObject::key_error&) {
s->run_dns_server = true;
}
try {
s->run_interactive_shell = d.at("RunInteractiveShell")->as_bool();
} catch (const JSONObject::key_error&) {
s->run_interactive_shell = true;
}
}
@@ -165,10 +172,16 @@ int main(int argc, char* argv[]) {
}
game_server->start();
for (;;) {
sigset_t s;
sigemptyset(&s);
sigsuspend(&s);
if (state->run_interactive_shell) {
log(INFO, "starting interactive shell");
run_shell(state);
} else {
for (;;) {
sigset_t s;
sigemptyset(&s);
sigsuspend(&s);
}
}
log(INFO, "waiting for servers to terminate");
@@ -179,4 +192,3 @@ int main(int argc, char* argv[]) {
return 0;
}
+2 -2
View File
@@ -2,10 +2,10 @@ OBJECTS=FileContentsCache.o Menu.o PSOProtocol.o Client.o Lobby.o \
ServerState.o Server.o License.o PSOEncryption.o Player.o SendCommands.o \
ChatCommands.o ReceiveSubcommands.o ReceiveCommands.o Version.o Items.o \
LevelTable.o Compression.o Quest.o RareItemSet.o Map.o NetworkAddresses.o \
Text.o DNSServer.o Main.o
Text.o DNSServer.o Shell.o Main.o
CXX=g++
CXXFLAGS=-I/opt/local/include -I/usr/local/include -std=c++14 -g -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H -Wall -Werror
LDFLAGS=-L/opt/local/lib -L/usr/local/lib -std=c++14 -levent -levent_pthreads -lphosg -lpthread
LDFLAGS=-L/opt/local/lib -L/usr/local/lib -std=c++14 -levent -levent_pthreads -lphosg -lpthread -lreadline
EXECUTABLE=newserv
all: $(EXECUTABLE)
+2 -1
View File
@@ -26,7 +26,8 @@ So, you've read all of the above and you want to try it out? Here's what you do:
- Build and install phosg (https://github.com/fuzziqersoftware/phosg).
- Run `make`.
- Edit system/config.json to your liking.
- Run `./newserv` in the newserv directory.
- Run `./newserv` in the newserv directory. This will start the game server and run the interactive shell. (You can disable the interactive shell later by editing config.json.)
- Use the interactive shell to add a license. Run `help` in the shell to see how to do this.
If you're running PSO on a real GameCube, you can make PSO connect to newserv by changing its default gateway and DNS server addresses to newserv's address.
+2 -1
View File
@@ -10,7 +10,8 @@ using namespace std;
ServerState::ServerState() : run_dns_server(true), next_lobby_id(1) {
ServerState::ServerState() : run_dns_server(true), run_interactive_shell(true),
next_lobby_id(1) {
this->main_menu.emplace_back(MAIN_MENU_GO_TO_LOBBY, u"Go to lobby",
u"Join the lobby.", 0);
this->main_menu.emplace_back(MAIN_MENU_INFORMATION, u"Information",
+1
View File
@@ -29,6 +29,7 @@ struct ServerState {
std::u16string name;
std::unordered_map<std::string, PortConfiguration> port_configuration;
bool run_dns_server;
bool run_interactive_shell;
std::shared_ptr<const QuestIndex> quest_index;
std::shared_ptr<const LevelTable> level_table;
std::shared_ptr<const BattleParamTable> battle_params;
+182
View File
@@ -0,0 +1,182 @@
#include "Shell.hh"
#include <readline/history.h>
#include <readline/readline.h>
#include <stdio.h>
#include <phosg/Strings.hh>
using namespace std;
class exit_shell : public runtime_error {
public:
exit_shell() : runtime_error("shell exited") { }
~exit_shell() = default;
};
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);
if (command_name == "exit") {
throw exit_shell();
} 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 bytes");
}
strcpy(l->gc_password, 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\'");
}
}
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);
// read and execute commands
bool should_continue = true;
while (should_continue) {
// read the command
char* command = readline("newserv> ");
if (!command) {
fprintf(stderr, " -- exit\n");
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);
}
// dispatch the command
try {
execute_command(state, command_to_execute);
} catch (const exit_shell&) {
should_continue = false;
} catch (const exception& e) {
fprintf(stderr, "FAILED: %s\n", e.what());
}
free(command);
}
}
+7
View File
@@ -0,0 +1,7 @@
#pragma once
#include <memory>
#include "ServerState.hh"
void run_shell(std::shared_ptr<ServerState> state);
+3
View File
@@ -18,6 +18,9 @@
"Threads": 1,
// Set to false to disable the DNS server
"RunDNSServer": true,
// Set to false to disable the interactive shell (do this if stdin is
// /dev/null, for example)
"RunInteractiveShell": true,
// ****************
// INFORMATION MENU