Example #1
0
        public static void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
        {
            //Log the initiation of the function
            log.LogInformation($"CheckDomains Timer trigger function executed at: {DateTime.Now}");
            //Query LogAnalytics by calling the "GetDomains" function in the LA workspace
            try
            {
                string queryBody            = GetEnvironmentVariable("query_string");
                Task <QueryResults> task    = LogAnalytics.QueryData(queryBody);
                QueryResults        results = task.Result;
                //Ok, now that we have our domains, for each domain returned, call the bootstrap service and get the responsible server
                log.LogInformation(string.Format("Retrieved: {0} rows.  Beginning resolution with Bootstrap lookups", results.tables[0].rows.Count));

                foreach (List <string> rowData in results.tables[0].rows)
                {
                    RDAPResponseRoot responseRoot = null;
                    rowData.ForEach(delegate(string value)
                    {
                        string TLD = value.Split(".")[1];
                        string uri = BootStrapTLD(TLD, log);
                        Console.WriteLine(string.Format("Results of BootStrap call: {0} serviced by {1}", value, uri));
                        if (uri != string.Empty)
                        {
                            //Call the responsible RDAP server
                            responseRoot = QueryRDAP(string.Format("{0}domain/{1}", uri, value), log);
                        }
                        else
                        {
                            Console.WriteLine(string.Format("Unable to process URI :'{0}' for value {1}", uri, value));
                        }
                        if (responseRoot != null)
                        {
                            // Store the results in LogAnalytics.
                            // Build the JSON body from the results
                            RDAPUpdate rdapUpdate = new RDAPUpdate();
                            // there are at least three "events" in the RDAP server response.  Only one of them is "interesting" to use here:  registration.
                            foreach (Event rdapEvent in responseRoot.events)
                            {
                                if (rdapEvent.eventAction == "registration")
                                {
                                    // update the update object with our update
                                    rdapUpdate.domainName       = value;
                                    rdapUpdate.registrationDate = rdapEvent.eventDate;
                                    // Call the WriteData function to store the data in our LA workspace.
                                    LogAnalytics.WriteData(JsonConvert.SerializeObject(rdapUpdate));
                                }
                            }
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Exception: {0}", ex.Message));
            }


            //Log the completion of the function
            log.LogInformation($"CheckDomains Timer trigger function completed at: {DateTime.Now}");
        }
Example #2
0
        /// <summary>
        /// Queries the rdap.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <param name="log">The log.</param>
        /// <returns>RDAPResponseRoot.</returns>
        public static RDAPResponseRoot QueryRDAP(string uri, ILogger log)
        {
            string responseMessage = string.Empty;


            //Log the request
            log.LogInformation(string.Format("QueryRDAP function processed a request for URI '{0}'", uri));

            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(uri);
            RDAPResponseRoot rootNode = null;

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            // Get the response from IANA
            HttpResponseMessage response = client.GetAsync(urlParameters).Result;  // Blocking call!

            if (response.IsSuccessStatusCode)
            {
                var jsonString = response.Content.ReadAsStringAsync();
                jsonString.Wait();
                rootNode = JsonConvert.DeserializeObject <RDAPResponseRoot>(jsonString.Result);
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
            // Dispose of the client since all HttpClient calls are complete.
            client.Dispose();
            // return the URI
            return(rootNode);
        }