timekoin /v1.9/main.php

Language PHP Lines 293
MD5 Hash c4cbe5830ac772c5b05cc9b9f7ab66e4 Estimated Cost $4,986 (why?)
Repository https://bitbucket.org/rev22/timekoin.git View Raw File View Project SPDX
  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
<?PHP
include 'configuration.php';
include 'function.php';

if($_GET["action"]=="begin_main")
{
	if(mysql_connect(MYSQL_IP,MYSQL_USERNAME,MYSQL_PASSWORD) == FALSE)
	{
		// Database connect error
		$datbase_error = TRUE;
	}

	if(mysql_select_db(MYSQL_DATABASE) == FALSE)
	{
		// Database select error
		$datbase_error = TRUE;
	}

	// Check last heartbeat and make sure it was more than X seconds ago
	$main_heartbeat_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_heartbeat_active' LIMIT 1"),0,"field_data");

	if($main_heartbeat_active == FALSE && $datbase_error == FALSE)
	{
		// Database Initialization
		initialization_database();

		mysql_query("UPDATE `main_loop_status` SET `field_data` = '" . time() . "' WHERE `main_loop_status`.`field_name` = 'main_last_heartbeat' LIMIT 1");

		// Set loop at active now
		mysql_query("UPDATE `main_loop_status` SET `field_data` = '1' WHERE `main_loop_status`.`field_name` = 'main_heartbeat_active' LIMIT 1");

		source_bg('main.php');

		activate(TIMEKOINSYSTEM, 1); // In case this was disabled from a emergency stop call in the server GUI
		header("Location: index.php?menu=system&code=1");
		exit;
	}
	else
	{
		header("Location: index.php?menu=system&code=99");
		exit;
	}
}

$mysql_link = mysql_connect(MYSQL_IP,MYSQL_USERNAME,MYSQL_PASSWORD);
mysql_select_db(MYSQL_DATABASE);
$context = stream_context_create(array('http' => array('header'=>'Connection: close'))); // Force close socket after complete
ini_set('user_agent', 'Timekoin Server (Main) v' . TIMEKOIN_VERSION);
ini_set('default_socket_timeout', 3); // Timeout for request in seconds

