~grubng-dev/grubng/tools-urlsdb

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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//  
//  Copyright (C) 2009,2010,2011 Bartek thindil Jasicki
// 
//  This file is part of Grubng
// 
//  Grubng is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Runtime.InteropServices;
using Grubng;

namespace urlsdb
{
	/// <summary>
	/// Main program class
	/// </summary>
	static class MainClass
	{
		/// <summary>
		/// Main program function. Add, delete URL's from database and Solr, create workunits, config file
		/// and URL's database.
		/// </summary>
		/// <param name="args">
		/// A <see cref="System.String"/> option to program. Run program without option for get help.
		/// </param>
		public static void Main(string[] args)
		{
			SetProcessName("grub-urlsdb");
			AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MainUnhandledException);
			//If no arguments for program, show help
			if (args.Length == 0)
			{
				Console.WriteLine("Syntax: mono urlsdb.exe [OPTION] [COMMAND]");
				Console.WriteLine("Options:");
				Console.WriteLine("--quiet - no console output (quiet mode, only error messages)");
				Console.WriteLine("Commands:");
				Console.WriteLine("createconf - create configuration file for program");
				Console.WriteLine("updateconf - update configuration file for program");
				Console.WriteLine("createdb - create database");
				Console.WriteLine("updatedb - update database");
				Console.WriteLine("filldb [FILENAME] - put URL's to database from FILENAME file");
				Console.WriteLine("fastfilldb [FILENAME] - put URL's to database from FILENAME file. For difference with filldb look at README.txt");
				Console.WriteLine("backup [FILENAME] - create database backup to FILENAME file (only URL's)");
				Console.WriteLine("addurl [URL] - add URL to database");
				Console.WriteLine("delurl [URL] - delete URL from database and index");
				Console.WriteLine("delurls [FILENAME] - delete URL's (list from FILENAME) from database and index");
				Console.WriteLine("createwu - create workunits from URL's from database.");
				Console.WriteLine("maintain - delete URL's from database (with HTTP status codes: 204, 301, 400-408, 500, 503), optimize database and Solr, create compressed backup of URL's");
				Console.WriteLine("optimizedb - optimize and flush database");
				Console.WriteLine("optimizesolr - optimize Solr index (if Solr is enabled)");
				return;
			}
			string command;
			Utils util;
			int pos = 1;
			bool quiet = false;
			if (args[0] != "--quiet")
			{
				command = args[0];
			}
			else
			{
				command = args[1];
				pos = 2;
				quiet = true;
			}
			util = new Utils(quiet);
			//Create configuration file
			if (command == "createconf")
			{
				util.Message("Creating configuration file ... ", false);
				Config.CreateConfig();
				util.Message("done.", true);
				util.Message("Please now edit it for set correct settings", true);
				return;
			}
			//Update configuration file
			if (command == "updateconf")
			{
				util.Message("Updating configuration file ... ", false);
				Config.UpdateConfig();
				util.Message("done.", true);
				util.Message("Please now edit it for set correct settings", true);
				return;
			}
			//Create database
			if (command == "createdb")
			{
				util.Message("Creating database ... ", false);
				Database.CreateDB();
				util.Message("done.", true);
				return;
			}
			Database db = new Database();
			//Update database
			if (command == "updatedb")
			{
				util.Message("Updating database ... ", false);
				db.UpdateDB();
				util.Message("done.", true);
				return;
			}
			//Backup URL's to plain text file
			Files file = new Files(quiet);
			if (command == "backup")
			{
				if (args.Length == pos)
				{
					Console.WriteLine("Please enter backup file name.");
					return;
				}
				file.Backup(args[pos]);
			}
			ParseURLs parseurl = new ParseURLs();
			int amount = 0;
			string record, hash1, hash2 = String.Empty;
			//Check if selected file exists
			if ((command == "filldb") || (command == "fastfilldb") || (command == "delurls"))
			{
				if (args.Length == 1)
				{
					Console.WriteLine("Please enter file name with urls.");
					return;
				}
				if (!File.Exists(args[pos]))
				{
					Console.WriteLine("File {0} not exist.", args[pos]);
					return;
				}
			}
			//Insert URL's from file to database
			if ((command == "filldb") || (command == "fastfilldb"))
			{
				if (command == "filldb")
				{
					file.AddURLs(args[pos], false);
				}
				else
				{
					file.AddURLs(args[pos], true);
				}
			}
			//Add single URL to database
			if (command == "addurl")
			{
				if (args.Length == 1)
				{
					Console.WriteLine("Please enter URL to add.");
					return;
				}
				record = ParseURLs.ParseURL(args[pos]);
				if (record.Length > 0)
				{
					hash1 = parseurl.GetHash(record);
					if (!record.StartsWith("www."))
					{
						hash2 = parseurl.GetHash("www." + record);
					}
					else
					{
						hash2 = parseurl.GetHash(record.Substring(4));
					}
					amount = db.InstertURL(hash1, record, hash2);
				}
				if (amount == 0)
				{
					util.Message("URL already exist in database.", true);
				}
				else
				{
					util.Message("URL added to database.", true);
				}
			}
			bool solrenabled = false;
			if (Config.ReadConfig("/configuration/enablesolr") == "Y")
			{
				solrenabled = true;
			}
			//Delete single URL from database
			if (command == "delurl")
			{
				if (args.Length == 1)
				{
					Console.WriteLine("Please enter URL to delete.");
					return;
				}
				amount = db.DeleteURL(parseurl.GetHash(args[pos]));
				if (solrenabled)
				{
					string solrcommand = "<delete><id>http://" + 
						System.Security.SecurityElement.Escape(ParseURLs.ParseURL(args[pos])) + 
						                                       "</id></delete>";
					solrcommand = Regex.Replace(solrcommand, @"[\p{IsC}]", String.Empty);
					MainClass.SendCommand(solrcommand, "Solr");
					solrcommand = "<commit/>";
					MainClass.SendCommand(solrcommand, "Solr");
				}
				if (amount == 0)
				{
					util.Message("URL not exist in database.", true);
				}
				else
				{
					util.Message("URL deleted from database.", true);
				}
			}
			parseurl.Dispose();
			//Delete URL's from file from database
			if (command == "delurls")
			{
				util.Message("Deleting URL's from database ... ", false);
				FileInfo finfo = new FileInfo(args[pos]);
				int length = (int)finfo.Length;
				finfo = null;
				StreamReader reader = new StreamReader(args[pos]);
				StringBuilder solrcommand = new StringBuilder();
				solrcommand.Append("<delete>");
				int i = 0, amount2 = 0;
				string scommand = String.Empty;
				System.Collections.Generic.List<string> records = new System.Collections.Generic.List<string>();
				util.ProgressStart(length, String.Empty);
				while (!reader.EndOfStream)
				{
					for (int j = 0; j < 25000; j++)
					{
						record = reader.ReadLine();
						if (record == null)
						{
							break;
						}
						records.Add(record);
					}
					amount += db.DeleteURLs(records);
					foreach (string record3 in records)
					{
						if (solrenabled)
						{
							solrcommand.Append("<id>http://");
							solrcommand.Append(System.Security.SecurityElement.Escape(ParseURLs.ParseURL(record3)));
							solrcommand.Append("</id>");
							amount2 ++;
							if (amount2 == 2000)
							{
								solrcommand.Append("</delete>");
								scommand = Regex.Replace(solrcommand.ToString(), @"[\p{IsC}]", String.Empty);
								MainClass.SendCommand(scommand, "Solr");
								MainClass.SendCommand("<commit/>", "Solr");
								solrcommand.Remove(0, solrcommand.Length);
								solrcommand.Append("<delete>");
								amount2 = 0;
							}
						}
						util.Curamount += record3.Length;
						i ++;
					}
					records.Clear();
					records.TrimExcess();
				}
				records = null;
				reader.Close();
				reader.Dispose();
				if (solrenabled)
				{
					solrcommand.Append("</delete>");
					scommand = Regex.Replace(solrcommand.ToString(), @"[\p{IsC}]", String.Empty);
					MainClass.SendCommand(scommand, "Solr");
					MainClass.SendCommand("<commit/>", "Solr");
				}
				util.ProgressStop();
				util.Message(String.Empty, true);
				util.Message(amount.ToString() + " URL's was deleted from " + i.ToString() + " URL's.", true);
			}
			//Create workunits from URL's from database
			if (command == "createwu")
			{
				util.Message("Creating workunits ... ", false);
				string urls, path, host;
				string wupass = Config.ReadConfig("/configuration/workunitspassword");
				string useragent = Config.ReadConfig("/configuration/useragent");
				int urlsamount = Convert.ToInt32(Config.ReadConfig("/configuration/urlsamount"));
				string httpversion = Config.ReadConfig("/configuration/httpversion");
				string accept = Config.ReadConfig("/configuration/accept");
				string[] urlsArray, urlparts;
				int offset = 0;
				urls = db.SelectURLs(-1);
				if (urls.Length == 0)
				{
					return;
				}
				string workunitsdirectory = Config.ReadConfig("/configuration/workunitsdirectory");
				string tempwu = workunitsdirectory + "wu.temp";
				StreamWriter writer = new StreamWriter(tempwu);
				DateTime modtime;
				urlsArray = urls.Split(new char[] {'\n'});
				StringBuilder key = new StringBuilder();
				System.Security.Cryptography.SHA1Managed SHhash = new System.Security.Cryptography.SHA1Managed();
				byte[] PureHash = Encoding.UTF8.GetBytes(wupass);
				byte[] HashValue = SHhash.ComputeHash(PureHash);
				foreach(byte b in HashValue) 
				{
					key.Append(String.Format("{0:x2}", b));
				}
				foreach (string url in urlsArray)
				{
					urlparts = url.Split(new char[] {' '});
					if (urlparts.Length != 2)
					{
						continue;
					}
					if (urlparts[0].IndexOf('/') == -1)
					{
						path = "/";
						host = urlparts[0];
					}
					else
					{
						path = urlparts[0].Substring(url.IndexOf('/'));
						host = urlparts[0].Remove(url.IndexOf('/'));
					}
					writer.Write("GET " + path + " HTTP/" + httpversion +"\r\n");
					writer.Write("Host: " + host + "\r\n");
					writer.Write("User-Agent: " + useragent + "\r\n");
					writer.Write("Accept: " + accept +"\r\n");
					if (urlparts[1] != "0")
					{
						modtime = new DateTime(Convert.ToInt32(urlparts[1].Substring(0, 4)), 
						                       Convert.ToInt32(urlparts[1].Substring(4, 2)),
						                       Convert.ToInt32(urlparts[1].Substring(6, 2)),
						                       Convert.ToInt32(urlparts[1].Substring(8, 2)),
						                       Convert.ToInt32(urlparts[1].Substring(10, 2)),
						                       Convert.ToInt32(urlparts[1].Substring(12)));
						writer.Write("If-Modified-Since: " + modtime.ToString("r") + "\r\n");
					}
					writer.Write("\r\n");
					writer.Flush();
					PureHash = Encoding.UTF8.GetBytes(key.ToString() + " " + host + " " + path);
					key.Remove(0, key.Length);
					HashValue = SHhash.ComputeHash(PureHash);
					foreach(byte b in HashValue) 
					{
						key.Append(String.Format("{0:x2}", b));
					}
					offset ++;
					if (offset == urlsamount)
					{
						writer.Close();
						writer.Dispose();
						File.Copy(tempwu, workunitsdirectory + key.ToString() + ".wu1", true);
						File.Delete(tempwu);
						key.Remove(0, key.Length);
						PureHash = Encoding.UTF8.GetBytes(wupass);
						HashValue = SHhash.ComputeHash(PureHash);
						foreach(byte b in HashValue) 
						{
							key.Append(String.Format("{0:x2}", b));
						}
						writer = new StreamWriter(tempwu);
						offset = 0;
					}
				}
				writer.Close();
				writer.Dispose();
				File.Delete(tempwu);
				util.Message("done.", true);
			}
			//Maintenance work on URL's database and Solr
			if ((command == "maintain") || (command == "optimizedb") || (command == "optimizesolr"))
			{
				MainClass.SendCommand("serverenabled=N,enableupload=N", "Upload server");
			}
			if (command == "maintain")
			{
				string[] codes = new string[] {"204", "301", "400", "401", "402", "403", "404", "405", "406", "408", "500", "503"};
				foreach (string code in codes)
				{
					util.Message("Deleting URL's with HTTP status code " + code, true);
					amount = db.CleanURLs(code, solrenabled);
					util.Message(amount.ToString() + " URL's was deleted.", true);
				}
			}
			//Optimize database
			if ((command == "optimizedb") || (command == "maintain"))
			{
				util.Message("Optimizing database ... ", false);
				db.OptimizeDB();
				util.Message("done.", true);
			}
			//Optimize Solr index
			if ((command == "optimizesolr") || (command == "maintain"))
			{
				if (!solrenabled)
				{
					return;
				}
				util.Message("Optimizing Solr ... ", false);
				MainClass.SendCommand("<optimize/>", "Solr");
				util.Message("done.", true);
			}
			//Create database backup during maintenance work
			if (command == "maintain")
			{
				file.Backup(DateTime.UtcNow.ToString("yyyy" + "MM" + "dd") + ".txt");
			}
			if ((command == "maintain") || (command == "optimizedb") || (command == "optimizesolr"))
			{
				MainClass.SendCommand("serverenabled=Y,enableupload=Y", "Upload server");
			}
			util.Dispose();
		}
		
