-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
405 lines (385 loc) · 19 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
using System;
using System.Net;
using System.Net.Http.Headers;
using System.Security.Authentication;
#pragma warning disable CS0618
#pragma warning disable SYSLIB0039
namespace RequestFaker
{
internal class Program
{
private static string? URI = null, method = null, body = null, customVersion = null, protocol = "1.1";
private static string[]? proxy = null;
private static bool autoRedirects = true, ssl = false;
private static Dictionary<string, string>? headers = null, cookies = null;
private static Request.ProtocolVersion? version = null;
private static HttpVersionPolicy? policy = null;
private static SslProtocols? SSLprotocol = null;
private static DecompressionMethods? decompression = null;
public static string? Protocol { set { protocol = value; } }
private static void Main(string[] args)
{
bool ok = true;
while (true)
{
Initialize();
Console.Clear();
Console.WriteLine("---------------------------------------------------------");
Console.WriteLine("REQUESTFAKER:\n");
Console.WriteLine("Usage: RequestFaker <URI> <method> <headersNumberWithoutCookies> <body> <cookiesNumber> <version> <versionPolicy> <useProxy> <allowRedirects> <SSLVerification> <SSLProtocol> <decompressionMethod>");
Console.WriteLine("---------------------------------------------------------\n\n");
// URI
if (args.Length > 0)
{
URI = args[0];
Console.WriteLine($"\nURI: {URI}");
}
else
{
Console.Write("\nURI: ");
URI = Console.ReadLine();
}
// METHOD
if (args.Length > 1)
{
method = args[1];
Console.WriteLine($"\nMethod: {method}");
}
else
{
Console.Write("\nMethod: ");
method = Console.ReadLine();
}
// HEADERS
string? headersNumber;
if (args.Length > 2)
{
headersNumber = args[2];
Console.WriteLine($"\nHeaders Number (Without Cookies): {headersNumber}");
}
else
{
Console.Write("\nHeaders Number (Without Cookies): ");
headersNumber = Console.ReadLine();
}
if (int.TryParse(headersNumber, out int hNum) && hNum >= 1)
{
headers = new(hNum);
for (int i = 0; i < hNum; i++)
{
Console.Write($"- Header {i + 1} Key: ");
string? key = Console.ReadLine();
if (string.IsNullOrWhiteSpace(key)) { ok = false; break; }
Console.Write($"- Header {i + 1} Value: ");
string? value = Console.ReadLine();
if (string.IsNullOrWhiteSpace(value)) { ok = false; break; }
headers.Add(key, value);
}
if (!ok) { continue; }
}
// BODY
if (args.Length > 3)
{
body = args[3];
Console.WriteLine($"\nBody: {body}");
}
else
{
Console.Write("\nBody: ");
body = Console.ReadLine();
}
// COOKIES
string? cookiesNumber;
if (args.Length > 4)
{
cookiesNumber = args[4];
Console.WriteLine($"\nCookies Number: {cookiesNumber}");
}
else
{
Console.Write("\nCookies Number: ");
cookiesNumber = Console.ReadLine();
}
if (int.TryParse(cookiesNumber, out int cNum) && cNum >= 1)
{
cookies = new(cNum);
for (int i = 0; i < cNum; i++)
{
Console.Write($"- Cookie {i + 1} Name: ");
string? key = Console.ReadLine();
if (string.IsNullOrWhiteSpace(key)) { ok = false; break; }
Console.Write($"- Cookie {i + 1} Value: ");
string? value = Console.ReadLine();
if (string.IsNullOrWhiteSpace(value)) { ok = false; break; }
cookies.Add(key, value);
}
if (!ok) { continue; }
}
// VERSION
string? sVersion;
if (args.Length > 5)
{
sVersion = args[5];
Console.WriteLine($"\nVersion (Default is 1.1): {sVersion}");
}
else
{
Console.Write("\nVersion (Default is 1.1): ");
sVersion = Console.ReadLine();
}
if (!string.IsNullOrWhiteSpace(sVersion) && !sVersion.StartsWith("0"))
{
if (sVersion == "1" || sVersion == "1.0" || sVersion == "10" || sVersion == "v10" || sVersion == "v1.0" || sVersion == "v1")
{
version = Request.ProtocolVersion.v10;
}
else if (sVersion == "1.1" || sVersion == "11" || sVersion == "v11" || sVersion == "v1.1")
{
version = Request.ProtocolVersion.v11;
}
else if (sVersion == "2" || sVersion == "2.0" || sVersion == "20" || sVersion == "v20" || sVersion == "v2.0" || sVersion == "v2")
{
version = Request.ProtocolVersion.v20;
}
else if (sVersion == "3" || sVersion == "3.0" || sVersion == "30" || sVersion == "v30" || sVersion == "v3.0" || sVersion == "v3")
{
version = Request.ProtocolVersion.v30;
}
else if (sVersion.Equals("Unknown", StringComparison.OrdinalIgnoreCase))
{
version = Request.ProtocolVersion.Unknown;
}
else
{
customVersion = sVersion;
}
}
// VERSION POLICY
string? pVersion;
if (args.Length > 6)
{
pVersion = args[6];
Console.WriteLine($"\nVersion Policy: {pVersion}");
}
else
{
Console.Write("\nVersion Policy: ");
pVersion = Console.ReadLine();
}
if (!string.IsNullOrWhiteSpace(pVersion))
{
if (pVersion.Equals("RequestVersionExact", StringComparison.OrdinalIgnoreCase) || pVersion.Equals("Exact", StringComparison.OrdinalIgnoreCase))
{
policy = HttpVersionPolicy.RequestVersionExact;
}
else if (pVersion.Equals("RequestVersionOrHigher", StringComparison.OrdinalIgnoreCase) || pVersion.Equals("Higher", StringComparison.OrdinalIgnoreCase))
{
policy = HttpVersionPolicy.RequestVersionOrHigher;
}
else if (pVersion.Equals("RequestVersionOrLower", StringComparison.OrdinalIgnoreCase) || pVersion.Equals("Lower", StringComparison.OrdinalIgnoreCase))
{
policy = HttpVersionPolicy.RequestVersionOrLower;
}
}
// PROXY
string? proxyConfS;
if (args.Length > 7)
{
proxyConfS = args[7];
Console.WriteLine($"\nUse Proxy (yes/no) (Default is No)? {proxyConfS}");
}
else
{
Console.Write("\nUse Proxy (yes/no) (Default is No)? ");
proxyConfS = Console.ReadLine();
}
if (!string.IsNullOrWhiteSpace(proxyConfS) && (proxyConfS.Equals("y", StringComparison.OrdinalIgnoreCase) || proxyConfS.Equals("yes", StringComparison.OrdinalIgnoreCase) || proxyConfS.Equals("true", StringComparison.OrdinalIgnoreCase)))
{
proxy = new string[3];
Console.Write("\n- Proxy URI: ");
string? proxyURI = Console.ReadLine();
if (!string.IsNullOrEmpty(proxyURI))
{
proxy[0] = proxyURI;
}
Console.Write("\n- Use Credentials? ");
string? credentialsConfS = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(credentialsConfS) && (credentialsConfS.Equals("y", StringComparison.OrdinalIgnoreCase) || credentialsConfS.Equals("yes", StringComparison.OrdinalIgnoreCase) || credentialsConfS.Equals("true", StringComparison.OrdinalIgnoreCase)))
{
Console.Write("\n- Username: ");
string? proxyUsername = Console.ReadLine();
Console.Write("\n- Password: ");
string? proxyPassword = Console.ReadLine();
if (!string.IsNullOrEmpty(proxyUsername) && !string.IsNullOrEmpty(proxyPassword))
{
proxy[1] = proxyUsername;
proxy[2] = proxyPassword;
}
}
}
// REDIRECTS
string? redirectConfS;
if (args.Length > 8)
{
redirectConfS = args[8];
Console.WriteLine($"\nAllow AutoRedirects (Default is True)?: {redirectConfS}");
}
else
{
Console.Write("\nAllow AutoRedirects (Default is True)? ");
redirectConfS = Console.ReadLine();
}
if (!string.IsNullOrWhiteSpace(redirectConfS) && (redirectConfS.Equals("n", StringComparison.OrdinalIgnoreCase) || redirectConfS.Equals("no", StringComparison.OrdinalIgnoreCase) || redirectConfS.Equals("false", StringComparison.OrdinalIgnoreCase)))
{
autoRedirects = false;
}
// SSL VERIFICATION
string? SSLConfS;
if (args.Length > 9)
{
SSLConfS = args[9];
Console.WriteLine($"\nUse SSL Verification (Default is False)? {SSLConfS}");
}
else
{
Console.Write("\nUse SSL Verification (Default is False)? ");
SSLConfS = Console.ReadLine();
}
if (!string.IsNullOrWhiteSpace(SSLConfS) && (SSLConfS.Equals("y", StringComparison.OrdinalIgnoreCase) || SSLConfS.Equals("yes", StringComparison.OrdinalIgnoreCase) || SSLConfS.Equals("true", StringComparison.OrdinalIgnoreCase)))
{
ssl = true;
}
// SSL PROTOCOL
string? SSLp;
if (args.Length > 10)
{
SSLp = args[10];
Console.WriteLine($"\nSSL Protocol: {SSLp}");
}
else
{
Console.Write("\nSSL Protocol (Default is System Automatic): ");
SSLp = Console.ReadLine();
}
if (!string.IsNullOrWhiteSpace(SSLp))
{
if (SSLp.Equals("n", StringComparison.OrdinalIgnoreCase) || SSLp.Equals("no", StringComparison.OrdinalIgnoreCase) || SSLp.Equals("none", StringComparison.OrdinalIgnoreCase))
{
SSLprotocol = SslProtocols.None;
}
else if (SSLp.Equals("d", StringComparison.OrdinalIgnoreCase) || SSLp.Equals("default", StringComparison.OrdinalIgnoreCase))
{
SSLprotocol = SslProtocols.Default;
}
else if (SSLp.Equals("SSL2", StringComparison.OrdinalIgnoreCase) || SSLp.Equals("SSL 2", StringComparison.OrdinalIgnoreCase))
{
SSLprotocol = SslProtocols.Ssl2;
}
else if (SSLp.Equals("SSL3", StringComparison.OrdinalIgnoreCase) || SSLp.Equals("SSL 3", StringComparison.OrdinalIgnoreCase))
{
SSLprotocol = SslProtocols.Ssl3;
}
else if (SSLp.Equals("TLS", StringComparison.OrdinalIgnoreCase))
{
SSLprotocol = SslProtocols.Tls;
}
else if (SSLp.Equals("TLS11", StringComparison.OrdinalIgnoreCase) || SSLp.Equals("TLS1.1", StringComparison.OrdinalIgnoreCase) || SSLp.Equals("TLS 11", StringComparison.OrdinalIgnoreCase) || SSLp.Equals("TLS 1.1", StringComparison.OrdinalIgnoreCase))
{
SSLprotocol = SslProtocols.Tls11;
}
else if (SSLp.Equals("TLS12", StringComparison.OrdinalIgnoreCase) || SSLp.Equals("TLS1.2", StringComparison.OrdinalIgnoreCase) || SSLp.Equals("TLS 12", StringComparison.OrdinalIgnoreCase) || SSLp.Equals("TLS 1.2", StringComparison.OrdinalIgnoreCase))
{
SSLprotocol = SslProtocols.Tls12;
}
else if (SSLp.Equals("TLS13", StringComparison.OrdinalIgnoreCase) || SSLp.Equals("TLS1.3", StringComparison.OrdinalIgnoreCase) || SSLp.Equals("TLS 13", StringComparison.OrdinalIgnoreCase) || SSLp.Equals("TLS 1.3", StringComparison.OrdinalIgnoreCase))
{
SSLprotocol = SslProtocols.Tls13;
}
}
// DECOMPRESSION METHOD
string? decP;
if (args.Length > 11)
{
decP = args[11];
Console.WriteLine($"\nDecompression Method: {decP}");
}
else
{
Console.Write("\nDecompression Method: ");
decP = Console.ReadLine();
}
if (!string.IsNullOrWhiteSpace(decP))
{
if (decP.Equals("a", StringComparison.OrdinalIgnoreCase) || decP.Equals("all", StringComparison.OrdinalIgnoreCase) || decP.Equals("everything", StringComparison.OrdinalIgnoreCase))
{
decompression = DecompressionMethods.All;
}
else if (decP.Equals("b", StringComparison.OrdinalIgnoreCase) || decP.Equals("brotli", StringComparison.OrdinalIgnoreCase))
{
decompression = DecompressionMethods.Brotli;
}
else if (decP.Equals("d", StringComparison.OrdinalIgnoreCase) || decP.Equals("deflate", StringComparison.OrdinalIgnoreCase))
{
decompression = DecompressionMethods.Deflate;
}
else if (decP.Equals("g", StringComparison.OrdinalIgnoreCase) || decP.Equals("gzip", StringComparison.OrdinalIgnoreCase))
{
decompression = DecompressionMethods.GZip;
}
else if (decP.Equals("n", StringComparison.OrdinalIgnoreCase) || decP.Equals("no", StringComparison.OrdinalIgnoreCase) || decP.Equals("none", StringComparison.OrdinalIgnoreCase))
{
decompression = DecompressionMethods.None;
}
}
Console.Clear();
HttpResponseMessage? response = Request.Create(URI, method, headers, body, cookies, version, customVersion, policy, proxy, autoRedirects, ssl, SSLprotocol, decompression).GetAwaiter().GetResult();
if (response != null)
{
Dictionary<string, string> heads = response.Headers.ToDictionary(a => a.Key, a => string.Join(";", a.Value));
Dictionary<string, string> trailingHeaders = response.TrailingHeaders.ToDictionary(a => a.Key, a => string.Join(";", a.Value));
Console.WriteLine("RESPONSE:\n-----------------------------\n");
Console.WriteLine("Status Code: " + response.StatusCode);
if (!string.IsNullOrWhiteSpace(response.ReasonPhrase)) { Console.WriteLine("Reason Phrase: " + response.ReasonPhrase); }
Console.WriteLine($"Protocol Version (Request -> Response): {protocol} -> {response.Version}");
Console.WriteLine("Headers:");
for (int i = 0; i < heads.Count; i++)
{
Console.WriteLine($"- {heads.ElementAt(i).Key}: {heads.ElementAt(i).Value}");
}
Console.WriteLine("Trailing Headers:");
for (int i = 0; i < trailingHeaders.Count; i++)
{
Console.WriteLine($"- {trailingHeaders.ElementAt(i).Key}: {trailingHeaders.ElementAt(i).Value}");
}
Console.Write("\nBody:\n" + response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
}
if (args.Length > 0)
{
Console.WriteLine(Environment.NewLine);
break;
}
else
{
Console.ReadKey();
}
}
}
private static void Initialize()
{
URI = null;
method = null;
body = null;
customVersion = null;
protocol = "1.1";
proxy = null;
autoRedirects = true;
ssl = false;
headers = null;
cookies = null;
version = null;
policy = null;
SSLprotocol = null;
decompression = null;
}
}
}