-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
226 lines (200 loc) · 7.36 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
using System.Text.RegularExpressions;
using static System.Console;
namespace RobotsSniffer;
internal static class Program
{
private static string? _url;
private static string? _urlList;
private static string? _output;
private static int _timeout = 5000;
private static void Main(string[] args)
{
if (args.Length == 0)
{
WriteLine("!", "No arguments were passed.");
Environment.Exit(1);
}
try
{
foreach (var arg in ParseArgs(args))
switch (arg.Key)
{
case "-u":
IsArgEmpty(arg.Key, arg.Value);
_url = arg.Value;
WriteLine(">", $"Url: {arg.Value}");
break;
case "-l":
IsArgEmpty(arg.Key, arg.Value);
_urlList = arg.Value;
WriteLine(">", $"Urls List: {arg.Value}");
break;
case "-o":
IsArgEmpty(arg.Key, arg.Value);
_output = arg.Value;
WriteLine(">", $"Output: {arg.Value}");
break;
case "-timeout":
IsArgEmpty(arg.Key, arg.Value);
if (!int.TryParse(arg.Value, out _timeout))
throw new ArgumentException("Invalid timeout value. Must be an integer.");
WriteLine(">", $"Timeout: {_timeout}ms");
break;
default:
WriteLine("!", $"Unknown argument: {arg.Key}");
break;
}
}
catch (Exception ex)
{
WriteLine("!", $"Error parsing arguments: {ex.Message}");
Environment.Exit(1);
}
Sniffer();
}
private static void Sniffer()
{
try
{
if (!string.IsNullOrEmpty(_output) && !File.Exists(_output))
File.Create(_output).Close();
if (!string.IsNullOrEmpty(_url)) ProcessUrl(_url);
if (!string.IsNullOrEmpty(_urlList))
{
WriteLine("+", "Checking URLs from the list...");
WriteLine("!", "Printing is disabled since it will bug out the console.");
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = 4 };
Parallel.ForEach(File.ReadAllLines(_urlList), parallelOptions, url => { ProcessUrl(url, false); });
WriteLine("+", "Finished checking URLs from the list.");
ReadKey();
}
}
catch (Exception ex)
{
WriteLine("!", $"Error during sniffing: {ex.Message}");
}
}
private static void ProcessUrl(string url, bool verbose = true)
{
try
{
if (verbose) WriteLine("+", $"Processing URL: {url}");
var html = GetHtml(url).Result;
var hasRobots = IdentifyRobots(html);
if (verbose) WriteLine(hasRobots ? "+" : "-", hasRobots ? "Robots.txt found." : "Robots.txt not found.");
if (hasRobots)
{
var robots = ParseRobots(html);
WriteRobotsOutput(url, robots, verbose);
}
var sitemap = IdentifySitemap(html);
if (verbose) WriteLine("?", $"Sitemap: {sitemap}");
}
catch (Exception ex)
{
WriteLine("!", $"Error processing URL {url}: {ex.Message}");
}
}
private static async Task<string> GetHtml(string url)
{
if (!url.EndsWith("/")) url += "/";
url += "robots.txt";
using var client = new HttpClient();
client.Timeout = TimeSpan.FromMilliseconds(_timeout);
var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
private static Dictionary<string, List<string>> ParseRobots(string content)
{
var allowed = new List<string>();
var disallowed = new List<string>();
foreach (var line in content.Split('\n'))
{
var cleanedLine = line.Trim();
if (cleanedLine.StartsWith("Allow:"))
allowed.Add(cleanedLine[6..].Trim());
else if (cleanedLine.StartsWith("Disallow:")) disallowed.Add(cleanedLine[9..].Trim());
}
return new Dictionary<string, List<string>>
{
{ "Allowed", allowed },
{ "Disallowed", disallowed }
};
}
private static void WriteRobotsOutput(string url, Dictionary<string, List<string>> robots, bool verbose = true)
{
if (verbose)
{
WriteLine("?", "Robots.txt content:");
WriteLine("?", "Allowed:");
robots["Allowed"].ForEach(path => WriteLine("+", path, padding: true));
WriteLine("?", "Disallowed:");
robots["Disallowed"].ForEach(path => WriteLine("-", path, padding: true));
}
if (!string.IsNullOrEmpty(_output))
{
var outputLines = new List<string>
{
new('=', 40),
$"Url: {url}",
"Allowed:",
string.Join(Environment.NewLine, robots["Allowed"]),
"Disallowed:",
string.Join(Environment.NewLine, robots["Disallowed"]),
new('=', 40)
};
File.AppendAllLines(_output, outputLines);
}
}
private static bool IdentifyRobots(string content)
{
return Regex.IsMatch(content, @"User-agent:\s*\*", RegexOptions.IgnoreCase);
}
private static string IdentifySitemap(string content)
{
var sitemap = new Regex(@"(?<=Sitemap:\s*).*?(?=\z)",
RegexOptions.Singleline | RegexOptions.IgnoreCase);
var match = sitemap.Match(content);
return match.Success ? match.Value : "Sitemap not found.";
}
private static void WriteLine(string option, string value, bool newLine = true, bool padding = false)
{
ForegroundColor = ConsoleColor.DarkGray;
if (padding) Write(" ");
Write("[");
ForegroundColor = option switch
{
"!" => ConsoleColor.Cyan,
"?" => ConsoleColor.Yellow,
">" => ConsoleColor.Magenta,
"+" => ConsoleColor.Green,
"-" => ConsoleColor.Red,
_ => ForegroundColor
};
Write(option);
ForegroundColor = ConsoleColor.DarkGray;
Write("] ");
ForegroundColor = ConsoleColor.White;
if (newLine) Console.WriteLine(value);
else Write(value);
}
private static void IsArgEmpty(string arg, string value)
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException($"The argument '{arg}' is empty.");
}
private static Dictionary<string, string> ParseArgs(string[] args)
{
var result = new Dictionary<string, string>();
for (var i = 0; i < args.Length; i++)
if (args[i].StartsWith("-"))
{
if (i + 1 < args.Length && !args[i + 1].StartsWith("-"))
result[args[i]] = args[++i];
else
result[args[i]] = "";
}
return result;
}
}