1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#include "stdafx.h"
void CToxProto::BootstrapUdpNode(Tox *tox, const char *address, int port, const char *hexKey)
{
if (address == nullptr || hexKey == nullptr)
return;
ToxBinAddress binKey(hexKey, TOX_PUBLIC_KEY_SIZE * 2);
TOX_ERR_BOOTSTRAP error;
if (!tox_bootstrap(tox, address, port, binKey, &error))
debugLogA(__FUNCTION__ ": failed to bootstrap node %s:%d \"%s\" (%d)", address, port, hexKey, error);
}
void CToxProto::BootstrapTcpRelay(Tox *tox, const char *address, int port, const char *hexKey)
{
if (address == nullptr || hexKey == nullptr)
return;
ToxBinAddress binKey(hexKey, TOX_PUBLIC_KEY_SIZE * 2);
TOX_ERR_BOOTSTRAP error;
if (!tox_add_tcp_relay(tox, address, port, binKey, &error))
debugLogA(__FUNCTION__ ": failed to add tcp relay %s:%d \"%s\" (%d)", address, port, hexKey, error);
}
void CToxProto::BootstrapNodesFromDb(Tox *tox, bool isIPv6)
{
char module[MAX_PATH];
mir_snprintf(module, "%s_Nodes", m_szModuleName);
int nodeCount = db_get_w(0, module, TOX_SETTINGS_NODE_COUNT, 0);
if (nodeCount == 0)
return;
char setting[MAX_PATH];
for (int i = 0; i < nodeCount; i++) {
mir_snprintf(setting, TOX_SETTINGS_NODE_IPV4, i);
ptrA address(db_get_sa(0, module, setting));
mir_snprintf(setting, TOX_SETTINGS_NODE_PORT, i);
int port = db_get_w(0, module, setting, 33445);
mir_snprintf(setting, TOX_SETTINGS_NODE_PKEY, i);
ptrA pubKey(db_get_sa(0, module, setting));
BootstrapUdpNode(tox, address, port, pubKey);
BootstrapTcpRelay(tox, address, port, pubKey);
if (isIPv6) {
mir_snprintf(setting, TOX_SETTINGS_NODE_IPV6, i);
address = db_get_sa(0, module, setting);
BootstrapUdpNode(tox, address, port, pubKey);
BootstrapTcpRelay(tox, address, port, pubKey);
}
}
}
void CToxProto::BootstrapNodesFromJson(Tox *tox, bool isIPv6)
{
VARSW path(TOX_JSON_PATH);
long lastUpdate = getDword("NodesUpdate", 0);
if (!IsFileExists(path) || lastUpdate < (now() - 86400 /* 24h */))
UpdateNodes();
JSONNode nodes = ParseNodes();
auto nodeCount = nodes.size();
if (nodeCount == 0)
return;
static int j = rand() % nodeCount;
int i = 0;
while (i < 2) {
JSONNode node = nodes[j % nodeCount];
bool udpStatus = node.at("status_udp").as_bool();
bool tcpStatus = node.at("status_tcp").as_bool();
if (!udpStatus && !tcpStatus) {
nodeCount = j - 1;
j = rand() % nodeCount;
if (j == 0)
return;
continue;
}
JSONNode address = node.at("ipv4");
JSONNode pubKey = node.at("public_key");
if (udpStatus) {
int port = node.at("port").as_int();
BootstrapUdpNode(tox, address.as_string().c_str(), port, pubKey.as_string().c_str());
if (isIPv6) {
address = node.at("ipv6");
BootstrapUdpNode(tox, address.as_string().c_str(), port, pubKey.as_string().c_str());
}
}
if (tcpStatus) {
JSONNode tcpPorts = node.at("tcp_ports").as_array();
for (size_t k = 0; k < tcpPorts.size(); k++) {
int port = tcpPorts[k].as_int();
BootstrapTcpRelay(tox, address.as_string().c_str(), port, pubKey.as_string().c_str());
if (isIPv6) {
address = node.at("ipv6");
BootstrapTcpRelay(tox, address.as_string().c_str(), port, pubKey.as_string().c_str());
}
}
}
j++;
i++;
}
}
void CToxProto::BootstrapNodes(Tox *tox)
{
debugLogA(__FUNCTION__": bootstraping DHT");
bool isIPv6 = getBool("EnableIPv6", 0);
BootstrapNodesFromJson(tox, isIPv6);
BootstrapNodesFromDb(tox, isIPv6);
}
void CToxProto::UpdateNodes()
{
VARSW path(TOX_JSON_PATH);
debugLogA(__FUNCTION__": updating nodes");
HttpRequest request(REQUEST_GET, "https://nodes.tox.chat/json");
NLHR_PTR response(request.Send(m_hNetlibUser));
if (!response || response->resultCode != HTTP_CODE_OK || !response->pData) {
debugLogA(__FUNCTION__": failed to dowload tox.json");
return;
}
JSONNode root = JSONNode::parse(response->pData);
if (root.empty()) {
debugLogA(__FUNCTION__": failed to dowload tox.json");
return;
}
DWORD lastUpdate = root.at("last_scan").as_int();
if (lastUpdate <= getDword("NodesUpdate", 0))
return;
FILE *hFile = _wfopen(path, L"w");
if (hFile == nullptr) {
debugLogA(__FUNCTION__": failed to open tox.json");
return;
}
if (fwrite(response->pData, sizeof(char), response->dataLength, hFile) != (size_t)response->dataLength)
debugLogA(__FUNCTION__": failed to write tox.json");
fclose(hFile);
setDword("NodesUpdate", lastUpdate);
}
JSONNode CToxProto::ParseNodes()
{
VARSW path(TOX_JSON_PATH);
if (!IsFileExists(path)) {
debugLogA(__FUNCTION__": could not find tox.json");
return JSONNode(JSON_ARRAY);
}
FILE *hFile = _wfopen(path, L"r");
if (hFile == nullptr) {
debugLogA(__FUNCTION__": failed to open tox.json");
return JSONNode(JSON_ARRAY);
}
_fseeki64(hFile, 0, SEEK_END);
size_t size = _ftelli64(hFile);
ptrA json((char*)mir_calloc(size));
rewind(hFile);
fread(json, sizeof(char), size, hFile);
fclose(hFile);
JSONNode root = JSONNode::parse(json);
if (root.empty()) {
debugLogA(__FUNCTION__": failed to parse tox.json");
return JSONNode(JSON_ARRAY);
}
return root.at("nodes").as_array();
}
|