forked from TheBlueMatt/dnsseed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.php
301 lines (254 loc) · 9.5 KB
/
global.php
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/*
* These are the various backend functions that can be modified to support whatever backend you wish
*/
// MySQL/PowerDNS version
/*
// Functions used by bitcoin-scan.php and bitcoin-scan-net.php
function connect_to_db() {
global $db, $CONFIG;
try {
$db = new mysqli($CONFIG['MYSQL_HOST'], $CONFIG['MYSQL_USER'], $CONFIG['MYSQL_PASS'], $CONFIG['MYSQL_BITCOIN_DB']);
if ($db->connect_errno)
throw new Exception($db->connect_error);
if (empty($db))
throw new Exception("\$db is empty");
} catch (Exception $e) {
exit;
}
}
// Functions used only by bitcoin-scan.php
function start_db_transaction() {
}
function commit_db_transaction() {
}
function add_node_to_dns($ip, $version) {
global $db, $CONFIG;
if (empty($db))
connect_to_db();
if (!empty($ip) && ip2long($ip) != 0 && is_numeric($version) && $version > 0) {
$db->query("INSERT INTO `".$CONFIG['MYSQL_BITCOIN_TABLE']."` "
."(`ipv4`, `accepts_incoming`, `last_check`, `version`, `first_up`) VALUES "
."('" . ip2long($ip) . "' , b'1', NOW(), '" . $version . "', NOW());");
$db->query("UPDATE `".$CONFIG['MYSQL_BITCOIN_TABLE']."` SET "
."`accepts_incoming` = b'1', "
."`last_check` = NOW(), "
."`version` = '" . $version . "', "
."`first_up` = IF(`first_up` IS NULL, NOW(), `first_up`) "
."WHERE `ipv4` = '" . ip2long($ip) . "' AND `port` = '8333';");
}
if (!empty($ip) && ip2long($ip) != 0 && $version >= $CONFIG['MIN_VERSION'])
$db->query("INSERT INTO `".$CONFIG['MYSQL_PDNS_DB']."`.`".$CONFIG['MYSQL_PDNS_RECORDS_TABLE']."` "
."(`domain_id`, `name`, `type`, `content`, `ttl`, `prio`, `change_date`) VALUES "
."('" . $CONFIG['PDNS_DOMAIN_ID'] . "', '" . $CONFIG['DOMAIN_NAME'] . "', 'A', '" . $ip . "', '" . $CONFIG['PDNS_RECORD_TTL'] . "', '0', '" . date("Ymd") . "00');");
}
function add_untested_node($ip, $port) {
global $db, $CONFIG;
if (empty($db))
connect_to_db();
if (!empty($ip) && ip2long($ip) != 0 && !empty($port) && is_numeric($port) && $port != 0) {
$db->query("INSERT INTO `".$CONFIG['MYSQL_BITCOIN_TABLE']."` "
."(`ipv4`, `port`) VALUES "
."('" . ip2long($ip) . "', '" . $port . "');");
$db->query("UPDATE `".$CONFIG['MYSQL_BITCOIN_TABLE']."` SET "
."`last_seen` = NOW() WHERE "
."`ipv4` = '" . ip2long($ip) . "' AND "
."`port` = '" . $port . "';");
}
}
function remove_node($ip, $port) {
global $db, $CONFIG;
if (empty($db))
connect_to_db();
if (!empty($ip) && ip2long($ip) != 0 && !empty($port) && is_numeric($port) && $port != 0)
$db->query("UPDATE `".$CONFIG['MYSQL_BITCOIN_TABLE']."` SET "
."`last_check` = NOW(), "
."`accepts_incoming` = b'0' WHERE "
."`ipv4` = '" . ip2long($ip) . "' AND "
."`port` = '" . $port . "';");
if (!empty($ip) && ip2long($ip) != 0)
$db->query("DELETE FROM `".$CONFIG['MYSQL_PDNS_DB']."`.`".$CONFIG['MYSQL_PDNS_RECORDS_TABLE']."` WHERE "
."`domain_id` = '" . $CONFIG['PDNS_DOMAIN_ID'] . "' AND "
."`name` = '" . $CONFIG['DOMAIN_NAME'] . "' AND "
."`type` = 'A' AND "
."`content` = '" . $ip . "' AND "
."`ttl` = '" . $CONFIG['PDNS_RECORD_TTL'] . "' AND "
."`prio` = '0';");
}
// Functions used only by bitcoin-scan-net.php
function scan_node($ip, $port) {
exec("nohup ./bitcoin-scan.php ".long2ip($ip).":".$port." > /dev/null 2>/dev/null &");
}
function query_unchecked() {
global $db, $CONFIG;
return $result = $db->query("SELECT `ipv4`, `port` FROM `".$CONFIG['MYSQL_BITCOIN_TABLE']."` WHERE `last_check` IS NULL;");
}
function query_unaccepting() {
global $db, $CONFIG;
return $db->query("SELECT `ipv4`, `port` FROM `".$CONFIG['MYSQL_BITCOIN_TABLE']."` WHERE `last_check` < NOW() - INTERVAL " . $CONFIG['UNACCEP_CHECK_RATE'] . " SECOND AND `accepts_incoming` = b'0' ORDER BY `last_check` ASC;");
}
function query_accepting() {
global $db, $CONFIG;
return $db->query("SELECT `ipv4`, `port` FROM `".$CONFIG['MYSQL_BITCOIN_TABLE']."` WHERE `last_check` < NOW() - INTERVAL " . $CONFIG['ACCEP_CHECK_RATE'] . " SECOND AND `accepts_incoming` = b'1' ORDER BY `last_check` ASC;");
}
function init_results($result) {
return $result;
}
function get_count_of_results($result) {
return $result->num_rows;
}
function get_assoc_result_row($result) {
return $result->fetch_assoc();
}
function prune_nodes() {
global $db, $CONFIG;
$db->query("DELETE FROM `".$CONFIG['MYSQL_BITCOIN_TABLE']."` WHERE `last_seen` < NOW() - INTERVAL " . $CONFIG['PURGE_AGE'] . " SECOND AND `accepts_incoming` = b'0';");
}*/
// SQLite/BIND version
// Functions used by bitcoin-scan.php and bitcoin-scan-net.php
function connect_to_db() {
global $db, $CONFIG;
$db = new SQLite3($CONFIG['SQLITE_FILE']);
if (empty($db))
die("\$db is empty");
$db->busyTimeout(5000);
$db->exec("CREATE TABLE IF NOT EXISTS nodes (
ipv4 INT NOT NULL,
port INT NOT NULL DEFAULT 8333,
last_check INT DEFAULT NULL,
accepts_incoming INT NOT NULL DEFAULT 0,
version INT DEFAULT NULL,
last_seen INT NOT NULL,
first_up INT DEFAULT NULL,
PRIMARY KEY (ipv4,port)
);
CREATE INDEX IF NOT EXISTS last_seen ON nodes(last_seen);
CREATE INDEX IF NOT EXISTS last_check ON nodes(last_check);
CREATE INDEX IF NOT EXISTS ipv4_port ON nodes(ipv4, port);");
}
// Functions used only by bitcoin-scan.php
function start_db_transaction() {
global $transaction_open, $db, $CONFIG;
if (empty($db))
connect_to_db();
if (!isset($transaction_open))
$transaction_open = false;
if (!$transaction_open) {
if (!$db->exec("BEGIN TRANSACTION;"))
die ("Transaction create failed");
}
$transaction_open = true;
}
function commit_db_transaction() {
global $transaction_open, $db, $CONFIG;
if (empty($db))
connect_to_db();
if ($transaction_open)
@$db->exec("COMMIT TRANSACTION;");
$transaction_open = false;
}
function add_node_to_dns($ip, $version) {
global $db, $CONFIG;
if (empty($db))
connect_to_db();
if (!empty($ip) && ip2long($ip) != 0 && is_numeric($version) && $version > 0) {
@$db->exec("INSERT INTO nodes "
."(ipv4, accepts_incoming, last_check, version, last_seen, first_up) VALUES "
."(" . ip2long($ip) . " , 1, ".time().", " . $version . ",".time().", ".time().");");
$db->exec("UPDATE nodes SET "
."accepts_incoming = 1, "
."last_check = ".time().", "
."version = " . $version . ", "
."last_seen = ".time().", "
."first_up = CASE WHEN first_up > 0 THEN first_up ELSE ".time()." END "
."WHERE ipv4 = " . ip2long($ip) . " AND port = 8333;");
}
}
function add_untested_node($ip, $port) {
global $db, $CONFIG;
if (empty($db))
connect_to_db();
if (!empty($ip) && ip2long($ip) != 0 && !empty($port) && is_numeric($port) && $port != 0) {
@$db->exec("INSERT INTO nodes "
."(ipv4, port, last_seen) VALUES "
."(" . ip2long($ip) . ", " . $port . ", ".time().");");
$db->exec("UPDATE nodes SET "
."last_seen = " . time() . " WHERE "
."ipv4 = " . ip2long($ip) . " AND "
."port = " . $port . ";");
}
}
function remove_node($ip, $port) {
global $db, $CONFIG;
if (empty($db))
connect_to_db();
if (!empty($ip) && ip2long($ip) != 0 && !empty($port) && is_numeric($port) && $port != 0)
$db->exec("UPDATE nodes SET "
."last_check = " . time() . ", "
."accepts_incoming = 0 WHERE "
."ipv4 = " . ip2long($ip) . " AND "
."port = " . $port . ";");
}
// Functions used only by bitcoin-scan-net.php
function scan_node($ip, $port) {
exec("nohup ./bitcoin-scan.php ".long2ip($ip).":".$port." > /dev/null 2>/dev/null &");
}
function query_unchecked() {
global $db, $CONFIG;
return $result = $db->query("SELECT ipv4, port FROM nodes WHERE last_check IS NULL;");
}
function query_unaccepting() {
global $db, $CONFIG;
$check_time = time() - $CONFIG['UNACCEP_CHECK_RATE'];
if ($CONFIG['MIN_UP_TIME_TO_CHECK'] != 0) {
$up_time = time() - $CONFIG['MIN_UP_TIME_TO_CHECK'];
return $db->query("SELECT ipv4, port FROM nodes WHERE last_check < " . $check_time . " AND accepts_incoming = 0 AND first_up <= " . $up_time . " ORDER BY last_check ASC;");
}else{
return $db->query("SELECT ipv4, port FROM nodes WHERE last_check < " . $check_time . " AND accepts_incoming = 0 ORDER BY last_check ASC;");
}
}
function init_results($result) {
$rows = array();
$row = $result->fetchArray(SQLITE3_ASSOC);
while (!empty($row)) {
$rows[] = $row;
$row = $result->fetchArray(SQLITE3_ASSOC);
}
$result->finalize();
return $rows;
}
function query_accepting() {
global $db, $CONFIG;
$current_time = time() - $CONFIG['ACCEP_CHECK_RATE'];
return $db->query("SELECT ipv4, port FROM nodes WHERE last_check < " . $current_time . " AND accepts_incoming = 1 ORDER BY last_check ASC;");
}
function get_count_of_results($result) {
return count($result);
}
function get_assoc_result_row(&$result) {
return array_shift($result);
}
function prune_nodes() {
global $db, $CONFIG;
$current_time = time() - $CONFIG['PURGE_AGE'];
$db->exec("DELETE FROM nodes WHERE last_seen < " . $current_time . " AND accepts_incoming = 0;");
}
// Functions used only by fill-dns.php
function get_list_of_nodes_for_dns() {
global $db, $CONFIG;
return $db->query("SELECT ipv4 FROM nodes WHERE accepts_incoming = 1 AND port = 8333 AND version >= ".$CONFIG['MIN_VERSION']." ORDER BY last_check DESC LIMIT 20;");
}
// Functions used only by count-nodes.php
function query_version_count() {
global $db, $CONFIG;
return $db->query("SELECT COUNT(*), version FROM nodes WHERE accepts_incoming = 1 AND port = 8333 GROUP BY version ORDER BY version;");
}
function query_dns_total() {
global $db, $CONFIG;
return $db->query("SELECT COUNT(*) FROM nodes WHERE accepts_incoming = 1 AND port = 8333 AND version >= ".$CONFIG['MIN_VERSION'].";");
}
function query_total() {
global $db, $CONFIG;
return $db->query("SELECT COUNT(*) FROM nodes;");
}
?>