bwi/v2.3.0 #29
@ -1,15 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <cctype>
|
||||
#include <rublon/memory.hpp>
|
||||
#include <rublon/error.hpp>
|
||||
#include <rublon/memory.hpp>
|
||||
#include <rublon/static_string.hpp>
|
||||
#include <rublon/utils.hpp>
|
||||
|
||||
#include <cctype>
|
||||
#include <fstream>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory_resource>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
namespace rublon {
|
||||
class ConfigurationFactory;
|
||||
|
||||
@ -20,7 +25,7 @@ class Configuration {
|
||||
std::pmr::memory_resource * memoryResource;
|
||||
|
||||
public:
|
||||
Configuration() : memoryResource{memory::default_resource()} {}
|
||||
Configuration(std::pmr::memory_resource * mr = memory::default_resource()) : memoryResource{mr} {}
|
||||
|
||||
// change to StaticString
|
||||
std::pmr::string systemToken{memoryResource};
|
||||
@ -43,16 +48,15 @@ class Configuration {
|
||||
bool proxyEnabled{}; // defaulted
|
||||
};
|
||||
|
||||
|
||||
class ConfigurationReader {
|
||||
public:
|
||||
ConfigurationReader(std::pmr::memory_resource * memResource = memory::default_resource()) : memoryResource(memResource) {}
|
||||
|
||||
// Load config from file path
|
||||
bool loadFromFile(const std::string & filepath) {
|
||||
bool loadFromFile(std::string_view filepath) {
|
||||
using namespace memory::literals;
|
||||
memory::MonotonicStackResource< 8_kB > stackResource;
|
||||
std::ifstream file(filepath);
|
||||
std::ifstream file(filepath.data());
|
||||
if(not file.good())
|
||||
return false;
|
||||
|
||||
@ -73,7 +77,7 @@ class ConfigurationReader {
|
||||
auto posEqual = line.find('=');
|
||||
key = line.substr(0, posEqual);
|
||||
value = line.substr(posEqual + 1);
|
||||
|
||||
|
||||
keyValues[std::move(key)] = std::move(value);
|
||||
}
|
||||
|
||||
@ -84,7 +88,7 @@ class ConfigurationReader {
|
||||
tl::expected< bool, ConfigurationError > applyTo(Configuration & config) {
|
||||
// Helper lambdas for conversion
|
||||
using string = std::pmr::string;
|
||||
|
||||
|
||||
auto getStringOpt = [&](const string & key) -> std::optional< std::pmr::string > {
|
||||
auto it = keyValues.find(key);
|
||||
if(it == keyValues.end()) {
|
||||
@ -112,12 +116,12 @@ class ConfigurationReader {
|
||||
auto it = keyValues.find(key);
|
||||
if(it == keyValues.end())
|
||||
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());
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
||||
std::pmr::string val{&memoryResource};
|
||||
val = it->second;
|
||||
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:
|
||||
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 {
|
||||
@ -208,10 +212,10 @@ class ConfigurationFactory {
|
||||
ConfigurationFactory() = default;
|
||||
|
||||
std::optional< Configuration > systemConfig() {
|
||||
std::optional< Configuration > conf{Configuration{}};
|
||||
ConfigurationReader reader;
|
||||
std::optional< Configuration > conf{};
|
||||
ConfigurationReader reader{};
|
||||
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 conf.value();
|
||||
|
||||
@ -2,9 +2,9 @@
|
||||
|
||||
#include "rublon/memory.hpp"
|
||||
#include <cstddef>
|
||||
#include <rublon/configuration.hpp>
|
||||
#include <rublon/error.hpp>
|
||||
#include <rublon/utils.hpp>
|
||||
#include <rublon/configuration.hpp>
|
||||
|
||||
#include <tl/expected.hpp>
|
||||
|
||||
@ -12,7 +12,6 @@
|
||||
|
||||
namespace rublon {
|
||||
|
||||
|
||||
namespace {
|
||||
size_t WriteMemoryCallback(void * contents, size_t size, size_t nmemb, void * userp) {
|
||||
const size_t realsize = size * nmemb;
|
||||
@ -55,9 +54,11 @@ struct Response {
|
||||
|
||||
class CURL {
|
||||
std::unique_ptr< ::CURL, void (*)(::CURL *) > curl;
|
||||
const Configuration &_config;
|
||||
const Configuration & _config;
|
||||
|
||||
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 >
|
||||
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());
|
||||
curl_headers.reset(curl_slist_append(curl_headers.release(), (header.first + ": " + header.second).c_str()));
|
||||
});
|
||||
|
||||
|
||||
// Optional: Build full proxy URL if proxy is enabled
|
||||
if (_config.proxyEnabled) {
|
||||
if(_config.proxyEnabled) {
|
||||
// configuration reader check if proxy has needed fields
|
||||
assert(_config.proxyType.has_value());
|
||||
assert(_config.proxyServer.has_value());
|
||||
|
||||
|
||||
std::pmr::string proxyUrl{&memoryResource};
|
||||
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 += "://";
|
||||
proxyUrl += *_config.proxyServer;
|
||||
if (_config.proxyPort > 0) {
|
||||
if(_config.proxyPort > 0) {
|
||||
proxyUrl += ":";
|
||||
proxyUrl += std::to_string(*_config.proxyPort);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
} else if (_config.proxyType == "socks5") {
|
||||
} else if(_config.proxyType == "socks5") {
|
||||
curl_easy_setopt(curl.get(), CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
|
||||
} else {
|
||||
curl_easy_setopt(curl.get(), CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
||||
}
|
||||
|
||||
if (_config.proxyAuthRequired) {
|
||||
|
||||
if(_config.proxyAuthRequired) {
|
||||
assert(_config.proxyUsername.has_value());
|
||||
assert(_config.proxyPass.has_value());
|
||||
|
||||
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;
|
||||
if(_config.proxyPass->size()) {
|
||||
// can proxy have name but no pass?
|
||||
// can proxy have name but no pass?
|
||||
proxyAuth += ":";
|
||||
proxyAuth += *_config.proxyPass;
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <memory_resource>
|
||||
#include <set>
|
||||
#include <string_view>
|
||||
#include <tl/expected.hpp>
|
||||
@ -129,12 +130,15 @@ class MethodSelect {
|
||||
int _prompts;
|
||||
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:
|
||||
template < typename Array_t >
|
||||
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");
|
||||
using namespace std::string_view_literals;
|
||||
_methodsAvailable.reserve(std::size(methodsEnabledInCore));
|
||||
|
||||
@ -8,11 +8,11 @@ namespace rublon {
|
||||
|
||||
class AuthenticationStatus {
|
||||
public:
|
||||
using tokenT = std::optional< StaticString< 64 > >;
|
||||
using Token_t = std::optional< StaticString< 64 > >;
|
||||
enum class Action { Denied, Confirmed, Bypass };
|
||||
|
||||
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 {
|
||||
return _action == Action::Confirmed;
|
||||
@ -23,12 +23,14 @@ class AuthenticationStatus {
|
||||
}
|
||||
|
||||
std::string_view accessToken() const {
|
||||
if(not _authenticationToken)
|
||||
return "";
|
||||
return {_authenticationToken->c_str(), _authenticationToken->size()};
|
||||
}
|
||||
|
||||
private:
|
||||
Action _action;
|
||||
tokenT _authenticationToken; /// TODO dynamic mem
|
||||
Token_t _authenticationToken;
|
||||
};
|
||||
|
||||
} // namespace rublon
|
||||
|
||||
Loading…
Reference in New Issue
Block a user