proxy: always use string tokens

This commit is contained in:
Adrien Béraud committed 2018-04-10 12:48:19 -04:00
1 parent 6a2f61a0db
commit 7b88a2122a
6 files changed
+64 -52

No files matched your search

+1 -1
View File
@@ -314,7 +314,7 @@ private:
struct Listener;
struct ProxySearch;
size_t listener_token_ {0};
size_t listenerToken_ {0};
std::map<InfoHash, ProxySearch> searches_;
mutable std::mutex searchLock_;
+1 -1
View File
@@ -239,7 +239,7 @@ private:
struct PushListener;
std::mutex lockPushListeners_;
std::map<std::string, PushListener> pushListeners_;
unsigned tokenPushNotif_ {0};
uint64_t tokenPushNotif_ {0};
void cancelPushListen(const std::string& pushToken, const InfoHash& key, unsigned token);
#endif //OPENDHT_PUSH_NOTIFICATIONS
+4
View File
@@ -996,6 +996,10 @@ unpackVector(const std::vector<std::shared_ptr<Value>>& vals) {
return ret;
}
#if OPENDHT_PROXY_SERVER || OPENDHT_PROXY_CLIENT
uint64_t unpackId(const Json::Value& json, const std::string& key);
#endif
}
MSGPACK_ADD_ENUM(dht::Value::Field)
+6 -6
View File
@@ -639,7 +639,7 @@ DhtProxyClient::doListen(const InfoHash& key, ValueCallback cb, Value::Filter fi
}
DHT_LOG.d(key, "[search %s] sending %s", key.to_c_str(), deviceKey_.empty() ? "listen" : "subscribe");
auto token = ++listener_token_;
auto token = ++listenerToken_;
auto l = search->second.listeners.find(token);
if (l == search->second.listeners.end()) {
auto f = filter;
@@ -719,8 +719,7 @@ DhtProxyClient::doListen(const InfoHash& key, ValueCallback cb, Value::Filter fi
auto* char_data = reinterpret_cast<const char*>(&body[0]);
if (reader->parse(char_data, char_data + body.size(), &json, &err)) {
if (!json.isMember("token")) return;
*pushNotifToken = json["token"].asLargestUInt();
*pushNotifToken = unpackId(json, "token");
} else {
state->ok = false;
}
@@ -926,14 +925,15 @@ DhtProxyClient::pushNotificationReceived(const std::map<std::string, std::string
#if OPENDHT_PUSH_NOTIFICATIONS
try {
std::lock_guard<std::mutex> lock(searchLock_);
auto token = std::stoul(notification.at("token"));
bool timeout = notification.find("timeout") == notification.cend();
uint64_t token = std::stoull(notification.at("token"));
for (auto& search: searches_) {
for (auto& list : search.second.listeners) {
auto& listener = list.second;
if (*listener.pushNotifToken!= token)
continue;
DHT_LOG.d(search.first, "[search %s] handling push notification", search.first.to_c_str());
if (notification.find("timeout") == notification.cend()) {
if (timeout) {
// Wake up daemon and get values
auto cb = listener.cb;
auto filter = listener.filter;
@@ -993,7 +993,7 @@ DhtProxyClient::resubscribe(const InfoHash& key, Listener& listener)
auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
if (reader->parse(char_data, char_data + body.size(), &json, &err)) {
if (!json.isMember("token")) return;
*pushNotifToken = json["token"].asLargestUInt();
*pushNotifToken = unpackId(json, "token");
}
} catch (std::runtime_error&) {
// NOTE: Http::close() can occurs here. Ignore this.
+5 -5
View File
@@ -341,12 +341,12 @@ DhtProxyServer::subscribe(const std::shared_ptr<restbed::Session>& session)
}
auto pushToken = root["key"].asString();
if (pushToken.empty()) return;
auto tokenFromReq = root.isMember("token") ? root["token"].asLargestUInt() : 0;
auto tokenFromReq = unpackId(root, "token");
auto platform = root["platform"].asString();
auto isAndroid = platform == "android";
auto clientId = root.isMember("client_id") ? root["client_id"].asString() : std::string();
auto token = 0;
uint64_t token = 0;
{
std::lock_guard<std::mutex> lock(lockListener_);
// Check if listener is already present and refresh timeout if launched
@@ -376,7 +376,7 @@ DhtProxyServer::subscribe(const std::shared_ptr<restbed::Session>& session)
// Build message content.
Json::Value json;
json["to"] = clientId;
json["token"] = token;
json["token"] = std::to_string(token);
sendPushNotification(pushToken, json, isAndroid);
return true;
}
@@ -389,7 +389,7 @@ DhtProxyServer::subscribe(const std::shared_ptr<restbed::Session>& session)
Json::Value json;
json["timeout"] = infoHash.toString();
json["to"] = clientId;
json["token"] = token;
json["token"] = std::to_string(token);
sendPushNotification(pushToken, json, isAndroid);
}
);
@@ -431,7 +431,7 @@ DhtProxyServer::unsubscribe(const std::shared_ptr<restbed::Session>& session)
}
auto pushToken = root["key"].asString();
if (pushToken.empty()) return;
auto token = std::stoull(root["token"].asString());
auto token = unpackId(root, "token");
if (token == 0) return;
cancelPushListen(pushToken, infoHash, token);
+47 -39
View File
@@ -175,45 +175,37 @@ Value::msgpack_unpack_body(const msgpack::object& o)
#if OPENDHT_PROXY_SERVER || OPENDHT_PROXY_CLIENT
Value::Value(Json::Value& json)
{
try {
if (json.isMember("id")) {
if (json["id"].isString()) {
id = Value::Id(std::stoull(json["id"].asString()));
} else {
id = Value::Id(json["id"].asLargestUInt());
}
}
} catch (...) { }
if (json.isMember("cypher")) {
auto cypherStr = json["cypher"].asString();
cypherStr = base64_decode(cypherStr);
cypher = std::vector<unsigned char>(cypherStr.begin(), cypherStr.end());
}
if (json.isMember("sig")) {
auto sigStr = json["sig"].asString();
sigStr = base64_decode(sigStr);
signature = std::vector<unsigned char>(sigStr.begin(), sigStr.end());
}
if (json.isMember("seq"))
seq = json["seq"].asInt();
if (json.isMember("owner")) {
auto ownerStr = json["owner"].asString();
auto ownerBlob = std::vector<unsigned char>(ownerStr.begin(), ownerStr.end());
owner = std::make_shared<const crypto::PublicKey>(ownerBlob);
}
if (json.isMember("to")) {
auto toStr = json["to"].asString();
recipient = InfoHash(toStr);
}
if (json.isMember("type"))
type = json["type"].asInt();
if (json.isMember("data")){
auto dataStr = json["data"].asString();
dataStr = base64_decode(dataStr);
data = std::vector<unsigned char>(dataStr.begin(), dataStr.end());
}
if (json.isMember("utype"))
user_type = json["utype"].asString();
id = Value::Id(unpackId(json, "id"));
if (json.isMember("cypher")) {
auto cypherStr = json["cypher"].asString();
cypherStr = base64_decode(cypherStr);
cypher = std::vector<unsigned char>(cypherStr.begin(), cypherStr.end());
}
if (json.isMember("sig")) {
auto sigStr = json["sig"].asString();
sigStr = base64_decode(sigStr);
signature = std::vector<unsigned char>(sigStr.begin(), sigStr.end());
}
if (json.isMember("seq"))
seq = json["seq"].asInt();
if (json.isMember("owner")) {
auto ownerStr = json["owner"].asString();
auto ownerBlob = std::vector<unsigned char>(ownerStr.begin(), ownerStr.end());
owner = std::make_shared<const crypto::PublicKey>(ownerBlob);
}
if (json.isMember("to")) {
auto toStr = json["to"].asString();
recipient = InfoHash(toStr);
}
if (json.isMember("type"))
type = json["type"].asInt();
if (json.isMember("data")){
auto dataStr = json["data"].asString();
dataStr = base64_decode(dataStr);
data = std::vector<unsigned char>(dataStr.begin(), dataStr.end());
}
if (json.isMember("utype"))
user_type = json["utype"].asString();
}
Json::Value
@@ -240,6 +232,22 @@ Value::toJson() const
}
return val;
}
uint64_t
unpackId(const Json::Value& json, const std::string& key) {
uint64_t ret = 0;
try {
if (json.isMember(key)) {
const auto& t = json[key];
if (t.isString()) {
ret = std::stoull(t.asString());
} else {
ret = t.asLargestUInt();
}
}
} catch (...) {}
return ret;
}
#endif //OPENDHT_PROXY_SERVER
bool