// Send a heartbeat to all reserve peers in our list to make sure they are still online $sql = "SELECT * FROM `new_peers_list`"; $sql_result = mysql_query($sql); $sql_num_results = mysql_num_rows($sql_result); for ($i = 0; $i < $sql_num_results; $i++) { $sql_row = mysql_fetch_array($sql_result); if (rand(1, 3) == 2) { $ip_address = $sql_row["IP_Address"]; $domain = $sql_row["domain"]; $subfolder = $sql_row["subfolder"]; $port_number = $sql_row["port_number"]; $poll_failures = $sql_row["poll_failures"]; //Send a challenge hash to see if a timekoin server is active $poll_challenge = rand(1, 999999); $hash_solution = hash('crc32', $poll_challenge); $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 10, "peerlist.php?action=poll&challenge={$poll_challenge}"); if ($poll_peer == $hash_solution) { //Got a response from an active Timekoin server $sql = "UPDATE `new_peers_list` SET `poll_failures` = 0 WHERE `IP_Address` = '{$ip_address}' AND `domain` = '{$domain}' AND `subfolder` = '{$subfolder}' AND `port_number` = {$port_number} LIMIT 1"; mysql_query($sql); } else { //No response, record polling failure for future reference $poll_failures++; $sql = "UPDATE `new_peers_list` SET `poll_failures` = '{$poll_failures}' WHERE `IP_Address` = '{$ip_address}' AND `domain` = '{$domain}' AND `subfolder` = '{$subfolder}' AND `port_number` = {$port_number} LIMIT 1"; mysql_query($sql); } } // End Randomize Check } // End for Loop // Clean up reserve peer list by removing those that have no responded for over 30 poll attempts
$sql_row2 = mysql_fetch_array($sql_result2); $ip_address = $sql_row2["IP_Address"]; $domain = $sql_row2["domain"]; $subfolder = $sql_row2["subfolder"]; $port_number = $sql_row2["port_number"]; $qhash = $timestamp . base64_encode($public_key) . $crypt1 . $crypt2 . $crypt3 . $hash_check . $attribute; $qhash = hash('md5', $qhash); // Create map with request parameters $params = array('timestamp' => $timestamp, 'public_key' => base64_encode($public_key), 'crypt_data1' => $crypt1, 'crypt_data2' => $crypt2, 'crypt_data3' => $crypt3, 'hash' => $hash_check, 'attribute' => $attribute, 'qhash' => $qhash); // Build Http query using params $query = http_build_query($params); // Create Http context details $contextData = array('method' => 'POST', 'header' => "Connection: close\r\n" . "Content-Length: " . strlen($query) . "\r\n", 'content' => $query); // Create context resource for our request $context = stream_context_create(array('http' => $contextData)); $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 5, "queueclerk.php?action=input_transaction", $context); if ($poll_peer == "OK") { // Insert to the peer remotely was accepted switch ($attribute) { case "G": write_log("Timekoin Currency Generation Broadcast Accepted by remote Peer {$ip_address}{$domain}:{$port_number}/{$subfolder}", "G"); modify_peer_grade($ip_address, $domain, $subfolder, $port_number, -3); break; case "T": write_log("Standard Transaction Broadcast Accepted by remote Peer {$ip_address}{$domain}:{$port_number}/{$subfolder}", "T"); modify_peer_grade($ip_address, $domain, $subfolder, $port_number, -2); break; } } else { if ($poll_peer == "DUP") { // Insert to the peer, transaction is already there
function initialization_database() { // Clear IP Activity and Banlist for next start mysql_query("TRUNCATE TABLE `ip_activity`"); mysql_query("TRUNCATE TABLE `ip_banlist`"); // Clear Active & New Peers List mysql_query("DELETE FROM `active_peer_list` WHERE `active_peer_list`.`join_peer_list` != 0"); // Permanent Peers Ignored mysql_query("TRUNCATE TABLE `new_peers_list`"); // Record when started mysql_query("UPDATE `options` SET `field_data` = '" . time() . "' WHERE `options`.`field_name` = 'timekoin_start_time' LIMIT 1"); //************************************** // Upgrade Database from v3.x earlier versions // Auto IP Update Settings $new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'auto_update_generation_IP' LIMIT 1"), 0, 0); if ($new_record_check === FALSE) { // Does not exist, create it mysql_query("INSERT INTO `options` (`field_name` ,`field_data`) VALUES ('auto_update_generation_IP', '0')"); } // CLI Mode Settings $new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'cli_mode' LIMIT 1"), 0, 0); if ($new_record_check === FALSE) { // Does not exist, create it mysql_query("INSERT INTO `options` (`field_name` ,`field_data`) VALUES ('cli_mode', '1')"); } // CLI Mode Port Settings $new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'cli_port' LIMIT 1"), 0, 0); if ($new_record_check === FALSE) { // Does not exist, create it mysql_query("INSERT INTO `options` (`field_name` ,`field_data`) VALUES ('cli_port', '')"); } // IPv4 + IPv6 Network Mode $new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'network_mode' LIMIT 1"), 0, 0); if ($new_record_check === FALSE) { // Does not exist, create it mysql_query("INSERT INTO `options` (`field_name` ,`field_data`) VALUES ('network_mode', '1')"); } // IPv6 Generation IP Field $new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'generation_IP_v6' LIMIT 1"), 0, 0); if ($new_record_check === FALSE) { // Does not exist, create it mysql_query("INSERT INTO `options` (`field_name` ,`field_data`) VALUES ('generation_IP_v6', '')"); } //************************************** // Check for an empty generation IP address, // if none exist, attempt to auto-detect one // and fill in the field. $poll_IP = mysql_result(mysql_query("SELECT field_data FROM `options` WHERE `field_name` = 'generation_IP' LIMIT 1"), 0, 0); if (empty($poll_IP) == TRUE) { ini_set('user_agent', 'Timekoin Server (Main) v' . TIMEKOIN_VERSION); ini_set('default_socket_timeout', 3); // Timeout for request in seconds $poll_IP = filter_sql(poll_peer(NULL, 'timekoin.net', NULL, 80, 46, "ipv4.php")); if (empty($poll_IP) == FALSE) { mysql_query("UPDATE `options` SET `field_data` = '{$poll_IP}' WHERE `options`.`field_name` = 'generation_IP' LIMIT 1"); } } //************************************** // Main Loop Status & Active Options Setup // Truncate to Free RAM mysql_query("TRUNCATE TABLE `main_loop_status`"); $time = time(); //************************************** mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('balance_last_heartbeat', '1')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('foundation_last_heartbeat', '1')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('generation_last_heartbeat', '1')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('genpeer_last_heartbeat', '1')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('main_heartbeat_active', '0')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('main_last_heartbeat', '{$time}')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('peerlist_last_heartbeat', '1')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('queueclerk_last_heartbeat', '1')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('transclerk_last_heartbeat', '1')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('treasurer_last_heartbeat', '1')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('watchdog_heartbeat_active', '0')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('watchdog_last_heartbeat', '{$time}')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('peer_transaction_start_blocks', '1')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('peer_transaction_performance', '10')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('block_check_back', '1')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('block_check_start', '0')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('firewall_blocked_peer', '0')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('foundation_block_check', '0')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('foundation_block_check_end', '0')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('foundation_block_check_start', '0')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('generation_peer_list_no_sync', '0')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('no_peer_activity', '0')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('time_sync_error', '0')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('transaction_history_block_check', '0')"); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('update_available', '0')"); //************************************** // Copy values from Database to RAM Database $db_to_RAM = mysql_result(mysql_query("SELECT field_data FROM `options` WHERE `field_name` = 'allow_ambient_peer_restart' LIMIT 1"), 0, 0); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('allow_ambient_peer_restart', '{$db_to_RAM}')"); $db_to_RAM = mysql_result(mysql_query("SELECT field_data FROM `options` WHERE `field_name` = 'allow_LAN_peers' LIMIT 1"), 0, 0); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('allow_LAN_peers', '{$db_to_RAM}')"); $db_to_RAM = mysql_result(mysql_query("SELECT field_data FROM `options` WHERE `field_name` = 'server_request_max' LIMIT 1"), 0, 0); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('server_request_max', '{$db_to_RAM}')"); $db_to_RAM = mysql_result(mysql_query("SELECT field_data FROM `options` WHERE `field_name` = 'max_active_peers' LIMIT 1"), 0, 0); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('max_active_peers', '{$db_to_RAM}')"); $db_to_RAM = mysql_result(mysql_query("SELECT field_data FROM `options` WHERE `field_name` = 'max_new_peers' LIMIT 1"), 0, 0); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('max_new_peers', '{$db_to_RAM}')"); $db_to_RAM = mysql_result(mysql_query("SELECT field_data FROM `options` WHERE `field_name` = 'trans_history_check' LIMIT 1"), 0, 0); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('trans_history_check', '{$db_to_RAM}')"); $db_to_RAM = mysql_result(mysql_query("SELECT field_data FROM `options` WHERE `field_name` = 'super_peer' LIMIT 1"), 0, 0); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('super_peer', '{$db_to_RAM}')"); $db_to_RAM = mysql_result(mysql_query("SELECT field_data FROM `options` WHERE `field_name` = 'perm_peer_priority' LIMIT 1"), 0, 0); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('perm_peer_priority', '{$db_to_RAM}')"); $db_to_RAM = mysql_result(mysql_query("SELECT field_data FROM `options` WHERE `field_name` = 'auto_update_generation_IP' LIMIT 1"), 0, 0); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('auto_update_generation_IP', '{$db_to_RAM}')"); $db_to_RAM = mysql_result(mysql_query("SELECT field_data FROM `options` WHERE `field_name` = 'peer_failure_grade' LIMIT 1"), 0, 0); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('peer_failure_grade', '{$db_to_RAM}')"); $db_to_RAM = mysql_result(mysql_query("SELECT field_data FROM `options` WHERE `field_name` = 'network_mode' LIMIT 1"), 0, 0); mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('network_mode', '{$db_to_RAM}')"); //************************************** return 0; }
if (hash('md5', $queue_hash_test) == $current_hash) { // This Transaction Already Exist in the Queue $hash_match = TRUE; break; } else { // No match, continue searching $hash_match = NULL; } // No match, move on to next record $queue_hash_test = NULL; } } } if (empty($hash_match) == TRUE) { // This peer has a different transaction, ask for the full details of it $poll_hash = filter_sql(poll_peer($ip_address, $domain, $subfolder, $port_number, 1500, "queueclerk.php?action=transaction&number={$current_hash}")); $transaction_timestamp = intval(find_string("-----timestamp=", "-----public_key", $poll_hash)); $transaction_public_key = find_string("-----public_key=", "-----crypt1", $poll_hash); $transaction_crypt1 = find_string("-----crypt1=", "-----crypt2", $poll_hash); $transaction_crypt2 = find_string("-----crypt2=", "-----crypt3", $poll_hash); $transaction_crypt3 = find_string("-----crypt3=", "-----hash", $poll_hash); $transaction_hash = find_string("-----hash=", "-----attribute", $poll_hash); $transaction_attribute = find_string("-----attribute=", "-----end", $poll_hash); $transaction_qhash = find_string("---qhash=", "---endqhash", $poll_hash); // If a qhash is included, use this to verify the data if (empty($transaction_qhash) == FALSE) { $qhash = $transaction_timestamp . $transaction_public_key . $transaction_crypt1 . $transaction_crypt2 . $transaction_crypt3 . $transaction_hash . $transaction_attribute; $qhash = hash('md5', $qhash); // Compare hashes to make sure data is intact if ($transaction_qhash != $qhash) { write_log("Queue Hash Data MisMatch for Public Key: " . $transaction_public_key, "QC");
} if (rand(1, 4) == 2) { // Poll a random block from a random peer for random accuracy :) // Within the range of the current foundation block to now $current_foundation_block = foundation_cycle(0, TRUE) * 500; $random_block = rand($current_foundation_block, transaction_cycle(-1, TRUE)); $sql = perm_peer_mode(); $sql_result = mysql_query($sql); $sql_num_results = mysql_num_rows($sql_result); if ($sql_num_results > 0) { $sql_row = mysql_fetch_array($sql_result); $ip_address = $sql_row["IP_Address"]; $domain = $sql_row["domain"]; $subfolder = $sql_row["subfolder"]; $port_number = $sql_row["port_number"]; $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 65, "transclerk.php?action=block_hash&block_number={$random_block}"); if (empty($poll_peer) == TRUE) { // Add failure points to the peer in case further issues modify_peer_grade($ip_address, $domain, $subfolder, $port_number, 4); } if (empty($poll_peer) == FALSE && strlen($poll_peer) > 60) { // Do a real hash compare $current_generation_block = transaction_cycle(0, TRUE); $time1 = transaction_cycle(0 - $current_generation_block + $random_block); $time2 = transaction_cycle(0 - $current_generation_block + 1 + $random_block); $sql = "SELECT hash FROM `transaction_history` WHERE `timestamp` >= {$time1} AND `timestamp` < {$time2} ORDER BY `timestamp`, `hash`"; $sql_result = mysql_query($sql); $sql_num_results = mysql_num_rows($sql_result); $random_hash_build = 0; if ($sql_num_results > 0) { for ($i = 0; $i < $sql_num_results; $i++) {
$transaction_info = tk_decrypt($public_key, base64_decode($crypt1)); if ($transaction_info == $crypt2) { // Check the IP/Domain field and poll the IP to see if // there is a valid Timekoin server at the address. $crypt3_data = tk_decrypt($public_key, base64_decode($crypt3)); $peer_ip = find_string("---ip=", "---domain", $crypt3_data); $peer_domain = find_string("---domain=", "---subfolder", $crypt3_data); $peer_subfolder = find_string("---subfolder=", "---port", $crypt3_data); $peer_port_number = find_string("---port=", "---end", $crypt3_data); $delete_request = find_string("---end=", "---end2", $crypt3_data); // Check if IP is already in the generation peer list $IP_exist1 = mysql_result(mysql_query("SELECT * FROM `generating_peer_list` WHERE `IP_Address` = '{$peer_ip}' LIMIT 1"), 0, 1); // Calculate public key half-crypt-hash $arr1 = str_split($public_key, 181); // Poll the address that was encrypted to check for valid Timekoin server $gen_key_crypt = base64_decode(poll_peer($peer_ip, $peer_domain, $peer_subfolder, $peer_port_number, 256, "genpeer.php?action=gen_key_crypt")); $gen_key_crypt = tk_decrypt($public_key, $gen_key_crypt); $domain_fail = FALSE; // Reset Variable if (empty($peer_domain) == FALSE) { // Check if the hostname and IP fields actually match // and not made up or unrelated. $dns_ip = gethostbyname($peer_domain); if ($dns_ip != $peer_ip) { // No match between Domain IP and Encoded IP $domain_fail = TRUE; } else { $domain_fail = FALSE; } } // Does the public key half match what is encrypted in the 3rd crypt field from
if ($hash == $current_foundation_hash) { write_log("Transaction Foundation #{$rand_block} checks out OK", "FO"); $repair_block = FALSE; } else { write_log("Transaction Foundation #{$rand_block} did NOT pass verification test. Transactions in this Foundation will be repaired.", "FO"); $repair_block = TRUE; } } else { // Check foundation hash with peers for ($i = 0; $i < $sql_num_results; $i++) { $sql_row = mysql_fetch_array($sql_result); $ip_address = $sql_row["IP_Address"]; $domain = $sql_row["domain"]; $subfolder = $sql_row["subfolder"]; $port_number = $sql_row["port_number"]; $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 65, "foundation.php?action=block_hash&block_number={$rand_block}"); if (empty($poll_peer) == TRUE) { // Add failure points to the peer in case further issues modify_peer_grade($ip_address, $domain, $subfolder, $port_number, 4); } if ($current_foundation_hash === $poll_peer) { $foundation_hash_match++; } else { if (empty($poll_peer) == FALSE && strlen($poll_peer) > 60) { $foundation_hash_different++; } else { // Polling Errors can cause false corruption assumptions $poll_errors++; } } }
//Send a challenge hash to see if a timekoin server is active $poll_challenge = rand(1, 999999); $hash_solution = hash('crc32', $poll_challenge); $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 10, "peerlist.php?action=poll&challenge={$poll_challenge}"); if ($poll_peer == $hash_solution) { //Got a response from an active Timekoin server $poll_failures--; mysql_query("UPDATE `new_peers_list` SET `poll_failures` = {$poll_failures} WHERE `IP_Address` = '{$ip_address}' AND `domain` = '{$domain}' AND `subfolder` = '{$subfolder}' AND `port_number` = {$port_number} LIMIT 1"); } else { //No response, record polling failure for future reference $poll_failures++; mysql_query("UPDATE `new_peers_list` SET `poll_failures` = '{$poll_failures}' WHERE `IP_Address` = '{$ip_address}' AND `domain` = '{$domain}' AND `subfolder` = '{$subfolder}' AND `port_number` = {$port_number} LIMIT 1"); } } else { // Is the server full to capacity with peers? $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 10, "peerlist.php?action=join"); if ($poll_peer == "FULL") { //Server is full, ramp up failure points to get it purged quicker $poll_failures += 10; mysql_query("UPDATE `new_peers_list` SET `poll_failures` = '{$poll_failures}' WHERE `IP_Address` = '{$ip_address}' AND `domain` = '{$domain}' AND `subfolder` = '{$subfolder}' AND `port_number` = {$port_number} LIMIT 1"); } else { if ($poll_peer == "OK") { //Got a response from an active Timekoin server that is not full to capacity yet $poll_failures -= 5; mysql_query("UPDATE `new_peers_list` SET `poll_failures` = {$poll_failures} WHERE `IP_Address` = '{$ip_address}' AND `domain` = '{$domain}' AND `subfolder` = '{$subfolder}' AND `port_number` = {$port_number} LIMIT 1"); } } } } // End Randomize Check }
<tr><th>Peer</th><th>My Failure Score</th></tr>'; $my_domain = my_domain(); $my_subfolder = my_subfolder(); $my_port = my_port_number(); // Polling what the active peers have $sql = "SELECT * FROM `active_peer_list`"; $sql_result = mysql_query($sql); $sql_num_results = mysql_num_rows($sql_result); for ($i = 0; $i < $sql_num_results; $i++) { $sql_row = mysql_fetch_array($sql_result); $ip_address = $sql_row["IP_Address"]; $domain = $sql_row["domain"]; $subfolder = $sql_row["subfolder"]; $port_number = $sql_row["port_number"]; // Poll and give my domain to check against $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 5, "peerlist.php?action=poll_failure&domain={$my_domain}&subfolder={$my_subfolder}&port={$my_port}"); if ($poll_peer == "") { $poll_peer = "No Response"; } else { $poll_peer = intval($poll_peer); } $body_string .= '<tr><td class="style2"><p style="word-wrap:break-word; font-size:12px;">' . $ip_address . $domain . ':' . $port_number . '/' . $subfolder . '</p></td>'; $body_string .= '<td class="style2"><p style="font-size:12px;">' . $poll_peer . '</p></td></tr>'; } $body_string .= '</table></div>'; $quick_info = '<strong>Failure Scores</strong> that other peers have recorded for your server.'; home_screen('Failure Scores From Peers', NULL, $body_string, $quick_info); exit; } if ($_GET["edit"] == "peer") { if ($_GET["type"] == "new") {
function send_timekoins($my_private_key, $my_public_key, $send_to_public_key, $amount, $message) { if (empty($my_private_key) == TRUE || empty($my_public_key) == TRUE || empty($send_to_public_key) == TRUE) { return FALSE; } ini_set('user_agent', 'Timekoin Client v' . TIMEKOIN_VERSION); ini_set('default_socket_timeout', 3); // Timeout for request in seconds $arr1 = str_split($send_to_public_key, 181); $encryptedData1 = tk_encrypt($my_private_key, $arr1[0]); $encryptedData64_1 = base64_encode($encryptedData1); $encryptedData2 = tk_encrypt($my_private_key, $arr1[1]); $encryptedData64_2 = base64_encode($encryptedData2); // Sanitization of message // Filter symbols that might lead to a transaction hack attack $symbols = array("|", "?", "="); // SQL + URL $message = str_replace($symbols, "", $message); // Trim any message to 64 characters max and filter any sql $message = filter_sql(substr($message, 0, 64)); $transaction_data = "AMOUNT={$amount}---TIME=" . time() . "---HASH=" . hash('sha256', $encryptedData64_1 . $encryptedData64_2) . "---MSG={$message}"; $encryptedData3 = tk_encrypt($my_private_key, $transaction_data); $encryptedData64_3 = base64_encode($encryptedData3); $triple_hash_check = hash('sha256', $encryptedData64_1 . $encryptedData64_2 . $encryptedData64_3); $timestamp = transaction_cycle(0) + 1; $attribute = "T"; $qhash = $timestamp . base64_encode($my_public_key) . $encryptedData64_1 . $encryptedData64_2 . $encryptedData64_3 . $triple_hash_check . $attribute; $qhash = hash('md5', $qhash); // Create map with request parameters $params = array('timestamp' => $timestamp, 'public_key' => base64_encode($my_public_key), 'crypt_data1' => $encryptedData64_1, 'crypt_data2' => $encryptedData64_2, 'crypt_data3' => $encryptedData64_3, 'hash' => $triple_hash_check, 'attribute' => $attribute, 'qhash' => $qhash); // Build Http query using params $query = http_build_query($params); // Create Http context details $contextData = array('method' => 'POST', 'header' => "Connection: close\r\n" . "Content-Length: " . strlen($query) . "\r\n", 'content' => $query); // Create context resource for our request $context = stream_context_create(array('http' => $contextData)); // Try all Active Peer Servers $sql_result = mysql_query("SELECT * FROM `active_peer_list` ORDER BY RAND()"); $sql_num_results = mysql_num_rows($sql_result); $return_results; for ($i = 0; $i < $sql_num_results; $i++) { $sql_row = mysql_fetch_array($sql_result); $ip_address = $sql_row["IP_Address"]; $domain = $sql_row["domain"]; $subfolder = $sql_row["subfolder"]; $port_number = $sql_row["port_number"]; $code = $sql_row["code"]; $poll_peer = filter_sql(poll_peer($ip_address, $domain, $subfolder, $port_number, 5, "api.php?action=send_tk&hash={$code}", $context)); if ($poll_peer == "OK") { write_log("Peer: [{$ip_address}{$domain}:{$port_number}/{$subfolder}] Accepted the Transaction for Processing", "T"); $return_results = TRUE; } } if ($return_results == TRUE) { // Success in sending transaction return TRUE; } else { // No peer servers accepted the transaction data :( write_log("No Peers Accepted the Transaction", "T"); return FALSE; } }
$body_string = '<div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" > <tr><th>Peer</th><th>Time</th><th>Variance</th><th>Ping</th></tr>'; // Add more possible peers to the new peer list by polling what the active peers have $sql = "SELECT * FROM `active_peer_list`"; $sql_result = mysql_query($sql); $sql_num_results = mysql_num_rows($sql_result); $response_counter = 0; $variance_total = 0; for ($i = 0; $i < $sql_num_results; $i++) { $sql_row = mysql_fetch_array($sql_result); $ip_address = $sql_row["IP_Address"]; $domain = $sql_row["domain"]; $subfolder = $sql_row["subfolder"]; $port_number = $sql_row["port_number"]; $my_micro_time = microtime(TRUE); $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 12, "peerlist.php?action=polltime"); $my_time = time(); if ($my_time == $poll_peer && empty($poll_peer) == FALSE) { $variance = '0 seconds'; $micro_time_variance = round((microtime(TRUE) - $my_micro_time) * 1000) . " ms"; $response_counter++; } else { if (empty($poll_peer) == FALSE) { $variance = $my_time - $poll_peer; $response_counter++; $variance_total = $variance_total + abs($variance); $micro_time_variance = round((microtime(TRUE) - $my_micro_time) * 1000) . " ms"; if ($variance > 1) { $variance = '+' . $variance . ' seconds'; } else { if ($variance == 1) {
function peer_list() { ini_set('user_agent', 'Timekoin Client (Peerlist) v' . TIMEKOIN_VERSION); ini_set('default_socket_timeout', 2); // Timeout for request in seconds $max_active_peers = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'max_active_peers' LIMIT 1"), 0, "field_data"); $max_new_peers = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'max_new_peers' LIMIT 1"), 0, "field_data"); // How many active peers do we have? $active_peers = mysql_result(mysql_query("SELECT COUNT(join_peer_list) FROM `active_peer_list`"), 0); $new_peers = mysql_result(mysql_query("SELECT COUNT(poll_failures) FROM `new_peers_list`"), 0); if ($active_peers == 0) { // No active or new peers to poll from, start with the first contact servers // and copy them to the new peer list $sql = "SELECT * FROM `options` WHERE `field_name` = 'first_contact_server'"; $sql_result = mysql_query($sql); $sql_num_results = mysql_num_rows($sql_result); write_log("Peer List Empty. Adding First Contact Servers.", "PL"); // First Contact Server Format //---ip=192.168.0.1---domain=timekoin.com---subfolder=timekoin---port=80---code=guest---end for ($i = 0; $i < $sql_num_results; $i++) { $sql_row = mysql_fetch_array($sql_result); $peer_ip = find_string("---ip=", "---domain", $sql_row["field_data"]); $peer_domain = find_string("---domain=", "---subfolder", $sql_row["field_data"]); $peer_subfolder = find_string("---subfolder=", "---port", $sql_row["field_data"]); $peer_port_number = find_string("---port=", "---code", $sql_row["field_data"]); $peer_code = find_string("---code=", "---end", $sql_row["field_data"]); // Insert into database as first contact server(s) $sql = "INSERT INTO `active_peer_list` (`IP_Address` ,`domain` ,`subfolder` ,`port_number` ,`last_heartbeat`, `join_peer_list`, `failed_sent_heartbeat`, `code`)\n\t\t\tVALUES ('{$peer_ip}', '{$peer_domain}', '{$peer_subfolder}', '{$peer_port_number}', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), '0', '{$peer_code}');"; mysql_query($sql); $active_peers++; } } if ($active_peers < $max_active_peers) { //Start polling peers from the new peers list $sql = "SELECT * FROM `new_peers_list` ORDER BY RAND() LIMIT 10"; $sql_result = mysql_query($sql); $sql_num_results = mysql_num_rows($sql_result); // Peer difference $peer_difference_count = $max_active_peers - $active_peers; for ($i = 0; $i < $sql_num_results; $i++) { $sql_row = mysql_fetch_array($sql_result); $ip_address = $sql_row["IP_Address"]; $domain = $sql_row["domain"]; $subfolder = $sql_row["subfolder"]; $port_number = $sql_row["port_number"]; $poll_failures = $sql_row["poll_failures"]; $code = $sql_row["code"]; // Check to make sure that this peer is not already in our active peer list $duplicate_check1 = mysql_result(mysql_query("SELECT * FROM `active_peer_list` WHERE `IP_Address` = '{$ip_address}' LIMIT 1"), 0, 0); $duplicate_check2 = mysql_result(mysql_query("SELECT * FROM `active_peer_list` WHERE `domain` LIKE '{$domain}' LIMIT 1"), 0, 1); if (empty($ip_address) == TRUE) { //Don't have an IP address, check for duplicate domain or my own domain if (empty($duplicate_check2) == TRUE) { // Neither IP nor Domain exist $duplicate_peer = FALSE; } else { $duplicate_peer = TRUE; } } else { // Using IP only, is there a duplicate IP or Domain if (empty($duplicate_check1) == TRUE && empty($duplicate_check2) == TRUE) { $duplicate_peer = FALSE; } else { $duplicate_peer = TRUE; } } if ($duplicate_peer == FALSE) { // Poll Peer for Access if (empty($code) == TRUE) { // Try guest access $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 5, "api.php?action=tk_hash_status&hash=guest"); } else { // Using custom code for peer $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 5, "api.php?action=tk_hash_status&hash={$code}"); } if ($poll_peer == TRUE) { // Add this peer to the active list // Insert this peer into our active peer table // Save only domain name if both IP and Domain exist if (empty($domain) == FALSE) { $ip_address = NULL; } if (empty($code) == TRUE) { $code = "guest"; } // Store new peer in active list $sql = "INSERT INTO `active_peer_list` (`IP_Address` ,`domain` ,`subfolder` ,`port_number` ,`last_heartbeat` ,`join_peer_list` ,`failed_sent_heartbeat` ,`code`)\n\t\t\tVALUES ('{$ip_address}', '{$domain}', '{$subfolder}', '{$port_number}', '" . time() . "', '" . time() . "', '0', '{$code}');"; if (mysql_query($sql) == TRUE) { // Subtract 1 from the peer difference count $peer_difference_count--; write_log("Joined with Peer {$ip_address}:{$domain}:{$port_number}/{$subfolder}", "PL"); } } else { //No response, record polling failure for future reference $poll_failures++; mysql_query("UPDATE `new_peers_list` SET `poll_failures` = '{$poll_failures}' WHERE `IP_Address` = '{$ip_address}' AND `domain` = '{$domain}' AND `subfolder` = '{$subfolder}' AND `port_number` = {$port_number} LIMIT 1"); } } else { // Active response will remove poll failures mysql_query("UPDATE `new_peers_list` SET `poll_failures` = 0 WHERE `IP_Address` = '{$ip_address}' AND `domain` = '{$domain}' AND `subfolder` = '{$subfolder}' AND `port_number` = {$port_number} LIMIT 1"); } // Check to see if enough peers have been added if ($peer_difference_count <= 0) { // Break out of loop break; } } // End For Loop } // End Active vs Max Peer Check //*********************************************************************************** // Add more peers to the new peers list to satisfy new peer limit // How many new peers do we have now? $sql = "SELECT * FROM `new_peers_list`"; $new_peers_numbers = mysql_num_rows(mysql_query($sql)); if ($new_peers_numbers < $max_new_peers && rand(1, 3) == 2) { // Add more possible peers to the new peer list by polling what the active peers have $sql = "SELECT * FROM `active_peer_list` ORDER BY RAND() LIMIT 10"; $sql_result = mysql_query($sql); $sql_num_results = mysql_num_rows($sql_result); $new_peer_difference = $max_new_peers - $new_peers_numbers; for ($i = 0; $i < $sql_num_results; $i++) { $sql_row = mysql_fetch_array($sql_result); $ip_address = $sql_row["IP_Address"]; $domain = $sql_row["domain"]; $subfolder = $sql_row["subfolder"]; $port_number = $sql_row["port_number"]; $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 10000, "peerlist.php?action=new_peers"); $peer_counter = 1; // Reset peer counter while ($peer_counter <= 15) { $peer_IP = NULL; $peer_domain = NULL; $peer_subfolder = NULL; $peer_port_number = NULL; // Sort Data $peer_IP = find_string("-----IP{$peer_counter}=", "-----domain{$peer_counter}", $poll_peer); $peer_domain = find_string("-----domain{$peer_counter}=", "-----subfolder{$peer_counter}", $poll_peer); $peer_subfolder = find_string("-----subfolder{$peer_counter}=", "-----port_number{$peer_counter}", $poll_peer); $peer_port_number = find_string("-----port_number{$peer_counter}=", "-----", $poll_peer); if (is_domain_valid($peer_domain) == FALSE) { // Someone is using an IP address or Localhost :p $peer_domain = NULL; } if (empty($peer_port_number) == TRUE && empty($peer_subfolder) == TRUE) { // No more peers, end this loop early break; } if (empty($peer_IP) == TRUE && empty($peer_domain) == TRUE) { $duplicate_peer == TRUE; // Flag to avoid putting blank entry in database } else { // Check to make sure that this peer is not already in our new peer list $duplicate_check1 = mysql_result(mysql_query("SELECT * FROM `new_peers_list` WHERE `IP_Address` = '{$peer_IP}' LIMIT 1"), 0, 0); $duplicate_check2 = mysql_result(mysql_query("SELECT * FROM `new_peers_list` WHERE `domain` LIKE '{$peer_domain}' LIMIT 1"), 0, 1); if (empty($peer_IP) == TRUE) { //Don't have an IP address, check for duplicate domain if (empty($duplicate_check2) == TRUE) { // Neither IP nor Domain exist $duplicate_peer = FALSE; } else { $duplicate_peer = TRUE; } } else { // Using IP only, is there a duplicate if (empty($duplicate_check1) == TRUE) { if (empty($duplicate_check2) == FALSE) { $duplicate_peer = TRUE; } else { $duplicate_peer = TRUE; } } else { $duplicate_peer = TRUE; } } } if ($duplicate_peer == FALSE) { // Save only domain name if both IP and Domain exist if (empty($peer_domain) == FALSE) { $peer_IP = NULL; } if (empty($peer_IP) == FALSE || empty($peer_domain) == FALSE) { // This is a fresh new peer, add it to the database list $sql = "INSERT INTO `new_peers_list` (`IP_Address` ,`domain` ,`subfolder` ,`port_number` ,`poll_failures` ,`code`)\n\t\t\t\tVALUES ('{$peer_IP}', '{$peer_domain}', '{$peer_subfolder}', '{$peer_port_number}', '0', 'guest')"; if (mysql_query($sql) == TRUE) { // Subtract one from total left to find $new_peer_difference--; } } } if ($new_peer_difference <= 0) { // Enough new peers saved, break out of while loop early break; } $peer_counter++; } // End While loop check if ($new_peer_difference <= 0) { // Enough new peers saved, break out of for loop early break; } } // End For loop check } // End New Peers vs Max New Peers check //*********************************************************************************** // Send a heartbeat to all active peers in our list to make sure they are still online $sql = "SELECT * FROM `active_peer_list`"; $sql_result = mysql_query($sql); $sql_num_results = mysql_num_rows($sql_result); for ($i = 0; $i < $sql_num_results; $i++) { $sql_row = mysql_fetch_array($sql_result); if (rand(1, 3) == 2) { $ip_address = $sql_row["IP_Address"]; $domain = $sql_row["domain"]; $subfolder = $sql_row["subfolder"]; $port_number = $sql_row["port_number"]; $last_heartbeat = $sql_row["last_heartbeat"]; $join_peer_list = $sql_row["join_peer_list"]; $failed_sent_heartbeat = $sql_row["failed_sent_heartbeat"]; $code = $sql_row["code"]; $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 5, "api.php?action=tk_hash_status&hash={$code}"); if ($poll_peer == TRUE) { //Got a response from an active Timekoin server mysql_query("UPDATE `active_peer_list` SET `last_heartbeat` = '" . time() . "', `failed_sent_heartbeat` = 0 WHERE `IP_Address` = '{$ip_address}' AND `domain` = '{$domain}' AND `subfolder` = '{$subfolder}' AND `port_number` = {$port_number} LIMIT 1"); } else { //No response, record polling failure for future reference $failed_sent_heartbeat++; mysql_query("UPDATE `active_peer_list` SET `failed_sent_heartbeat` = '{$failed_sent_heartbeat}' WHERE `IP_Address` = '{$ip_address}' AND `domain` = '{$domain}' AND `subfolder` = '{$subfolder}' AND `port_number` = {$port_number} LIMIT 1"); } } // End Randomize Check } // End for Loop // Remove all active peers that are offline for more than 5 minutes if (rand(1, 2) == 2) { mysql_query("DELETE QUICK FROM `active_peer_list` WHERE `last_heartbeat` < " . (time() - 300) . " AND `join_peer_list` != 0"); } //*********************************************************************************** // Send a heartbeat to all reserve peers in our list to make sure they are still online $sql = "SELECT * FROM `new_peers_list`"; $sql_result = mysql_query($sql); $sql_num_results = mysql_num_rows($sql_result); for ($i = 0; $i < $sql_num_results; $i++) { $sql_row = mysql_fetch_array($sql_result); if (rand(1, 3) == 2) { $ip_address = $sql_row["IP_Address"]; $domain = $sql_row["domain"]; $subfolder = $sql_row["subfolder"]; $port_number = $sql_row["port_number"]; $poll_failures = $sql_row["poll_failures"]; // Query Server for valid Hashcode $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 5, "api.php?action=tk_hash_status&hash={$code}"); if ($poll_peer == TRUE) { //Got a response from an active Timekoin server mysql_query("UPDATE `new_peers_list` SET `poll_failures` = 0 WHERE `IP_Address` = '{$ip_address}' AND `domain` = '{$domain}' AND `subfolder` = '{$subfolder}' AND `port_number` = {$port_number} LIMIT 1"); } else { //No response, record polling failure for future reference $poll_failures++; mysql_query("UPDATE `new_peers_list` SET `poll_failures` = '{$poll_failures}' WHERE `IP_Address` = '{$ip_address}' AND `domain` = '{$domain}' AND `subfolder` = '{$subfolder}' AND `port_number` = {$port_number} LIMIT 1"); } } // End Randomize Check } // End for Loop // Clean up reserve peer list by removing those that have no responded for over 6 poll attempts if (rand(1, 2) == 2) { mysql_query("DELETE QUICK FROM `new_peers_list` WHERE `poll_failures` > 6"); } return; }