Fix Format

This commit is contained in:
Elia Zammuto
2021-05-29 22:29:10 +02:00
parent 4835366a0c
commit ffb80c5fc3
11 changed files with 298 additions and 360 deletions
+2 -2
View File
@@ -4,8 +4,8 @@
#include <bitset> #include <bitset>
#include <chrono> #include <chrono>
#include <optional> #include <optional>
#include <unordered_map>
#include <string> #include <string>
#include <unordered_map>
#include <vector> #include <vector>
namespace config { namespace config {
@@ -105,5 +105,5 @@ extern sunshine_t sunshine;
int parse(int argc, char *argv[]); int parse(int argc, char *argv[]);
std::unordered_map<std::string, std::string> parse_config(std::string_view file_content); std::unordered_map<std::string, std::string> parse_config(std::string_view file_content);
} } // namespace config
#endif #endif
+50 -92
View File
@@ -6,32 +6,31 @@
#include <filesystem> #include <filesystem>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp> #include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/asio/ssl/context.hpp> #include <boost/asio/ssl/context.hpp>
#include <Simple-Web-Server/server_http.hpp>
#include <Simple-Web-Server/crypto.hpp> #include <Simple-Web-Server/crypto.hpp>
#include <Simple-Web-Server/server_http.hpp>
#include <boost/asio/ssl/context_base.hpp> #include <boost/asio/ssl/context_base.hpp>
#include "config.h" #include "config.h"
#include "utility.h"
#include "rtsp.h"
#include "crypto.h"
#include "confighttp.h" #include "confighttp.h"
#include "platform/common.h" #include "crypto.h"
#include "httpcommon.h" #include "httpcommon.h"
#include "main.h"
#include "network.h" #include "network.h"
#include "nvhttp.h" #include "nvhttp.h"
#include "platform/common.h"
#include "rtsp.h"
#include "utility.h"
#include "uuid.h" #include "uuid.h"
#include "main.h"
std::string read_file(std::string path); std::string read_file(std::string path);
namespace confighttp namespace confighttp {
{
using namespace std::literals; using namespace std::literals;
constexpr auto PORT_HTTP = 47990; constexpr auto PORT_HTTP = 47990;
@@ -44,14 +43,12 @@ using args_t = SimpleWeb::CaseInsensitiveMultimap;
using resp_https_t = std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTPS>::Response>; using resp_https_t = std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTPS>::Response>;
using req_https_t = std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTPS>::Request>; using req_https_t = std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTPS>::Request>;
enum class op_e enum class op_e {
{
ADD, ADD,
REMOVE REMOVE
}; };
void send_unauthorized(resp_https_t response, req_https_t request) void send_unauthorized(resp_https_t response, req_https_t request) {
{
auto address = request->remote_endpoint_address(); auto address = request->remote_endpoint_address();
BOOST_LOG(info) << '[' << address << "] -- denied"sv; BOOST_LOG(info) << '[' << address << "] -- denied"sv;
const SimpleWeb::CaseInsensitiveMultimap headers { const SimpleWeb::CaseInsensitiveMultimap headers {
@@ -60,8 +57,7 @@ void send_unauthorized(resp_https_t response, req_https_t request)
response->write(SimpleWeb::StatusCode::client_error_unauthorized, headers); response->write(SimpleWeb::StatusCode::client_error_unauthorized, headers);
} }
bool authenticate(resp_https_t response, req_https_t request) bool authenticate(resp_https_t response, req_https_t request) {
{
auto address = request->remote_endpoint_address(); auto address = request->remote_endpoint_address();
auto ip_type = net::from_address(address); auto ip_type = net::from_address(address);
if(ip_type > http::origin_pin_allowed) { if(ip_type > http::origin_pin_allowed) {
@@ -87,8 +83,7 @@ bool authenticate(resp_https_t response, req_https_t request)
} }
template<class T> template<class T>
void not_found(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> 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);
@@ -101,8 +96,7 @@ void not_found(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> resp
<< data.str(); << data.str();
} }
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");
@@ -110,8 +104,7 @@ void getIndexPage(resp_https_t response, req_https_t request)
} }
template<class T> template<class T>
void getPinPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> 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");
@@ -119,8 +112,7 @@ void getPinPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> res
} }
template<class T> template<class T>
void getAppsPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> 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");
@@ -128,8 +120,7 @@ void getAppsPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> re
} }
template<class T> template<class T>
void getClientsPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> 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");
@@ -137,23 +128,20 @@ void getClientsPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response>
} }
template<class T> template<class T>
void getConfigPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> 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);
} }
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();
@@ -165,8 +153,7 @@ void saveApp(resp_https_t response, req_https_t request)
response->write(data.str()); response->write(data.str());
}); });
pt::ptree inputTree, fileTree; pt::ptree inputTree, fileTree;
try try {
{
//TODO: Input Validation //TODO: Input Validation
pt::read_json(ss, inputTree); pt::read_json(ss, inputTree);
pt::read_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree); pt::read_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree);
@@ -176,23 +163,18 @@ void saveApp(resp_https_t response, req_https_t request)
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, copt 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) {
{ if(i == index) {
if (i == index)
{
newApps.push_back(std::make_pair("", inputTree)); newApps.push_back(std::make_pair("", inputTree));
} }
else else {
{
newApps.push_back(std::make_pair("", kv.second)); newApps.push_back(std::make_pair("", kv.second));
} }
i++; i++;
@@ -204,8 +186,7 @@ void saveApp(resp_https_t response, req_https_t request)
outputTree.put("status", "true"); outputTree.put("status", "true");
proc::refresh(SUNSHINE_ASSETS_DIR "/" APPS_JSON); proc::refresh(SUNSHINE_ASSETS_DIR "/" APPS_JSON);
} }
catch (std::exception &e) catch(std::exception &e) {
{
BOOST_LOG(warning) << e.what(); BOOST_LOG(warning) << e.what();
outputTree.put("status", "false"); outputTree.put("status", "false");
outputTree.put("error", "Invalid Input JSON"); outputTree.put("error", "Invalid Input JSON");
@@ -213,8 +194,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([&]() {
@@ -224,27 +204,22 @@ void deleteApp(resp_https_t response, req_https_t request)
response->write(data.str()); response->write(data.str());
}); });
pt::ptree fileTree; pt::ptree fileTree;
try try {
{
pt::read_json(config::stream.file_apps, fileTree); pt::read_json(config::stream.file_apps, fileTree);
auto &apps_node = fileTree.get_child("apps"s); auto &apps_node = fileTree.get_child("apps"s);
int index = stoi(request->path_match[1]); int index = stoi(request->path_match[1]);
BOOST_LOG(info) << index; BOOST_LOG(info) << index;
if (index <= 0) if(index <= 0) {
{
outputTree.put("status", "false"); outputTree.put("status", "false");
outputTree.put("error", "Invalid Index"); outputTree.put("error", "Invalid Index");
return; return;
} }
else else {
{
//Unfortuantely Boost PT does not allow to directly edit the array, copy 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) {
{ if(i != index) {
if (i != index)
{
newApps.push_back(std::make_pair("", kv.second)); newApps.push_back(std::make_pair("", kv.second));
} }
i++; i++;
@@ -256,8 +231,7 @@ void deleteApp(resp_https_t response, req_https_t request)
outputTree.put("status", "true"); outputTree.put("status", "true");
proc::refresh(SUNSHINE_ASSETS_DIR "/" APPS_JSON); proc::refresh(SUNSHINE_ASSETS_DIR "/" APPS_JSON);
} }
catch (std::exception &e) catch(std::exception &e) {
{
BOOST_LOG(warning) << e.what(); BOOST_LOG(warning) << e.what();
outputTree.put("status", "false"); outputTree.put("status", "false");
outputTree.put("error", "Invalid File JSON"); outputTree.put("error", "Invalid File JSON");
@@ -265,8 +239,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([&]() {
@@ -275,8 +248,7 @@ void getConfig(resp_https_t response, req_https_t request)
pt::write_json(data, outputTree); pt::write_json(data, outputTree);
response->write(data.str()); response->write(data.str());
}); });
try try {
{
outputTree.put("status", "true"); outputTree.put("status", "true");
outputTree.put("platform", SUNSHINE_PLATFORM); outputTree.put("platform", SUNSHINE_PLATFORM);
const char *config_file = SUNSHINE_ASSETS_DIR "/sunshine.conf"; const char *config_file = SUNSHINE_ASSETS_DIR "/sunshine.conf";
@@ -289,15 +261,13 @@ void getConfig(resp_https_t response, req_https_t request)
auto vars = config::parse_config(std::string { auto vars = config::parse_config(std::string {
// Quick and dirty // Quick and dirty
std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>(in),
std::istreambuf_iterator<char>() std::istreambuf_iterator<char>() });
});
for(auto &[name, value] : vars) { for(auto &[name, value] : vars) {
outputTree.put(std::move(name), std::move(value)); outputTree.put(std::move(name), std::move(value));
} }
} }
catch (std::exception &e) catch(std::exception &e) {
{
BOOST_LOG(warning) << e.what(); BOOST_LOG(warning) << e.what();
outputTree.put("status", "false"); outputTree.put("status", "false");
outputTree.put("error", "Invalid File JSON"); outputTree.put("error", "Invalid File JSON");
@@ -305,8 +275,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;
@@ -319,20 +288,17 @@ void saveConfig(resp_https_t response, req_https_t request)
response->write(data.str()); response->write(data.str());
}); });
pt::ptree inputTree; pt::ptree inputTree;
try try {
{
//TODO: Input Validation //TODO: Input Validation
pt::read_json(ss, inputTree); pt::read_json(ss, inputTree);
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());
} }
catch (std::exception &e) catch(std::exception &e) {
{
BOOST_LOG(warning) << e.what(); BOOST_LOG(warning) << e.what();
outputTree.put("status", "false"); outputTree.put("status", "false");
outputTree.put("error", e.what()); outputTree.put("error", e.what());
@@ -340,8 +306,7 @@ void saveConfig(resp_https_t response, req_https_t request)
} }
} }
void start(std::shared_ptr<safe::signal_t> shutdown_event) 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);
@@ -362,28 +327,23 @@ void start(std::shared_ptr<safe::signal_t> shutdown_event)
http_server.config.address = "0.0.0.0"s; http_server.config.address = "0.0.0.0"s;
http_server.config.port = PORT_HTTP; http_server.config.port = PORT_HTTP;
try try {
{
http_server.bind(); http_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) {
{
BOOST_LOG(fatal) << "Couldn't bind http server to ports ["sv << PORT_HTTP << "]: "sv << err.what(); BOOST_LOG(fatal) << "Couldn't bind http server to ports ["sv << PORT_HTTP << "]: "sv << err.what();
shutdown_event->raise(true); shutdown_event->raise(true);
return; return;
} }
auto accept_and_run = [&](auto *http_server) { auto accept_and_run = [&](auto *http_server) {
try try {
{
http_server->accept_and_run(); http_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 http_server->stop() from a different thread
if (shutdown_event->peek()) if(shutdown_event->peek()) {
{
return; return;
} }
@@ -401,18 +361,16 @@ void start(std::shared_ptr<safe::signal_t> shutdown_event)
tcp.join(); tcp.join();
} }
} } // namespace confighttp
std::string read_file(std::string path) std::string read_file(std::string path) {
{
std::ifstream in(path); std::ifstream in(path);
std::string input; std::string input;
std::string base64_cert; std::string base64_cert;
//FIXME: Being unable to read file could result in infinite loop //FIXME: Being unable to read file could result in infinite loop
while (!in.eof()) while(!in.eof()) {
{
std::getline(in, input); std::getline(in, input);
base64_cert += input + '\n'; base64_cert += input + '\n';
} }
+3 -5
View File
@@ -340,8 +340,7 @@ void md_ctx_destroy(EVP_MD_CTX *ctx) {
EVP_MD_CTX_destroy(ctx); EVP_MD_CTX_destroy(ctx);
} }
std::string rand_string(std::size_t bytes) std::string rand_string(std::size_t bytes) {
{
std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!%&()=-"; std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!%&()=-";
std::string value = rand(bytes); std::string value = rand(bytes);
for(std::size_t i = 0; i != value.size(); ++i) { for(std::size_t i = 0; i != value.size(); ++i) {
@@ -350,8 +349,7 @@ std::string rand_string(std::size_t bytes)
return value; return value;
} }
std::string hash_hexstr(const std::string_view &plaintext) std::string hash_hexstr(const std::string_view &plaintext) {
{
sha256_t hashBytes = crypto::hash(plaintext); sha256_t hashBytes = crypto::hash(plaintext);
std::ostringstream hashStream; std::ostringstream hashStream;
hashStream << std::hex << std::setfill('0'); hashStream << std::hex << std::setfill('0');
@@ -359,4 +357,4 @@ std::string hash_hexstr(const std::string_view &plaintext)
std::string hashString = hashStream.str(); std::string hashString = hashStream.str();
return hashString; return hashString;
} }
} } // namespace crypto
+1 -2
View File
@@ -7,12 +7,11 @@
#include <array> #include <array>
#include <cassert> #include <cassert>
#include <iomanip>
#include <openssl/evp.h> #include <openssl/evp.h>
#include <openssl/rand.h> #include <openssl/rand.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include <openssl/x509.h> #include <openssl/x509.h>
#include <openssl/rand.h>
#include <iomanip>
#include "utility.h" #include "utility.h"
+29 -45
View File
@@ -2,9 +2,9 @@
#include <filesystem> #include <filesystem>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp> #include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/asio/ssl/context.hpp> #include <boost/asio/ssl/context.hpp>
@@ -13,18 +13,17 @@
#include <boost/asio/ssl/context_base.hpp> #include <boost/asio/ssl/context_base.hpp>
#include "config.h" #include "config.h"
#include "utility.h"
#include "rtsp.h"
#include "crypto.h" #include "crypto.h"
#include "httpcommon.h"
#include "main.h"
#include "network.h"
#include "nvhttp.h" #include "nvhttp.h"
#include "platform/common.h" #include "platform/common.h"
#include "network.h" #include "rtsp.h"
#include "utility.h"
#include "uuid.h" #include "uuid.h"
#include "main.h"
#include "httpcommon.h"
namespace http namespace http {
{
using namespace std::literals; using namespace std::literals;
namespace fs = std::filesystem; namespace fs = std::filesystem;
namespace pt = boost::property_tree; namespace pt = boost::property_tree;
@@ -37,22 +36,18 @@ namespace http
std::string unique_id; std::string unique_id;
net::net_e origin_pin_allowed; net::net_e origin_pin_allowed;
void init(std::shared_ptr<safe::signal_t> shutdown_event) void init(std::shared_ptr<safe::signal_t> shutdown_event) {
{
bool clean_slate = config::sunshine.flags[config::flag::FRESH_STATE]; bool clean_slate = config::sunshine.flags[config::flag::FRESH_STATE];
origin_pin_allowed = net::from_enum_string(config::nvhttp.origin_pin_allowed); origin_pin_allowed = net::from_enum_string(config::nvhttp.origin_pin_allowed);
if (clean_slate) if(clean_slate) {
{
unique_id = util::uuid_t::generate().string(); unique_id = util::uuid_t::generate().string();
auto dir = std::filesystem::temp_directory_path() / "Sushine"sv; auto dir = std::filesystem::temp_directory_path() / "Sushine"sv;
config::nvhttp.cert = (dir / ("cert-"s + unique_id)).string(); config::nvhttp.cert = (dir / ("cert-"s + unique_id)).string();
config::nvhttp.pkey = (dir / ("pkey-"s + unique_id)).string(); config::nvhttp.pkey = (dir / ("pkey-"s + unique_id)).string();
} }
if (!fs::exists(config::nvhttp.pkey) || !fs::exists(config::nvhttp.cert)) if(!fs::exists(config::nvhttp.pkey) || !fs::exists(config::nvhttp.cert)) {
{ if(create_creds(config::nvhttp.pkey, config::nvhttp.cert)) {
if (create_creds(config::nvhttp.pkey, config::nvhttp.cert))
{
shutdown_event->raise(true); shutdown_event->raise(true);
return; return;
} }
@@ -69,8 +64,7 @@ namespace http
} }
} }
int generate_user_creds(const std::string &file) int generate_user_creds(const std::string &file) {
{
pt::ptree outputTree; pt::ptree outputTree;
try { try {
std::string username = "sunshine"; std::string username = "sunshine";
@@ -83,30 +77,30 @@ namespace http
BOOST_LOG(info) << "Username: " << username; BOOST_LOG(info) << "Username: " << username;
BOOST_LOG(info) << "Password: " << plainPassword; BOOST_LOG(info) << "Password: " << plainPassword;
pt::write_json(file, outputTree); pt::write_json(file, outputTree);
} catch (std::exception &e){ }
catch(std::exception &e) {
BOOST_LOG(fatal) << e.what(); BOOST_LOG(fatal) << e.what();
return 1; return 1;
} }
return 0; return 0;
} }
int reload_user_creds(const std::string &file) int reload_user_creds(const std::string &file) {
{
pt::ptree inputTree; pt::ptree inputTree;
try { try {
pt::read_json(file, inputTree); pt::read_json(file, inputTree);
config::sunshine.username = inputTree.get<std::string>("username"); config::sunshine.username = inputTree.get<std::string>("username");
config::sunshine.password = inputTree.get<std::string>("password"); config::sunshine.password = inputTree.get<std::string>("password");
config::sunshine.salt = inputTree.get<std::string>("salt"); config::sunshine.salt = inputTree.get<std::string>("salt");
} catch(std::exception &e){ }
catch(std::exception &e) {
BOOST_LOG(fatal) << e.what(); BOOST_LOG(fatal) << e.what();
return 1; return 1;
} }
return 0; return 0;
} }
int create_creds(const std::string &pkey, const std::string &cert) int create_creds(const std::string &pkey, const std::string &cert) {
{
fs::path pkey_path = pkey; fs::path pkey_path = pkey;
fs::path cert_path = cert; fs::path cert_path = cert;
@@ -119,27 +113,23 @@ namespace http
std::error_code err_code {}; std::error_code err_code {};
fs::create_directories(pkey_dir, err_code); fs::create_directories(pkey_dir, err_code);
if (err_code) if(err_code) {
{
BOOST_LOG(fatal) << "Couldn't create directory ["sv << pkey_dir << "] :"sv << err_code.message(); BOOST_LOG(fatal) << "Couldn't create directory ["sv << pkey_dir << "] :"sv << err_code.message();
return -1; return -1;
} }
fs::create_directories(cert_dir, err_code); fs::create_directories(cert_dir, err_code);
if (err_code) if(err_code) {
{
BOOST_LOG(fatal) << "Couldn't create directory ["sv << cert_dir << "] :"sv << err_code.message(); BOOST_LOG(fatal) << "Couldn't create directory ["sv << cert_dir << "] :"sv << err_code.message();
return -1; return -1;
} }
if (write_file(pkey.c_str(), creds.pkey)) if(write_file(pkey.c_str(), creds.pkey)) {
{
BOOST_LOG(fatal) << "Couldn't open ["sv << config::nvhttp.pkey << ']'; BOOST_LOG(fatal) << "Couldn't open ["sv << config::nvhttp.pkey << ']';
return -1; return -1;
} }
if (write_file(cert.c_str(), creds.x509)) if(write_file(cert.c_str(), creds.x509)) {
{
BOOST_LOG(fatal) << "Couldn't open ["sv << config::nvhttp.cert << ']'; BOOST_LOG(fatal) << "Couldn't open ["sv << config::nvhttp.cert << ']';
return -1; return -1;
} }
@@ -148,8 +138,7 @@ namespace http
fs::perms::owner_read | fs::perms::owner_write, fs::perms::owner_read | fs::perms::owner_write,
fs::perm_options::replace, err_code); fs::perm_options::replace, err_code);
if (err_code) if(err_code) {
{
BOOST_LOG(fatal) << "Couldn't change permissions of ["sv << config::nvhttp.pkey << "] :"sv << err_code.message(); BOOST_LOG(fatal) << "Couldn't change permissions of ["sv << config::nvhttp.pkey << "] :"sv << err_code.message();
return -1; return -1;
} }
@@ -158,20 +147,17 @@ namespace http
fs::perms::owner_read | fs::perms::group_read | fs::perms::others_read | fs::perms::owner_write, fs::perms::owner_read | fs::perms::group_read | fs::perms::others_read | fs::perms::owner_write,
fs::perm_options::replace, err_code); fs::perm_options::replace, err_code);
if (err_code) if(err_code) {
{
BOOST_LOG(fatal) << "Couldn't change permissions of ["sv << config::nvhttp.cert << "] :"sv << err_code.message(); BOOST_LOG(fatal) << "Couldn't change permissions of ["sv << config::nvhttp.cert << "] :"sv << err_code.message();
return -1; return -1;
} }
return 0; return 0;
} }
int write_file(const char *path, const std::string_view &contents) int write_file(const char *path, const std::string_view &contents) {
{
std::ofstream out(path); std::ofstream out(path);
if (!out.is_open()) if(!out.is_open()) {
{
return -1; return -1;
} }
@@ -180,20 +166,18 @@ namespace http
return 0; return 0;
} }
std::string read_file(const char *path) std::string read_file(const char *path) {
{
std::ifstream in(path); std::ifstream in(path);
std::string input; std::string input;
std::string base64_cert; std::string base64_cert;
//FIXME: Being unable to read file could result in infinite loop //FIXME: Being unable to read file could result in infinite loop
while (!in.eof()) while(!in.eof()) {
{
std::getline(in, input); std::getline(in, input);
base64_cert += input + '\n'; base64_cert += input + '\n';
} }
return base64_cert; return base64_cert;
} }
} } // namespace http
+1 -1
View File
@@ -6,4 +6,4 @@ namespace http{
int write_file(const char *path, const std::string_view &contents); int write_file(const char *path, const std::string_view &contents);
extern std::string unique_id; extern std::string unique_id;
extern net::net_e origin_pin_allowed; extern net::net_e origin_pin_allowed;
} } // namespace http
+2 -2
View File
@@ -15,9 +15,9 @@
#include <boost/log/sources/severity_logger.hpp> #include <boost/log/sources/severity_logger.hpp>
#include "config.h" #include "config.h"
#include "nvhttp.h"
#include "httpcommon.h"
#include "confighttp.h" #include "confighttp.h"
#include "httpcommon.h"
#include "nvhttp.h"
#include "rtsp.h" #include "rtsp.h"
#include "thread_pool.h" #include "thread_pool.h"
#include "video.h" #include "video.h"
+2 -4
View File
@@ -20,6 +20,7 @@
#include "config.h" #include "config.h"
#include "crypto.h" #include "crypto.h"
#include "httpcommon.h"
#include "main.h" #include "main.h"
#include "network.h" #include "network.h"
#include "nvhttp.h" #include "nvhttp.h"
@@ -27,9 +28,6 @@
#include "rtsp.h" #include "rtsp.h"
#include "utility.h" #include "utility.h"
#include "uuid.h" #include "uuid.h"
#include "main.h"
#include "httpcommon.h"
namespace nvhttp { namespace nvhttp {
@@ -858,4 +856,4 @@ void start(std::shared_ptr<safe::signal_t> shutdown_event) {
ssl.join(); ssl.join();
tcp.join(); tcp.join();
} }
} } // namespace nvhttp
+6 -5
View File
@@ -5,11 +5,11 @@
#ifndef SUNSHINE_NVHTTP_H #ifndef SUNSHINE_NVHTTP_H
#define SUNSHINE_NVHTTP_H #define SUNSHINE_NVHTTP_H
#include <functional> #include "thread_safe.h"
#include <string>
#include <Simple-Web-Server/server_http.hpp> #include <Simple-Web-Server/server_http.hpp>
#include <Simple-Web-Server/server_https.hpp> #include <Simple-Web-Server/server_https.hpp>
#include "thread_safe.h" #include <functional>
#include <string>
#define CA_DIR SUNSHINE_ASSETS_DIR "/demoCA" #define CA_DIR SUNSHINE_ASSETS_DIR "/demoCA"
#define PRIVATE_KEY_FILE CA_DIR "/cakey.pem" #define PRIVATE_KEY_FILE CA_DIR "/cakey.pem"
@@ -17,7 +17,8 @@
namespace nvhttp { namespace nvhttp {
void start(std::shared_ptr<safe::signal_t> shutdown_event); void start(std::shared_ptr<safe::signal_t> shutdown_event);
template<class T> void pin(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request); template<class T>
} void pin(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request);
} // namespace nvhttp
#endif //SUNSHINE_NVHTTP_H #endif //SUNSHINE_NVHTTP_H