/* Starshatter: The Open Source Project Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors Copyright (c) 1997-2006, Destroyer Studios LLC. AUTHOR: John DiCamillo OVERVIEW ======== HTTP Servlet Engine for Multiplayer Admin */ #include "NetAdminServer.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "NetAdminChat.h" // +-------------------------------------------------------------------+ // +-------------------------------------------------------------------+ // +-------------------------------------------------------------------+ class NetAdminLogin : public NetAdminServlet { public: NetAdminLogin() { } virtual ~NetAdminLogin() { } virtual bool DoGet(HttpRequest& request, HttpResponse& response) { NetServerConfig* config = NetServerConfig::GetInstance(); Text admin_name = "system"; Text admin_pass = "manager"; if (config) { admin_name = config->GetAdminName(); admin_pass = config->GetAdminPass(); } Text name = request.GetParam("user"); Text pass = request.GetParam("pass"); Sleep(500); if (CheckUser(request, response)) { response.SetStatus(HttpResponse::SC_TEMPORARY_REDIRECT); response.SetHeader("MIME-Version", "1.0"); response.SetHeader("Content-Type", "text/html"); response.SetHeader("Cache-Control", "no-cache"); response.SetHeader("Expires", "-1"); response.SetHeader("Location", "/home"); response.SetContent(GetHead("Login") + "
You are already logged in.
" + GetBodyClose()); } else if (name == admin_name && pass == admin_pass) { user = new NetUser(name); user->SetAddress(request.GetClientAddr()); if (session) user->SetSessionID(session->GetID()); admin->AddUser(user); response.SetStatus(HttpResponse::SC_TEMPORARY_REDIRECT); response.SetHeader("MIME-Version", "1.0"); response.SetHeader("Content-Type", "text/html"); response.SetHeader("Cache-Control", "no-cache"); response.SetHeader("Expires", "-1"); response.SetHeader("Location", "/home"); response.SetContent(GetHead("Login") + "
You have successfully logged in.
" + GetBodyClose()); } else { response.SetStatus(HttpResponse::SC_OK); response.SetHeader("MIME-Version", "1.0"); response.SetHeader("Content-Type", "text/html"); response.SetHeader("Cache-Control", "no-cache"); response.SetHeader("Expires", "-1"); response.SetContent(GetHead("Login") + GetTitleBar(0, "onLoad=\"self.focus();document.loginForm.user.focus();\"") + GetContent() + GetBodyClose()); } return true; } virtual Text GetContent() { Text content = " \n\ \n\ \n\ \n\ \n\
  

\n\ Welcome to the Starshatter Server!

\n\ Login to access the server "; NetServerConfig* config = NetServerConfig::GetInstance(); if (config) content += config->Name(); else content += "server"; content += "
\n\
\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\
Username:
Password:
 
\n\
\n\
 
\n"; return content; } }; // +-------------------------------------------------------------------+ // +-------------------------------------------------------------------+ // +-------------------------------------------------------------------+ class NetAdminFile : public NetAdminServlet { public: NetAdminFile() { } virtual ~NetAdminFile() { } virtual bool DoGet(HttpRequest& request, HttpResponse& response) { if (!CheckUser(request, response)) return true; Text content; Text path = request.GetParam("path"); Text name = request.GetParam("name"); if (name.length()) { BYTE* buffer = 0; DataLoader* loader = DataLoader::GetLoader(); if (loader) { bool use_file_system = loader->IsFileSystemEnabled(); loader->UseFileSystem(true); loader->SetDataPath(path); int len = loader->LoadBuffer(name, buffer); if (len) { content = Text((const char*) buffer, len); } loader->ReleaseBuffer(buffer); loader->SetDataPath(0); loader->UseFileSystem(use_file_system); } } response.SetStatus(HttpResponse::SC_OK); response.AddHeader("MIME-Version", "1.0"); response.AddHeader("Cache-Control", "no-cache"); response.AddHeader("Expires", "-1"); response.AddHeader("Content-Type", "text/plain"); response.SetContent(content); return true; } }; // +-------------------------------------------------------------------+ // +-------------------------------------------------------------------+ // +-------------------------------------------------------------------+ class NetAdminUserList : public NetAdminServlet { public: NetAdminUserList() { } virtual ~NetAdminUserList() { } virtual bool DoGet(HttpRequest& request, HttpResponse& response) { if (CheckUser(request, response)) { response.SetStatus(HttpResponse::SC_OK); response.SetHeader("MIME-Version", "1.0"); response.SetHeader("Content-Type", "text/html"); response.SetHeader("Cache-Control", "no-cache"); response.SetHeader("Expires", "-1"); response.SetContent(GetHead("User List") + GetTitleBar() + GetContent() + GetBodyClose()); } return true; } virtual Text GetContent() { Text content = "\n\
\n\ \n\ \n\ \n\ \n\
\n\  User List\n\
\n\n"; content += " \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n"; NetLobbyServer* lobby = NetLobbyServer::GetInstance(); if (lobby) { ListIter u_iter = lobby->GetUsers(); while (++u_iter) { NetUser* u = u_iter.value(); NetAddr a = u->GetAddress(); char addr_dotted[32]; char addr_hex[16]; char user_stats[16]; sprintf_s(addr_dotted, "%d.%d.%d.%d", a.B1(), a.B2(), a.B3(), a.B4()); sprintf_s(addr_hex, "%08x", a.IPAddr()); sprintf_s(user_stats, "%d / %d / %d", u->Missions(), u->Kills(), u->Losses()); content += "\n\n\ \n"; } } content += "
 NameAddressIs HostSquadronStatsBan
 "; content += u->Name(); content += ""; content += addr_dotted; content += ""; content += u->IsHost() ? "*" : " "; content += ""; content += u->Squadron(); content += ""; content += user_stats; content += ""; content += "Name()); content += "&addr="; content += addr_hex; content += "\">BAN
