コード例 #1
0
        public async Task <ActionResult> Hostname(string ipAddress)
        {
            string hostname = null;

            try {
                IPAddress hostIPAddress = IPAddress.Parse(ipAddress);
                hostname = (await Dns.GetHostEntryAsync(hostIPAddress)).HostName;

                if (!string.IsNullOrEmpty(hostname) && hostname != ipAddress)
                {
                    await LogMessageRelay.StoreHostname(ipAddress, hostname);
                }
            }
            catch (Exception ex) {
                _logMessageRelay.SendMessage($"ERROR: {ipAddress} => {ex.Message}");
            }

            return(Content(string.IsNullOrEmpty(hostname) ? ipAddress : hostname));
        }
コード例 #2
0
        public async Task <ActionResult> Description([FromQuery] DescriptionRequest descriptionRequest)
        {
            try {
                // if there is no IP address info in the request, then returns
                // the description stored in the database if there is one
                if (string.IsNullOrEmpty(descriptionRequest.ipAddress))
                {
                    var www = $"www.{descriptionRequest.domain}";

                    LogMessageRelay.DescriptionsStorage.TryGetValue(descriptionRequest.domain, out string description);

                    if (string.IsNullOrEmpty(description) && !descriptionRequest.domain.StartsWith("www."))
                    {
                        LogMessageRelay.DescriptionsStorage.TryGetValue(www, out description);
                    }

                    return(new JsonResult(new Description {
                        icon = LogMessageRelay.GetIcon(descriptionRequest.domain) ?? LogMessageRelay.GetIcon(www),
                        description = description
                    }));
                }

                ServicePointManager.SecurityProtocol
                    = SecurityProtocolType.Tls
                      | SecurityProtocolType.Tls11
                      | SecurityProtocolType.Tls12;

                ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

                return(await Description(new Uri(descriptionRequest.url), descriptionRequest));
            }
            catch (Exception ex) {
                _logMessageRelay.SendMessage($"ERROR: {descriptionRequest.domain} => {ex.Message}");
            }

            return(Content(null));
        }
コード例 #3
0
 public async Task Hostname(string ipAddress, [FromForm] string hostname)
 {
     await LogMessageRelay.StoreHostname(ipAddress, hostname);
 }
コード例 #4
0
 public ResolveController(LogMessageRelay logMessageRelay)
 {
     _logMessageRelay = logMessageRelay;
 }
コード例 #5
0
        private async Task <ActionResult> Description(HtmlDocument document, DescriptionRequest descriptionRequest)
        {
            var result = new Description();

            if (document == null || string.IsNullOrEmpty(descriptionRequest.url))
            {
                return(Content(null));
            }

            var metaTags = document.DocumentNode.SelectNodes("//meta");

            if (metaTags != null)
            {
                foreach (var tag in metaTags)
                {
                    var name    = tag.Attributes["name"];
                    var content = tag.Attributes["content"];
                    if (name != null && name.Value == "description" && content != null)
                    {
                        result.description = content.Value;
                        break;
                    }
                }
            }

            var linkTags = document.DocumentNode.SelectNodes("//link");

            if (linkTags != null)
            {
                foreach (var tag in linkTags)
                {
                    var rel  = tag.Attributes["rel"];
                    var href = tag.Attributes["href"];
                    if (rel != null && rel.Value.Contains("icon") && href != null)
                    {
                        result.icon = href.Value;
                        if (!result.icon.StartsWith("data:"))
                        {
                            if (result.icon.StartsWith("//"))
                            {
                                result.icon = descriptionRequest.protocol + ":" + result.icon;
                            }
                            else if (result.icon.StartsWith("/"))
                            {
                                result.icon = descriptionRequest.GetBaseUrl() + result.icon;
                            }
                            else if (!result.icon.StartsWith("http"))
                            {
                                result.icon = descriptionRequest.GetBaseUrl() + "/" + result.icon;
                            }
                            result.icon = await DownloadIcon(new Uri(result.icon), descriptionRequest);
                        }
                        break;
                    }
                }
            }

            var titleTag = document.DocumentNode.SelectSingleNode("//title");

            if (titleTag != null)
            {
                result.title = titleTag.InnerHtml;
            }

            // download the icon from the default location if the icon
            // cannot be retrieved from the document
            if (result.icon == null)
            {
                result.icon = await DownloadIcon(new Uri(string.Format("{0}://{1}/favicon.ico",
                                                                       descriptionRequest.protocol, descriptionRequest.domain)), descriptionRequest);
            }

            if (!string.IsNullOrEmpty(result.description))
            {
                await LogMessageRelay.StoreDescription(descriptionRequest.domain, result.description);
            }

            result.bodyText = document.DocumentNode.SelectSingleNode("//body").ToPlainText();

            return(new JsonResult(result));
        }