fix formatting
This commit is contained in:
parent
e5cc1f619e
commit
aa334ea60a
@ -1,15 +1,20 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cctype>
|
|
||||||
#include <rublon/memory.hpp>
|
|
||||||
#include <rublon/error.hpp>
|
#include <rublon/error.hpp>
|
||||||
|
#include <rublon/memory.hpp>
|
||||||
#include <rublon/static_string.hpp>
|
#include <rublon/static_string.hpp>
|
||||||
#include <rublon/utils.hpp>
|
#include <rublon/utils.hpp>
|
||||||
|
|
||||||
|
#include <cctype>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <memory_resource>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace rublon {
|
namespace rublon {
|
||||||
class ConfigurationFactory;
|
class ConfigurationFactory;
|
||||||
|
|
||||||
@ -20,7 +25,7 @@ class Configuration {
|
|||||||
std::pmr::memory_resource * memoryResource;
|
std::pmr::memory_resource * memoryResource;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Configuration() : memoryResource{memory::default_resource()} {}
|
Configuration(std::pmr::memory_resource * mr = memory::default_resource()) : memoryResource{mr} {}
|
||||||
|
|
||||||
// change to StaticString
|
// change to StaticString
|
||||||
std::pmr::string systemToken{memoryResource};
|
std::pmr::string systemToken{memoryResource};
|
||||||
@ -43,16 +48,15 @@ class Configuration {
|
|||||||
bool proxyEnabled{}; // defaulted
|
bool proxyEnabled{}; // defaulted
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class ConfigurationReader {
|
class ConfigurationReader {
|
||||||
public:
|
public:
|
||||||
ConfigurationReader(std::pmr::memory_resource * memResource = memory::default_resource()) : memoryResource(memResource) {}
|
ConfigurationReader(std::pmr::memory_resource * memResource = memory::default_resource()) : memoryResource(memResource) {}
|
||||||
|
|
||||||
// Load config from file path
|
// Load config from file path
|
||||||
bool loadFromFile(const std::string & filepath) {
|
bool loadFromFile(std::string_view filepath) {
|
||||||
using namespace memory::literals;
|
using namespace memory::literals;
|
||||||
memory::MonotonicStackResource< 8_kB > stackResource;
|
memory::MonotonicStackResource< 8_kB > stackResource;
|
||||||
std::ifstream file(filepath);
|
std::ifstream file(filepath.data());
|
||||||
if(not file.good())
|
if(not file.good())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -73,7 +77,7 @@ class ConfigurationReader {
|
|||||||
auto posEqual = line.find('=');
|
auto posEqual = line.find('=');
|
||||||
key = line.substr(0, posEqual);
|
key = line.substr(0, posEqual);
|
||||||
value = line.substr(posEqual + 1);
|
value = line.substr(posEqual + 1);
|
||||||
|
|
||||||
keyValues[std::move(key)] = std::move(value);
|
keyValues[std::move(key)] = std::move(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +88,7 @@ class ConfigurationReader {
|
|||||||
tl::expected< bool, ConfigurationError > applyTo(Configuration & config) {
|
tl::expected< bool, ConfigurationError > applyTo(Configuration & config) {
|
||||||
// Helper lambdas for conversion
|
// Helper lambdas for conversion
|
||||||
using string = std::pmr::string;
|
using string = std::pmr::string;
|
||||||
|
|
||||||
auto getStringOpt = [&](const string & key) -> std::optional< std::pmr::string > {
|
auto getStringOpt = [&](const string & key) -> std::optional< std::pmr::string > {
|
||||||
auto it = keyValues.find(key);
|
auto it = keyValues.find(key);
|
||||||
if(it == keyValues.end()) {
|
if(it == keyValues.end()) {
|
||||||
@ -112,12 +116,12 @@ class ConfigurationReader {
|
|||||||
auto it = keyValues.find(key);
|
auto it = keyValues.find(key);
|
||||||
if(it == keyValues.end())
|
if(it == keyValues.end())
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
|
|
||||||
if (it->second.size() > 5 ){
|
if(it->second.size() > 5) {
|
||||||
log(LogLevel::Warning, "Configuration value %s is too long, please check", key.c_str());
|
log(LogLevel::Warning, "Configuration value %s is too long, please check", key.c_str());
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pmr::string val{&memoryResource};
|
std::pmr::string val{&memoryResource};
|
||||||
val = it->second;
|
val = it->second;
|
||||||
std::transform(val.begin(), val.end(), val.begin(), [](unsigned char c) { return static_cast< char >(std::tolower(c)); });
|
std::transform(val.begin(), val.end(), val.begin(), [](unsigned char c) { return static_cast< char >(std::tolower(c)); });
|
||||||
@ -200,7 +204,7 @@ class ConfigurationReader {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::pmr::memory_resource * memoryResource;
|
std::pmr::memory_resource * memoryResource;
|
||||||
std::pmr::map< std::pmr::string, std::pmr::string > keyValues{memoryResource};
|
std::pmr::unordered_map< std::pmr::string, std::pmr::string > keyValues{memoryResource};
|
||||||
};
|
};
|
||||||
|
|
||||||
class ConfigurationFactory {
|
class ConfigurationFactory {
|
||||||
@ -208,10 +212,10 @@ class ConfigurationFactory {
|
|||||||
ConfigurationFactory() = default;
|
ConfigurationFactory() = default;
|
||||||
|
|
||||||
std::optional< Configuration > systemConfig() {
|
std::optional< Configuration > systemConfig() {
|
||||||
std::optional< Configuration > conf{Configuration{}};
|
std::optional< Configuration > conf{};
|
||||||
ConfigurationReader reader;
|
ConfigurationReader reader{};
|
||||||
reader.loadFromFile("/etc/rublon.config");
|
reader.loadFromFile("/etc/rublon.config");
|
||||||
if(auto ok = reader.applyTo(conf.value()); not ok.has_value()){
|
if(auto ok = reader.applyTo(conf.value()); not ok.has_value()) {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
return conf.value();
|
return conf.value();
|
||||||
|
|||||||
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
#include "rublon/memory.hpp"
|
#include "rublon/memory.hpp"
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <rublon/configuration.hpp>
|
||||||
#include <rublon/error.hpp>
|
#include <rublon/error.hpp>
|
||||||
#include <rublon/utils.hpp>
|
#include <rublon/utils.hpp>
|
||||||
#include <rublon/configuration.hpp>
|
|
||||||
|
|
||||||
#include <tl/expected.hpp>
|
#include <tl/expected.hpp>
|
||||||
|
|
||||||
@ -12,7 +12,6 @@
|
|||||||
|
|
||||||
namespace rublon {
|
namespace rublon {
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
size_t WriteMemoryCallback(void * contents, size_t size, size_t nmemb, void * userp) {
|
size_t WriteMemoryCallback(void * contents, size_t size, size_t nmemb, void * userp) {
|
||||||
const size_t realsize = size * nmemb;
|
const size_t realsize = size * nmemb;
|
||||||
@ -55,9 +54,11 @@ struct Response {
|
|||||||
|
|
||||||
class CURL {
|
class CURL {
|
||||||
std::unique_ptr< ::CURL, void (*)(::CURL *) > curl;
|
std::unique_ptr< ::CURL, void (*)(::CURL *) > curl;
|
||||||
const Configuration &_config;
|
const Configuration & _config;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CURL(const Configuration &config) : curl{std::unique_ptr< ::CURL, void (*)(::CURL *) >(curl_easy_init(), curl_easy_cleanup)}, _config{config} {}
|
CURL(const Configuration & config)
|
||||||
|
: curl{std::unique_ptr< ::CURL, void (*)(::CURL *) >(curl_easy_init(), curl_easy_cleanup)}, _config{config} {}
|
||||||
|
|
||||||
tl::expected< std::reference_wrapper< Response >, ConnectionError >
|
tl::expected< std::reference_wrapper< Response >, ConnectionError >
|
||||||
request(std::string_view uri, const Request & request, Response & response) const {
|
request(std::string_view uri, const Request & request, Response & response) const {
|
||||||
@ -72,45 +73,46 @@ class CURL {
|
|||||||
log(LogLevel::Debug, "%s header: %s: %s", "CURL", header.first.c_str(), header.second.c_str());
|
log(LogLevel::Debug, "%s header: %s: %s", "CURL", header.first.c_str(), header.second.c_str());
|
||||||
curl_headers.reset(curl_slist_append(curl_headers.release(), (header.first + ": " + header.second).c_str()));
|
curl_headers.reset(curl_slist_append(curl_headers.release(), (header.first + ": " + header.second).c_str()));
|
||||||
});
|
});
|
||||||
|
|
||||||
// Optional: Build full proxy URL if proxy is enabled
|
// Optional: Build full proxy URL if proxy is enabled
|
||||||
if (_config.proxyEnabled) {
|
if(_config.proxyEnabled) {
|
||||||
// configuration reader check if proxy has needed fields
|
// configuration reader check if proxy has needed fields
|
||||||
assert(_config.proxyType.has_value());
|
assert(_config.proxyType.has_value());
|
||||||
assert(_config.proxyServer.has_value());
|
assert(_config.proxyServer.has_value());
|
||||||
|
|
||||||
std::pmr::string proxyUrl{&memoryResource};
|
std::pmr::string proxyUrl{&memoryResource};
|
||||||
proxyUrl.reserve(conservative_estimate(_config.proxyType, _config.proxyServer, _config.proxyPort) + 10);
|
proxyUrl.reserve(conservative_estimate(_config.proxyType, _config.proxyServer, _config.proxyPort) + 10);
|
||||||
|
|
||||||
if (_config.proxyType == "http" || _config.proxyType == "https" || _config.proxyType == "socks4" || _config.proxyType == "socks5") {
|
if(_config.proxyType == "http" || _config.proxyType == "https" || _config.proxyType == "socks4" ||
|
||||||
|
_config.proxyType == "socks5") {
|
||||||
proxyUrl = *_config.proxyType;
|
proxyUrl = *_config.proxyType;
|
||||||
proxyUrl += "://";
|
proxyUrl += "://";
|
||||||
proxyUrl += *_config.proxyServer;
|
proxyUrl += *_config.proxyServer;
|
||||||
if (_config.proxyPort > 0) {
|
if(_config.proxyPort > 0) {
|
||||||
proxyUrl += ":";
|
proxyUrl += ":";
|
||||||
proxyUrl += std::to_string(*_config.proxyPort);
|
proxyUrl += std::to_string(*_config.proxyPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
curl_easy_setopt(curl.get(), CURLOPT_PROXY, proxyUrl.c_str());
|
curl_easy_setopt(curl.get(), CURLOPT_PROXY, proxyUrl.c_str());
|
||||||
|
|
||||||
if (_config.proxyType == "socks4") {
|
if(_config.proxyType == "socks4") {
|
||||||
curl_easy_setopt(curl.get(), CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
|
curl_easy_setopt(curl.get(), CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
|
||||||
} else if (_config.proxyType == "socks5") {
|
} else if(_config.proxyType == "socks5") {
|
||||||
curl_easy_setopt(curl.get(), CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
|
curl_easy_setopt(curl.get(), CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
|
||||||
} else {
|
} else {
|
||||||
curl_easy_setopt(curl.get(), CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
curl_easy_setopt(curl.get(), CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_config.proxyAuthRequired) {
|
if(_config.proxyAuthRequired) {
|
||||||
assert(_config.proxyUsername.has_value());
|
assert(_config.proxyUsername.has_value());
|
||||||
assert(_config.proxyPass.has_value());
|
assert(_config.proxyPass.has_value());
|
||||||
|
|
||||||
std::pmr::string proxyAuth{&memoryResource};
|
std::pmr::string proxyAuth{&memoryResource};
|
||||||
proxyAuth.reserve(conservative_estimate(_config.proxyUsername->size() + _config.proxyPass->size()));
|
proxyAuth.reserve(conservative_estimate(_config.proxyUsername, _config.proxyPass));
|
||||||
|
|
||||||
proxyAuth += *_config.proxyUsername;
|
proxyAuth += *_config.proxyUsername;
|
||||||
if(_config.proxyPass->size()) {
|
if(_config.proxyPass->size()) {
|
||||||
// can proxy have name but no pass?
|
// can proxy have name but no pass?
|
||||||
proxyAuth += ":";
|
proxyAuth += ":";
|
||||||
proxyAuth += *_config.proxyPass;
|
proxyAuth += *_config.proxyPass;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <memory_resource>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <tl/expected.hpp>
|
#include <tl/expected.hpp>
|
||||||
@ -129,12 +130,15 @@ class MethodSelect {
|
|||||||
int _prompts;
|
int _prompts;
|
||||||
bool _autopushPrompt;
|
bool _autopushPrompt;
|
||||||
|
|
||||||
std::vector< std::string > _methodsAvailable; // TODO pmr
|
std::pmr::memory_resource * _mr;
|
||||||
|
|
||||||
|
// method name is really short, there is almost no chance that thos strings will allocate
|
||||||
|
std::pmr::vector< std::pmr::string > _methodsAvailable;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template < typename Array_t >
|
template < typename Array_t >
|
||||||
MethodSelect(Session & session, const Array_t & methodsEnabledInCore, int prompts, bool autopushPrompt)
|
MethodSelect(Session & session, const Array_t & methodsEnabledInCore, int prompts, bool autopushPrompt)
|
||||||
: _session{session}, _prompts{prompts}, _autopushPrompt{autopushPrompt} {
|
: _session{session}, _prompts{prompts}, _autopushPrompt{autopushPrompt}, _mr{memory::default_resource()}, _methodsAvailable{_mr} {
|
||||||
rublon::log(LogLevel::Debug, "Checking what methods from core are supported");
|
rublon::log(LogLevel::Debug, "Checking what methods from core are supported");
|
||||||
using namespace std::string_view_literals;
|
using namespace std::string_view_literals;
|
||||||
_methodsAvailable.reserve(std::size(methodsEnabledInCore));
|
_methodsAvailable.reserve(std::size(methodsEnabledInCore));
|
||||||
|
|||||||
@ -8,11 +8,11 @@ namespace rublon {
|
|||||||
|
|
||||||
class AuthenticationStatus {
|
class AuthenticationStatus {
|
||||||
public:
|
public:
|
||||||
using tokenT = std::optional< StaticString< 64 > >;
|
using Token_t = std::optional< StaticString< 64 > >;
|
||||||
enum class Action { Denied, Confirmed, Bypass };
|
enum class Action { Denied, Confirmed, Bypass };
|
||||||
|
|
||||||
AuthenticationStatus(Action action, const char * token = nullptr)
|
AuthenticationStatus(Action action, const char * token = nullptr)
|
||||||
: _action{action}, _authenticationToken{token == nullptr ? tokenT{std::nullopt} : tokenT{token}} {}
|
: _action{action}, _authenticationToken{token == nullptr ? Token_t{std::nullopt} : Token_t{token}} {}
|
||||||
|
|
||||||
constexpr bool userAuthorized() const {
|
constexpr bool userAuthorized() const {
|
||||||
return _action == Action::Confirmed;
|
return _action == Action::Confirmed;
|
||||||
@ -23,12 +23,14 @@ class AuthenticationStatus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string_view accessToken() const {
|
std::string_view accessToken() const {
|
||||||
|
if(not _authenticationToken)
|
||||||
|
return "";
|
||||||
return {_authenticationToken->c_str(), _authenticationToken->size()};
|
return {_authenticationToken->c_str(), _authenticationToken->size()};
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Action _action;
|
Action _action;
|
||||||
tokenT _authenticationToken; /// TODO dynamic mem
|
Token_t _authenticationToken;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace rublon
|
} // namespace rublon
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user