Files
openrouter/src/settings/ssh/port.cpp
T
wt 6e85d8e922
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Waiting to run
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Waiting to run
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Waiting to run
added full support for config file
2025-02-11 03:24:55 +07:00

107 lines
4.4 KiB
C++

#include <settings/ssh/ssh.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 SSH_PORT_REQUIRED_STRING R"({"required":"[token,port]"})"
#define SSH_PORT_PATH "/etc/openrouter/ssh/port"
namespace api
{
namespace settings
{
namespace ssh
{
void get_ssh_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 ssh_port = config::config.getConfig()["settings"]["ssh"]["port"];
std::stringstream responsedata;
responsedata << R"({"port":")" << ssh_port << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
}
void set_ssh_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(SSH_PORT_REQUIRED_STRING, JSON_TYPE);
return;
}
if (!authenticate(json_body["token"]))
{
response.set_content(R"({"access":"reject"})", JSON_TYPE);
return;
}
std::ifstream oldsshportfile(SSH_PORT_PATH);
int old_ssh_port;
oldsshportfile >> old_ssh_port;
oldsshportfile.close();
std::ofstream sshportfileout(SSH_PORT_PATH);
std::string new_ssh_port = json_body["port"];
sshportfileout << stoi(new_ssh_port);
sshportfileout.close();
std::stringstream systemrequest;
if (get_ssh_server() == DROPBEAR_SSH)
{
systemrequest << "sed -i \"s/DROPBEAR_PORT=" << old_ssh_port << "/DROPBEAR_PORT=" << new_ssh_port << "/g\" /etc/default/dropbear";
} else if (get_ssh_server() == OPENSSH_SSH)
{
systemrequest << "sed -i \"/Port " << old_ssh_port << "/s//Port " << new_ssh_port << "/\" /etc/ssh/sshd_config";
}
std::cout << systemrequest.str() << std::endl;
system(systemrequest.str().c_str());
if (get_ssh_server() == OPENSSH_SSH)
{
system("rc restart ssh");
} else if (get_ssh_server() == DROPBEAR_SSH)
{
system("rc restart dropbear");
}
std::string syslogstring = "SSH port changed to " + new_ssh_port;
syslog(LOG_INFO, "%s", syslogstring.c_str());
config::config.setConfig(std::string("../config.json"));
nlohmann::json new_config = config::config.getConfig();
new_config["settings"]["ssh"]["port"] = std::stoi(new_ssh_port);
config::config.setConfig(new_config);
config::config.saveConfig();
int ssh_port = config::config.getConfig()["settings"]["ssh"]["port"];
std::stringstream responsedata;
responsedata << R"({"port":")" << ssh_port << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
}
}
}
}