forked from aherd2985/UtilityBelt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
580 lines (501 loc) · 16.9 KB
/
Program.cs
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
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Mail;
using System.Text.Json;
using System.Web;
using UtilityBelt.Helpers;
using UtilityBelt.Models;
namespace UtilityBelt
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(Properties.Resources.ASCIIart);
Console.WriteLine("Loading...");
IServiceProvider services = ServiceProviderBuilder.GetServiceProvider(args);
IOptions<SecretsModel> options = services.GetRequiredService<IOptions<SecretsModel>>();
bool showMenu = true;
do
{
MenuOptions(options);
showMenu = RecursiveOptions();
} while (showMenu);
}
#region Choice Handler
static bool RecursiveOptions()
{
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.Green;
string testUserInput = null;
do
{
Console.Write("Would you like to run another option?: ");
testUserInput = Console.ReadLine();
if (int.TryParse(testUserInput, out _)) //If user input was a number...
{
Console.WriteLine("");
Console.WriteLine("I am sorry, your answer could not be translated to a yes/no.");
Console.WriteLine("Please try to reformat your answer.\n");
testUserInput = null;
}
} while (testUserInput == null);
try
{
return FromString(testUserInput);
}
catch
{
Console.WriteLine("");
Console.WriteLine("I am sorry, your answer could not be translated to a yes/no.");
Console.WriteLine("Please try to reformat your answer.");
return RecursiveOptions();
}
}
static void MenuOptions(IOptions<SecretsModel> options)
{
Console.WriteLine("");
Console.WriteLine("Select the Tool");
Console.WriteLine("1) Port Scanner");
Console.WriteLine("2) Text Message");
Console.WriteLine("3) Random Chuck Norris Joke");
Console.WriteLine("4) Random Cat Fact");
Console.WriteLine("5) Bitcoin Prices");
Console.WriteLine("6) Who is in Space");
Console.WriteLine("7) Weather forecast");
Console.WriteLine("8) Country Information");
Console.WriteLine("9) Discord sender");
Console.WriteLine("10) Random Quote");
Console.WriteLine("11) Random Insult");
Console.WriteLine("12) Who Stole the Cookie");
Console.WriteLine("13) Random Taco Recipe");
Console.WriteLine("");
Console.Write("Your choice:");
string optionPicked = Console.ReadLine().ToLower();
switch (optionPicked)
{
case "1":
case "port":
case "port scanner":
portScanner();
break;
case "2":
case "ssms":
case "text":
case "text message":
TextMessage(options);
break;
case "3":
case "random chuck norris joke":
case "chuck norris joke":
case "chuck norris":
case "joke":
RandomChuckNorrisJoke();
break;
case "4":
case "cat fact":
case "cat":
CatFact();
break;
case "5":
case "bitcoin prices":
case "bitcoin":
BitcoinPrices();
break;
case "6":
case "who is in space":
case "space":
Space();
break;
case "7":
case "weather":
case "wf":
case "weather forecast":
WeatherForecast(options);
break;
case "8":
case "Country":
CountryInformation();
break;
case "9":
case "discord":
case "ds":
case "webhook":
case "wh":
DiscordWebhook(options);
break;
case "10":
case "quote":
RandomQuote();
break;
case "11":
case "insult":
RandomInsult();
break;
case "12":
case "cookie":
CookieAccusation();
break;
case "13":
case "taco":
RandomTaco();
break;
default:
Console.WriteLine("Please make a valid option");
MenuOptions(options);
break;
}
}
#endregion
#region Choice Processors
#region Weather
static void WeatherForecast(IOptions<SecretsModel> options)
{
var openWeatherMapApiKey = options.Value.OpenWeatherMapApiKey;
if (String.IsNullOrEmpty(openWeatherMapApiKey))
{
Console.WriteLine("Whoops! API key is not defined.");
return;
}
Console.Write("Enter your town name:");
string town = Console.ReadLine();
string resp = "";
using (var wc = new WebClient())
{
resp = wc.DownloadString($"http://api.openweathermap.org/data/2.5/weather?q={town}&appid={openWeatherMapApiKey}");
}
WeatherRoot wr = JsonSerializer.Deserialize<WeatherRoot>(resp);
Console.WriteLine();
Console.WriteLine("Temperature: " + Weather.KtoF(wr.Main.Temp) + "°F or " + Weather.KtoC(wr.Main.Temp) + "°C. Feels like: " + Weather.KtoF(wr.Main.Temp) + "°F or " + Weather.KtoC(wr.Main.Temp) + "°C");
Console.WriteLine("Wind speed: " + wr.Wind.Speed + " m/s. Air pressure is " + wr.Main.Pressure + "mmHg or " + Math.Round(wr.Main.Pressure * 133.322, 1) + " Pascals.");
long currentTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
bool SunWhat = currentTime > wr.Sys.Sunrise;
long whatNext = SunWhat ? wr.Sys.Sunset : wr.Sys.Sunrise;
long diff = whatNext - currentTime;
var dto = DateTimeOffset.FromUnixTimeSeconds(diff);
if (SunWhat) //If sun should be setting...
{
Console.WriteLine("It's day right now. The sun will set in " + dto.ToString("HH:mm:ss"));
}
else
{
Console.WriteLine("It's night right now. The sun will rise in " + dto.ToString("HH:mm:ss"));
}
}
#endregion
#region Port Scanner
static void portScanner()
{
Console.Write("Please enter a domain:");
string domain = Console.ReadLine().ToLower();
Console.Write("Please enter a starting Port Number:");
int lowPort = int.Parse(Console.ReadLine());
Console.Write("Please enter an ending Port Number:");
int highPort = int.Parse(Console.ReadLine());
PortScanner.Scanner(domain, lowPort, highPort);
}
#endregion
#region Text Message
static void TextMessage(IOptions<SecretsModel> options)
{
Console.WriteLine();
Console.Write("Input number to receive message:");
string send2Number = Console.ReadLine().ToLower();
SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential(options.Value.Email, options.Value.EmailPassword),
EnableSsl = true
};
MailMessage message = new MailMessage();
message.From = new MailAddress(options.Value.Email);
var carrierType = MenuEnum<CarrierType>("Carrier");
var meta = EnumHelper.GetAttributeOfType<CarrierMetaAttribute>(carrierType);
message.To.Add(new MailAddress(send2Number + $"@{meta?.Domain}"));
message.Subject = "This is my subject";
message.Body = "This is the content";
try
{
client.Send(message);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Sent successfully");
}
catch
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Text Message failed");
}
message.Dispose();
client.Dispose();
}
static T MenuEnum<T>(string subject)
where T : struct, Enum
{
Console.WriteLine("");
Console.WriteLine($"Select the {subject}");
Array values = Enum.GetValues(typeof(T));
for (int i = 0; i < values.Length; i++)
{
var current = values.GetValue(i) as Enum;
var meta = EnumHelper.GetAttributeOfType<CarrierMetaAttribute>(current);
string description = meta?.Name ?? Enum.GetName(typeof(T), current);
Console.WriteLine($"{i + 1}) {description}");
}
Console.WriteLine("");
Console.Write("Your choice:");
string optionPicked = Console.ReadLine().ToLower();
bool isValid = Enum.TryParse(optionPicked, true, out T result);
if (!isValid || !Enum.IsDefined(typeof(T), result))
{
Console.WriteLine("Please select a valid option");
return MenuEnum<T>(subject);
}
return result;
}
#endregion
#region Chuck Norris Jokes
static void RandomChuckNorrisJoke()
{
string content = string.Empty;
string url = "https://api.chucknorris.io/jokes/random";
using (var wc = new WebClient())
{
content = wc.DownloadString(url);
}
ChuckJokeModel chuckJoke = JsonSerializer.Deserialize<ChuckJokeModel>(content);
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(chuckJoke.Value);
Console.WriteLine();
}
#endregion
#region Bitcoin Prices
static void BitcoinPrices()
{
string content = string.Empty;
string bitUrl = "https://api.coindesk.com/v1/bpi/currentprice.json";
using (var wc = new WebClient())
{
content = wc.DownloadString(bitUrl);
}
BitcoinPrice bitFact = JsonSerializer.Deserialize<BitcoinPrice>(content);
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("As Of - " + bitFact.Time.Updated);
Console.WriteLine("USD - $ " + bitFact.Bpi.USD.Rate);
Console.WriteLine();
}
#endregion
#region Cat facts
static void CatFact()
{
string content = string.Empty;
string catUrl = "https://cat-fact.herokuapp.com/facts/random";
using (var wc = new WebClient())
{
content = wc.DownloadString(catUrl);
}
CatFactModel catFact = JsonSerializer.Deserialize<CatFactModel>(content);
Console.WriteLine();
if (catFact.Status != null && catFact.Status.Verified)
Console.ForegroundColor = ConsoleColor.Yellow;
else
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(catFact.Text);
Console.WriteLine();
}
#endregion
#region People in space
static void Space()
{
string content = string.Empty;
string spacePeopleUrl = "http://api.open-notify.org/astros.json";
using (var wc = new WebClient())
{
content = wc.DownloadString(spacePeopleUrl);
}
SpacePersonModel spacePeopleFact = JsonSerializer.Deserialize<SpacePersonModel>(content);
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("There are " + spacePeopleFact.People.Count + " in space right now!");
foreach (SpacePerson spacePerson in spacePeopleFact.People)
{
Console.WriteLine(spacePerson.Name + " is in " + spacePerson.Craft);
}
Console.WriteLine();
}
#endregion
#region Country information
static void CountryInformation()
{
string content = string.Empty;
Console.WriteLine("");
Console.WriteLine("Please enter a country name:");
string countryName = Console.ReadLine().ToLower();
string url = $"https://restcountries.eu/rest/v2/name/{countryName}";
try
{
using (var wc = new WebClient())
{
content = wc.DownloadString(url);
}
var countryInformationResult = JsonSerializer.Deserialize<List<CountryInformation>>(content);
foreach (var item in countryInformationResult)
{
Console.WriteLine("===============================================");
Console.WriteLine($"Country Name: {item.Name}");
Console.WriteLine($"Capital: {item.Capital}");
Console.WriteLine($"Region: {item.Region}");
Console.WriteLine($"Population: {item.Population.ToString("N1")}");
Console.WriteLine($"Area: {item.Area.ToString("N1")} km²");
Console.WriteLine("Currencies");
foreach (var moneda in item.Currencies)
{
Console.WriteLine($"*Code:\t\t{moneda.Code}");
Console.WriteLine($"*Name:\t\t{moneda.Name}");
Console.WriteLine($"*Symbol:\t{moneda.Symbol}");
}
Console.WriteLine("Languages");
foreach (var language in item.Languages)
{
Console.WriteLine($"* Name:\t\t{language.Name} / {language.NativeName}");
}
Console.WriteLine("===============================================");
}
}
catch (WebException)
{
Console.WriteLine("Country not found");
}
}
#endregion
#region Discord Sender
static void DiscordWebhook(IOptions<SecretsModel> options)
{
string whook = options.Value.DiscordWebhook;
if (String.IsNullOrEmpty(whook))
{
Console.WriteLine("Whoops! You dont have a webhook defined in your config!"); return;
}
Console.Write("Enter the message:");
string msg = Console.ReadLine();
WebHookContent cont = new WebHookContent()
{
content = msg
};
string json = JsonSerializer.Serialize(cont);
using (var www = new HttpClient())
{
var content = new StringContent(json);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var task = www.PostAsync(whook, content);
task.Wait();
}
Console.WriteLine("Message sent!");
}
#endregion
#region Random quote
static void RandomQuote()
{
string content = string.Empty;
string quoteUrl = "https://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json";
using (var wc = new WebClient())
{
content = wc.DownloadString(quoteUrl);
}
QuoteModel quote = JsonSerializer.Deserialize<QuoteModel>(content);
Console.WriteLine();
Console.WriteLine(quote.QuoteText);
Console.WriteLine($"--{quote.QuoteAuthor}");
Console.WriteLine();
}
#endregion
#region Random insult
static void RandomInsult()
{
string content = string.Empty;
string apiUrl = "https://evilinsult.com/generate_insult.php?lang=en&type=json";
using (var wc = new WebClient())
{
content = wc.DownloadString(apiUrl);
}
EvilInsultModel insultResponse = JsonSerializer.Deserialize<EvilInsultModel>(content);
Console.WriteLine();
Console.WriteLine(HttpUtility.HtmlDecode(insultResponse.Insult));
Console.WriteLine();
}
#endregion
#region Who stole the cookie
static void CookieAccusation()
{
string content = string.Empty;
string suspectUrl = "https://randomuser.me/api/?inc=name&format=json";
using (var wc = new WebClient())
{
content = wc.DownloadString(suspectUrl);
}
CookieSuspectModel cookieSuspect = JsonSerializer.Deserialize<CookieSuspectModel>(content);
string suspectFullName = cookieSuspect.results[0].name.first + " " + cookieSuspect.results[0].name.last;
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("");
Console.WriteLine("Jacques Clouseau: " + suspectFullName + " stole the cookie from the cookie jar.");
Console.WriteLine("");
Console.WriteLine(suspectFullName + ": Who, me?");
Console.WriteLine("");
Console.WriteLine("Jacques Clouseau: Yes, you!");
Console.WriteLine("");
Console.WriteLine(suspectFullName + ": Couldn't be!");
Console.WriteLine("");
Console.WriteLine("Jacques Clouseau: Then who?");
Console.WriteLine();
}
#endregion
#region Random taco recipe
static void RandomTaco()
{
string content = string.Empty;
string tacoRandomizerUrl = "http://taco-randomizer.herokuapp.com/random/";
using (var wc = new WebClient())
{
content = wc.DownloadString(tacoRandomizerUrl);
}
TacoRecipeModel recipe = JsonSerializer.Deserialize<TacoRecipeModel>(content);
Console.WriteLine();
Console.WriteLine(@$"Why not try {recipe.BaseLayer.Name} with {recipe.Mixin.Name},");
Console.WriteLine(@$"seasoned with {recipe.Seasoning.Name}, topped off with {recipe.Condiment.Name}");
Console.WriteLine(@$"and wrapped in delicious {recipe.Shell.Name}.");
Console.WriteLine();
}
#endregion
#region Utility
internal class WebHookContent
{
public string content { get; set; }
}
enum BooleanAliases
{
YES = 1,
AYE = 1,
COOL = 1,
TRUE = 1,
Y = 1,
YEAH = 1,
NAW = 0,
NO = 0,
FALSE = 0,
N = 0
}
static bool FromString(string str)
{
return Convert.ToBoolean(Enum.Parse(typeof(BooleanAliases), str.ToUpper()));
}
#endregion
#endregion
}
}