		/// <summary>
		/// Function set process name on Unix systems. Code borrowed from 
		/// http://abock.org/2006/02/09/changing-process-name-in-mono/
		/// </summary>
		/// <param name="name">
		/// A <see cref="System.String"/> new name of process
		/// </param>
		public static void SetProcessName (string name)
		{
			try 
			{
				if (NativeMethods.prctl (15 /* PR_SET_NAME */, Encoding.ASCII.GetBytes (name + "\0"),
				           IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) != 0) {
					throw new ApplicationException ("Error setting process name: " +
					                                Mono.Unix.Native.Stdlib.GetLastError().ToString());
				}
			} 
			catch (EntryPointNotFoundException) 
			{
				NativeMethods.setproctitle (Encoding.ASCII.GetBytes ("%s\0"),
				              Encoding.ASCII.GetBytes (name + "\0"));
			}
		}
		
		/// <summary>
		/// Function send XML Update command to remote server
		/// </summary>
		/// <param name="command">
		/// A <see cref="System.String"/> command to send.
		/// </param>
		/// <param name="destination">
		/// A <see cref="System.String"/> destination for command (Solr, upload server).
		public static void SendCommand(string command, string destination)
		{
			HttpWebRequest request;
			if (destination == "Solr")
			{
				request = (HttpWebRequest)WebRequest.Create(Config.ReadConfig("/configuration/solraddress"));
				request.Credentials = new NetworkCredential(Config.ReadConfig("/configuration/solrusername"), 
				                                            Config.ReadConfig("configuration/solrpassword"));
				request.PreAuthenticate = true;
				request.ContentType = "text/xml";
			}
			else
			{
				request = (HttpWebRequest)WebRequest.Create(Config.ReadConfig("/configuration/uploadaddress"));
				command = "config," + Config.ReadConfig("/configuration/uploadusername") + "," + 
				                      Config.ReadConfig("/configuration/uploadpassword") + ",set," + command;
			}
			request.Proxy = null;
			request.Method = "POST";
			request.Timeout = 1000000000;
			byte[] buffer = Encoding.UTF8.GetBytes(command);
			using (Stream streamw1 = request.GetRequestStream())
			{
				streamw1.Write(buffer, 0, buffer.Length);
				streamw1.Close();
			}
			try
			{
				HttpWebResponse response = (HttpWebResponse)request.GetResponse();
				if (response != null)
				{
					response.Close();
				}
			}
			catch (WebException e)
			{
				if (e.Response != null)
				{
					using (Stream stream = e.Response.GetResponseStream())
					{
						using (FileStream errorfile = new FileStream("error.log", FileMode.Append))
						{
							string tdate = DateTime.Now.ToString("yyyy/MM/dd/HH:mm:ss");
							buffer = Encoding.UTF8.GetBytes(tdate + " " + destination + " error:" + Environment.NewLine);
							errorfile.Write(buffer, 0, buffer.Length);
							errorfile.Flush();
							buffer = new byte[1024];
							int bytesRead = 0;
							while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
							{
								errorfile.Write(buffer, 0, bytesRead);
								errorfile.Flush();
							}
							errorfile.Close();
						}
						stream.Close();
					}
					e.Response.Close();
				}
			}
		}
		
