Commit b4833f39 authored by Adrien Béraud's avatar Adrien Béraud

jamiaccount: cleanup cache server choice, default

Change-Id: Iaed539f54987389c75c18d97b8d16aabed010d95
parent 1aa474b3
......@@ -287,10 +287,14 @@ JamiAccount::JamiAccount(const std::string& accountID, bool /* presenceEnabled *
turnServerRealm_ = DEFAULT_TURN_REALM;
turnEnabled_ = true;
proxyListUrl_ = DHT_DEFAULT_PROXY_LIST_URL;
proxyServer_ = DHT_DEFAULT_PROXY;
try {
std::istringstream is(fileutils::loadCacheTextFile(cachePath_ + DIR_SEPARATOR_STR "dhtproxy", std::chrono::hours(24 * 7)));
std::getline(is, proxyServerCached_);
} catch (...) {
} catch (const std::exception& e) {
JAMI_DBG("Can't load proxy URL from cache: %s", e.what());
}
setActiveCodecs({});
......@@ -895,6 +899,9 @@ void JamiAccount::unserialize(const YAML::Node &node)
} catch (const std::exception& e) {
proxyListUrl_ = DHT_DEFAULT_PROXY_LIST_URL;
}
// TODO remove
if (proxyListUrl_.empty())
proxyListUrl_ = DHT_DEFAULT_PROXY_LIST_URL;
parseValueOptional(node, DRing::Account::ConfProperties::RING_DEVICE_NAME, ringDeviceName_);
parseValueOptional(node, DRing::Account::ConfProperties::MANAGER_URI, managerUri_);
......@@ -1874,12 +1881,13 @@ JamiAccount::doRegister_()
config.dht_config.node_config.persist_path = cachePath_+DIR_SEPARATOR_STR "dhtstate";
config.dht_config.id = id_;
config.dht_config.cert_cache_all = true;
config.proxy_server = getDhtProxyServer(proxyServer_);
config.push_node_id = getAccountID();
config.push_token = deviceKey_;
config.threaded = true;
config.peer_discovery = dhtPeerDiscovery_;
config.peer_publish = dhtPeerDiscovery_;
if (proxyEnabled_)
config.proxy_server = proxyServerCached_;
if (not config.proxy_server.empty()) {
JAMI_INFO("[Account %s] using proxy server %s", getAccountID().c_str(), config.proxy_server.c_str());
......@@ -2464,7 +2472,7 @@ void
JamiAccount::loadCachedProxyServer(std::function<void(const std::string& proxy)> cb)
{
if (proxyEnabled_ and proxyServerCached_.empty()) {
JAMI_DBG("[Account %s] loading DHT proxy URL", getAccountID().c_str());
JAMI_DBG("[Account %s] loading DHT proxy URL: %s", getAccountID().c_str(), proxyListUrl_.c_str());
if (proxyListUrl_.empty()) {
cb(getDhtProxyServer(proxyServer_));
} else {
......@@ -2473,7 +2481,6 @@ JamiAccount::loadCachedProxyServer(std::function<void(const std::string& proxy)>
std::chrono::hours(24 * 3),
[w=weak(), cb=std::move(cb)](const dht::http::Response& response){
if (auto sthis = w.lock()) {
sthis->proxyServerCached_.clear();
if (response.status_code == 200) {
cb(sthis->getDhtProxyServer(response.body));
} else {
......@@ -2491,7 +2498,6 @@ JamiAccount::loadCachedProxyServer(std::function<void(const std::string& proxy)>
std::string
JamiAccount::getDhtProxyServer(const std::string& serverList)
{
if (!proxyEnabled_) return {};
if (proxyServerCached_.empty()) {
std::vector<std::string> proxys;
// Split the list of servers
......@@ -2513,10 +2519,10 @@ JamiAccount::getDhtProxyServer(const std::string& serverList)
}
}
if (proxys.empty())
return {};
return {};
// Select one of the list as the current proxy.
auto randIt = proxys.begin();
std::advance(randIt, std::rand() % proxys.size());
std::advance(randIt, std::uniform_int_distribution<unsigned long>(0, proxys.size() - 1)(rand));
proxyServerCached_ = *randIt;
// Cache it!
fileutils::check_dir(cachePath_.c_str(), 0700);
......@@ -2527,7 +2533,6 @@ JamiAccount::getDhtProxyServer(const std::string& serverList)
file << proxyServerCached_;
else
JAMI_WARN("Cannot write into %s", proxyCachePath.c_str());
return proxyServerCached_;
}
return proxyServerCached_;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment