From 71dc907c4f7bac0cb099a3006e14b88842065e9a Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Thu, 28 Apr 2016 13:14:07 +0000 Subject: Tox: loading nodes from json git-svn-id: http://svn.miranda-ng.org/main/trunk@16788 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/tox_network.cpp | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'protocols/Tox/src/tox_network.cpp') diff --git a/protocols/Tox/src/tox_network.cpp b/protocols/Tox/src/tox_network.cpp index d3a88056b7..65934e533a 100644 --- a/protocols/Tox/src/tox_network.cpp +++ b/protocols/Tox/src/tox_network.cpp @@ -77,12 +77,58 @@ void CToxProto::BootstrapNodesFromIni(bool isIPv6) } } +void CToxProto::BootstrapNodesFromJson(bool isIPv6) +{ + char *json = NULL; + + ptrT path(mir_tstrdup((TCHAR*)VARST(_T(TOX_JSON_PATH)))); + // todo: download from https://nodes.tox.chat/json + + if (IsFileExists(path)) + { + FILE *hFile = _tfopen(path, L"r"); + if (hFile != NULL) + { + _fseeki64(hFile, 0, SEEK_END); + size_t size = _ftelli64(hFile); + json = (char*)mir_calloc(size); + rewind(hFile); + fread(json, sizeof(char), size, hFile); + fclose(hFile); + } + } + + if (json) + { + JSONNode root = JSONNode::parse(json); + if (!root.empty()) + { + JSONNode nodes = root.at("nodes").as_array(); + for (size_t i = 0; i < nodes.size(); i++) + { + JSONNode node = nodes[i]; + + JSONNode address = node.at("ipv4"); + int port = node.at("port").as_int(); + JSONNode pubKey = node.at("public_key"); + BootstrapNode(address.as_string().c_str(), port, pubKey.as_string().c_str()); + if (isIPv6) + { + address = node.at("ipv6"); + BootstrapNode(address.as_string().c_str(), port, pubKey.as_string().c_str()); + } + } + } + } +} + void CToxProto::BootstrapNodes() { logger->Log(__FUNCTION__": bootstraping DHT"); bool isIPv6 = getBool("EnableIPv6", 0); BootstrapNodesFromDb(isIPv6); BootstrapNodesFromIni(isIPv6); + BootstrapNodesFromJson(isIPv6); } void CToxProto::TryConnect() -- cgit v1.2.3