mirror of
https://git.jami.net/savoirfairelinux/jami-daemon.git
synced 2025-08-12 22:09:25 +08:00
jamidht: fix namedir & login to server account
Change-Id: I32e1a1cee8b8b1c7a814b4538dab7109850d328e Reviewed-by: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
This commit is contained in:

committed by
Sébastien Blin

parent
ae2d4efa98
commit
ddf7baef1e
@ -1 +1 @@
|
||||
fe4675faf918ccdcca6eac17ffca7a010edf77296db16c7adb02ea4709199dac1ef11815e54fb1203b9401a98c8cc58d78280e79e434159c82a77398f47a5807 opendht-63afc8c29a01f4b062b596608e0a5886e4f05a18.tar.gz
|
||||
e9401823db7130560969e97174329ac7ae72a07eedb6806f1b5ac43c2f366e3e12c9a6fab981e531e0afdd9430c0cd200ba57cbd2e79b566eead6453a660c98a opendht-b03a7732e3dca3a48689e64e345fe418e130b6ac.tar.gz
|
||||
|
@ -1,5 +1,5 @@
|
||||
# OPENDHT
|
||||
OPENDHT_VERSION := 63afc8c29a01f4b062b596608e0a5886e4f05a18
|
||||
OPENDHT_VERSION := b03a7732e3dca3a48689e64e345fe418e130b6ac
|
||||
OPENDHT_URL := https://github.com/savoirfairelinux/opendht/archive/$(OPENDHT_VERSION).tar.gz
|
||||
|
||||
PKGS += opendht
|
||||
|
@ -49,9 +49,6 @@ constexpr const char* const QUERY_NAME {"/name/"};
|
||||
constexpr const char* const QUERY_ADDR {"/addr/"};
|
||||
constexpr const char* const CACHE_DIRECTORY {"namecache"};
|
||||
|
||||
constexpr const char* const HTTPS_URI {"https://"};
|
||||
static const std::string HTTPS_PROTO {"https"};
|
||||
|
||||
const std::string HEX_PREFIX = "0x";
|
||||
constexpr std::chrono::seconds SAVE_INTERVAL {5};
|
||||
|
||||
@ -87,13 +84,14 @@ NameDirectory::lookupUri(const std::string& uri, const std::string& default_serv
|
||||
cb("", Response::invalidResponse);
|
||||
}
|
||||
|
||||
NameDirectory::NameDirectory(const std::string& s, std::shared_ptr<dht::Logger> l)
|
||||
: serverHost_(s)
|
||||
, cachePath_(fileutils::get_cache_dir() + DIR_SEPARATOR_STR + CACHE_DIRECTORY + DIR_SEPARATOR_STR + serverHost_)
|
||||
, logger_(l)
|
||||
NameDirectory::NameDirectory(const std::string& serverUrl, std::shared_ptr<dht::Logger> l)
|
||||
: serverUrl_(serverUrl), logger_(std::move(l))
|
||||
, httpContext_(Manager::instance().ioContext())
|
||||
, resolver_(std::make_shared<dht::http::Resolver>(*httpContext_, serverHost_, HTTPS_PROTO, true, logger_))
|
||||
{}
|
||||
{
|
||||
resolver_ = std::make_shared<dht::http::Resolver>(*httpContext_, serverUrl, logger_);
|
||||
cachePath_ = fileutils::get_cache_dir() + DIR_SEPARATOR_STR + CACHE_DIRECTORY +
|
||||
DIR_SEPARATOR_STR + resolver_->get_url().host;
|
||||
}
|
||||
|
||||
void
|
||||
NameDirectory::load()
|
||||
@ -102,9 +100,9 @@ NameDirectory::load()
|
||||
}
|
||||
|
||||
NameDirectory&
|
||||
NameDirectory::instance(const std::string& server, std::shared_ptr<dht::Logger> l)
|
||||
NameDirectory::instance(const std::string& serverUrl, std::shared_ptr<dht::Logger> l)
|
||||
{
|
||||
const std::string& s = server.empty() ? DEFAULT_SERVER_HOST : server;
|
||||
const std::string& s = serverUrl.empty() ? DEFAULT_SERVER_HOST : serverUrl;
|
||||
static std::mutex instanceMtx {};
|
||||
|
||||
std::lock_guard<std::mutex> lock(instanceMtx);
|
||||
@ -122,8 +120,7 @@ NameDirectory::instance(const std::string& server, std::shared_ptr<dht::Logger>
|
||||
|
||||
void
|
||||
NameDirectory::setHeaderFields(Request& request){
|
||||
const std::string host = HTTPS_PROTO + ":" + serverHost_;
|
||||
request.set_header_field(restinio::http_field_t::host, host);
|
||||
request.set_header_field(restinio::http_field_t::host, request.get_url().host + ":" + request.get_url().service);
|
||||
request.set_header_field(restinio::http_field_t::user_agent, "JamiDHT");
|
||||
request.set_header_field(restinio::http_field_t::accept, "*/*");
|
||||
request.set_header_field(restinio::http_field_t::content_type, "application/json");
|
||||
@ -141,11 +138,17 @@ NameDirectory::lookupAddress(const std::string& addr, LookupCallback cb)
|
||||
auto reqid = request->id();
|
||||
try {
|
||||
request->set_connection_type(restinio::http_connection_header_t::keep_alive);
|
||||
request->set_target(QUERY_ADDR + addr);
|
||||
auto target_prefix = request->get_url().target;
|
||||
if (target_prefix.back() == '/')
|
||||
target_prefix.pop_back();
|
||||
request->set_target(target_prefix + QUERY_ADDR + addr);
|
||||
request->set_method(restinio::http_method_get());
|
||||
setHeaderFields(*request);
|
||||
|
||||
const std::string uri = HTTPS_URI + serverHost_ + QUERY_ADDR + addr;
|
||||
std::string uri = serverUrl_;
|
||||
if (uri.back() == '/')
|
||||
uri.pop_back();
|
||||
uri += QUERY_ADDR + addr;
|
||||
JAMI_DBG("Address lookup for %s: %s", addr.c_str(), uri.c_str());
|
||||
request->add_on_state_change_callback([this, cb=std::move(cb), reqid, addr]
|
||||
(Request::State state, const dht::http::Response& response){
|
||||
@ -223,11 +226,17 @@ NameDirectory::lookupName(const std::string& n, LookupCallback cb)
|
||||
auto reqid = request->id();
|
||||
try {
|
||||
request->set_connection_type(restinio::http_connection_header_t::keep_alive);
|
||||
request->set_target(QUERY_NAME + name);
|
||||
auto target_prefix = request->get_url().target;
|
||||
if (target_prefix.back() == '/')
|
||||
target_prefix.pop_back();
|
||||
request->set_target(target_prefix + QUERY_NAME + name);
|
||||
request->set_method(restinio::http_method_get());
|
||||
setHeaderFields(*request);
|
||||
|
||||
const std::string uri = HTTPS_URI + serverHost_ + QUERY_NAME + name;
|
||||
std::string uri = serverUrl_;
|
||||
if (uri.back() == '/')
|
||||
uri.pop_back();
|
||||
uri += QUERY_NAME + name;
|
||||
JAMI_DBG("Name lookup for %s: %s", name.c_str(), uri.c_str());
|
||||
|
||||
request->add_on_state_change_callback([this, reqid, name, cb=std::move(cb)]
|
||||
@ -337,7 +346,10 @@ void NameDirectory::registerName(const std::string& addr, const std::string& n,
|
||||
auto reqid = request->id();
|
||||
try {
|
||||
request->set_connection_type(restinio::http_connection_header_t::keep_alive);
|
||||
request->set_target(QUERY_NAME + name);
|
||||
auto target_prefix = request->get_url().target;
|
||||
if (target_prefix.back() == '/')
|
||||
target_prefix.pop_back();
|
||||
request->set_target(target_prefix + QUERY_NAME + name);
|
||||
request->set_method(restinio::http_method_post());
|
||||
setHeaderFields(*request);
|
||||
request->set_body(body);
|
||||
|
@ -62,10 +62,10 @@ public:
|
||||
using LookupCallback = std::function<void(const std::string& result, Response response)>;
|
||||
using RegistrationCallback = std::function<void(RegistrationResponse response)>;
|
||||
|
||||
NameDirectory(const std::string& s, std::shared_ptr<dht::Logger> l = {});
|
||||
NameDirectory(const std::string& serverUrl, std::shared_ptr<dht::Logger> l = {});
|
||||
void load();
|
||||
|
||||
static NameDirectory& instance(const std::string& server, std::shared_ptr<dht::Logger> l = {});
|
||||
static NameDirectory& instance(const std::string& serverUrl, std::shared_ptr<dht::Logger> l = {});
|
||||
static NameDirectory& instance() { return instance(DEFAULT_SERVER_HOST); }
|
||||
|
||||
static void lookupUri(const std::string& uri, const std::string& default_server,
|
||||
@ -78,18 +78,14 @@ public:
|
||||
const std::string& owner, RegistrationCallback cb,
|
||||
const std::string& signedname, const std::string& publickey);
|
||||
|
||||
const std::string& getServer() const {
|
||||
return serverHost_;
|
||||
}
|
||||
|
||||
private:
|
||||
NON_COPYABLE(NameDirectory);
|
||||
NameDirectory(NameDirectory&&) = delete;
|
||||
NameDirectory& operator=(NameDirectory&&) = delete;
|
||||
constexpr static const char* const DEFAULT_SERVER_HOST = "ns.jami.net";
|
||||
constexpr static const char* const DEFAULT_SERVER_HOST = "https://ns.jami.net";
|
||||
|
||||
const std::string serverHost_ {DEFAULT_SERVER_HOST};
|
||||
const std::string cachePath_;
|
||||
std::string serverUrl_;
|
||||
std::string cachePath_;
|
||||
|
||||
std::mutex cacheLock_ {};
|
||||
std::shared_ptr<dht::Logger> logger_;
|
||||
|
@ -50,6 +50,14 @@ ServerAccountManager::ServerAccountManager(
|
||||
[](char const* m, va_list args) { Logger::vlog(LOG_DEBUG, nullptr, 0, true, m, args); }))
|
||||
{};
|
||||
|
||||
void
|
||||
ServerAccountManager::setHeaderFields(Request& request){
|
||||
request.set_header_field(restinio::http_field_t::host, request.get_url().host + ":" + request.get_url().service);
|
||||
request.set_header_field(restinio::http_field_t::user_agent, "Jami");
|
||||
request.set_header_field(restinio::http_field_t::accept, "application/json");
|
||||
request.set_header_field(restinio::http_field_t::content_type, "application/json");
|
||||
}
|
||||
|
||||
void
|
||||
ServerAccountManager::initAuthentication(
|
||||
CertRequest csrRequest,
|
||||
@ -71,11 +79,13 @@ ServerAccountManager::initAuthentication(
|
||||
onChange_ = std::move(onChange);
|
||||
|
||||
const std::string url = managerHostname_ + PATH_DEVICE_REGISTER;
|
||||
JAMI_WARN("[Auth] authentication with: %s %s to %s", ctx->credentials->username.c_str(), ctx->credentials->password.c_str(), url.c_str());
|
||||
JAMI_WARN("[Auth] authentication with: %s to %s", ctx->credentials->username.c_str(), url.c_str());
|
||||
auto request = std::make_shared<Request>(*Manager::instance().ioContext(), url, logger_);
|
||||
auto reqid = request->id();
|
||||
request->set_connection_type(restinio::http_connection_header_t::close);
|
||||
request->set_method(restinio::http_method_post());
|
||||
restinio::http_request_header_t header;
|
||||
header.method(restinio::http_method_post());
|
||||
request->set_header(header);
|
||||
request->set_target(PATH_DEVICE_REGISTER);
|
||||
request->set_auth(ctx->credentials->username, ctx->credentials->password);
|
||||
{
|
||||
std::stringstream ss;
|
||||
@ -86,12 +96,10 @@ ServerAccountManager::initAuthentication(
|
||||
JAMI_WARN("[Auth] Sending request: %s", csr.c_str());
|
||||
request->set_body(ss.str());
|
||||
}
|
||||
|
||||
request->set_header_field(restinio::http_field_t::user_agent, "Jami");
|
||||
request->set_header_field(restinio::http_field_t::accept, "application/json");
|
||||
request->set_header_field(restinio::http_field_t::content_type, "application/json");
|
||||
setHeaderFields(*request);
|
||||
request->set_header_field(restinio::http_field_t::expect, "100-continue");
|
||||
request->add_on_state_change_callback([reqid, ctx, onAsync = onAsync_]
|
||||
(Request::State state, const dht::http::Response& response){
|
||||
(Request::State state, const dht::http::Response& response){
|
||||
|
||||
JAMI_ERR("[Auth] Got server response: %d", (int)state);
|
||||
if (state != Request::State::DONE)
|
||||
@ -186,16 +194,15 @@ ServerAccountManager::syncDevices()
|
||||
if (not creds_)
|
||||
return;
|
||||
const std::string url = managerHostname_ + PATH_DEVICES;
|
||||
JAMI_WARN("[Auth] syncDevices with: %s %s to %s", creds_->username.c_str(), creds_->password.c_str(), url.c_str());
|
||||
JAMI_WARN("[Auth] syncDevices with: %s to %s", creds_->username.c_str(), url.c_str());
|
||||
auto request = std::make_shared<Request>(*Manager::instance().ioContext(), url, logger_);
|
||||
auto reqid = request->id();
|
||||
request->set_connection_type(restinio::http_connection_header_t::close);
|
||||
request->set_method(restinio::http_method_get());
|
||||
restinio::http_request_header_t header;
|
||||
header.method(restinio::http_method_get());
|
||||
request->set_header(header);
|
||||
request->set_target(PATH_DEVICES);
|
||||
request->set_auth(creds_->username, creds_->password);
|
||||
request->set_header_field(restinio::http_field_t::user_agent, "Jami");
|
||||
request->set_header_field(restinio::http_field_t::accept, "application/json");
|
||||
request->set_header_field(restinio::http_field_t::content_type, "application/json");
|
||||
|
||||
setHeaderFields(*request);
|
||||
request->add_on_state_change_callback([reqid, onAsync = onAsync_]
|
||||
(Request::State state, const dht::http::Response& response){
|
||||
onAsync([reqid, state, response] (AccountManager& accountManager) {
|
||||
|
@ -76,6 +76,8 @@ private:
|
||||
std::shared_ptr<dht::Logger> logger_;
|
||||
std::map<unsigned int /*id*/, std::shared_ptr<dht::http::Request>> requests_;
|
||||
std::unique_ptr<ServerAccountCredentials> creds_;
|
||||
|
||||
void setHeaderFields(dht::http::Request& request);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user