Skip to content

Instantly share code, notes, and snippets.

@troyhunt
Last active September 25, 2016 08:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troyhunt/fea2e817964708dd7e80d2a624ebf826 to your computer and use it in GitHub Desktop.
Save troyhunt/fea2e817964708dd7e80d2a624ebf826 to your computer and use it in GitHub Desktop.
Pick an IP address up off a queue and block it in CloudFlare
using System;
using System.Configuration;
public static void Run(string myQueueItem, TraceWriter log)
{
log.Info($"C# Queue trigger function processed: {myQueueItem}");
BlockIp(myQueueItem).Wait();
}
static async Task BlockIp(string ipAddress)
{
var zoneId = ConfigurationManager.AppSettings["ZoneId"];
var xAuthEmail = ConfigurationManager.AppSettings["X-Auth-Email"];
var xAuthKey = ConfigurationManager.AppSettings["X-Auth-Key"];
var path = $"/client/v4/zones/{zoneId}/firewall/access_rules/rules";
var body = new
{
mode = "js_challenge",
configuration = new
{
target = "ip",
value = ipAddress
},
notes = $"rate-limit-abuse-{DateTime.UtcNow.ToString("s")}"
};
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.cloudflare.com");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("X-Auth-Email", xAuthEmail);
client.DefaultRequestHeaders.Add("X-Auth-Key", xAuthKey);
await client.PostAsJsonAsync(path, body);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment