Skip to content

Instantly share code, notes, and snippets.

@TheDarkCode
Created November 6, 2018 23:13
Show Gist options
  • Save TheDarkCode/b0793964835589c4088b311a0940fdff to your computer and use it in GitHub Desktop.
Save TheDarkCode/b0793964835589c4088b311a0940fdff to your computer and use it in GitHub Desktop.
FBI Election Complaint Submission Client
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace FBI.ElectionComplaint.Client
{
class Program
{
private static HttpClient _client;
private static string _fbiComplaintUrl = "https://www.justice.gov/crt/complaint/votintake/index.php";
private static string _defaultReferer => _fbiComplaintUrl;
private static string _acceptLang = "en-US,en;q=0.5";
private static string _acceptEncoding = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
private static string _defaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36";
private static long failures = 0;
private static long success = 0;
private static long returnedQuery = 0;
private static long rateLimited = 0;
static void Main(string[] args)
{
// Create Client (Singleton)
_client = new HttpClient();
ExecuteSync(_client, args[0], args[1], args[2], args[3], args[4] ?? "", args[5] ?? "", args[6] ?? "", args[7] ?? "", args[8] ?? "", args[9] ?? "en");
//var host = new JobHost();
//// The following code ensures that the WebJob will be running continuously
//host.RunAndBlock();
//Task.Run(() => ExecuteAsync());
//Console.ReadKey();
}
private static void ExecuteSync(HttpClient client, string state, string jurisdiction, string description, string firstName = "", string lastName = "", string email = "", string phone = "", string phone2 = "", string organization = "", string language = "en")
{
while (true)
{
try
{
var complaint = SubmitElectionComplaint(client, state, jurisdiction, description, firstName, lastName, email, phone, phone2, organization, language).Result;
Console.WriteLine("Complaint has been " + (complaint ? "submitted!" : "not been submitted :("));
//Thread.Sleep(50);
if (!complaint)
{
failures++;
// Rate limited.
Thread.Sleep(60000);
}
else
{
success++;
}
}
catch (Exception e)
{
LogException(e);
}
}
}
private async static void ExecuteAsync(HttpClient client, string state, string jurisdiction, string description, string firstName = "", string lastName = "", string email = "", string phone = "", string phone2 = "", string organization = "", string language = "en")
{
while (true)
{
try
{
var complaint = await SubmitElectionComplaint(client, state, jurisdiction, description, firstName, lastName, email, phone, phone2, organization, language);
Console.WriteLine("Complaint has been " + (complaint ? "submitted!" : "not been submitted :("));
//Thread.Sleep(50);
if (!complaint)
{
failures++;
// Rate limited.
Thread.Sleep(60000);
}
else
{
success++;
}
}
catch (Exception e)
{
LogException(e);
}
}
}
public async static Task<bool> SubmitElectionComplaint(HttpClient client, string state, string jurisdiction, string description, string firstName = "", string lastName = "", string email = "", string phone = "", string phone2 = "", string organization = "", string language = "en")
{
Dictionary<string, string> complaintBody = new Dictionary<string, string>();
// language=en&firstname=&lastname=&org=&phone=&phone2=&email=&state={YOUR_STATE}&juris={YOUR_JURIS}&descr={YOUR_DESCRIPTION}.&btn_accept=This%2Bis%2Bcorrect
complaintBody.Add("language", language);
complaintBody.Add("firstname", firstName);
complaintBody.Add("lastname", lastName);
complaintBody.Add("org", organization);
complaintBody.Add("phone", phone);
complaintBody.Add("phone2", phone2);
complaintBody.Add("email", email);
complaintBody.Add("state", state);
complaintBody.Add("juris", jurisdiction);
complaintBody.Add("descr", description);
complaintBody.Add("btn_accept", "This+is+correct"); // "Submit" if trying to get confirmation page.
using (var postContent = new FormUrlEncodedContent(complaintBody)) {
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, _fbiComplaintUrl);
requestMessage.Headers.Add("Accept", _acceptEncoding);
requestMessage.Headers.Add("Accept-Language", _acceptLang);
requestMessage.Headers.Add("Cache-Control", "no-cache");
requestMessage.Headers.Add("Connection", "keep-alive");
requestMessage.Headers.Add("Cookie", "has_js=1");
requestMessage.Headers.Add("DNT", "1");
requestMessage.Headers.Add("Host", "www.justice.gov");
requestMessage.Headers.Add("Pragma", "no-cache");
requestMessage.Headers.Add("Referer", _defaultReferer);
requestMessage.Headers.Add("Upgrade-Insecure-Requests", "1");
requestMessage.Headers.Add("User-Agent", _defaultUserAgent);
requestMessage.Content = postContent;
using (HttpResponseMessage responseMessage = await client.SendAsync(requestMessage).ConfigureAwait(false)) {
if (!responseMessage.IsSuccessStatusCode) {
return false;
}
else
{
if (responseMessage.Content is null || !(await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false)).Contains("Thank you for your report.")) {
return false;
} else {
return true;
}
}
}
}
}
/// <summary>
/// Log exception error message to the console
/// </summary>
/// <param name="e">The caught exception.</param>
private static void LogException(Exception e)
{
//ConsoleColor color = Console.ForegroundColor;
//Console.ForegroundColor = ConsoleColor.Red;
Exception baseException = e.GetBaseException();
//Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
//Console.ForegroundColor = color;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment