some hot fixes
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

This commit is contained in:
wt committed 2025-02-08 18:37:21 +07:00
1 parent 84733a2644
commit ad266d0332
23 files changed
+1126 -1067

No files matched your search

+77 -74
View File
@@ -17,87 +17,90 @@ namespace api
{
namespace settings
{
void get_ssh_port(const httplib::Request &request, httplib::Response &response)
namespace ssh
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (!check_body(request))
void get_ssh_port(const httplib::Request &request, httplib::Response &response)
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
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;
}
std::ifstream sshportfile(SSH_PORT_PATH);
int ssh_port;
sshportfile >> ssh_port;
sshportfile.close();
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 sshportfile(SSH_PORT_PATH);
int ssh_port;
sshportfile >> ssh_port;
sshportfile.close();
std::stringstream responsedata;
responsedata << R"({"port":")" << ssh_port << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
}
if (!authenticate(json_body["token"]))
void set_ssh_port(const httplib::Request &request, httplib::Response &response)
{
response.set_content(R"({"access":"reject"})", JSON_TYPE);
return;
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());
std::ifstream sshportfile(SSH_PORT_PATH);
int ssh_port;
sshportfile >> ssh_port;
sshportfile.close();
std::stringstream responsedata;
responsedata << R"({"port":")" << ssh_port << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
}
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());
std::ifstream sshportfile(SSH_PORT_PATH);
int ssh_port;
sshportfile >> ssh_port;
sshportfile.close();
std::stringstream responsedata;
responsedata << R"({"port":")" << ssh_port << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
}
}
}