\n\n"; content += "
\n\n"; content += GetCopyright(); return content; } }; // +-------------------------------------------------------------------+ // +-------------------------------------------------------------------+ // +-------------------------------------------------------------------+ class NetAdminBanUser : public NetAdminServlet { public: NetAdminBanUser() { } virtual ~NetAdminBanUser() { } virtual bool DoGet(HttpRequest& request, HttpResponse& response) { if (CheckUser(request, response)) { Text name = request.GetParam("name"); bool completed = false; NetLobbyServer* lobby = NetLobbyServer::GetInstance(); if (lobby) { ListIter u_iter = lobby->GetUsers(); while (++u_iter && !completed) { NetUser* u = u_iter.value(); if (u->Name() == name) { NetLobbyServer* nls = NetLobbyServer::GetInstance(); if (nls) { nls->BanUser(u); completed = true; } } } } response.SetStatus(HttpResponse::SC_TEMPORARY_REDIRECT); response.SetHeader("MIME-Version", "1.0"); response.SetHeader("Content-Type", "text/html"); response.SetHeader("Cache-Control", "no-cache"); response.SetHeader("Expires", "-1"); response.SetHeader("Location", "/users"); response.SetContent(GetHead("User List") + GetTitleBar() + "
User Banned.
" + GetBodyClose()); } return true; } }; // +-------------------------------------------------------------------+ // +-------------------------------------------------------------------+ // +-------------------------------------------------------------------+ static NetAdminServer* net_Admin_server = 0; NetAdminServer* NetAdminServer::GetInstance(WORD port) { if (!net_Admin_server && port > 0) net_Admin_server = new NetAdminServer(port); return net_Admin_server; } NetAdminServer::NetAdminServer(WORD port) : HttpServletExec(port) { http_server_name = Text("Starshatter NetAdminServer ") + versionInfo; } NetAdminServer::~NetAdminServer() { if (net_Admin_server == this) net_Admin_server = 0; } // +--------------------------------------------------------------------+ HttpServlet* NetAdminServer::GetServlet(HttpRequest& request) { Text path = request.URI(); path.setSensitive(false); if (path.indexOf("/login") == 0) return new NetAdminLogin; if (path.indexOf("/chat") == 0) return new NetAdminChat; if (path.indexOf("/file") == 0) return new NetAdminFile; if (path.indexOf("/user") == 0) return new NetAdminUserList; if (path.indexOf("/ban") == 0) return new NetAdminBanUser; return new NetAdminServlet; } // +-------------------------------------------------------------------+ void NetAdminServer::AddChat(NetUser* user, const char* msg) { if (user && msg && *msg) { NetLobbyServer* lobby = NetLobbyServer::GetInstance(); if (lobby) lobby->AddChat(user, msg); } } ListIter NetAdminServer::GetChat() { NetLobbyServer* lobby = NetLobbyServer::GetInstance(); if (lobby) return lobby->GetChat(); static List idle_chatter; return idle_chatter; } // +-------------------------------------------------------------------+ void NetAdminServer::AddUser(NetUser* user) { if (user && !admin_users.contains(user)) admin_users.append(user); } void NetAdminServer::DelUser(NetUser* user) { if (user) { admin_users.remove(user); delete user; } } int NetAdminServer::NumUsers() { return admin_users.size(); } List& NetAdminServer::GetUsers() { return admin_users; } bool NetAdminServer::HasHost() { bool result = false; NetLobbyServer* lobby = NetLobbyServer::GetInstance(); if (lobby) result = lobby->HasHost(); return result; } NetUser* NetAdminServer::FindUserBySession(Text id) { ListIter iter = admin_users; while (++iter) { NetUser* u = iter.value(); if (u->GetSessionID() == id) return u; } return 0; } void NetAdminServer::DoSyncedCheck() { ListIter iter = admin_users; while (++iter) { NetUser* u = iter.value(); bool found = false; ListIter s_iter = sessions; while (++s_iter && !found) { HttpSession* s = s_iter.value(); if (s->GetID() == u->GetSessionID()) found = true; } if (!found) delete iter.removeItem(); } } // +-------------------------------------------------------------------+ // +-------------------------------------------------------------------+ // +-------------------------------------------------------------------+ NetAdminServlet::NetAdminServlet() { admin = NetAdminServer::GetInstance(); user = 0; } // +-------------------------------------------------------------------+ bool NetAdminServlet::DoGet(HttpRequest& request, HttpResponse& response) { if (CheckUser(request, response)) { if (request.URI() == "/home") response.SetStatus(HttpResponse::SC_OK); else response.SetStatus(HttpResponse::SC_TEMPORARY_REDIRECT); response.SetHeader("MIME-Version", "1.0"); response.SetHeader("Content-Type", "text/html"); response.SetHeader("Cache-Control", "no-cache"); response.SetHeader("Expires", "-1"); response.SetHeader("Location", "/home"); response.SetContent(GetHead() + GetTitleBar(GetStatLine()) + GetContent() + GetBodyClose()); } return true; } // +-------------------------------------------------------------------+ bool NetAdminServlet::CheckUser(HttpRequest& request, HttpResponse& response) { if (!user) { if (session) user = admin->FindUserBySession(session->GetID()); if (!user) { response.SetStatus(HttpResponse::SC_TEMPORARY_REDIRECT); response.SetHeader("MIME-Version", "1.0"); response.SetHeader("Content-Type", "text/plain"); response.SetHeader("Cache-Control", "no-cache"); response.SetHeader("Expires", "-1"); response.SetHeader("Location", "/login"); response.SetContent("You are not logged in."); } } return user != 0; } // +-------------------------------------------------------------------+ Text NetAdminServlet::GetCSS() { return "body { font-family:arial,helvetica,sans-serif; color:black; background-color:white }\n\ a:link { text-decoration:none; font-weight:normal; font-size:10pt; color:black }\n\ a:visited { text-decoration:none; font-weight:normal; font-size:10pt; color:black }\n\ a:hover { text-decoration:underline; font-weight:normal; font-size:10pt; color:black }\n\ .std { font-size:10pt }\n\ .tiny { font-size:8pt }\n\ .heading { font-size:14pt; font-weight:bold; background-color:#99BBEE }\n\ .subhead { font-size:11pt; font-weight:bold }\n\ .status { font-size:10pt; color:white }\n\ .content { padding-right: 4pt; padding-left: 4pt; padding-bottom: 4pt; padding-top: 4pt; margin: 4pt; }\n\ .copy { font-size:8pt; }\n\ .top-bar { color: white; background-color: #336699 }\n\ .top-line { color: yellow; background-color: black }\n\ .topbarbig { line-height:24px; color:white; font-size:18px; font-weight:bold; }\n\ .topbarsmall { line-height:18px; color:white; font-size:14px; }\n"; } Text NetAdminServlet::GetHead(const char* title) { Text head = "\n\nStarshatter Server"; if (title && *title) { head += " - "; head += title; } head += "\n\n\n"; return head; } Text NetAdminServlet::GetBody() { return GetTitleBar(GetStatLine()) + GetContent() + GetBodyClose(); } Text NetAdminServlet::GetTitleBar(const char* statline, const char* onload) { Text bar = "\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\
 \n\ Administration Console
\n"; if (statline) { bar += ""; } bar += "Starshatter Server "; bar += versionInfo; bar += ""; if (statline) { bar += ""; } bar += "\n\
"; if (statline && *statline) bar += statline; else bar += " "; bar += "
\n\n"; return bar; } Text NetAdminServlet::GetStatLine() { NetServerConfig* config = NetServerConfig::GetInstance(); Text line = " \n\ \n\ \n\ \n\ \n\ \n\
\n\   Connected to "; char buffer[256]; sprintf_s(buffer, "%s:%d", config->Name().data(), config->GetAdminPort()); line += buffer; line += "\n\ \n\ Server Mode: "; NetLobbyServer* lobby = NetLobbyServer::GetInstance(); if (lobby) { switch (lobby->GetStatus()) { default: case NetServerInfo::OFFLINE: line += "Offline"; break; case NetServerInfo::LOBBY: line += "Lobby"; break; case NetServerInfo::BRIEFING: line += "Briefing"; break; case NetServerInfo::ACTIVE: line += "Active"; break; case NetServerInfo::DEBRIEFING: line += "Debriefing"; break; case NetServerInfo::PERSISTENT: line += "PERSISTENT"; break; } } else { line += "Unknown"; } line += "\n\ \n\ "; line += FormatTimeString(); line += "  \n\
\n"; return line; } Text NetAdminServlet::GetContent() { Text content = "\n\
\n\ \n\ \n\ \n\ \n\
\n\  Game Admin Functions\n\
\n\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\
 \n\ Lobby Chat\n\ \n\ Mission List\n\
 \n\ View Error Log\n\ \n\ Player List\n\
 \n\ View Server Log\n\ \n\ Ban List\n\
 
\n\n"; content += "
\n\n"; content += GetCopyright(); return content; } Text NetAdminServlet::GetBodyClose() { return "\n\n\n\n"; } Text NetAdminServlet::GetCopyright() { return "
    Copyright © 1997-2004 Destroyer Studios. All rights reserved.
"; }