Last active
September 25, 2016 08:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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