ftpiiu_plugin/source/ftpServer.cpp

1084 lines
24 KiB
C++
Raw Normal View History

2020-04-05 21:16:16 +02:00
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2024 Michael Theall
2020-04-05 21:16:16 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "ftpServer.h"
#include "fs.h"
#include "ftpConfig.h"
#include "ftpSession.h"
2020-04-10 04:21:25 +02:00
#include "log.h"
#include "platform.h"
#include "sockAddr.h"
2020-04-17 22:32:39 +02:00
#include "socket.h"
2020-04-05 21:16:16 +02:00
#ifndef __WIIU__
2020-04-05 21:16:16 +02:00
#include "imgui.h"
#include "licenses.h"
#endif
#ifndef __NDS__
#include "mdns.h"
#endif
2020-04-05 21:16:16 +02:00
#ifdef __NDS__
2020-04-17 22:32:39 +02:00
#include <dswifi9.h>
#endif
#ifdef __3DS__
#include <citro3d.h>
#endif
#ifndef CLASSIC
#include <imgui.h>
#include <jansson.h>
#include <curl/easy.h>
#include <curl/multi.h>
#ifndef NDEBUG
#include <curl/curl.h>
#endif
#endif
2020-04-23 00:15:23 +02:00
#include <sys/statvfs.h>
using statvfs_t = struct statvfs;
2020-04-05 21:16:16 +02:00
2021-06-02 03:01:15 +02:00
#include <algorithm>
#include <array>
#include <atomic>
2020-12-12 02:15:33 +01:00
#include <cctype>
2020-04-05 21:16:16 +02:00
#include <chrono>
#include <cstdint>
2020-04-05 21:16:16 +02:00
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
2020-04-05 21:16:16 +02:00
#include <mutex>
#include <string>
2020-12-12 02:15:33 +01:00
#include <string_view>
#include <utility>
#include <vector>
2020-04-05 21:16:16 +02:00
using namespace std::chrono_literals;
#ifdef __NDS__
2020-04-17 22:32:39 +02:00
#define LOCKED(x) x
#else
#define LOCKED(x) \
do \
{ \
2023-03-11 14:02:43 +01:00
auto const lock = std::scoped_lock (m_lock); \
x; \
} while (0)
2020-04-17 22:32:39 +02:00
#endif
2020-04-05 21:16:16 +02:00
namespace
{
2020-04-06 07:36:03 +02:00
/// \brief Application start time
auto const s_startTime = std::time (nullptr);
#ifdef __3DS__
/// \brief Timezone offset in seconds (only used on 3DS)
int s_tzOffset = 0;
#endif
#ifndef __NDS__
2020-04-06 07:36:03 +02:00
/// \brief Mutex for s_freeSpace
2020-04-05 21:16:16 +02:00
platform::Mutex s_lock;
2020-04-17 22:32:39 +02:00
#endif
2020-04-06 07:36:03 +02:00
/// \brief Free space string
2020-04-05 21:16:16 +02:00
std::string s_freeSpace;
2022-10-04 04:52:19 +02:00
#ifndef CLASSIC
#ifndef NDEBUG
std::string printable (std::string_view const data_)
2022-10-04 04:52:19 +02:00
{
unsigned count = 0;
for (auto const &c : data_)
{
if (c != '%' && (std::isprint (c) || std::isspace (c)))
++count;
else
count += 3;
}
2022-10-04 04:52:19 +02:00
std::string result;
result.reserve (count);
2022-10-04 04:52:19 +02:00
for (auto const &c : data_)
2022-10-04 04:52:19 +02:00
{
if (c != '%' && (std::isprint (c) || std::isspace (c)))
result.push_back (c);
2022-10-04 04:52:19 +02:00
else
{
result.push_back ('%');
auto const upper = (static_cast<unsigned char> (c) >> 4u) & 0xF;
auto const lower = (static_cast<unsigned char> (c) >> 0u) & 0xF;
result.push_back (gsl::narrow_cast<char> (upper < 10 ? upper + '0' : upper + 'A' - 10));
result.push_back (gsl::narrow_cast<char> (lower < 10 ? lower + '0' : lower + 'A' - 10));
2022-10-04 04:52:19 +02:00
}
}
return result;
}
2023-03-11 14:02:43 +01:00
int curlDebug (CURL *const handle_,
curl_infotype const type_,
char *const data_,
std::size_t const size_,
void *const user_)
2022-10-04 04:52:19 +02:00
{
(void)handle_;
2022-10-04 04:52:19 +02:00
(void)user_;
auto const text = printable (std::string_view (data_, size_));
2022-10-04 04:52:19 +02:00
switch (type_)
{
case CURLINFO_TEXT:
info ("== Info: %s", text.c_str ());
break;
case CURLINFO_HEADER_OUT:
info ("=> Send header: %s", text.c_str ());
break;
case CURLINFO_DATA_OUT:
info ("=> Send data: %s", text.c_str ());
break;
case CURLINFO_SSL_DATA_OUT:
info ("=> Send SSL data: %s", text.c_str ());
break;
case CURLINFO_HEADER_IN:
info ("<= Receive header: %s", text.c_str ());
break;
case CURLINFO_DATA_IN:
info ("<= Receive data: %s", text.c_str ());
break;
case CURLINFO_SSL_DATA_IN:
info ("<= Receive SSL data: %s", text.c_str ());
break;
default:
break;
}
return 0;
}
#endif
2023-03-11 14:02:43 +01:00
std::size_t curlCallback (void *const contents_,
std::size_t const size_,
std::size_t const count_,
void *const user_)
2022-10-04 04:52:19 +02:00
{
auto const total = size_ * count_;
2023-03-11 14:02:43 +01:00
auto const start = static_cast<char *> (contents_);
2022-10-04 04:52:19 +02:00
auto const end = start + total;
2023-03-11 14:02:43 +01:00
auto &result = *static_cast<std::string *> (user_);
2022-10-04 04:52:19 +02:00
result.insert (std::end (result), start, end);
return total;
}
#endif
2020-04-05 21:16:16 +02:00
}
///////////////////////////////////////////////////////////////////////////
FtpServer::~FtpServer ()
{
m_quit = true;
#ifndef __NDS__
2020-04-05 21:16:16 +02:00
m_thread.join ();
2020-04-17 22:32:39 +02:00
#endif
2022-10-04 04:52:19 +02:00
#ifndef CLASSIC
if (m_uploadLogCurl)
{
curl_multi_remove_handle (m_uploadLogCurlM, m_uploadLogCurl);
curl_easy_cleanup (m_uploadLogCurl);
curl_mime_free (m_uploadLogMime);
}
if (m_uploadLogCurlM)
curl_multi_cleanup (m_uploadLogCurlM);
#endif
2020-04-05 21:16:16 +02:00
}
FtpServer::FtpServer (UniqueFtpConfig config_)
: m_config (std::move (config_))
#ifndef CLASSIC
,
m_hostnameSetting (m_config->hostname ())
#endif
2020-04-05 21:16:16 +02:00
{
#ifndef __NDS__
mdns::setHostname (m_config->hostname ());
2020-04-05 21:16:16 +02:00
m_thread = platform::Thread (std::bind (&FtpServer::threadFunc, this));
2020-04-17 22:32:39 +02:00
#endif
#ifdef __3DS__
s64 tzOffsetMinutes;
if (R_SUCCEEDED (svcGetSystemInfo (&tzOffsetMinutes, 0x10000, 0x103)))
s_tzOffset = tzOffsetMinutes * 60;
#endif
2020-04-05 21:16:16 +02:00
}
void FtpServer::draw ()
{
#ifdef __NDS__
2020-04-17 22:32:39 +02:00
loop ();
#endif
#ifdef CLASSIC
2020-04-23 00:15:23 +02:00
{
char port[7];
#ifndef __NDS__
2022-10-04 04:52:19 +02:00
auto const lock = std::scoped_lock (m_lock);
2020-04-23 00:15:23 +02:00
#endif
if (m_socket)
std::sprintf (port, ":%u", m_socket->sockName ().port ());
2023-11-19 13:55:58 +01:00
#ifndef NO_CONSOLE
2020-04-23 00:15:23 +02:00
consoleSelect (&g_statusConsole);
std::printf ("\x1b[0;0H\x1b[32;1m%s \x1b[36;1m%s%s",
STATUS_STRING,
m_socket ? m_socket->sockName ().name () : "Waiting on WiFi",
m_socket ? port : "");
2023-11-19 13:55:58 +01:00
#endif
2020-04-23 00:15:23 +02:00
#ifndef __NDS__
2020-04-23 00:15:23 +02:00
char timeBuffer[16];
auto const now = std::time (nullptr);
std::strftime (timeBuffer, sizeof (timeBuffer), "%H:%M:%S", std::localtime (&now));
std::printf (" \x1b[37;1m%s", timeBuffer);
#endif
std::fputs ("\x1b[K", stdout);
std::fflush (stdout);
}
2020-04-17 22:32:39 +02:00
{
#ifndef __NDS__
2022-10-04 04:52:19 +02:00
auto const lock = std::scoped_lock (s_lock);
2020-04-17 22:32:39 +02:00
#endif
if (!s_freeSpace.empty ())
{
2023-11-19 13:55:58 +01:00
#ifndef NO_CONSOLE
2020-04-17 22:32:39 +02:00
consoleSelect (&g_statusConsole);
std::printf ("\x1b[0;%uH\x1b[32;1m%s",
static_cast<unsigned> (g_statusConsole.windowWidth - s_freeSpace.size () + 1),
s_freeSpace.c_str ());
std::fflush (stdout);
2023-11-19 13:55:58 +01:00
#endif
2020-04-17 22:32:39 +02:00
}
}
{
#ifndef __NDS__
2022-10-04 04:52:19 +02:00
auto const lock = std::scoped_lock (m_lock);
2020-04-17 22:32:39 +02:00
#endif
2023-11-19 13:55:58 +01:00
#ifndef NO_CONSOLE
2020-04-17 22:32:39 +02:00
consoleSelect (&g_sessionConsole);
std::fputs ("\x1b[2J", stdout);
for (auto &session : m_sessions)
{
session->draw ();
if (&session != &m_sessions.back ())
std::fputc ('\n', stdout);
}
2020-04-23 00:15:23 +02:00
std::fflush (stdout);
2023-11-19 13:55:58 +01:00
#endif
2020-04-17 22:32:39 +02:00
}
2020-04-23 00:15:23 +02:00
drawLog ();
2020-04-17 22:32:39 +02:00
#else
auto const &io = ImGui::GetIO ();
2020-04-05 21:16:16 +02:00
auto const width = io.DisplaySize.x;
auto const height = io.DisplaySize.y;
ImGui::SetNextWindowPos (ImVec2 (0, 0), ImGuiCond_FirstUseEver);
2022-03-31 18:53:47 +02:00
#ifdef __3DS__
2020-04-05 21:16:16 +02:00
// top screen
ImGui::SetNextWindowSize (ImVec2 (width, height * 0.5f));
2020-04-05 21:16:16 +02:00
#else
ImGui::SetNextWindowSize (ImVec2 (width, height));
#endif
{
std::array<char, 64> title{};
2020-04-23 00:15:23 +02:00
2020-04-05 21:16:16 +02:00
{
2022-10-04 04:52:19 +02:00
auto const serverLock = std::scoped_lock (m_lock);
std::snprintf (title.data (),
title.size (),
2020-04-23 00:15:23 +02:00
STATUS_STRING " %s###ftpd",
m_socket ? m_name.c_str () : "Waiting for WiFi...");
2020-04-05 21:16:16 +02:00
}
ImGui::Begin (title.data (),
2020-04-23 00:15:23 +02:00
nullptr,
2020-04-24 22:51:43 +02:00
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize
2022-03-31 18:53:47 +02:00
#ifndef __3DS__
2020-04-24 22:51:43 +02:00
| ImGuiWindowFlags_MenuBar
#endif
);
}
2022-03-31 18:53:47 +02:00
#ifndef __3DS__
2020-04-24 22:51:43 +02:00
showMenu ();
#endif
2022-03-31 18:53:47 +02:00
#ifndef __3DS__
2020-04-23 07:25:05 +02:00
ImGui::BeginChild (
2020-04-24 22:51:43 +02:00
"Logs", ImVec2 (0, 0.5f * height), false, ImGuiWindowFlags_HorizontalScrollbar);
2020-04-05 21:16:16 +02:00
#endif
2020-04-10 04:21:25 +02:00
drawLog ();
2022-03-31 18:53:47 +02:00
#ifndef __3DS__
2020-04-05 21:16:16 +02:00
ImGui::EndChild ();
2020-04-23 00:15:23 +02:00
#endif
2020-04-05 21:16:16 +02:00
2022-03-31 18:53:47 +02:00
#ifdef __3DS__
2020-04-05 21:16:16 +02:00
ImGui::End ();
// bottom screen
ImGui::SetNextWindowSize (ImVec2 (width * 0.8f, height * 0.5f));
ImGui::SetNextWindowPos (ImVec2 (width * 0.1f, height * 0.5f), ImGuiCond_FirstUseEver);
2020-04-05 21:16:16 +02:00
ImGui::Begin ("Sessions",
nullptr,
2020-04-24 22:51:43 +02:00
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_MenuBar);
showMenu ();
2020-04-05 21:16:16 +02:00
#else
ImGui::Separator ();
#endif
{
2022-10-04 04:52:19 +02:00
auto const lock = std::scoped_lock (m_lock);
for (auto &session : m_sessions)
session->draw ();
}
2020-04-05 21:16:16 +02:00
ImGui::End ();
2020-04-17 22:32:39 +02:00
#endif
2020-04-05 21:16:16 +02:00
}
bool FtpServer::quit ()
{
return m_quit;
}
2020-04-24 22:51:43 +02:00
UniqueFtpServer FtpServer::create ()
2020-04-05 21:16:16 +02:00
{
updateFreeSpace ();
2020-04-24 22:51:43 +02:00
auto config = FtpConfig::load (FTPDCONFIG);
return UniqueFtpServer (new FtpServer (std::move (config)));
2020-04-05 21:16:16 +02:00
}
2020-04-23 00:15:23 +02:00
std::string FtpServer::getFreeSpace ()
{
#ifndef __NDS__
2022-10-04 04:52:19 +02:00
auto const lock = std::scoped_lock (s_lock);
2020-04-23 00:15:23 +02:00
#endif
return s_freeSpace;
}
2020-04-05 21:16:16 +02:00
void FtpServer::updateFreeSpace ()
{
statvfs_t st = {};
#if defined(__NDS__) || defined(__3DS__) || defined(__SWITCH__)
2020-04-05 21:16:16 +02:00
if (::statvfs ("sdmc:/", &st) != 0)
2020-04-23 00:15:23 +02:00
#else
if (::statvfs ("/", &st) != 0)
#endif
2020-04-05 21:16:16 +02:00
return;
2020-04-17 22:32:39 +02:00
auto freeSpace = fs::printSize (static_cast<std::uint64_t> (st.f_bsize) * st.f_bfree);
#ifndef __NDS__
2022-10-04 04:52:19 +02:00
auto const lock = std::scoped_lock (s_lock);
2020-04-23 00:15:23 +02:00
#endif
2020-04-17 22:32:39 +02:00
if (freeSpace != s_freeSpace)
s_freeSpace = std::move (freeSpace);
2020-04-05 21:16:16 +02:00
}
std::time_t FtpServer::startTime ()
{
return s_startTime;
}
#ifdef __3DS__
int FtpServer::tzOffset ()
{
return s_tzOffset;
}
#endif
void FtpServer::handleNetworkFound ()
2020-04-05 21:16:16 +02:00
{
2020-12-12 02:15:33 +01:00
SockAddr addr;
if (!platform::networkAddress (addr))
return;
std::uint16_t port;
{
#ifndef __NDS__
2020-12-12 02:15:33 +01:00
auto const lock = m_config->lockGuard ();
2020-04-05 21:16:16 +02:00
#endif
2020-12-12 02:15:33 +01:00
port = m_config->port ();
}
addr.setPort (port);
2020-04-05 21:16:16 +02:00
auto socket = Socket::create (Socket::eStream);
2020-04-05 21:16:16 +02:00
if (!socket)
return;
2020-12-12 02:15:33 +01:00
if (port != 0 && !socket->setReuseAddress (true))
2020-04-05 21:16:16 +02:00
return;
if (!socket->bind (addr))
return;
if (!socket->listen (10))
return;
auto const &sockName = socket->sockName ();
auto const name = sockName.name ();
m_name.resize (std::strlen (name) + 3 + 5);
2020-12-12 02:15:33 +01:00
m_name.resize (std::sprintf (m_name.data (), "[%s]:%u", name, sockName.port ()));
2020-04-05 21:16:16 +02:00
2020-04-10 04:21:25 +02:00
info ("Started server at %s\n", m_name.c_str ());
2020-04-05 21:16:16 +02:00
LOCKED (m_socket = std::move (socket));
#ifndef __NDS__
socket = mdns::createSocket ();
if (!socket)
return;
LOCKED (m_mdnsSocket = std::move (socket));
#endif
2020-04-05 21:16:16 +02:00
}
void FtpServer::handleNetworkLost ()
2020-04-05 21:16:16 +02:00
{
{
// destroy sessions
std::vector<UniqueFtpSession> sessions;
LOCKED (sessions = std::move (m_sessions));
}
{
UniqueSocket sock;
// destroy command socket
LOCKED (sock = std::move (m_socket));
#ifndef __NDS__
// destroy mDNS socket
LOCKED (sock = std::move (m_mdnsSocket));
#endif
}
2020-04-10 04:21:25 +02:00
info ("Stopped server at %s\n", m_name.c_str ());
2020-04-05 21:16:16 +02:00
}
2020-12-12 02:15:33 +01:00
#ifndef CLASSIC
2020-04-24 22:51:43 +02:00
void FtpServer::showMenu ()
{
auto const prevShowSettings = m_showSettings;
2020-04-26 00:47:55 +02:00
auto const prevShowAbout = m_showAbout;
2020-04-24 22:51:43 +02:00
if (ImGui::BeginMenuBar ())
{
2022-03-31 18:53:47 +02:00
#if defined(__3DS__) || defined(__SWITCH__)
2022-10-04 04:52:19 +02:00
if (ImGui::BeginMenu ("Menu \xee\x80\x83")) // Y Button
2020-04-24 22:51:43 +02:00
#else
if (ImGui::BeginMenu ("Menu"))
#endif
{
if (ImGui::MenuItem ("Settings"))
m_showSettings = true;
2022-10-04 04:52:19 +02:00
if (ImGui::MenuItem ("Upload Log"))
{
#ifndef __NDS__
2022-10-04 04:52:19 +02:00
auto const lock = std::scoped_lock (m_lock);
#endif
if (!m_uploadLogCurlM)
m_uploadLogCurlM = curl_multi_init ();
if (m_uploadLogCurlM && !m_uploadLogCurl.load (std::memory_order_relaxed))
{
m_uploadLogData = getLog ();
auto const handle = curl_easy_init ();
#ifndef NDEBUG
curl_easy_setopt (handle, CURLOPT_DEBUGFUNCTION, &curlDebug);
curl_easy_setopt (handle, CURLOPT_DEBUGDATA, nullptr);
curl_easy_setopt (handle, CURLOPT_VERBOSE, 1L);
#endif
// write result into string
m_uploadLogResult.clear ();
curl_easy_setopt (handle, CURLOPT_WRITEFUNCTION, &curlCallback);
curl_easy_setopt (handle, CURLOPT_WRITEDATA, &m_uploadLogResult);
// set headers
static char contentType[] = "Content-Type: text/plain";
2023-03-11 14:02:43 +01:00
static curl_slist const headers = {contentType, nullptr};
curl_easy_setopt (handle, CURLOPT_URL, "https://pastie.io/documents");
2022-10-04 04:52:19 +02:00
curl_easy_setopt (handle, CURLOPT_HTTPHEADER, &headers);
// set form data
auto const mime = curl_mime_init (handle);
auto const part = curl_mime_addpart (mime);
curl_mime_name (part, "data");
curl_mime_data (part, m_uploadLogData.data (), m_uploadLogData.size ());
curl_easy_setopt (handle, CURLOPT_MIMEPOST, mime);
// add to multi handle
curl_multi_add_handle (m_uploadLogCurlM, handle);
// signal network thread to process
m_uploadLogMime = mime;
m_uploadLogCurl.store (handle, std::memory_order_relaxed);
}
}
ImGui::Separator ();
2020-04-26 00:47:55 +02:00
if (ImGui::MenuItem ("About"))
m_showAbout = true;
ImGui::Separator ();
if (ImGui::MenuItem ("Quit"))
m_quit = true;
2020-04-24 22:51:43 +02:00
ImGui::EndMenu ();
}
ImGui::EndMenuBar ();
}
2020-04-26 00:47:55 +02:00
if (m_showSettings)
2020-04-24 22:51:43 +02:00
{
2020-04-26 00:47:55 +02:00
if (!prevShowSettings)
{
#ifndef __NDS__
2020-12-12 02:15:33 +01:00
auto const lock = m_config->lockGuard ();
#endif
2020-04-26 00:47:55 +02:00
m_userSetting = m_config->user ();
m_userSetting.resize (32);
2020-04-24 22:51:43 +02:00
2020-04-26 00:47:55 +02:00
m_passSetting = m_config->pass ();
m_passSetting.resize (32);
2020-04-24 22:51:43 +02:00
m_hostnameSetting = m_config->hostname ();
m_hostnameSetting.resize (32);
2020-04-26 00:47:55 +02:00
m_portSetting = m_config->port ();
2020-04-24 22:51:43 +02:00
2022-03-31 18:53:47 +02:00
#ifdef __3DS__
2020-04-26 00:47:55 +02:00
m_getMTimeSetting = m_config->getMTime ();
2020-04-24 22:51:43 +02:00
#endif
2020-12-12 02:15:33 +01:00
#ifdef __SWITCH__
m_enableAPSetting = m_config->enableAP ();
m_ssidSetting = m_config->ssid ();
m_ssidSetting.resize (19);
m_passphraseSetting = m_config->passphrase ();
m_passphraseSetting.resize (63);
#endif
2020-04-26 00:47:55 +02:00
ImGui::OpenPopup ("Settings");
}
showSettings ();
2020-04-24 22:51:43 +02:00
}
2020-04-26 00:47:55 +02:00
if (m_showAbout)
{
if (!prevShowAbout)
ImGui::OpenPopup ("About");
showAbout ();
}
}
void FtpServer::showSettings ()
{
2022-03-31 18:53:47 +02:00
#ifdef __3DS__
2020-04-24 22:51:43 +02:00
auto const &io = ImGui::GetIO ();
auto const width = io.DisplaySize.x;
auto const height = io.DisplaySize.y;
ImGui::SetNextWindowSize (ImVec2 (width * 0.8f, height * 0.5f));
ImGui::SetNextWindowPos (ImVec2 (width * 0.1f, height * 0.5f));
if (ImGui::BeginPopupModal ("Settings",
nullptr,
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize))
#else
if (ImGui::BeginPopupModal ("Settings", nullptr, ImGuiWindowFlags_AlwaysAutoResize))
#endif
{
2020-12-12 02:15:33 +01:00
ImGui::InputText ("User",
m_userSetting.data (),
m_userSetting.size (),
ImGuiInputTextFlags_AutoSelectAll);
2020-04-24 22:51:43 +02:00
ImGui::InputText ("Pass",
2020-12-12 02:15:33 +01:00
m_passSetting.data (),
2020-04-24 22:51:43 +02:00
m_passSetting.size (),
ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_Password);
ImGui::InputText ("Hostname",
m_hostnameSetting.data (),
m_hostnameSetting.size (),
ImGuiInputTextFlags_AutoSelectAll);
2020-04-24 22:51:43 +02:00
ImGui::InputScalar ("Port",
ImGuiDataType_U16,
&m_portSetting,
nullptr,
nullptr,
"%u",
ImGuiInputTextFlags_AutoSelectAll);
2022-03-31 18:53:47 +02:00
#ifdef __3DS__
2020-04-24 22:51:43 +02:00
ImGui::Checkbox ("Get mtime", &m_getMTimeSetting);
#endif
2020-12-12 02:15:33 +01:00
#ifdef __SWITCH__
ImGui::Checkbox ("Enable Access Point", &m_enableAPSetting);
ImGui::InputText ("SSID",
m_ssidSetting.data (),
m_ssidSetting.size (),
ImGuiInputTextFlags_AutoSelectAll);
auto const ssidError = platform::validateSSID (m_ssidSetting);
if (ssidError)
ImGui::TextColored (ImVec4 (1.0f, 0.4f, 0.4f, 1.0f), ssidError);
ImGui::InputText ("Passphrase",
m_passphraseSetting.data (),
m_passphraseSetting.size (),
ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_Password);
auto const passphraseError = platform::validatePassphrase (m_passphraseSetting);
if (passphraseError)
ImGui::TextColored (ImVec4 (1.0f, 0.4f, 0.4f, 1.0f), passphraseError);
#endif
2021-06-02 03:01:15 +02:00
static ImVec2 const sizes[] = {
ImGui::CalcTextSize ("Apply"),
ImGui::CalcTextSize ("Save"),
ImGui::CalcTextSize ("Reset"),
ImGui::CalcTextSize ("Cancel"),
};
static auto const maxWidth = std::max_element (
std::begin (sizes), std::end (sizes), [] (auto const &lhs_, auto const &rhs_) {
return lhs_.x < rhs_.x;
})->x;
static auto const maxHeight = std::max_element (
std::begin (sizes), std::end (sizes), [] (auto const &lhs_, auto const &rhs_) {
return lhs_.y < rhs_.y;
})->y;
auto const &style = ImGui::GetStyle ();
auto const width = maxWidth + 2 * style.FramePadding.x;
auto const height = maxHeight + 2 * style.FramePadding.y;
auto const apply = ImGui::Button ("Apply", ImVec2 (width, height));
2020-04-24 22:51:43 +02:00
ImGui::SameLine ();
2021-06-02 03:01:15 +02:00
auto const save = ImGui::Button ("Save", ImVec2 (width, height));
2020-04-24 22:51:43 +02:00
ImGui::SameLine ();
2021-06-02 03:01:15 +02:00
auto const reset = ImGui::Button ("Reset", ImVec2 (width, height));
2020-12-30 19:37:02 +01:00
ImGui::SameLine ();
2021-06-02 03:01:15 +02:00
auto const cancel = ImGui::Button ("Cancel", ImVec2 (width, height));
2020-04-24 22:51:43 +02:00
if (apply || save)
{
m_showSettings = false;
ImGui::CloseCurrentPopup ();
#ifndef __NDS__
2020-12-12 02:15:33 +01:00
auto const lock = m_config->lockGuard ();
#endif
2020-04-24 22:51:43 +02:00
m_config->setUser (m_userSetting);
m_config->setPass (m_passSetting);
m_config->setHostname (m_hostnameSetting);
2020-04-24 22:51:43 +02:00
m_config->setPort (m_portSetting);
2022-03-31 18:53:47 +02:00
#ifdef __3DS__
2020-04-24 22:51:43 +02:00
m_config->setGetMTime (m_getMTimeSetting);
#endif
2020-12-12 02:15:33 +01:00
#ifdef __SWITCH__
m_config->setEnableAP (m_enableAPSetting);
m_config->setSSID (m_ssidSetting);
m_config->setPassphrase (m_passphraseSetting);
m_apError = false;
#endif
2020-04-24 22:51:43 +02:00
UniqueSocket socket;
LOCKED (socket = std::move (m_socket));
mdns::setHostname (m_hostnameSetting);
2020-04-24 22:51:43 +02:00
}
2020-12-30 19:37:02 +01:00
if (save)
2020-12-12 02:15:33 +01:00
{
#ifndef __NDS__
2020-12-12 02:15:33 +01:00
auto const lock = m_config->lockGuard ();
#endif
2020-12-30 19:37:02 +01:00
if (!m_config->save (FTPDCONFIG))
2020-12-12 02:15:33 +01:00
error ("Failed to save config\n");
}
2020-04-24 22:51:43 +02:00
2020-12-30 19:37:02 +01:00
if (reset)
{
static auto const defaults = FtpConfig::create ();
m_userSetting = defaults->user ();
m_passSetting = defaults->pass ();
m_hostnameSetting = defaults->hostname ();
m_portSetting = defaults->port ();
2022-03-31 18:53:47 +02:00
#ifdef __3DS__
2020-12-30 19:37:02 +01:00
m_getMTimeSetting = defaults->getMTime ();
#endif
#ifdef __SWITCH__
m_enableAPSetting = defaults->enableAP ();
m_ssidSetting = defaults->ssid ();
m_passphraseSetting = defaults->passphrase ();
#endif
}
2020-04-24 22:51:43 +02:00
if (apply || save || cancel)
{
m_showSettings = false;
ImGui::CloseCurrentPopup ();
}
ImGui::EndPopup ();
}
2020-04-26 00:47:55 +02:00
}
void FtpServer::showAbout ()
{
auto const &io = ImGui::GetIO ();
auto const width = io.DisplaySize.x;
auto const height = io.DisplaySize.y;
2022-03-31 18:53:47 +02:00
#ifdef __3DS__
2020-04-26 00:47:55 +02:00
ImGui::SetNextWindowSize (ImVec2 (width * 0.8f, height * 0.5f));
ImGui::SetNextWindowPos (ImVec2 (width * 0.1f, height * 0.5f));
#else
ImGui::SetNextWindowSize (ImVec2 (width * 0.8f, height * 0.8f));
ImGui::SetNextWindowPos (ImVec2 (width * 0.1f, height * 0.1f));
#endif
if (ImGui::BeginPopupModal ("About",
nullptr,
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize))
{
ImGui::TextUnformatted (STATUS_STRING);
ImGui::TextWrapped ("Copyright © 2024 Michael Theall, Dave Murphy, TuxSH");
2020-04-26 00:47:55 +02:00
ImGui::Separator ();
ImGui::Text ("Platform: %s", io.BackendPlatformName);
ImGui::Text ("Renderer: %s", io.BackendRendererName);
#ifdef __3DS__
ImGui::Text ("Command Buffer Usage: %.1f%%", 100.0f * C3D_GetCmdBufUsage ());
ImGui::Text ("GPU Processing Usage: %.1f%%", 6.0f * C3D_GetProcessingTime ());
ImGui::Text ("GPU Drawing Usage: %.1f%%", 6.0f * C3D_GetDrawingTime ());
#endif
2020-04-26 00:47:55 +02:00
if (ImGui::Button ("OK", ImVec2 (100, 0)))
{
m_showAbout = false;
ImGui::CloseCurrentPopup ();
}
ImGui::Separator ();
if (ImGui::TreeNode ("Connections"))
{
for (auto const &session : m_sessions)
session->drawConnections ();
ImGui::TreePop ();
}
2020-04-26 00:47:55 +02:00
ImGui::Separator ();
if (ImGui::TreeNode (g_dearImGuiVersion))
{
2023-03-11 14:02:43 +01:00
ImGui::TextWrapped ("%s", g_dearImGuiCopyright);
2020-04-26 00:47:55 +02:00
ImGui::Separator ();
2023-03-11 14:02:43 +01:00
ImGui::TextWrapped ("%s", g_mitLicense);
2020-04-26 00:47:55 +02:00
ImGui::TreePop ();
}
#if defined(__NDS__)
2022-03-31 18:53:47 +02:00
#elif defined(__3DS__)
2020-04-26 00:47:55 +02:00
if (ImGui::TreeNode (g_libctruVersion))
{
2023-03-11 14:02:43 +01:00
ImGui::TextWrapped ("%s", g_zlibLicense);
2020-04-26 00:47:55 +02:00
ImGui::Separator ();
2023-03-11 14:02:43 +01:00
ImGui::TextWrapped ("%s", g_zlibLicense);
2020-04-26 00:47:55 +02:00
ImGui::TreePop ();
}
if (ImGui::TreeNode (g_citro3dVersion))
{
2023-03-11 14:02:43 +01:00
ImGui::TextWrapped ("%s", g_citro3dCopyright);
2020-04-26 00:47:55 +02:00
ImGui::Separator ();
2023-03-11 14:02:43 +01:00
ImGui::TextWrapped ("%s", g_zlibLicense);
2020-04-26 00:47:55 +02:00
ImGui::TreePop ();
}
#elif defined(__SWITCH__)
if (ImGui::TreeNode (g_libnxVersion))
{
2023-03-11 14:02:43 +01:00
ImGui::TextWrapped ("%s", g_libnxCopyright);
2020-04-26 00:47:55 +02:00
ImGui::Separator ();
2023-03-11 14:02:43 +01:00
ImGui::TextWrapped ("%s", g_libnxLicense);
2020-04-26 00:47:55 +02:00
ImGui::TreePop ();
}
if (ImGui::TreeNode (g_deko3dVersion))
{
2023-03-11 14:02:43 +01:00
ImGui::TextWrapped ("%s", g_deko3dCopyright);
2020-04-26 00:47:55 +02:00
ImGui::Separator ();
2023-03-11 14:02:43 +01:00
ImGui::TextWrapped ("%s", g_zlibLicense);
2020-04-26 00:47:55 +02:00
ImGui::TreePop ();
}
if (ImGui::TreeNode (g_zstdVersion))
{
2023-03-11 14:02:43 +01:00
ImGui::TextWrapped ("%s", g_zstdCopyright);
2020-04-26 00:47:55 +02:00
ImGui::Separator ();
ImGui::TextWrapped ("%s", g_zstdLicense);
2020-04-26 00:47:55 +02:00
ImGui::TreePop ();
}
#else
if (ImGui::TreeNode (g_glfwVersion))
{
2023-03-11 14:02:43 +01:00
ImGui::TextWrapped ("%s", g_glfwCopyright);
2020-04-26 00:47:55 +02:00
ImGui::Separator ();
2023-03-11 14:02:43 +01:00
ImGui::TextWrapped ("%s", g_zlibLicense);
2020-04-26 00:47:55 +02:00
ImGui::TreePop ();
}
#endif
#if defined(__NDS__) || defined(__3DS__) || defined(__SWITCH__)
if (ImGui::TreeNode (g_globVersion))
{
ImGui::TextWrapped ("%s", g_globCopyright);
ImGui::Separator ();
ImGui::TextWrapped ("%s", g_globLicense);
ImGui::TreePop ();
}
if (ImGui::TreeNode (g_collateVersion))
{
ImGui::TextWrapped ("%s", g_collateCopyright);
ImGui::Separator ();
ImGui::TextWrapped ("%s", g_collateLicense);
ImGui::TreePop ();
}
#endif
2020-04-26 00:47:55 +02:00
ImGui::EndPopup ();
}
2020-04-24 22:51:43 +02:00
}
2020-12-12 02:15:33 +01:00
#endif
2020-04-24 22:51:43 +02:00
2020-04-05 21:16:16 +02:00
void FtpServer::loop ()
{
if (!m_socket)
{
2021-06-02 03:01:15 +02:00
#ifndef CLASSIC
2020-12-12 02:15:33 +01:00
#ifdef __SWITCH__
if (!m_apError)
{
bool enable;
std::string ssid;
std::string passphrase;
{
auto const lock = m_config->lockGuard ();
enable = m_config->enableAP ();
ssid = m_config->ssid ();
passphrase = m_config->passphrase ();
}
m_apError = !platform::enableAP (enable, ssid, passphrase);
}
2021-06-02 03:01:15 +02:00
#endif
2020-12-12 02:15:33 +01:00
#endif
if (platform::networkVisible ())
handleNetworkFound ();
}
2022-10-04 04:52:19 +02:00
#ifndef CLASSIC
{
auto const lock = std::scoped_lock (m_lock);
if (m_uploadLogCurl.load (std::memory_order_relaxed))
{
2023-03-11 14:02:43 +01:00
int busy = 0;
2022-10-04 04:52:19 +02:00
auto const mc = curl_multi_perform (m_uploadLogCurlM, &busy);
if (mc != CURLM_OK)
{
error ("curl_multi_perform: %d %s\n", mc, curl_multi_strerror (mc));
curl_multi_remove_handle (m_uploadLogCurlM, m_uploadLogCurl);
curl_easy_cleanup (m_uploadLogCurl);
curl_mime_free (m_uploadLogMime);
m_uploadLogMime = nullptr;
m_uploadLogCurl.store (nullptr, std::memory_order_relaxed);
}
else
{
2023-03-11 14:02:43 +01:00
int count = 0;
2022-10-04 04:52:19 +02:00
auto const msg = curl_multi_info_read (m_uploadLogCurlM, &count);
if (msg && msg->msg == CURLMSG_DONE && msg->easy_handle == m_uploadLogCurl)
{
if (msg->data.result != CURLE_OK)
info ("cURL finished with status %d\n", msg->data.result);
json_error_t err;
auto const root = json_loads (m_uploadLogResult.c_str (), 0, &err);
if (json_is_object (root))
2022-10-04 04:52:19 +02:00
{
auto const key = json_object_get (root, "key");
if (json_is_string (key))
info (
"Log uploaded to https://pastie.io/%s\n", json_string_value (key));
2022-10-04 04:52:19 +02:00
}
else
error ("Failed to upload log\n");
json_decref (root);
2022-10-04 04:52:19 +02:00
curl_multi_remove_handle (m_uploadLogCurlM, m_uploadLogCurl);
curl_easy_cleanup (m_uploadLogCurl);
curl_mime_free (m_uploadLogMime);
m_uploadLogMime = nullptr;
m_uploadLogCurl.store (nullptr, std::memory_order_relaxed);
}
}
}
}
#endif
// poll listen socket
if (m_socket)
2020-04-05 21:16:16 +02:00
{
Socket::PollInfo info{*m_socket, POLLIN, 0};
auto const rc = Socket::poll (&info, 1, 0ms);
if (rc < 0)
{
handleNetworkLost ();
return;
}
if (rc > 0 && (info.revents & POLLIN))
2020-04-05 21:16:16 +02:00
{
auto socket = m_socket->accept ();
if (socket)
2020-04-05 21:16:16 +02:00
{
2020-04-24 22:51:43 +02:00
auto session = FtpSession::create (*m_config, std::move (socket));
LOCKED (m_sessions.emplace_back (std::move (session)));
2020-04-05 21:16:16 +02:00
}
else
{
handleNetworkLost ();
return;
}
2020-04-05 21:16:16 +02:00
}
}
#ifndef __NDS__
// poll mDNS socket
if (m_socket && m_mdnsSocket)
mdns::handleSocket (m_mdnsSocket.get (), m_socket->sockName ());
#endif
2020-04-05 21:16:16 +02:00
{
std::vector<UniqueFtpSession> deadSessions;
{
// remove dead sessions
#ifndef __NDS__
2022-10-04 04:52:19 +02:00
auto const lock = std::scoped_lock (m_lock);
2020-04-17 22:32:39 +02:00
#endif
auto it = std::begin (m_sessions);
while (it != std::end (m_sessions))
{
auto &session = *it;
if (session->dead ())
{
deadSessions.emplace_back (std::move (session));
it = m_sessions.erase (it);
}
else
++it;
}
}
2020-04-05 21:16:16 +02:00
}
2020-04-06 07:36:03 +02:00
// poll sessions
2020-04-05 21:16:16 +02:00
if (!m_sessions.empty ())
2020-04-07 04:17:30 +02:00
{
if (!FtpSession::poll (m_sessions))
handleNetworkLost ();
2020-04-07 04:17:30 +02:00
}
#ifndef __NDS__
2020-04-06 07:36:03 +02:00
// avoid busy polling in background thread
2020-04-05 21:16:16 +02:00
else
platform::Thread::sleep (16ms);
2020-04-17 22:32:39 +02:00
#endif
2020-04-05 21:16:16 +02:00
}
void FtpServer::threadFunc ()
{
while (!m_quit)
loop ();
}