		/// <summary>
		/// Main function for catch unhandled exceptions. Write informations about exception to error.log.
		/// </summary>
		/// <param name="sender">
		/// A <see cref="System.Object"/> unused.
		/// </param>
		/// <param name="args">
		/// A <see cref="UnhandledExceptionEventArgs"/> provide informations about error (source, stacktrace,
		/// general info about error)
		/// </param>
		static void MainUnhandledException(object sender, UnhandledExceptionEventArgs args)
		{
			if (sender != null)
			{
				sender = null;
			}
			Exception e = (Exception)args.ExceptionObject;
			string tdate = DateTime.Now.ToString("yyyy/MM/dd/HH:mm:ss");
			using (FileStream errorlog = File.Open("error.log", FileMode.Append))
			{
				using (StreamWriter logstream = new StreamWriter(errorlog))
				{
					logstream.WriteLine(tdate + " Caught: " + e.GetType().ToString() + " " + e.Message);
					logstream.WriteLine("  Source: " + e.Source);
					logstream.WriteLine("  StackTrace: " + e.StackTrace);
					logstream.WriteLine("  TargetSite: " + e.TargetSite);
					logstream.Close();
				}
			}
		}
	}
	
	/// <summary>
	/// Provide p/invoke for native Unix methods
	/// </summary>
	internal static class NativeMethods
	{
		[DllImport ("libc")] // GNU/Linux
		public static extern int prctl (int option, byte [] arg2, IntPtr arg3,
		                                 IntPtr arg4, IntPtr arg5);

		[DllImport ("libc")] // BSD
		public static extern void setproctitle (byte [] fmt, byte [] str_arg);
	}
}