Code Cleanup
This commit is contained in:
+50
-40
@@ -76,14 +76,14 @@ bool authenticate(resp_https_t response, req_https_t request) {
|
|||||||
int index = authData.find(':');
|
int index = authData.find(':');
|
||||||
std::string username = authData.substr(0, index);
|
std::string username = authData.substr(0, index);
|
||||||
std::string password = authData.substr(index + 1);
|
std::string password = authData.substr(index + 1);
|
||||||
std::string hash = crypto::hash_hexstr(password + config::sunshine.salt);
|
std::string hash = util::hex(crypto::hash(password + config::sunshine.salt)).to_string();
|
||||||
if(username == config::sunshine.username && hash == config::sunshine.password) return true;
|
if(username == config::sunshine.username && hash == config::sunshine.password) return true;
|
||||||
|
|
||||||
send_unauthorized(response, request);
|
send_unauthorized(response, request);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
void not_found(resp_https_t response, req_https_t request) {
|
||||||
void not_found(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request) {
|
|
||||||
pt::ptree tree;
|
pt::ptree tree;
|
||||||
tree.put("root.<xmlattr>.status_code", 404);
|
tree.put("root.<xmlattr>.status_code", 404);
|
||||||
|
|
||||||
@@ -98,46 +98,47 @@ void not_found(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> resp
|
|||||||
|
|
||||||
void getIndexPage(resp_https_t response, req_https_t request) {
|
void getIndexPage(resp_https_t response, req_https_t request) {
|
||||||
if(!authenticate(response, request)) return;
|
if(!authenticate(response, request)) return;
|
||||||
|
|
||||||
std::string header = read_file(WEB_DIR "header.html");
|
std::string header = read_file(WEB_DIR "header.html");
|
||||||
std::string content = read_file(WEB_DIR "index.html");
|
std::string content = read_file(WEB_DIR "index.html");
|
||||||
response->write(header + content);
|
response->write(header + content);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
void getPinPage(resp_https_t response, req_https_t request) {
|
||||||
void getPinPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request) {
|
|
||||||
if(!authenticate(response, request)) return;
|
if(!authenticate(response, request)) return;
|
||||||
|
|
||||||
std::string header = read_file(WEB_DIR "header.html");
|
std::string header = read_file(WEB_DIR "header.html");
|
||||||
std::string content = read_file(WEB_DIR "pin.html");
|
std::string content = read_file(WEB_DIR "pin.html");
|
||||||
response->write(header + content);
|
response->write(header + content);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
void getAppsPage(resp_https_t response, req_https_t request) {
|
||||||
void getAppsPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request) {
|
|
||||||
if(!authenticate(response, request)) return;
|
if(!authenticate(response, request)) return;
|
||||||
|
|
||||||
std::string header = read_file(WEB_DIR "header.html");
|
std::string header = read_file(WEB_DIR "header.html");
|
||||||
std::string content = read_file(WEB_DIR "apps.html");
|
std::string content = read_file(WEB_DIR "apps.html");
|
||||||
response->write(header + content);
|
response->write(header + content);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
void getClientsPage(resp_https_t response, req_https_t request) {
|
||||||
void getClientsPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request) {
|
|
||||||
if(!authenticate(response, request)) return;
|
if(!authenticate(response, request)) return;
|
||||||
|
|
||||||
std::string header = read_file(WEB_DIR "header.html");
|
std::string header = read_file(WEB_DIR "header.html");
|
||||||
std::string content = read_file(WEB_DIR "clients.html");
|
std::string content = read_file(WEB_DIR "clients.html");
|
||||||
response->write(header + content);
|
response->write(header + content);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
void getConfigPage(resp_https_t response, req_https_t request) {
|
||||||
void getConfigPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request) {
|
|
||||||
if(!authenticate(response, request)) return;
|
if(!authenticate(response, request)) return;
|
||||||
|
|
||||||
std::string header = read_file(WEB_DIR "header.html");
|
std::string header = read_file(WEB_DIR "header.html");
|
||||||
std::string content = read_file(WEB_DIR "config.html");
|
std::string content = read_file(WEB_DIR "config.html");
|
||||||
response->write(header + content);
|
response->write(header + content);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
void getPasswordPage(resp_https_t response, req_https_t request) {
|
||||||
void getPasswordPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request) {
|
|
||||||
if(!authenticate(response, request)) return;
|
if(!authenticate(response, request)) return;
|
||||||
|
|
||||||
std::string header = read_file(WEB_DIR "header.html");
|
std::string header = read_file(WEB_DIR "header.html");
|
||||||
std::string content = read_file(WEB_DIR "password.html");
|
std::string content = read_file(WEB_DIR "password.html");
|
||||||
response->write(header + content);
|
response->write(header + content);
|
||||||
@@ -145,12 +146,14 @@ void getPasswordPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response
|
|||||||
|
|
||||||
void getApps(resp_https_t response, req_https_t request) {
|
void getApps(resp_https_t response, req_https_t request) {
|
||||||
if(!authenticate(response, request)) return;
|
if(!authenticate(response, request)) return;
|
||||||
|
|
||||||
std::string content = read_file(SUNSHINE_ASSETS_DIR "/" APPS_JSON);
|
std::string content = read_file(SUNSHINE_ASSETS_DIR "/" APPS_JSON);
|
||||||
response->write(content);
|
response->write(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
void saveApp(resp_https_t response, req_https_t request) {
|
void saveApp(resp_https_t response, req_https_t request) {
|
||||||
if(!authenticate(response, request)) return;
|
if(!authenticate(response, request)) return;
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << request->content.rdbuf();
|
ss << request->content.rdbuf();
|
||||||
pt::ptree outputTree;
|
pt::ptree outputTree;
|
||||||
@@ -170,12 +173,13 @@ void saveApp(resp_https_t response, req_https_t request) {
|
|||||||
BOOST_LOG(info) << inputTree.get_child("prep-cmd").empty();
|
BOOST_LOG(info) << inputTree.get_child("prep-cmd").empty();
|
||||||
if(inputTree.get_child("prep-cmd").empty())
|
if(inputTree.get_child("prep-cmd").empty())
|
||||||
inputTree.erase("prep-cmd");
|
inputTree.erase("prep-cmd");
|
||||||
|
|
||||||
inputTree.erase("index");
|
inputTree.erase("index");
|
||||||
if(index == -1) {
|
if(index == -1) {
|
||||||
apps_node.push_back(std::make_pair("", inputTree));
|
apps_node.push_back(std::make_pair("", inputTree));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
//Unfortuantely Boost PT does not allow to directly edit the array, copt should do the trick
|
//Unfortuantely Boost PT does not allow to directly edit the array, copy should do the trick
|
||||||
pt::ptree newApps;
|
pt::ptree newApps;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for(const auto &kv : apps_node) {
|
for(const auto &kv : apps_node) {
|
||||||
@@ -204,6 +208,7 @@ void saveApp(resp_https_t response, req_https_t request) {
|
|||||||
|
|
||||||
void deleteApp(resp_https_t response, req_https_t request) {
|
void deleteApp(resp_https_t response, req_https_t request) {
|
||||||
if(!authenticate(response, request)) return;
|
if(!authenticate(response, request)) return;
|
||||||
|
|
||||||
pt::ptree outputTree;
|
pt::ptree outputTree;
|
||||||
auto g = util::fail_guard([&]() {
|
auto g = util::fail_guard([&]() {
|
||||||
std::ostringstream data;
|
std::ostringstream data;
|
||||||
@@ -249,6 +254,7 @@ void deleteApp(resp_https_t response, req_https_t request) {
|
|||||||
|
|
||||||
void getConfig(resp_https_t response, req_https_t request) {
|
void getConfig(resp_https_t response, req_https_t request) {
|
||||||
if(!authenticate(response, request)) return;
|
if(!authenticate(response, request)) return;
|
||||||
|
|
||||||
pt::ptree outputTree;
|
pt::ptree outputTree;
|
||||||
auto g = util::fail_guard([&]() {
|
auto g = util::fail_guard([&]() {
|
||||||
std::ostringstream data;
|
std::ostringstream data;
|
||||||
@@ -285,6 +291,7 @@ void getConfig(resp_https_t response, req_https_t request) {
|
|||||||
|
|
||||||
void saveConfig(resp_https_t response, req_https_t request) {
|
void saveConfig(resp_https_t response, req_https_t request) {
|
||||||
if(!authenticate(response, request)) return;
|
if(!authenticate(response, request)) return;
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
std::stringstream configStream;
|
std::stringstream configStream;
|
||||||
ss << request->content.rdbuf();
|
ss << request->content.rdbuf();
|
||||||
@@ -302,6 +309,7 @@ void saveConfig(resp_https_t response, req_https_t request) {
|
|||||||
for(const auto &kv : inputTree) {
|
for(const auto &kv : inputTree) {
|
||||||
std::string value = inputTree.get<std::string>(kv.first);
|
std::string value = inputTree.get<std::string>(kv.first);
|
||||||
if(value.length() == 0 || value.compare("null") == 0) continue;
|
if(value.length() == 0 || value.compare("null") == 0) continue;
|
||||||
|
|
||||||
configStream << kv.first << " = " << value << std::endl;
|
configStream << kv.first << " = " << value << std::endl;
|
||||||
}
|
}
|
||||||
http::write_file(SUNSHINE_ASSETS_DIR "/sunshine.conf", configStream.str());
|
http::write_file(SUNSHINE_ASSETS_DIR "/sunshine.conf", configStream.str());
|
||||||
@@ -316,6 +324,7 @@ void saveConfig(resp_https_t response, req_https_t request) {
|
|||||||
|
|
||||||
void savePassword(resp_https_t response, req_https_t request) {
|
void savePassword(resp_https_t response, req_https_t request) {
|
||||||
if(!authenticate(response, request)) return;
|
if(!authenticate(response, request)) return;
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
std::stringstream configStream;
|
std::stringstream configStream;
|
||||||
ss << request->content.rdbuf();
|
ss << request->content.rdbuf();
|
||||||
@@ -337,14 +346,15 @@ void savePassword(resp_https_t response, req_https_t request) {
|
|||||||
std::string newPassword = inputTree.get<std::string>("newPassword");
|
std::string newPassword = inputTree.get<std::string>("newPassword");
|
||||||
std::string confirmPassword = inputTree.get<std::string>("confirmNewPassword");
|
std::string confirmPassword = inputTree.get<std::string>("confirmNewPassword");
|
||||||
if(newUsername.length() == 0) newUsername = username;
|
if(newUsername.length() == 0) newUsername = username;
|
||||||
std::string hash = crypto::hash_hexstr(password + config::sunshine.salt);
|
|
||||||
|
std::string hash = util::hex(crypto::hash(password + config::sunshine.salt)).to_string();
|
||||||
if(username == config::sunshine.username && hash == config::sunshine.password){
|
if(username == config::sunshine.username && hash == config::sunshine.password){
|
||||||
if(newPassword != confirmPassword){
|
if(newPassword != confirmPassword){
|
||||||
outputTree.put("status",false);
|
outputTree.put("status",false);
|
||||||
outputTree.put("error","Password Mismatch");
|
outputTree.put("error","Password Mismatch");
|
||||||
}
|
}
|
||||||
fileTree.put("username",newUsername);
|
fileTree.put("username",newUsername);
|
||||||
fileTree.put("password",crypto::hash_hexstr(newPassword + config::sunshine.salt));
|
fileTree.put("password",util::hex(crypto::hash(newPassword + config::sunshine.salt)).to_string());
|
||||||
fileTree.put("salt",config::sunshine.salt);
|
fileTree.put("salt",config::sunshine.salt);
|
||||||
pt::write_json(config::sunshine.credentials_file,fileTree);
|
pt::write_json(config::sunshine.credentials_file,fileTree);
|
||||||
http::reload_user_creds(config::sunshine.credentials_file);
|
http::reload_user_creds(config::sunshine.credentials_file);
|
||||||
@@ -366,27 +376,27 @@ void start(std::shared_ptr<safe::signal_t> shutdown_event) {
|
|||||||
auto ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tls);
|
auto ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tls);
|
||||||
ctx->use_certificate_chain_file(config::nvhttp.cert);
|
ctx->use_certificate_chain_file(config::nvhttp.cert);
|
||||||
ctx->use_private_key_file(config::nvhttp.pkey, boost::asio::ssl::context::pem);
|
ctx->use_private_key_file(config::nvhttp.pkey, boost::asio::ssl::context::pem);
|
||||||
https_server_t http_server { ctx, 0 };
|
https_server_t server { ctx, 0 };
|
||||||
http_server.default_resource = not_found<SimpleWeb::HTTPS>;
|
server.default_resource = not_found;
|
||||||
http_server.resource["^/$"]["GET"] = getIndexPage;
|
server.resource["^/$"]["GET"] = getIndexPage;
|
||||||
http_server.resource["^/pin$"]["GET"] = getPinPage<SimpleWeb::HTTPS>;
|
server.resource["^/pin$"]["GET"] = getPinPage;
|
||||||
http_server.resource["^/apps$"]["GET"] = getAppsPage<SimpleWeb::HTTPS>;
|
server.resource["^/apps$"]["GET"] = getAppsPage;
|
||||||
http_server.resource["^/clients$"]["GET"] = getClientsPage<SimpleWeb::HTTPS>;
|
server.resource["^/clients$"]["GET"] = getClientsPage;
|
||||||
http_server.resource["^/config$"]["GET"] = getConfigPage<SimpleWeb::HTTPS>;
|
server.resource["^/config$"]["GET"] = getConfigPage;
|
||||||
http_server.resource["^/password$"]["GET"] = getPasswordPage<SimpleWeb::HTTPS>;
|
server.resource["^/password$"]["GET"] = getPasswordPage;
|
||||||
http_server.resource["^/pin/([0-9]+)$"]["GET"] = nvhttp::pin<SimpleWeb::HTTPS>;
|
server.resource["^/pin/([0-9]+)$"]["GET"] = nvhttp::pin<SimpleWeb::HTTPS>;
|
||||||
http_server.resource["^/api/apps$"]["GET"] = getApps;
|
server.resource["^/api/apps$"]["GET"] = getApps;
|
||||||
http_server.resource["^/api/apps$"]["POST"] = saveApp;
|
server.resource["^/api/apps$"]["POST"] = saveApp;
|
||||||
http_server.resource["^/api/config$"]["GET"] = getConfig;
|
server.resource["^/api/config$"]["GET"] = getConfig;
|
||||||
http_server.resource["^/api/config$"]["POST"] = saveConfig;
|
server.resource["^/api/config$"]["POST"] = saveConfig;
|
||||||
http_server.resource["^/api/password$"]["POST"] = savePassword;
|
server.resource["^/api/password$"]["POST"] = savePassword;
|
||||||
http_server.resource["^/api/apps/([0-9]+)$"]["DELETE"] = deleteApp;
|
server.resource["^/api/apps/([0-9]+)$"]["DELETE"] = deleteApp;
|
||||||
http_server.config.reuse_address = true;
|
server.config.reuse_address = true;
|
||||||
http_server.config.address = "0.0.0.0"s;
|
server.config.address = "0.0.0.0"s;
|
||||||
http_server.config.port = PORT_HTTP;
|
server.config.port = PORT_HTTP;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
http_server.bind();
|
server.bind();
|
||||||
BOOST_LOG(info) << "Configuration UI available at [https://localhost:"sv << PORT_HTTP << "]";
|
BOOST_LOG(info) << "Configuration UI available at [https://localhost:"sv << PORT_HTTP << "]";
|
||||||
}
|
}
|
||||||
catch(boost::system::system_error &err) {
|
catch(boost::system::system_error &err) {
|
||||||
@@ -395,12 +405,12 @@ void start(std::shared_ptr<safe::signal_t> shutdown_event) {
|
|||||||
shutdown_event->raise(true);
|
shutdown_event->raise(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto accept_and_run = [&](auto *http_server) {
|
auto accept_and_run = [&](auto *server) {
|
||||||
try {
|
try {
|
||||||
http_server->accept_and_run();
|
server->accept_and_run();
|
||||||
}
|
}
|
||||||
catch(boost::system::system_error &err) {
|
catch(boost::system::system_error &err) {
|
||||||
// It's possible the exception gets thrown after calling http_server->stop() from a different thread
|
// It's possible the exception gets thrown after calling server->stop() from a different thread
|
||||||
if(shutdown_event->peek()) {
|
if(shutdown_event->peek()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -410,12 +420,12 @@ void start(std::shared_ptr<safe::signal_t> shutdown_event) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
std::thread tcp { accept_and_run, &http_server };
|
std::thread tcp { accept_and_run, &server };
|
||||||
|
|
||||||
// Wait for any event
|
// Wait for any event
|
||||||
shutdown_event->view();
|
shutdown_event->view();
|
||||||
|
|
||||||
http_server.stop();
|
server.stop();
|
||||||
|
|
||||||
tcp.join();
|
tcp.join();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ int generate_user_creds(const std::string &file) {
|
|||||||
std::string salt = crypto::rand_string(16);
|
std::string salt = crypto::rand_string(16);
|
||||||
outputTree.put("username", "sunshine");
|
outputTree.put("username", "sunshine");
|
||||||
outputTree.put("salt", salt);
|
outputTree.put("salt", salt);
|
||||||
outputTree.put("password", crypto::hash_hexstr(plainPassword + salt));
|
outputTree.put("password", util::hex(crypto::hash(plainPassword + salt)).to_string());
|
||||||
BOOST_LOG(info) << "New credentials has been created";
|
BOOST_LOG(info) << "New credentials has been created";
|
||||||
BOOST_LOG(info) << "Username: " << username;
|
BOOST_LOG(info) << "Username: " << username;
|
||||||
BOOST_LOG(info) << "Password: " << plainPassword;
|
BOOST_LOG(info) << "Password: " << plainPassword;
|
||||||
|
|||||||
Reference in New Issue
Block a user