コード例 #1
0
ファイル: GetRBLs.cs プロジェクト: cpolydorou/RBLCheck
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)
        {
            // Log the start
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            // Initialize the configuration object
            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            // Get the storage account information
            string storageAccountName = config["StorageAccountName"];
            string storageAccountKey  = config["StorageAccountKey"];

            // Create the resources
            Azure az = new Azure(storageAccountName, storageAccountKey);

            // Get the hosts
            List <RBL> RBLs = az.GetRBLs();

            // Log the end
            log.LogInformation($"C# Timer trigger function completed at: {DateTime.Now}");

            // Return the result
            var jsonToReturn = JsonConvert.SerializeObject(RBLs);

            return((ActionResult) new OkObjectResult(jsonToReturn));
        }
コード例 #2
0
        public static void Run([TimerTrigger("0 */30 * * * *")] TimerInfo myTimer, ILogger log, ExecutionContext context)
        {
            // Log the start
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            // Temporarily disable the task
            log.LogInformation($"This task has been temporarily disabled");
            return;


            // Initialize the configuration object
            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            // Get the storage account information
            string storageAccountName = config["StorageAccountName"];
            string storageAccountKey  = config["StorageAccountKey"];

            // Create the resources
            Azure az = new Azure(storageAccountName, storageAccountKey);

            // Read the RBLs from the configuration
            List <RBL> MyRBLs = az.GetRBLs();

            // Read the hosts from the configuration
            List <Host> MyHosts = az.GetHosts();

            // Process each host
            foreach (Host host in MyHosts)
            {
                log.LogInformation($"Processing host: {host.Name}");

                foreach (RBL l in MyRBLs)
                {
                    RBLHostResult r = l.QueryHost(host.IP);

                    if (r.IsListed)
                    {
                        log.LogInformation($"\tHost {r.Host} is listed on {r.RBL}");
                    }
                    else
                    {
                        log.LogInformation($"\tHost {r.Host} is NOT listed on {r.RBL}");
                    }
                }
            }

            // Log the end
            log.LogInformation($"C# Timer trigger function completed at: {DateTime.Now}");
        }
コード例 #3
0
ファイル: CheckHost.cs プロジェクト: cpolydorou/RBLCheck
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log, Microsoft.Azure.WebJobs.ExecutionContext context)
        {
            // Log the start
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            // Initialize the configuration object
            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            // Get the storage account information
            string storageAccountName = config["StorageAccountName"];
            string storageAccountKey  = config["StorageAccountKey"];

            // Get the parallelization configuration
            int maxThreads = 0;

            try
            {
                int.TryParse(config["Threads"], out maxThreads);
            }
            catch (Exception e)
            {
                maxThreads = 20;
            }

            // Get the values from the request
            string ip = req.Query["IP"];

            // Validate the input
            if (ip == null)
            {
                return((ActionResult) new BadRequestObjectResult("Failed to get host. IP cannot be null."));
            }

            // Create the resources
            Azure az = new Azure(storageAccountName, storageAccountKey);

            // Check if the host exists
            if (!az.ExistsHost(ip))
            {
                return((ActionResult) new BadRequestObjectResult("The supplied host does not exist."));
            }

            // Read the RBLs from the configuration
            List <RBL> MyRBLs = az.GetRBLs().Where(x => x.Type.ToUpper().Equals("IP")).ToList();

            // Process each host
            log.LogInformation($"Processing host: {ip}");

            // The results
            List <RBLHostResult> results = new List <RBLHostResult>();

            // Create the tasks
            List <Task <RBLHostResult> > tasks = new List <Task <RBLHostResult> >();

            foreach (RBL r in MyRBLs)
            {
                tasks.Add(new Task <RBLHostResult>(() => r.QueryHost(ip)));
            }

            // Start the first batch of tasks
            for (int i = 0; i < maxThreads && i < tasks.Count; i++)
            {
                tasks[i].Start();
            }

            // While there are tasks that are not completed,
            while (tasks.Count > 0)
            {
                // Wait for a task to complete
                var t = await Task.WhenAny(tasks);

                tasks.Remove(t);

                // Process the result
                try
                {
                    results.Add(t.Result);
                }
                catch (OperationCanceledException) { }
                catch (Exception exc) { }

                // Start the next task
                if (tasks.Count > 0)
                {
                    foreach (Task <RBLHostResult> nextTask in tasks)
                    {
                        if (nextTask.Status == TaskStatus.Created)
                        {
                            nextTask.Start();
                            break;
                        }
                    }
                }
            }

            // Wait for all tasks (in case we missed one).
            Task.WaitAll(tasks.ToArray());

            // Log the end
            log.LogInformation($"C# Timer trigger function completed at: {DateTime.Now}");

            // Return the result
            var jsonToReturn = JsonConvert.SerializeObject(results);

            return((ActionResult) new OkObjectResult(jsonToReturn));
        }