void WebServerPage::applySettings() noexcept { auto plainserverPort = Util::toInt(Text::fromT(WinUtil::getEditText(ctrlPort))); auto tlsServerPort = Util::toInt(Text::fromT(WinUtil::getEditText(ctrlTlsPort))); WEBCFG(PLAIN_PORT).setCurValue(plainserverPort); WEBCFG(TLS_PORT).setCurValue(tlsServerPort); webMgr->getUserManager().replaceWebUsers(webUserList); }
LRESULT WebServerPage::onInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) { PropPage::translate((HWND)(*this), texts); ::SetWindowText(GetDlgItem(IDC_WEBSERVER_PORT_LABEL), CTSTRING_F(WEB_SERVER_PORT, "HTTP")); ::SetWindowText(GetDlgItem(IDC_WEBSERVER_TLSPORT_LABEL), CTSTRING_F(WEB_SERVER_PORT, "HTTPS")); ::SetWindowText(GetDlgItem(IDC_WEBSERVER_PORT), Util::toStringW(WEBCFG(PLAIN_PORT).num()).c_str()); ::SetWindowText(GetDlgItem(IDC_WEBSERVER_TLSPORT), Util::toStringW(WEBCFG(TLS_PORT).num()).c_str()); ctrlTlsPort.Attach(GetDlgItem(IDC_WEBSERVER_TLSPORT)); ctrlPort.Attach(GetDlgItem(IDC_WEBSERVER_PORT)); url.SubclassWindow(GetDlgItem(IDC_LINK)); url.SetHyperLinkExtendedStyle(HLINK_UNDERLINEHOVER); // TODO: add better help link url.SetHyperLink(_T("http://www.airdcpp.net/component/k2/24-web-server")); url.SetLabel(CTSTRING(MORE_INFORMATION)); ctrlWebUsers.Attach(GetDlgItem(IDC_WEBSERVER_USERS)); ctrlWebUsers.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT); CRect rc; ctrlWebUsers.GetClientRect(rc); ctrlWebUsers.InsertColumn(0, CTSTRING(USERNAME), LVCFMT_LEFT, rc.Width() / 2, 0); ctrlWebUsers.InsertColumn(1, CTSTRING(LAST_SEEN), LVCFMT_LEFT, rc.Width() / 2, 1); ctrlRemove.Attach(GetDlgItem(IDC_WEBSERVER_REMOVE_USER)); ctrlAdd.Attach(GetDlgItem(IDC_WEBSERVER_ADD_USER)); ctrlChange.Attach(GetDlgItem(IDC_WEBSERVER_CHANGE)); ctrlStart.Attach(GetDlgItem(IDC_WEBSERVER_START)); ctrlStatus.Attach(GetDlgItem(IDC_WEBSERVER_STATUS)); updateState(webMgr->isRunning() ? STATE_STARTED : STATE_STOPPED); webUserList = webMgr->getUserManager().getUsers(); for (const auto& u : webUserList) { if (!u->isAdmin()) { continue; } addListItem(u); } webMgr->addListener(this); return TRUE; }
api_return SystemApi::handleGetStats(ApiRequest& aRequest) { auto server = session->getServer(); aRequest.setResponseBody({ { "server_threads", WEBCFG(SERVER_THREADS).num() }, { "active_sessions", server->getUserManager().getUserSessionCount() }, }); return websocketpp::http::status_code::ok; }
void WebServerPage::write() { auto plainserverPort = Util::toInt(Text::fromT(WinUtil::getEditText(ctrlPort))); auto tlsServerPort = Util::toInt(Text::fromT(WinUtil::getEditText(ctrlTlsPort))); auto needServerRestart = WEBCFG(PLAIN_PORT).num() != plainserverPort || WEBCFG(TLS_PORT).num() != tlsServerPort; const auto errorF = [](const string& aError) { LogManager::getInstance()->message(aError, LogMessage::SEV_ERROR); }; applySettings(); webMgr->save(errorF); if (needServerRestart) { if (webMgr->isRunning()) { webMgr->getInstance()->stop(); } webMgr->getInstance()->start(errorF); } }
websocketpp::http::status_code::value SessionApi::handleLogin(ApiRequest& aRequest, bool aIsSecure, const WebSocketPtr& aSocket, const string& aIP) { const auto& reqJson = aRequest.getRequestBody(); auto username = JsonUtil::getField<string>("username", reqJson, false); auto password = JsonUtil::getField<string>("password", reqJson, false); auto inactivityMinutes = JsonUtil::getOptionalFieldDefault<uint64_t>("max_inactivity", reqJson, WEBCFG(DEFAULT_SESSION_IDLE_TIMEOUT).uint64()); auto userSession = JsonUtil::getOptionalFieldDefault<bool>("user_session", reqJson, false); auto session = WebServerManager::getInstance()->getUserManager().authenticate(username, password, aIsSecure, inactivityMinutes, userSession, aIP); if (!session) { aRequest.setResponseErrorStr("Invalid username or password"); return websocketpp::http::status_code::unauthorized; } json retJson = { { "permissions", session->getUser()->getPermissions() }, { "token", session->getAuthToken() }, { "user", session->getUser()->getUserName() }, { "system", getSystemInfo(aIP) }, { "run_wizard", SETTING(WIZARD_RUN) }, { "cid", ClientManager::getInstance()->getMyCID().toBase32() }, }; if (aSocket) { session->onSocketConnected(aSocket); aSocket->setSession(session); } aRequest.setResponseBody(retJson); return websocketpp::http::status_code::ok; }