<%@ WebService Language="C#" Class="ServerInfo" %> // /S03_AJAXControls/ServerInfo.asmx // Implementation of a service for the AJAXPopUp exampl. // Copyright (c) by Matthias Hertel, http://www.mathertel.de // This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx // ----- // Implementation of a service for the AJAXPopUp exampl. // ----- // 09.06.2006 created by Matthias Hertel // 14.02.2016 added Scheme and Authority using System; using System.Text; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = ServerInfo.LOCALNAMESPACE)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class ServerInfo : System.Web.Services.WebService { private struct IPC { public Int64 ipFrom; public Int64 ipTo; public string ipCountry; } private const string LOCALNAMESPACE = @"http://www.mathertel.de/ServerInfo/"; private static IPC[] _countryList = null; [WebMethod] public string GetDetails(string url) { StringBuilder sOut = new StringBuilder(); Uri u = new Uri(url); if (! u.IsAbsoluteUri) throw new ArgumentOutOfRangeException("url", "Only absolute urls are accepted."); if (! u.Scheme.StartsWith("http")) throw new ArgumentOutOfRangeException("url", "Only http urls are accepted."); System.Net.IPAddress[] ipList= System.Net.Dns.GetHostAddresses(u.Host); if (ipList.Length == 0) throw new ArgumentOutOfRangeException("url", "Unknown Host:" + u.Host); Int64 adr = IpToInt(ipList[0]); string country3 = country(adr); sOut.AppendFormat("<img src='{0}://{1}/favicon.ico' style='position:absolute;right:4px;' />", u.Scheme, u.Authority); sOut.Append("<b>" + u.Host + "</b><br />"); //sOut.AppendFormat("<img src='http://{0}/favicon.ico' /><br/>", u.Host); sOut.AppendFormat("IP-Address: <b>{0}</b><br />", ipList[0]); sOut.AppendFormat("Int64: <b>{0}</b><br />", adr); sOut.AppendFormat("Country: <b>{0}</b><br />", country3); sOut.AppendFormat("<img src='ipdata/{0}-flag.gif' />", country3); return sOut.ToString(); } // convert an ip address to a Int64 private Int64 IpToInt(System.Net.IPAddress ip) { Int64 ipn = 0; foreach (byte b in ip.GetAddressBytes()) { ipn = ipn * 256 + b; } return (ipn); } // IpToInt // return the country short info for an ip address private string country(Int64 ip) { IPC[] ipList = load(); string c = "unknown"; for (int n = 0; n < ipList.Length; n++) { if (ipList[n].ipFrom > ip) { break; } // if if (ipList[n].ipTo > ip) { c = ipList[n].ipCountry; break; } } // for return (c); } // country // load or return the ip to country list private IPC[] load() { IPC[] ret = _countryList; if (ret == null) { // read the ip2contry file string rawText = System.IO.File.ReadAllText(Server.MapPath("ipdata/ip-to-country.csv")); string[] raw = rawText.Split('\n'); rawText = null; ret = new IPC[raw.Length-1]; for (int n = 0; n < raw.Length-1; n++) { string[] entry = raw[n].Split(','); ret[n].ipFrom = Int64.Parse(entry[0].Replace("\"", String.Empty)); ret[n].ipTo = Int64.Parse(entry[1].Replace("\"", String.Empty)); ret[n].ipCountry = entry[3].Replace("\"", String.Empty); } // foreach raw = null; _countryList = ret; } // if return(ret); } // load }
This page is part of the http://www.mathertel.de/ web site.
For updates and discussions see http://ajaxaspects.blogspot.com/.