15class OPENDHT_PUBLIC
SecureDht final :
public DhtInterface
18 typedef std::function<void(
bool)> SignatureCheckCallback;
22 static dht::Config getConfig(
const SecureDht::Config& conf)
24 auto c = conf.node_config;
25 if (not c.node_id and conf.id.second)
26 c.
node_id = InfoHash::get(
"node:" + conf.id.second->getId().toString());
38 IdentityAnnouncedCb iacb = {},
39 const std::shared_ptr<Logger>& l = {});
43 InfoHash getId()
const {
return key_ ? key_->getPublicKey().getId() : InfoHash(); }
44 PkId getLongId()
const {
return key_ ? key_->getPublicKey().getLongId() : PkId(); }
45 Sp<crypto::PublicKey> getPublicKey()
const {
return key_ ? key_->getSharedPublicKey() : Sp<crypto::PublicKey> {}; }
47 ValueType secureType(ValueType&& type);
49 ValueType secureType(
const ValueType& type)
51 ValueType tmp_type = type;
52 return secureType(std::move(tmp_type));
55 void registerType(
const ValueType& type)
override
58 dht_->registerType(secureType(type));
60 void registerType(ValueType&& type)
63 dht_->registerType(secureType(std::forward<ValueType>(type)));
65 void registerInsecureType(
const ValueType& type)
68 dht_->registerType(type);
76 void get(
const InfoHash&
id, GetCallback cb, DoneCallback donecb = {},
Value::Filter&& = {}, Where&& w = {})
override;
77 void get(
const InfoHash&
id,
79 DoneCallbackSimple donecb = {},
80 Value::Filter&& f = {},
81 Where&& w = {})
override
83 get(
id, cb, bindDoneCb(donecb), std::forward<Value::Filter>(f), std::forward<Where>(w));
85 void get(
const InfoHash& key,
87 DoneCallback donecb = {},
88 Value::Filter&& f = {},
89 Where&& w = {})
override
91 get(key, bindGetCb(cb), donecb, std::forward<Value::Filter>(f), std::forward<Where>(w));
93 void get(
const InfoHash& key,
95 DoneCallbackSimple donecb,
96 Value::Filter&& f = {},
97 Where&& w = {})
override
99 get(key, bindGetCb(cb), bindDoneCb(donecb), std::forward<Value::Filter>(f), std::forward<Where>(w));
105 void putSigned(
const InfoHash& hash, Sp<Value> val, DoneCallback callback,
bool permanent =
false);
106 void putSigned(
const InfoHash& hash,
Value&& v, DoneCallback callback,
bool permanent =
false)
108 putSigned(hash, std::make_shared<Value>(std::move(v)), callback, permanent);
117 const InfoHash& hash,
const InfoHash& to, Sp<Value> val, DoneCallback callback,
bool permanent =
false);
119 [[deprecated(
"Use the shared_ptr version instead")]]
120 void putEncrypted(
const InfoHash& hash,
const InfoHash& to,
Value&& v, DoneCallback callback,
bool permanent =
false)
122 putEncrypted(hash, to, std::make_shared<Value>(std::move(v)), callback, permanent);
124 void putEncrypted(
const InfoHash& hash,
127 DoneCallback callback,
128 bool permanent =
false);
130 [[deprecated(
"Use the shared_ptr version instead")]]
132 const InfoHash& hash,
const crypto::PublicKey& to,
Value&& v, DoneCallback callback,
bool permanent =
false)
134 putEncrypted(hash, to, std::make_shared<Value>(std::move(v)), callback, permanent);
137 void putEncrypted(
const InfoHash& hash,
const PkId& to, Sp<Value> val, DoneCallback callback,
bool permanent =
false);
148 void findCertificate(
const InfoHash& node,
const std::function<
void(
const Sp<crypto::Certificate>)>& cb);
149 void findPublicKey(
const InfoHash& node,
const std::function<
void(
const Sp<crypto::PublicKey>)>& cb);
151 void findCertificate(
const PkId&
id,
const std::function<
void(
const Sp<crypto::Certificate>)>& cb);
152 void findPublicKey(
const PkId&
id,
const std::function<
void(
const Sp<crypto::PublicKey>)>& cb);
154 void registerCertificate(
const Sp<crypto::Certificate>& cert);
156 Sp<crypto::Certificate> getCertificate(
const InfoHash& node)
const;
157 Sp<crypto::PublicKey> getPublicKey(
const InfoHash& node)
const;
159 Sp<crypto::Certificate> getCertificate(
const PkId& node)
const;
160 Sp<crypto::PublicKey> getPublicKey(
const PkId& node)
const;
169 localQueryMethodLegacy_ = std::move(query_method);
171 void setLocalCertificateStore(CertificateStoreQuery&& query_method) { localQueryMethod_ = std::move(query_method); }
172 void setOnPublicAddressChanged(PublicAddressChangedCb cb)
override { dht_->setOnPublicAddressChanged(cb); }
177 void shutdown(ShutdownCallback cb,
bool stop =
false)
override { dht_->shutdown(cb, stop); }
178 void dumpTables()
const override { dht_->dumpTables(); }
179 inline const InfoHash&
getNodeId()
const override {
return dht_->getNodeId(); }
181 std::pair<size_t, size_t>
getStoreSize()
const override {
return dht_->getStoreSize(); }
182 std::pair<size_t, size_t> getLocalStoreSize()
const override {
return dht_->getLocalStoreSize(); }
183 std::string getStorageLog()
const override {
return dht_->getStorageLog(); }
184 std::string getStorageLog(
const InfoHash& h)
const override {
return dht_->getStorageLog(h); }
186 size_t getStorageLimit()
const override {
return dht_->getStorageLimit(); }
188 size_t getLocalStorageLimit()
const override {
return dht_->getLocalStorageLimit(); }
190 std::vector<NodeExport>
exportNodes()
const override {
return dht_->exportNodes(); }
191 std::vector<ValuesExport> exportValues()
const override {
return dht_->exportValues(); }
192 void importValues(
const std::vector<ValuesExport>& v)
override { dht_->importValues(v); }
193 NodeStats getNodesStats(sa_family_t af)
const override {
return dht_->getNodesStats(af); }
195 NodeInfo getNodeInfo()
199 info.node_id = getNodeId();
200 info.ipv4 = getNodesStats(AF_INET);
201 info.ipv6 = getNodesStats(AF_INET6);
202 std::tie(info.storage_size, info.storage_values) = getStoreSize();
203 std::tie(info.local_storage_size, info.local_storage_values) = getLocalStoreSize();
204 if (
auto sock = getSocket()) {
205 info.bound4 = sock->getBoundRef(AF_INET).getPort();
206 info.bound6 = sock->getBoundRef(AF_INET6).getPort();
211 std::vector<unsigned> getNodeMessageStats(
bool in =
false)
override {
return dht_->getNodeMessageStats(in); }
212 std::string getRoutingTablesLog(sa_family_t af)
const override {
return dht_->getRoutingTablesLog(af); }
213 std::string getSearchesLog(sa_family_t af)
const override {
return dht_->getSearchesLog(af); }
214 std::string getSearchLog(
const InfoHash& h, sa_family_t af = AF_UNSPEC)
const override
216 return dht_->getSearchLog(h, af);
218 std::vector<SockAddr> getPublicAddress(sa_family_t family = 0)
override {
return dht_->getPublicAddress(family); }
219 time_point periodic(
const uint8_t* buf,
size_t buflen, SockAddr sa,
const time_point& now)
override
221 return dht_->periodic(buf, buflen, std::move(sa), now);
224 const uint8_t* buf,
size_t buflen,
const sockaddr* from, socklen_t fromlen,
const time_point& now)
override
226 return dht_->periodic(buf, buflen, from, fromlen, now);
229 NodeStatus getStatus(sa_family_t af)
const override {
return dht_->getStatus(af); }
230 NodeStatus getStatus()
const override {
return dht_->getStatus(); }
231 net::DatagramSocket* getSocket()
const override {
return dht_->getSocket(); };
232 bool isRunning(sa_family_t af = 0)
const override {
return dht_->isRunning(af); }
233 const ValueType& getType(ValueType::Id type_id)
const override {
return dht_->getType(type_id); }
234 void addBootstrap(
const std::string& host,
const std::string& service)
override
236 dht_->addBootstrap(host, service);
238 void clearBootstrap()
override { dht_->clearBootstrap(); }
240 void insertNode(
const NodeExport& n)
override { dht_->insertNode(n); }
241 void pingNode(SockAddr sa, DoneCallbackSimple&& cb = {})
override { dht_->pingNode(std::move(sa), std::move(cb)); }
242 void query(
const InfoHash& key, QueryCallback cb, DoneCallback done_cb = {},
Query&& q = {})
override
244 dht_->query(key, cb, done_cb, std::move(q));
246 void query(
const InfoHash& key, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query&& q = {})
override
248 dht_->query(key, cb, done_cb, std::move(q));
252 return dht_->getLocal(key, f);
254 Sp<Value>
getLocalById(
const InfoHash& key, Value::Id vid)
const override {
return dht_->getLocalById(key, vid); }
255 void put(
const InfoHash& key,
257 DoneCallback cb =
nullptr,
258 time_point created = time_point::max(),
259 bool permanent =
false)
override
261 dht_->put(key, v, cb, created, permanent);
263 void put(
const InfoHash& key,
265 DoneCallbackSimple cb,
266 time_point created = time_point::max(),
267 bool permanent =
false)
override
269 dht_->put(key, v, cb, created, permanent);
272 void put(
const InfoHash& key,
274 DoneCallback cb =
nullptr,
275 time_point created = time_point::max(),
276 bool permanent =
false)
override
278 dht_->put(key, std::move(v), cb, created, permanent);
280 void put(
const InfoHash& key,
282 DoneCallbackSimple cb,
283 time_point created = time_point::max(),
284 bool permanent =
false)
override
286 dht_->put(key, std::move(v), cb, created, permanent);
288 std::vector<Sp<Value>>
getPut(
const InfoHash& h)
const override {
return dht_->getPut(h); }
289 Sp<Value>
getPut(
const InfoHash& h,
const Value::Id& vid)
const override {
return dht_->getPut(h, vid); }
290 bool cancelPut(
const InfoHash& h,
const Value::Id& vid)
override {
return dht_->cancelPut(h, vid); }
292 size_t listen(
const InfoHash& key, ValueCallback,
Value::Filter = {}, Where = {})
override;
294 size_t listen(
const InfoHash& key, GetCallbackSimple cb, Value::Filter f = {}, Where w = {})
override
296 return listen(key, bindGetCb(cb), f, w);
298 bool cancelListen(
const InfoHash& h,
size_t token)
override {
return dht_->cancelListen(h, token); }
300 void connectivityChanged()
override { dht_->connectivityChanged(); }
302 void forwardAllMessages(
bool forward) { forward_all_ = forward; }
304 void setPushNotificationToken(
const std::string& token =
"")
override { dht_->setPushNotificationToken(token); }
312 return dht_->pushNotificationReceived(notification);
315 void setLogger(
const std::shared_ptr<Logger>& logger)
override
317 DhtInterface::setLogger(logger);
318 dht_->setLogger(logger);
327 dht_->setLogFilter(f);
331 std::unique_ptr<DhtInterface> dht_;
336 Sp<Value> checkValue(
const Sp<Value>& v);
337 ValueCallback getCallbackFilter(
const ValueCallback&,
Value::Filter&&);
338 GetCallback getCallbackFilter(
const GetCallback&,
Value::Filter&&);
340 Sp<crypto::Certificate> registerCertificate(
const InfoHash& node,
const Blob& cert);
341 Sp<crypto::Certificate> registerCertificate(
const PkId& node,
const Blob& cert);
343 Sp<crypto::PrivateKey> key_ {};
344 Sp<crypto::Certificate> certificate_ {};
347 CertificateStoreQuery localQueryMethod_ {};
348 CertificateStoreQueryLegacy localQueryMethodLegacy_ {};
351 std::map<InfoHash, Sp<crypto::Certificate>> nodesCertificates_ {};
352 std::map<InfoHash, Sp<crypto::PublicKey>> nodesPubKeys_ {};
353 std::map<PkId, Sp<crypto::Certificate>> nodesCertificatesLong_ {};
354 std::map<PkId, Sp<crypto::PublicKey>> nodesPubKeysLong_ {};
356 std::atomic_bool forward_all_ {
false};
357 bool enableCache_ {
false};