refactorings

This commit is contained in:
wt committed 2024-07-16 00:37:08 +07:00
1 parent f5aff2ca7b
commit 48b15f450f
19 files changed
+134 -343

No files matched your search

+2
View File
@@ -35,6 +35,8 @@ add_executable(
src/OpenRouter/OpenRouter.cpp
src/auth/auth.cpp
src/libs/QR-Code-generator/qrcodegen.cpp
src/libs/QR-Code-generator/qrtosvg.cpp
)
+22
View File
@@ -0,0 +1,22 @@
#include "auth.hpp"
#include <sstream>
#include <httplib.h>
#include "../routes.hpp"
#include <json.hpp>
#include "../types.hpp"
bool authenticate(std::string token) {
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":")" << token << R"("})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
return false;
}
return true;
}
return false;
}
+8
View File
@@ -0,0 +1,8 @@
#ifndef AUTH_HPP
#define AUTH_HPP
#include <iostream>
bool authenticate(std::string token);
#endif // AUTH_HPP
+7 -24
View File
@@ -4,6 +4,7 @@
#include "../../routes.hpp"
#include "../../types.hpp"
#include <syslog.h>
#include "../../auth/auth.hpp"
#define DNS_REQUIRED_STRING R"({"required":"[token,enabled]"})"
@@ -22,18 +23,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream dnsfile("/etc/openrouter/dns");
int dns_enabled;
dnsfile >> dns_enabled;
@@ -41,7 +31,9 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"enabled":")" << dns_enabled << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
void set_dns(const httplib::Request &request, httplib::Response &response) {
@@ -63,18 +55,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ofstream dnsfileout("/etc/openrouter/dns");
std::string new_dns_enabled = json_body["enabled"];
if (new_dns_enabled == "0") {
@@ -95,6 +76,8 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"enabled":")" << dns_enabled << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+7 -24
View File
@@ -4,6 +4,7 @@
#include "../../routes.hpp"
#include "../../types.hpp"
#include <syslog.h>
#include "../../auth/auth.hpp"
namespace settings {
void get_local_domains(const httplib::Request &request, httplib::Response &response) {
@@ -20,18 +21,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream domainsfile("/etc/dnsmasq.conf.d/hosts");
std::stringstream responsedata;
responsedata << R"({"domains":[)";
@@ -56,7 +46,9 @@ namespace settings {
}
domainsfile.close();
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
void set_local_domains(const httplib::Request &request, httplib::Response &response) {
@@ -73,18 +65,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ofstream domainsfile("/etc/dnsmasq.conf.d/hosts");
nlohmann::json domains = json_body["domains"];
for (nlohmann::json_abi_v3_11_3::json domain : domains) {
@@ -97,6 +78,8 @@ namespace settings {
syslog(LOG_INFO, "DNS local domains changed");
std::string responsedata = R"({"success":"true"})";
response.set_content(responsedata, JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+7 -26
View File
@@ -4,8 +4,7 @@
#include "../../routes.hpp"
#include "../../types.hpp"
#include <syslog.h>
// #define DNS_REQUIRED_STRING R"({"required":"[token,enabled]"})"
#include "../../auth/auth.hpp"
namespace settings {
void get_dns_servers(const httplib::Request &request, httplib::Response &response) {
@@ -22,18 +21,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf");
std::stringstream responsedata;
responsedata << R"({"servers":[)";
@@ -52,7 +40,9 @@ namespace settings {
}
dnsserversfile.close();
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
void set_dns_servers(const httplib::Request &request, httplib::Response &response) {
@@ -69,18 +59,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ofstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf");
nlohmann::json servers = json_body["servers"];
for (nlohmann::json_abi_v3_11_3::json server : servers) {
@@ -95,6 +74,8 @@ namespace settings {
syslog(LOG_INFO, "DNS servers changed");
std::string responsedata = R"({"success":"true"})";
response.set_content(responsedata, JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+7 -24
View File
@@ -4,6 +4,7 @@
#include "../../routes.hpp"
#include "../../types.hpp"
#include <syslog.h>
#include "../../auth/auth.hpp"
#define FTP_REQUIRED_STRING R"({"required":"[token,enabled]"})"
@@ -22,18 +23,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream ftpfile("/etc/openrouter/ftp");
int ftp_enabled;
ftpfile >> ftp_enabled;
@@ -41,7 +31,9 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"enabled":")" << ftp_enabled << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
void set_ftp(const httplib::Request &request, httplib::Response &response) {
@@ -63,18 +55,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ofstream ftpfileout("/etc/openrouter/ftp");
std::string new_ftp_enabled = json_body["enabled"];
if (new_ftp_enabled == "0") {
@@ -95,6 +76,8 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"enabled":")" << ftp_enabled << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+8 -24
View File
@@ -4,6 +4,7 @@
#include "../../routes.hpp"
#include "../../types.hpp"
#include <syslog.h>
#include "../../auth/auth.hpp"
#define KEYS_PATH "/root/.ssh/authorized_keys"
@@ -22,18 +23,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream keysfile(KEYS_PATH);
std::stringstream responsedata;
responsedata << R"({"keys":[)";
@@ -54,8 +44,11 @@ namespace settings {
}
keysfile.close();
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
void set_ssh_keys(const httplib::Request &request, httplib::Response &response) {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
@@ -70,18 +63,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ofstream keysfile(KEYS_PATH);
nlohmann::json keys = json_body["keys"];
for (std::string key : keys) {
@@ -92,6 +74,8 @@ namespace settings {
syslog(LOG_INFO, "SSH keys changed");
std::string responsedata = R"({"success":"true"})";
response.set_content(responsedata, JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+8 -24
View File
@@ -8,6 +8,7 @@
#include <fstream>
#include <string>
#include <syslog.h>
#include "../../auth/auth.hpp"
#define SSH_PORT_REQUIRED_STRING R"({"required":"[token,port]"})"
#define SSH_PORT_PATH "/etc/openrouter/ssh/port"
@@ -27,18 +28,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream sshportfile(SSH_PORT_PATH);
int ssh_port;
sshportfile >> ssh_port;
@@ -46,8 +36,11 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"port":")" << ssh_port << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", 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", "*");
@@ -67,18 +60,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream oldsshportfile(SSH_PORT_PATH);
int old_ssh_port;
oldsshportfile >> old_ssh_port;
@@ -101,6 +83,8 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"port":")" << ssh_port << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+8 -24
View File
@@ -4,6 +4,7 @@
#include "../../routes.hpp"
#include "../../types.hpp"
#include <syslog.h>
#include "../../auth/auth.hpp"
#define SSH_REQUIRED_STRING R"({"required":"[token,status]"})"
@@ -22,18 +23,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream sshstatusfile("/etc/openrouter/ssh/status");
int ssh_status;
sshstatusfile >> ssh_status;
@@ -41,8 +31,11 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"status":")" << ssh_status << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
void set_ssh_status(const httplib::Request &request, httplib::Response &response) {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
@@ -62,18 +55,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ofstream sshfileout("/etc/openrouter/ssh/status");
std::string new_ssh_status = json_body["status"];
if (new_ssh_status == "0") {
@@ -94,6 +76,8 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"status":")" << ssh_status << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+7 -24
View File
@@ -8,6 +8,7 @@
#include <fstream>
#include <string>
#include <syslog.h>
#include "../../auth/auth.hpp"
#define TELNET_REQUIRED_STRING R"({"required":"[token,enabled]"})"
@@ -26,18 +27,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream telnetfile("/etc/openrouter/telnet/enabled");
int telnet_enabled;
telnetfile >> telnet_enabled;
@@ -45,7 +35,9 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"enabled":")" << telnet_enabled << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
void set_telnet(const httplib::Request &request, httplib::Response &response) {
@@ -67,18 +59,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ofstream telnetfileout("/etc/openrouter/telnet/enabled");
std::string new_telnet_enabled = json_body["enabled"];
if (new_telnet_enabled == "0") {
@@ -99,6 +80,8 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"enabled":")" << telnet_enabled << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+7 -24
View File
@@ -8,6 +8,7 @@
#include <fstream>
#include <string>
#include <syslog.h>
#include "../../auth/auth.hpp"
#define TELNET_PORT_REQUIRED_STRING R"({"required":"[token,port]"})"
@@ -26,18 +27,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream telnetportfile("/etc/openrouter/telnet/port");
int telnet_port;
telnetportfile >> telnet_port;
@@ -45,7 +35,9 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"port":")" << telnet_port << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
void set_telnet_port(const httplib::Request &request, httplib::Response &response) {
@@ -67,18 +59,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ofstream telnetportfileout("/etc/openrouter/telnet/port");
std::string new_telnet_port = json_body["port"];
telnetportfileout << stoi(new_telnet_port);
@@ -93,6 +74,8 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"port":")" << telnet_port << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+7 -24
View File
@@ -8,6 +8,7 @@
#include <fstream>
#include <string>
#include <syslog.h>
#include "../../auth/auth.hpp"
#define WIFI_CHANNEL_REQUIRED_STRING R"({"required":"[token,channel]"})"
@@ -26,18 +27,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH);
std::string wifi_channel;
std::string wifisettingsbuffer;
@@ -52,7 +42,9 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"channel":")" << wifi_channel << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
void set_wifi_channel(const httplib::Request &request, httplib::Response &response) {
@@ -74,18 +66,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH);
std::string old_channel;
std::string wifisettingsbuffer;
@@ -120,6 +101,8 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"channel":")" << wifi_channel << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+7 -24
View File
@@ -8,6 +8,7 @@
#include <fstream>
#include <string>
#include <syslog.h>
#include "../../auth/auth.hpp"
#define WIFI_NAME_REQUIRED_STRING R"({"required":"[token,name]"})"
@@ -26,18 +27,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH);
std::string wifi_name;
std::string wifisettingsbuffer;
@@ -52,7 +42,9 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"name":")" << wifi_name << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
void set_wifi_name(const httplib::Request &request, httplib::Response &response) {
@@ -74,18 +66,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH);
std::string old_wifi_name;
std::string wifisettingsbuffer;
@@ -120,6 +101,8 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"name":")" << wifi_name << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+7 -24
View File
@@ -8,6 +8,7 @@
#include <fstream>
#include <string>
#include <syslog.h>
#include "../../auth/auth.hpp"
#define WIFI_PASSWORD_REQUIRED_STRING R"({"required":"[token,password,newpassword]"})"
@@ -26,18 +27,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH);
std::string wifi_password;
std::string wifisettingsbuffer;
@@ -52,7 +42,9 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"password":")" << wifi_password << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
void set_wifi_password(const httplib::Request &request, httplib::Response &response) {
@@ -79,18 +71,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH);
std::string old_wifi_password;
std::string wifisettingsbuffer;
@@ -117,6 +98,8 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"success":"ok"})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+4 -13
View File
@@ -5,6 +5,7 @@
#include "../../types.hpp"
#include <QR-Code-generator/qrcodegen.hpp>
#include <QR-Code-generator/qrtosvg.hpp>
#include "../../auth/auth.hpp"
namespace settings {
void get_wifi_qr(const httplib::Request &request, httplib::Response &response) {
@@ -21,19 +22,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH);
std::string wifi_name;
std::string wifi_password;
@@ -57,6 +46,8 @@ namespace settings {
qrcodegen::QrCode qr0 = qrcodegen::QrCode::encodeText(toqrstring.str().c_str(), qrcodegen::QrCode::Ecc::MEDIUM);
std::string responsedata = toSvgString(qr0, 4);
response.set_content(responsedata, SVG_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+7 -24
View File
@@ -4,6 +4,7 @@
#include "../../routes.hpp"
#include "../../types.hpp"
#include <syslog.h>
#include "../../auth/auth.hpp"
#define WIFI_REQUIRED_STRING R"({"required":"[token,status]"})"
namespace settings {
@@ -21,18 +22,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ifstream wifistatusfile("/etc/openrouter/wifi/status");
int wifi_status;
wifistatusfile >> wifi_status;
@@ -40,7 +30,9 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"status":")" << wifi_status << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
void set_wifi_status(const httplib::Request &request, httplib::Response &response) {
@@ -62,18 +54,7 @@ namespace settings {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
std::ofstream wififileout("/etc/openrouter/wifi/status");
std::string new_wifi_status = json_body["status"];
if (new_wifi_status == "0") {
@@ -94,6 +75,8 @@ namespace settings {
std::stringstream responsedata;
responsedata << R"({"status":")" << wifi_status << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
return;
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+3 -12
View File
@@ -3,6 +3,7 @@
#include "../routes.hpp"
#include <colors.h>
#include "../types.hpp"
#include "../../auth/auth.hpp"
namespace systemfuncs {
void reboot(const httplib::Request &request, httplib::Response &response) {
@@ -19,19 +20,9 @@ namespace systemfuncs {
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) {
system("reboot");
}
response.set_content(R"({"access":"reject"})", JSON_TYPE);
}
}
+1 -4
View File
@@ -20,10 +20,7 @@ const reboot = () => {
}
const waitreboot = () => {
fetch(routes.telnet_status(), {
method: 'POST',
body: `{"token":"${localStorage.getItem("token")}"}`
}).then(res => res.json()).then(jsondata => {
fetch(routes.uptime()).then(res => res.json()).then(jsondata => {
stop_loader()
}).catch((error) => {
waitreboot()