while(1) // Begin Infinite Loop
{
	// Are we to remain active?
	$loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_heartbeat_active' LIMIT 1"),0,"field_data");

	if($loop_active === FALSE) // Databaes Error
	{
		// Database Error, try to re-establish a connection after 5 seconds
		mysql_close($mysql_link);
		sleep(5);
		$mysql_link = mysql_connect(MYSQL_IP,MYSQL_USERNAME,MYSQL_PASSWORD);
		mysql_select_db(MYSQL_DATABASE);

		// Keep track of errors in case this can't be recovered from
		$datbase_error = TRUE;
		$database_error_counter++;
	}
	else
	{
		$datbase_error = 0;
		$database_error_counter = 0;
	}

	if($loop_active == 1)
	{
		// Main loop work goes below
		// Set the working status of 2
		mysql_query("UPDATE `main_loop_status` SET `field_data` = '2' WHERE `main_loop_status`.`field_name` = 'main_heartbeat_active' LIMIT 1");
	timekoin_debug_msg("main", "main loop start");
	//*****************************************************************************************************
	//*****************************************************************************************************	
	// Do a random time sync check and report any errors to the user
	if(rand(1,25) == 10)
	{
	  source_bg('timecheck.php');
	}
	//*****************************************************************************************************
	//*****************************************************************************************************
	// Check for spamming IPs
	        timekoin_debug_msg("main", "check for spamming IPs");
		$sql = "SELECT * FROM `ip_activity` GROUP BY `ip`";
		$sql_result = mysql_query($sql);
		$sql_num_results = mysql_num_rows($sql_result);

		$request_max = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'server_request_max' LIMIT 1"),0,"field_data");

		for ($i = 0; $i < $sql_num_results; $i++)
		{
			$sql_row = mysql_fetch_array($sql_result);
			$select_IP = $sql_row["ip"];

			$sql = "SELECT * FROM `ip_activity` WHERE `ip` = '$select_IP'";
			$sql_num_results2 = mysql_num_rows(mysql_query($sql));

			if($sql_num_results2 > $request_max)
			{
				// More than X request per cycle means something is wrong
				// so this IP needs to be banned for a while
				mysql_query("INSERT INTO `ip_banlist` (`when` ,`ip`)VALUES (" . time() . ", '$select_IP')");
				write_log("IP Address $select_IP was added to the ban list due to excessive traffic. Default max query is $request_max per cycle, IP was doing $sql_num_results2 query per cycle instead.", "MA");
			}
		}

		// Clear IP Activity for next cycle
		mysql_query("TRUNCATE TABLE `ip_activity`");

		// Clear out ban list of IPs older than 1 day
		if(rand(1,60) == 30) // Randomize a little to save DB usage
		{
			mysql_query("DELETE FROM `ip_banlist` WHERE `ip_banlist`.`when` < " . (time() - 86400));
		}
	//*****************************************************************************************************
	//*****************************************************************************************************
		// Check to make sure we are not behind a firewall with no Inbound ports
	        timekoin_debug_msg("main", "check to make sure we are not behind a firewall with no Inbound port");
		if($sql_num_results == 0) // Randomize a little
		{
			if(rand(1,3) == 2)
			{
				// No activity from any peer, keep track of this
				$no_peer_activity = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'no_peer_activity' LIMIT 1"),0,"field_data");
				$no_peer_activity++;
				
				if($no_peer_activity < 9)
				{
					mysql_query("UPDATE `options` SET `field_data` = '$no_peer_activity' WHERE `options`.`field_name` = 'no_peer_activity' LIMIT 1");
				}
			}
		}
		else
		{
			if(rand(1,3) == 2)
			{
				$no_peer_activity = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'no_peer_activity' LIMIT 1"),0,"field_data");
				
				if($no_peer_activity > 1)
				{
					// Disable Firewalled Mode, Inbound is working again
					mysql_query("UPDATE `options` SET `field_data` = '0' WHERE `options`.`field_name` = 'firewall_blocked_peer' LIMIT 1");
					mysql_query("UPDATE `options` SET `field_data` = '0' WHERE `options`.`field_name` = 'no_peer_activity' LIMIT 1");
					$no_peer_activity = FALSE;
				}
			}
		}

		if($no_peer_activity > 5 && $no_peer_activity < 7)
		{
			// No Inbound connection working, the only way to submit transactions is out remotely.
			// Switch to firewalled mode.
			mysql_query("UPDATE `options` SET `field_data` = '1' WHERE `options`.`field_name` = 'firewall_blocked_peer' LIMIT 1");
		}
	//*****************************************************************************************************
	//*****************************************************************************************************
	        timekoin_debug_msg("main", "checking transaction clerk");
		$script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'transclerk_heartbeat_active' LIMIT 1"),0,"field_data");
		// Check if script is already running
		if($script_loop_active == 0)
		{
		  source_bg('transclerk.php');
		}

		sleep(1); // 1 second for sanity reasons

	        timekoin_debug_msg("main", "checking foundation manager");
		$script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'foundation_heartbeat_active' LIMIT 1"),0,"field_data");
		// Check if script is already running
		if($script_loop_active == 0)
		{
		  source_bg('foundation.php', TRUE);
		}

		sleep(1); // 1 second for sanity reasons

	        timekoin_debug_msg("main", "checking generation manager");
		$script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'generation_heartbeat_active' LIMIT 1"),0,"field_data");
		// Check if script is already running
		if($script_loop_active == 0)
		{
		  source_bg('generation.php');
		}

		sleep(1); // 1 second for sanity reasons

	        timekoin_debug_msg("main", "checking treasurer processor");
		$script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'treasurer_heartbeat_active' LIMIT 1"),0,"field_data");
		// Check if script is already running
		if($script_loop_active == 0)
		{
		  source_bg('treasurer.php');
		}

		sleep(2); // 1 second for sanity reasons

	        timekoin_debug_msg("main", "checking peer list processor");
		$script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'peerlist_heartbeat_active' LIMIT 1"),0,"field_data");
		// Check if script is already running
		if($script_loop_active == 0)
		{
		  source_bg('peerlist.php');
		}

		sleep(1); // 1 second for sanity reasons

	        timekoin_debug_msg("main", "checking queue clerk");
		$script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'queueclerk_heartbeat_active' LIMIT 1"),0,"field_data");
		// Check if script is already running
		if($script_loop_active == 0 || $script_loop_active == 1)
		{
		  source_bg('queueclerk.php');	
		}

		sleep(2); // 1 second for sanity reasons

	        timekoin_debug_msg("main", "checking genpeer manager");
		$script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'genpeer_heartbeat_active' LIMIT 1"),0,"field_data");
		// Check if script is already running
		if($script_loop_active == 0)
		{
		  source_bg('genpeer.php');	
		}

		sleep(1); // 1 second for sanity reasons

	        timekoin_debug_msg("main", "checking watchdog");
		if(rand(1,3) == 2) // Randomize checking to keep database load down
		{
			// Check watchdog script to make sure it is still running
			$script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_heartbeat_active' LIMIT 1"),0,"field_data");
			$watchdog_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_last_heartbeat' LIMIT 1"),0,"field_data");

			if($script_loop_active > 0)
			{
				// Watchdog should still be active
				if((time() - $watchdog_last_heartbeat) > 60) // Greater than double the loop time, something is wrong
				{
					// Watchdog stop was unexpected
					write_log("Watchdog is Stalled...", "MA");
				}
			}
		}
	//*****************************************************************************************************
	//*****************************************************************************************************	
		// (Very Last Thing to do in Script)
	        timekoin_debug_msg("main", "loop end");
		sleep(1);

		// Time to wake up and start again
		mysql_query("UPDATE `main_loop_status` SET `field_data` = '" . time() . "' WHERE `main_loop_status`.`field_name` = 'main_last_heartbeat' LIMIT 1");

		// Check loop status...
		$loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_heartbeat_active' LIMIT 1"),0,"field_data");

		if($loop_active == 3) // Do a final check to make sure we shouldn't stop running instead
		{
			// Stop the loop and reset status back to 0
			mysql_query("UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'main_heartbeat_active' LIMIT 1");
			exit;
		}
		else
		{
			mysql_query("UPDATE `main_loop_status` SET `field_data` = '1' WHERE `main_loop_status`.`field_name` = 'main_heartbeat_active' LIMIT 1");
		}
	} // Check if Active
	else
	{
		// Something is not working right, delay to avoid fast infinite loop
		if($datbase_error == TRUE && $database_error_counter < 6)
		{
		        timekoin_debug_msg("main", "database connection error");
			// Wait 2 seconds for database to come back online
			sleep(2);
		}
		else
		{
			// Script was called improperly from somewhere or while it was already running, exit to avoid loop stacking
			exit;
		}
	}
}
// End Infinite Loop

?>
Back to Top