Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Has been cancelled
83 lines
3.2 KiB
C++
83 lines
3.2 KiB
C++
#include <settings/telnet/telnet.hpp>
|
|
#include <colors.h>
|
|
#include <types.hpp>
|
|
#include <json.hpp>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <syslog.h>
|
|
#include <auth/auth.hpp>
|
|
#include <helpers.hpp>
|
|
#include <config/config.hpp>
|
|
|
|
#define TELNET_PORT_REQUIRED_STRING R"({"required":"[token,port]"})"
|
|
|
|
namespace api
|
|
{
|
|
namespace settings
|
|
{
|
|
namespace telnet
|
|
{
|
|
void get_telnet_port(const httplib::Request& request, httplib::Response& response)
|
|
{
|
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
|
response.set_header("Access-Control-Allow-Origin", "*");
|
|
if (!check_body(request))
|
|
{
|
|
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
|
return;
|
|
}
|
|
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
|
|
|
if (!authenticate(json_body["token"]))
|
|
{
|
|
response.set_content(R"({"access":"reject"})", JSON_TYPE);
|
|
return;
|
|
}
|
|
int telnet_port = config::config.getConfig()["settings"]["telnet"]["port"];
|
|
std::stringstream responsedata;
|
|
responsedata << R"({"port":")" << telnet_port << R"("})";
|
|
response.set_content(responsedata.str(), JSON_TYPE);
|
|
}
|
|
|
|
void set_telnet_port(const httplib::Request& request, httplib::Response& response)
|
|
{
|
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
|
response.set_header("Access-Control-Allow-Origin", "*");
|
|
if (!check_body(request))
|
|
{
|
|
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
|
return;
|
|
}
|
|
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
|
|
|
if (json_body["port"] == nullptr)
|
|
{
|
|
response.set_content(TELNET_PORT_REQUIRED_STRING, JSON_TYPE);
|
|
return;
|
|
}
|
|
|
|
if (!authenticate(json_body["token"]))
|
|
{
|
|
response.set_content(R"({"access":"reject"})", JSON_TYPE);
|
|
return;
|
|
}
|
|
std::string new_telnet_port = json_body["port"];
|
|
system("rc restart telnet");
|
|
std::string syslogstring = "Telnet port changed to " + new_telnet_port;
|
|
syslog(LOG_INFO, "%s", syslogstring.c_str());
|
|
std::stringstream responsedata;
|
|
config::config.setConfig(std::string("../config.json"));
|
|
nlohmann::json new_config = config::config.getConfig();
|
|
new_config["settings"]["telnet"]["port"] = stoi(new_telnet_port);
|
|
config::config.setConfig(new_config);
|
|
config::config.saveConfig();
|
|
int telnet_port = config::config.getConfig()["settings"]["telnet"]["port"];
|
|
responsedata << R"({"port":")" << telnet_port << R"("})";
|
|
response.set_content(responsedata.str(), JSON_TYPE);
|
|
}
|
|
}
|
|
}
|
|
}
|