diff --git a/Tubio/DownloadManager.cpp b/Tubio/DownloadManager.cpp index c4fe140..6d9b281 100644 --- a/Tubio/DownloadManager.cpp +++ b/Tubio/DownloadManager.cpp @@ -160,6 +160,7 @@ void DownloadManager::DownloadNext() ytdl_call_video_base = Internal::StringHelpers::Replace(ytdl_call_video_base, "$$DL_URL", entry->webpage_url); ytdl_call_video_base = Internal::StringHelpers::Replace(ytdl_call_video_base, "$$DL_PROG_BUF_FILE", XGConfig::downloader.cachedir + "/dlprogbuf/" + entry->tubio_id + ".buf"); + entry->downloaded_filename = XGConfig::downloader.cachedir + "/download/" + entry->tubio_id + ".mp4"; ss << ytdl_call_video_base; } @@ -175,6 +176,7 @@ void DownloadManager::DownloadNext() ytdl_call_audio_base = Internal::StringHelpers::Replace(ytdl_call_audio_base, "$$DL_URL", entry->webpage_url); ytdl_call_audio_base = Internal::StringHelpers::Replace(ytdl_call_audio_base, "$$DL_PROG_BUF_FILE", XGConfig::downloader.cachedir + "/dlprogbuf/" + entry->tubio_id + ".buf"); + entry->downloaded_filename = XGConfig::downloader.cachedir + "/download/" + entry->tubio_id + ".mp3"; ss << ytdl_call_audio_base; } diff --git a/Tubio/HttpServer.cpp b/Tubio/HttpServer.cpp index 4bbe415..6a8f807 100644 --- a/Tubio/HttpServer.cpp +++ b/Tubio/HttpServer.cpp @@ -81,43 +81,59 @@ void HttpServer::EventHandler(mg_connection* pNc, int ev, void* p) http_message* hpm = (http_message*)p; std::string requestedUri = FixUnterminatedString(hpm->uri.p, hpm->uri.len); - - try + + std::string peer_addr; { - if (requestedUri == "/api") - { - ProcessAPIRequest(pNc, ev, p); - } - else if (requestedUri.substr(0, 9) == "/download") - { - ServeDownloadeableResource(pNc, ev, p, requestedUri); - } - else - { - // Just serve the files requested - mg_serve_http(pNc, (struct http_message*)p, frontend_serve_opts); - } - } - catch (std::exception& e) - { - Json j; - j.CloneFrom(RestResponseTemplates::GetByCode(INTERNAL_SERVER_ERROR, e.what())); - ServeStringToConnection(pNc, j.Render(), INTERNAL_SERVER_ERROR); - } - catch (...) - { - Json j; - j.CloneFrom(RestResponseTemplates::GetByCode(INTERNAL_SERVER_ERROR, "Das not good")); - ServeStringToConnection(pNc, j.Render(), INTERNAL_SERVER_ERROR); + char buf[32]; + mg_sock_addr_to_str(&pNc->sa, buf, sizeof(buf), MG_SOCK_STRINGIFY_IP); + peer_addr = buf; } - break; + if ((XGConfig::general.onlyAllowLocalhost) && (peer_addr != "127.0.0.1")) + { + Json j; + j.CloneFrom(RestResponseTemplates::GetByCode(UNAUTHORIZED, "Only localhost allowed!")); + ServeStringToConnection(pNc, j.Render(), UNAUTHORIZED); + } + else + { + try + { + if (requestedUri == "/api") + { + ProcessAPIRequest(pNc, ev, p, peer_addr); + } + else if (requestedUri.substr(0, 9) == "/download") + { + ServeDownloadeableResource(pNc, ev, p, requestedUri); + } + else + { + // Just serve the files requested + mg_serve_http(pNc, (struct http_message*)p, frontend_serve_opts); + } + } + catch (std::exception& e) + { + Json j; + j.CloneFrom(RestResponseTemplates::GetByCode(INTERNAL_SERVER_ERROR, e.what())); + ServeStringToConnection(pNc, j.Render(), INTERNAL_SERVER_ERROR); + } + catch (...) + { + Json j; + j.CloneFrom(RestResponseTemplates::GetByCode(INTERNAL_SERVER_ERROR, "Das not good")); + ServeStringToConnection(pNc, j.Render(), INTERNAL_SERVER_ERROR); + } + + break; + } } return; } -void HttpServer::ProcessAPIRequest(mg_connection* pNc, int ev, void* p) +void HttpServer::ProcessAPIRequest(mg_connection* pNc, int ev, void* p, std::string peerAddress) { // Get struct with http message informations http_message* hpm = (http_message*)p; @@ -131,12 +147,11 @@ void HttpServer::ProcessAPIRequest(mg_connection* pNc, int ev, void* p) Json requestBody; requestBody.Parse(requestBodyRaw); - char peer_addr[32]; - mg_sock_addr_to_str(&pNc->sa, peer_addr, sizeof(peer_addr), MG_SOCK_STRINGIFY_IP); + JsonBlock responseBody; HTTP_STATUS_CODE returnCode; - RestQueryHandler::ProcessQuery(std::string(peer_addr), requestBody, responseBody, returnCode); + RestQueryHandler::ProcessQuery(peerAddress, requestBody, responseBody, returnCode); Json response(responseBody); ServeStringToConnection(pNc, response.Render(), returnCode); diff --git a/Tubio/HttpServer.h b/Tubio/HttpServer.h index e0a3901..1333e29 100644 --- a/Tubio/HttpServer.h +++ b/Tubio/HttpServer.h @@ -22,7 +22,7 @@ namespace Rest private: bool InitWebServer(); - static void ProcessAPIRequest(struct mg_connection* pNc, int ev, void* p); + static void ProcessAPIRequest(struct mg_connection* pNc, int ev, void* p, std::string peerAddress); static void ServeDownloadeableResource(struct mg_connection* pNc, int ev, void* p, std::string uri); static void EventHandler(struct mg_connection* pNc, int ev, void* p); diff --git a/Tubio/RestQueryHandler.cpp b/Tubio/RestQueryHandler.cpp index 8b94fdb..6972586 100644 --- a/Tubio/RestQueryHandler.cpp +++ b/Tubio/RestQueryHandler.cpp @@ -119,7 +119,11 @@ bool RestQueryHandler::FetchSessionCache(const JsonBlock& request, JsonBlock& re // This way we can ensure that only entries of this SESSION will appear! if (request["max_age"].AsInt >= 0) { +#ifdef _WIN max_age = min(request["max_age"].AsInt, max_age); +#else + max_age = std::min(request["max_age"].AsInt, max_age); +#endif } } if (ValidateField("max_num", JDType::INT, request, dummy)) @@ -234,7 +238,7 @@ bool RestQueryHandler::GetOSName(const JsonBlock& request, JsonBlock& responseBo log->cout << "Asking for server OS name..."; log->Flush(); - std::string osName = "other"; + std::string osName = "Other"; #ifdef _WIN osName = "Windows"; #elif __APPLE__ || __MACH__ diff --git a/Tubio/RestResponseTemplates.cpp b/Tubio/RestResponseTemplates.cpp index 6d84ed1..37f6425 100644 --- a/Tubio/RestResponseTemplates.cpp +++ b/Tubio/RestResponseTemplates.cpp @@ -16,7 +16,9 @@ JsonBlock Rest::RestResponseTemplates::GetByCode(HTTP_STATUS_CODE code, std::str case HTTP_STATUS_CODE::BAD_REQUEST: return BadRequest(message); case HTTP_STATUS_CODE::FORBIDDEN: - return Forbidden((message.length() > 0) ? message : "Could be disabled in the config file!"); + return Forbidden((message.length() > 0) ? message : "Forbidden!"); + case HTTP_STATUS_CODE::UNAUTHORIZED: + return Unauthorized((message.length() > 0) ? message : "Unauthorized"); case HTTP_STATUS_CODE::NOT_FOUND: return NotFound((message.length() > 0) ? message : "not found"); case HTTP_STATUS_CODE::INTERNAL_SERVER_ERROR: diff --git a/Tubio/XGConfig.cpp b/Tubio/XGConfig.cpp index 737469b..749772a 100644 --- a/Tubio/XGConfig.cpp +++ b/Tubio/XGConfig.cpp @@ -44,12 +44,8 @@ void XGConfig::InitializeDefaultValues() downloader.max_dlrate_per_thread = "100M"; downloader.num_threads = 1; - downloader.loginCredentials.use_account = false; - downloader.loginCredentials.username = ""; - downloader.loginCredentials.password = ""; - downloader.loginCredentials.twofactor = ""; - general.show_console = true; + general.onlyAllowLocalhost = true; return; } @@ -103,27 +99,14 @@ void XGConfig::LoadFromJson(const JasonPP::JsonBlock& json) downloader.max_dlrate_per_thread = json.ShorthandGet("downloader.max_dlrate_per_thread").AsString; } - if (IsJsonFieldValid(json, "downloader.loginCredentials.use_account", JDType::BOOL)) - { - downloader.loginCredentials.use_account = json.ShorthandGet("downloader.loginCredentials.use_account").AsBool; - } - if (IsJsonFieldValid(json, "downloader.loginCredentials.username", JDType::STRING)) - { - downloader.loginCredentials.username = json.ShorthandGet("downloader.loginCredentials.username").AsString; - } - if (IsJsonFieldValid(json, "downloader.loginCredentials.password", JDType::STRING)) - { - downloader.loginCredentials.password = json.ShorthandGet("downloader.loginCredentials.password").AsString; - } - if (IsJsonFieldValid(json, "downloader.loginCredentials.twofactor", JDType::STRING)) - { - downloader.loginCredentials.twofactor = json.ShorthandGet("downloader.loginCredentials.twofactor").AsString; - } - if (IsJsonFieldValid(json, "general.show_console", JDType::BOOL)) { general.show_console = json.ShorthandGet("general.show_console").AsBool; } + if (IsJsonFieldValid(json, "general.onlyAllowLocalhost", JDType::BOOL)) + { + general.onlyAllowLocalhost = json.ShorthandGet("general.onlyAllowLocalhost").AsBool; + } return; } @@ -145,15 +128,10 @@ JsonBlock XGConfig::CreateJson() Ele("cachedir", downloader.cachedir), Ele("max_dlrate_per_thread", downloader.max_dlrate_per_thread), Ele("num_threads", downloader.num_threads), - Ele("login_credentials", JsonBlock({ - Ele("use_account", downloader.loginCredentials.use_account), - Ele("username", downloader.loginCredentials.username), - Ele("password", downloader.loginCredentials.password), - Ele("twofactor", downloader.loginCredentials.twofactor) - })) })), Ele("general", JsonBlock({ - Ele("show_console", general.show_console) + Ele("show_console", general.show_console), + Ele("onlyAllowLocalhost", general.onlyAllowLocalhost) })) }); } diff --git a/Tubio/XGConfig.h b/Tubio/XGConfig.h index dfd10da..5a84b13 100644 --- a/Tubio/XGConfig.h +++ b/Tubio/XGConfig.h @@ -23,23 +23,14 @@ public: }; struct Downloader { - struct LoginCredentials - { - bool use_account; - std::string username; - std::string password; - std::string twofactor; - }; - std::string cachedir; std::string max_dlrate_per_thread; - LoginCredentials loginCredentials; - int num_threads; }; struct General { bool show_console; + bool onlyAllowLocalhost; }; static void PreInit(); diff --git a/Tubio/frontend/200.html b/Tubio/frontend/200.html index ac5705c..8541a0b 100644 --- a/Tubio/frontend/200.html +++ b/Tubio/frontend/200.html @@ -1,9 +1,9 @@
-