-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastfind.pike
executable file
·598 lines (494 loc) · 20.1 KB
/
fastfind.pike
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#!/usr/bin/env pike
/* Author: Alwyn Malachi Berkeley
* Created: 3-20-06
* Description: A simple application designed to utilize the dig program
* and a website in order to determine what 3 letter domain names are still
* available for purchase.
*
* This program 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; only version 2 of the License.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA. */
import ADT;
import Protocols.HTTP;
enum bool { false, true };
void appendLine(string strFileName, string strLineToAppend) {
// This function appends a line to a file, it will throw an
// exception if the file cannot be opened
Stdio.File OutputFile = Stdio.File();
if (OutputFile->open(strFileName, "caw")) { // open file
// write data
if (OutputFile->write(strLineToAppend) == -1) {
throw("Error #" + errno() + ": Couldn't write to " + strFileName + ".");
}
} else {
throw("Error #" + errno() + ": Couldn't open " + strFileName + ".");
}
OutputFile->close(); // close file
}
bool checkDomainExtension(string strExtension) {
// This function checks if a string is a valid domain extension
// then returns true if it is, false if it isn't
if (strlen(strExtension) != 0 && strExtension[0] == '.')
return true;
else
return false;
}
bool checkEmailAddress(string strEmailParam) {
// This function checks to see if a email address is valid.
// It returns true if the address is valid and false if incorrect
// check that there is only one "@" in the string
if (String.count(strEmailParam, "@") != 1)
return false;
// check that there is only one "." in the string
if (String.count(strEmailParam, ".") != 1)
return false;
// check that the "." comes after the "@"
int intSpotOfAtSign = search(strEmailParam, "@", 0);
int intSpotOfDotSign = search(strEmailParam, ".", 0);
if (intSpotOfDotSign < intSpotOfAtSign)
return false;
return true;
}
bool checkSequence(string strSequence) {
// This function checks if a string is a valid 3-letter domain sequence
// then returns true if it is, false if it isn't
if (strlen(strSequence) > 3) // no prefix or suffix as hyphen
return false;
else if (strSequence[0] == '-' || strSequence[2] == '-') // 3 letters
return false;
// cannot have letters aside from A-Z, a-z, or hyphen
return true;
}
Queue createList(array(string) DomainExtensionsParam, string strStartSequenceParam, string strEndSequenceParam) {
// This function creates the list of domains that need to be checked, it
// returns a queue with the domains in it
// create an array of letters a-z and 0-9
array(string) LetterList = ({ });
for (int x = 97; x < 123; x++)
LetterList += ({ String.int2char(x) });
for (int x = 48; x < 58; x++)
LetterList += ({ String.int2char(x) });
// generate every possible pattern
array(string) DomainNameList = ({ });
constant strPrefix = "www.";
string strCurrentSequence;
foreach (LetterList, string strFirstLetter) {
foreach(LetterList + ({ "-" }), string strSecondLetter) {
foreach(LetterList, string strThirdLetter) {
foreach((array)DomainExtensionsParam, string strSuffix) {
// prepare the current sequence
strCurrentSequence = strPrefix + strFirstLetter + strSecondLetter + strThirdLetter + strSuffix;
// add the current sequence to the queue
DomainNameList += ({ strCurrentSequence});
}
}
}
}
// finding where strStartSequenceParam is in the DomainNameList
string strLastExtensionSuffix = DomainExtensionsParam[0];
string strNeedle = strPrefix + strStartSequenceParam + strLastExtensionSuffix;
int intStartIndex = search(DomainNameList, strNeedle);
// finding where strEndSequenceParam is in the DomainNameList
string strFirstExtensionSuffix = DomainExtensionsParam[sizeof(DomainExtensionsParam) - 1];
strNeedle = strPrefix + strEndSequenceParam + strFirstExtensionSuffix;
int intEndIndex = search(DomainNameList, strNeedle);
// find the segment of the array that contains the values wanted
array(string) PertinentDomainNames = DomainNameList[intStartIndex..intEndIndex];
// convert the array segment that is left into a queue
Queue DomainNameQueue = Queue();
foreach (PertinentDomainNames, string strDomain)
DomainNameQueue->write(strDomain);
return DomainNameQueue;
}
mapping decipherParameters(array(string) ParameterVariable) {
// This function will decipher all the arguments sent to the program
// and return a mapping of the information sent
//
// NOTE: This function needs to ignore bad options somehow.
//constant INVALID_ARGUMENT = "Sorry, invalid argument, please try again ...\n";
// NOTE: This function also needs to support specifying the filename
mapping(string:string) ParameterInformation = ([ "extensions" : "no", "finish" : "no", "gui" : "no", "mail" : "no", "output" : "no", "start" : "no", "timeless" : "no", "verbose" : "no" ]);
// parsing parameters
array(array(array(string))) ArgumentDelimiters = ({
({ "extensions", Getopt.HAS_ARG, ({ "-e", "--extensions" }) }),
({ "finish", Getopt.HAS_ARG, ({ "-f", "--finish" }) }),
({ "gui", Getopt.NO_ARG, ({ "-g", "--gui" }) }),
({ "help", Getopt.NO_ARG, ({ "-h", "--help" }) }),
({ "mail", Getopt.HAS_ARG, ({ "-m", "--mail" }) }),
({ "output", Getopt.HAS_ARG, ({ "-o", "--output" }) }),
({ "start", Getopt.HAS_ARG, ({ "-s", "--start" }) }),
({ "timeless", Getopt.NO_ARG, ({ "-t", "--timeless" }) }),
({ "verbose", Getopt.NO_ARG, ({ "-v", "--verbose" }) }),
({ "version", Getopt.NO_ARG, ({ "-V", "--version" }) })
});
array(array(string)) Results;
Results = Getopt.find_all_options(ParameterVariable, ArgumentDelimiters, true, 0);
foreach (Results, array(string) ArgumentElement) {
// show help and exit if needed
if (ArgumentElement[0] == "help") {
write ("Usage: fastfind [OPTION]... [FILE]...\n");
write ("Find unregistered three letter domain names.\n\n");
write ("-e, --extensions=[extension,...] searches for only specified extensions.\n");
write ("-f, --finish=[sequence] ends the program at the sequence given.\n");
write ("-g, --gui run the GUI version of the program.\n");
write ("-h, --help displays the help dialog and then exits.\n");
write ("-m, --mail=[recepient,server,port] emails the results to a particular address.\n");
write ("-o, --output=[filename] logs output in the file specified.\n");
write ("-s, --start=[sequence] starts the program at the sequence given.\n");
write ("-t, --timeless report the output without the timestamps.\n");
write ("-v, --verbose run program in verbose mode.\n");
write ("-V, --version display version information and then exits.\n");
write ("\nThe results are checked by two sources, so they are accurate most of the time.\n");
write ("\nReport bugs to <malachix\@malachix.com>.\n");
exit(0);
}
// show version if needed
if (ArgumentElement[0] == "version") {
write ("fastfind v0.2.0 alpha\n");
write ("Written by Alwyn Malachi Berkeley.\n\n");
write ("This is free software; see the source for copying conditions. There is NO\nwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
exit(0);
}
// tally the latest information to the mapping
if (ArgumentElement[0] == "extensions")
ParameterInformation["extensions"] = ArgumentElement[1];
else if (ArgumentElement[0] == "finish")
ParameterInformation["finish"] = ArgumentElement[1];
else if (ArgumentElement[0] == "gui")
ParameterInformation["gui"] = "yes";
else if (ArgumentElement[0] == "mail")
ParameterInformation["mail"] = ArgumentElement[1];
else if (ArgumentElement[0] == "output")
ParameterInformation["output"] = ArgumentElement[1];
else if (ArgumentElement[0] == "start")
ParameterInformation["start"] = ArgumentElement[1];
else if (ArgumentElement[0] == "timeless")
ParameterInformation["timeless"] = "yes";
else if (ArgumentElement[0] == "verbose")
ParameterInformation["verbose"] = "yes";
}
// return the mapping that contains the variables wanted in the program
return ParameterInformation;
}
Queue digURLs(Queue DomainNamesParam, int intNumberToTest) {
// This function checks to see if a series of URLs are available by
// utilizing the "dig" utility
// add the domains to a map
int AmountOfDomainsLeft = sizeof((array)DomainNamesParam);
mapping(string:string) Domain2Result = ([ ]);
while (intNumberToTest-- > 0) {
if (AmountOfDomainsLeft-- == 0) break;
Domain2Result += ([ DomainNamesParam->read() : "" ]);
}
// find all the indices in the map
array(string) MapIndices = indices(Domain2Result);
// create the command that needs to be run
string strCommand = "";
foreach (MapIndices, string strMapIndex) {
// add the dig command needed
strCommand += "dig " + strMapIndex + " | grep 'HEADER'";
// add the && to the command string if needed
if (MapIndices[sizeof(MapIndices) - 1] != strMapIndex)
strCommand += " && ";
}
// running the command && splitting the output into elements
string strCommandResult = Process.popen(strCommand);
array(string) CommandOutput = strCommandResult / "\n";
// completing the mapping, each domain name corresponds with it's
// string output ex. "www.fhs.com" : ";; ->>HEADER<< ..." etc.
int OutputIndex = 0;
foreach (MapIndices, string strMapIndex) {
// sometime a "" occurs in strMapIndex after the last
// element, so this ensure the code does not error
if (CommandOutput[OutputIndex] == "") break;
// mapping domain
Domain2Result[strMapIndex] = CommandOutput[OutputIndex];
OutputIndex++;
}
// add all the domains that were non existent to the
// AvailableNames queue
Queue AvailableNames = Queue();
foreach (MapIndices, string strMapIndex) {
// tallies the domain if it exists
if (search(Domain2Result[strMapIndex], "NXDOMAIN", 0) != -1)
AvailableNames->write(strMapIndex);
}
return AvailableNames;
}
string readFile(string strFileName) {
// This function appends a line to a file, it will throw an
// exception if the file cannot be opened
//
// NOTE: returns an empty string if the file could not be opened
Stdio.File OutputFile = Stdio.File();
string strData;
if (OutputFile->open(strFileName, "r")) // open file
strData = OutputFile->read(); // read in the data
else
return ""; // return nothing if file failed to open
OutputFile->close(); // close file
return strData;
}
void sendEmail(string strRecepientParam, string strServerParam, string strPortParam, string strMessage) {
// This function uses a perl-based program called "sendEmail" to send
// an email
//
// NOTE: The program call "sendEmail" must be present on the system
// in order for this to work
// create the command for sending the email(s)
string strEmailCommand = "sendEmail -f DomainReport@DomainReport.com -t " + strRecepientParam + " -s " + strServerParam + ":" + strPortParam + " -u " + "'Three Letter Domains Report' -m \"" + strMessage + "\"";
// run the command
Process.system(strEmailCommand);
}
int surfURL(string strDomainNameParam) {
// This function checks to see if a URL is available by checking it
// with a whois tool hosted by http://www.whois-search.com
//
// NOTE: THis function returns true if the domain exists was found
constant BASE_URL = "http://www.whois-search.com/whois/";
// just leave the function if an empty string was the parameter
if (strDomainNameParam == "") return 0;
// get the source of the webpage that checks the domain
string strCheckDomainURL = BASE_URL + strDomainNameParam;
Query WebpageResults;
WebpageResults = get_url(strCheckDomainURL);
// return true if the function found a match
if (search(WebpageResults->data(), "No match", 0) == -1)
return 1;
return 0;
}
int main(int argc, array(string) argv) {
constant BEGIN_STATUS_MESSAGE = "Search beginning ...";
constant END_STATUS_MESSAGE = "Search ending ...";
Queue DomainNames = Queue();
Queue Results = Queue();
int intNumberOfDomainsFound = 0;
bool blnIsTimeless = false;
bool blnIsVerbose = false;
bool blnIsText = true; // notice, programs runs in text by default
// decipher parameters
mapping(string:string) Deciphered = decipherParameters(argv);
// set the options wanted according to the arguments
array(string) DomainExtensions = Deciphered["extensions"] / ",";
string strEndSequence = Deciphered["finish"];
if (Deciphered["gui"] == "yes") blnIsText = false;
string strLogFile = Deciphered["output"];
string strStartSequence = Deciphered["start"];
if (Deciphered["timeless"] == "yes") blnIsTimeless = true;
if (Deciphered["verbose"] == "yes") blnIsVerbose = true;
array(string) MailBuffer = Deciphered["mail"] / ",";
string strRecepient;
string strServer;
string strPort;
if (MailBuffer[0] != "no") {
strRecepient = MailBuffer[0];
strServer = MailBuffer[1];
strPort = MailBuffer[2];
}
if (blnIsText == true) { // text version
constant strIncorrectValue = "Sorry, that is an incorrect value, please try again ...\n";
// welcome the user
write ("\tThis program is going to find all 3 letter domains still available for purchase and place the output in a file upon completion.\n\n");
// ask for all pertinent data not supplied by arguments
write ("Please answer the following question(s):\n");
// ask for the domain extensions to use
bool blnExitQuestion = false;
string strResponse;
if (DomainExtensions[0] == "no") {
for (int x = 0; blnExitQuestion != true; x++) {
// prompt
if (x > 0) {
write ("Type another extension to search for, or \"done\" if you finished: ");
} else {
write ("Type a domain extension to search for (with the dot): ");
}
strResponse = Stdio.stdin->gets();
// lowercase just in case it wasn't before
lower_case(strResponse);
// evaluate response
if (checkDomainExtension(strResponse) == true) {
// the if statement makes sure the
// correct values are added when there
// were no arguments passed to the
// program
if (DomainExtensions[0] == "no")
DomainExtensions = ({ strResponse });
else
DomainExtensions += ({ strResponse });
} else if (strResponse == "done" && x > 0)
blnExitQuestion = true;
else
write (strIncorrectValue);
}
}
// ask which three letter sequence to start from
if (strStartSequence == "no") {
blnExitQuestion = false;
do {
// prompt
write ("Type the sequence of characters to start from, ex. \"aaa\": ");
strResponse = Stdio.stdin->gets();
// lowercase just in case it wasn't before
lower_case(strResponse);
// evaluate response
if (checkSequence(strResponse) == true) {
strStartSequence = strResponse;
blnExitQuestion = true;
} else write (strIncorrectValue);
} while(blnExitQuestion != true);
}
// ask which three letter sequence to end on
if (strEndSequence == "no") {
blnExitQuestion = false;
do {
// prompt
write ("Type the sequence of characters to end on, ex. \"zzz\": ");
strResponse = Stdio.stdin->gets();
// lowercase just in case it wasn't before
lower_case(strResponse);
// evaluate response
if (checkSequence(strResponse) == true) {
strEndSequence = strResponse;
blnExitQuestion = true;
} else write (strIncorrectValue);
} while(blnExitQuestion != true);
}
// ask for the output file name
if (strLogFile == "no") {
blnExitQuestion = false;
do {
// prompt
write ("Type the filename for the log file (ex. logfile.txt): ");
strResponse = Stdio.stdin->gets();
// lowercase just in case it wasn't before
lower_case(strResponse);
// evaluate response
if (strlen(strResponse) > 0 ) {
strLogFile = strResponse;
blnExitQuestion = true;
} else write (strIncorrectValue);
} while(blnExitQuestion != true);
}
if (MailBuffer[0] == "no") {
// ask if the user wants the final report emailed to them
blnExitQuestion = false;
do {
// prompt
write ("Would you like the results emailed somewhere (type yes or no): ");
strResponse = Stdio.stdin->gets();
// lowercase just in case it wasn't before
lower_case(strResponse);
// evaluate response
if (strResponse == "yes" || strResponse == "no") {
blnExitQuestion = true;
} else write (strIncorrectValue);
} while(blnExitQuestion != true);
if (strResponse == "yes") {
// ask for the recepient email address
blnExitQuestion = false;
do {
// prompt
write ("Type receipient's email address, ex. \"guy@place.com\": ");
strResponse = Stdio.stdin->gets();
// lowercase just in case it wasn't before
lower_case(strResponse);
// evaluate response
if (checkEmailAddress(strResponse) == true) {
strRecepient = strResponse;
blnExitQuestion = true;
} else write (strIncorrectValue);
} while(blnExitQuestion != true);
// ask for the server
blnExitQuestion = false;
do {
// prompt
write ("Type the mail server to use (ex. mail.servername.com): ");
strResponse = Stdio.stdin->gets();
// lowercase just in case it wasn't before
lower_case(strResponse);
// evaluate response
if (strlen(strResponse) > 0 ) {
strServer = strResponse;
blnExitQuestion = true;
} else write (strIncorrectValue);
} while(blnExitQuestion != true);
// ask for the port to use
blnExitQuestion = false;
do {
// prompt
write ("Type remote port number to use (ex. 25): ");
strResponse = Stdio.stdin->gets();
// lowercase just in case it wasn't before
lower_case(strResponse);
// evaluate response
// NOTE: Need to check that the input
// was all numeric
if (strlen(strResponse) > 0 ) {
strPort = strResponse;
blnExitQuestion = true;
} else write (strIncorrectValue);
} while(blnExitQuestion != true);
}
}
// send status messages
string strCurrentTime;
if (blnIsTimeless == false)
strCurrentTime = ctime(time());
else
strCurrentTime = "\n";
appendLine(strLogFile, BEGIN_STATUS_MESSAGE + strCurrentTime);
write ("\n" + BEGIN_STATUS_MESSAGE + strCurrentTime);
write ("Generating list of domain names to check...\n");
// create list of domains to check
DomainNames = createList(DomainExtensions, strStartSequence, strEndSequence);
// send status messages
write ("Finding available domain names...\n");
// determine available domains
string strDomain;
while (DomainNames->is_empty() == 0) {
// dig URLs to determine domain's availability
Results = digURLs(DomainNames, 200);
// update the number of domains found
intNumberOfDomainsFound += sizeof((array)Results);
// tally each non-existent domain
while (Results->is_empty() == 0) {
// double check those results first in order
// to ensure accuracy, they must not already
// exist
strDomain = Results->read();
if (surfURL(strDomain) == true) continue;
// put it in the log file
appendLine(strLogFile, strDomain + "\n");
// tell user we found one if in verbose mode
if (blnIsVerbose == true)
write ("Found Domain: " + strDomain + "\n");
}
}
// send status messages
if (blnIsTimeless == false)
strCurrentTime = ctime(time());
else
strCurrentTime = "\n";
appendLine(strLogFile, END_STATUS_MESSAGE + strCurrentTime);
write (END_STATUS_MESSAGE + strCurrentTime);
// send an automated mail to the user if needed
if (strlen(strRecepient) > 0 && strlen(strServer) > 0 && strlen(strPort) > 0) {
// read in the output file that was made
string strMessage = readFile(strLogFile);
// embed the output file into an email & send it
sendEmail(strRecepient, strServer, strPort, strMessage);
}
} else { // GUI version
}
return 0;
}