-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.php
206 lines (173 loc) · 7.76 KB
/
init.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
<?php
// Include database configuration
include 'config_1.php';
// Create a new database connection
$conn = getDbConnection();
// Load URLs from the database
$urls = [];
$result = $conn->query("SELECT * FROM urls");
if ($result) {
while ($row = $result->fetch_assoc()) {
$urls[] = $row; // Add each URL row to the array
}
} else {
echo "Error loading URLs from database: " . $conn->error . "\n";
exit;
}
// Initialize variables
$currentUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // Get current page URL
$currentUrlWithoutFile = strtok($currentUrl, '?'); // Remove query string if any
$currentUrlWithoutFile = preg_replace('/\/[^\/]*$/', '', $currentUrlWithoutFile); // Remove filename from URL
// Function to verify bootnode
function verifyBootnode($bootnodeUrl) {
$verificationFile = rtrim($bootnodeUrl, '/') . '/19990904.thr';
$verificationResponse = @file_get_contents($verificationFile); // Suppress warnings
if ($verificationResponse === FALSE) {
return false; // Bootnode is offline or file not found
}
$verificationData = json_decode($verificationResponse, true);
if ($verificationData !== null &&
isset($verificationData[0]['Key']) &&
$verificationData[0]['Key'] === 'ALHAMDULILLAH' &&
isset($verificationData[0]['date']) &&
$verificationData[0]['date'] == 19990904) {
return true; // Bootnode verification successful
}
return false; // Verification failed
}
// Try each bootnode in the list until one works
$bootnodeFound = false;
foreach ($urls as $urlData) {
if (isset($urlData['bootnode'])) {
$bootnodeUrl = $urlData['url']; // Get the bootnode URL
// Check if this bootnode is valid
if (verifyBootnode($bootnodeUrl)) {
// Fetch blocks from this valid bootnode
$response = @file_get_contents(rtrim($bootnodeUrl, '/') . '/init_blocks.php'); // Suppress warnings
if ($response !== FALSE) {
$blocks = json_decode($response, true);
if ($blocks !== null) {
// Save blocks to the database instead of a file
foreach ($blocks as $block) {
// Prepare and bind the SQL statement for inserting blocks
$stmt = $conn->prepare("INSERT INTO blocks (publicKey, founderKey, donateKey, blockHash, previousBlockHash, integrityHash, timestamp, transactions) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
if ($stmt) {
// Assuming block has the following fields; adjust as necessary.
$stmt->bind_param(
"ssssssss",
$block['publicKey'],
$block['founderKey'],
$block['donateKey'],
$block['blockHash'],
$block['previousBlockHash'],
$block['integrityHash'],
$block['timestamp'],
json_encode($block['transactions']) // Assuming transactions is an array or object that can be JSON encoded
);
// Execute the prepared statement
if (!$stmt->execute()) {
echo "Error executing statement: " . $stmt->error . "\n";
}
$stmt->close();
} else {
echo "Error preparing statement: " . $conn->error . "\n";
}
}
echo "Blocks saved successfully from bootnode.\n";
$bootnodeFound = true; // Mark that we found a valid bootnode
break; // Exit after successful fetch from a valid bootnode
}
}
}
}
}
// If all bootnodes fail, select a random URL and fetch blocks from it
if (!$bootnodeFound && !empty($urls)) {
// Select a random URL from the list of URLs
$randomUrl = $urls[array_rand($urls)];
// Construct full URL with filename for random URL
$url = rtrim($randomUrl['url'], '/') . '/init_blocks.php';
// Fetch blocks from random URL
echo "Fetching blocks from random URL: {$url}\n";
$response = @file_get_contents($url); // Suppress warnings
// Check if response is valid
if ($response === FALSE) {
die('Error fetching blocks from both bootnodes and random URL.');
}
// Decode JSON response
$blocks = json_decode($response, true);
// Check if decoding was successful
if ($blocks === null) {
die('Error decoding JSON response from random URL.');
}
// Save blocks to the database instead of a file
// Save blocks to the database instead of a file
foreach ($blocks as $block) {
// Prepare and bind the SQL statement for inserting blocks
$stmt = $conn->prepare("INSERT INTO blocks (publicKey, founderKey, donateKey, blockHash, previousBlockHash, integrityHash, timestamp, transactions) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
if ($stmt) {
// Check if transactions exist and encode accordingly
$transactions = !empty($block['transactions']) ? json_encode($block['transactions']) : '[]'; // Use '[]' directly
// Perform find and replace to remove quotes if transactions is "[]"
if ($transactions === '"[]"') {
$transactions = '[]'; // Replace with an actual empty JSON array
}
// Assuming block has the following fields; adjust as necessary.
$stmt->bind_param(
"ssssssss",
$block['publicKey'],
$block['founderKey'],
$block['donateKey'],
$block['blockHash'],
$block['previousBlockHash'],
$block['integrityHash'],
$block['timestamp'],
$transactions // Use the correctly encoded transactions
);
// Execute the prepared statement
if (!$stmt->execute()) {
echo "Error executing statement: " . $stmt->error . "\n";
}
$stmt->close();
} else {
echo "Error preparing statement: " . $conn->error . "\n";
}
}
echo "Blocks saved successfully from random node.\n";
}
// Step 2: Send current URL to all nodes (including bootnodes and random nodes)
$successfulSends = 0;
$failedSends = 0;
foreach ($urls as $urlData) {
$urlToSendTo = rtrim($urlData['url'], '/') . '/receive_url.php'; // Ensure proper formatting of URL
// Prepare data to send
$dataToSend = [
"timestamp" => date(DATE_ISO8601),
"url" => rtrim($currentUrlWithoutFile, '/'), // Ensure no trailing slashes before sending
"valid" => true,
];
// Send current URL to each node (including bootnodes)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlToSendTo);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($dataToSend));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if ($result === FALSE) {
$failedSends++;
echo "Failed to send current URL to {$urlToSendTo}: " . curl_error($ch) . "\n"; // Log error message for debugging.
} else {
echo "Successfully sent current URL to {$urlToSendTo}.\n"; // Log success message.
$successfulSends++;
}
curl_close($ch);
}
// Display summary message for sending URLs
echo "Successfully sent current URL to {$successfulSends} rivers.\n";
if ($failedSends > 0) {
echo "Failed to send current URL to {$failedSends} rivers.\n";
}
// Close the database connection at the end of the script.
$conn->close();
?>