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/stdafx.h | 4 ++++ protocols/Tox/src/tox_network.cpp | 46 +++++++++++++++++++++++++++++++++++++++ protocols/Tox/src/tox_options.cpp | 43 ++++++++++++++++++++++++++++++++++++ protocols/Tox/src/tox_proto.h | 1 + 4 files changed, 94 insertions(+) (limited to 'protocols/Tox/src') diff --git a/protocols/Tox/src/stdafx.h b/protocols/Tox/src/stdafx.h index 9433293742..21a3f7fa2a 100644 --- a/protocols/Tox/src/stdafx.h +++ b/protocols/Tox/src/stdafx.h @@ -41,6 +41,7 @@ DEFINE_PROPERTYKEY(PKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0 #include #include #include +#include #include #include @@ -63,6 +64,8 @@ struct CToxProto; #include "tox_chatrooms.h" #include "tox_proto.h" +#include "http_request.h" + extern HINSTANCE g_hInstance; #define MODULE "Tox" @@ -75,6 +78,7 @@ extern HINSTANCE g_hInstance; #define TOX_MAX_CALLS 1 #define TOX_INI_PATH "%miranda_path%\\Plugins\\tox.ini" +#define TOX_JSON_PATH "%miranda_path%\\Plugins\\tox.json" #define TOX_SETTINGS_ID "ToxID" #define TOX_SETTINGS_DNS "DnsID" 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() diff --git a/protocols/Tox/src/tox_options.cpp b/protocols/Tox/src/tox_options.cpp index 7b1a89ad16..db44e8f1af 100644 --- a/protocols/Tox/src/tox_options.cpp +++ b/protocols/Tox/src/tox_options.cpp @@ -450,6 +450,49 @@ void CToxOptionsNodeList::OnInitDialog() } } + ptrT path(mir_tstrdup((TCHAR*)VARST(_T(TOX_JSON_PATH)))); + if (CToxProto::IsFileExists(path)) + { + char *json = NULL; + + 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]; + + TCHAR *ipv4 = mir_utf8decodeT(node.at("ipv4").as_string().c_str()); + iItem = m_nodes.AddItem(ipv4, -1, NULL, 0); + + TCHAR *ipv6 = mir_utf8decodeT(node.at("ipv6").as_string().c_str()); + if (mir_tstrcmp(ipv6, _T("-"))) + m_nodes.SetItem(iItem, 1, ipv6); + + TCHAR *port = mir_utf8decodeT(node.at("port").as_string().c_str()); + m_nodes.SetItem(iItem, 2, port); + + TCHAR *pubKey = mir_utf8decodeT(node.at("public_key").as_string().c_str()); + m_nodes.SetItem(iItem, 3, pubKey); + } + } + } + } + char module[MAX_PATH], setting[MAX_PATH]; mir_snprintf(module, "%s_Nodes", m_proto->m_szModuleName); int nodeCount = db_get_w(NULL, module, TOX_SETTINGS_NODE_COUNT, 0); diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 07f5b4b5f2..fa138d3c23 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -96,6 +96,7 @@ private: void BootstrapNode(const char *address, int port, const char *pubKey); void BootstrapNodesFromDb(bool isIPv6); void BootstrapNodesFromIni(bool isIPv6); + void BootstrapNodesFromJson(bool isIPv6); void BootstrapNodes(); void TryConnect(); void CheckConnection(int &retriesCount); -- cgit v1.2.3