Advertisement
Guest User

.NET (C#) simple smartwhois class

a guest
Feb 11th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.39 KB | None | 0 0
  1. /*
  2. ********************************************************************************************
  3. using System;
  4.  
  5. namespace whois
  6. {
  7.     class Program
  8.     {
  9.         static int Main(string[] args)
  10.         {
  11.             int rc = 0;
  12.             WhoisClient wh;
  13.             string result;
  14.  
  15.             if (args.Length < 1)
  16.             {
  17.                 Console.WriteLine("usage: whois <host_or_ip>");
  18.                 return 1;
  19.             }
  20.  
  21.  
  22.             wh = new WhoisClient();
  23.             result = wh.Whois(args[0]);
  24.             if (!string.IsNullOrEmpty(wh.errMsg))
  25.             {
  26.                 Console.WriteLine(string.Format("# ERR: {0}", wh.errMsg));
  27.                 rc = 2;
  28.             }
  29.             if (string.IsNullOrEmpty(result))
  30.             {
  31.                 return 3;
  32.             }
  33.             Console.WriteLine(string.Format("# {0} :: {1}", wh.srvName, args[0]));
  34.             Console.Write(result);
  35.             return rc;
  36.         }
  37.     }
  38. }
  39. ********************************************************************************************
  40. */
  41.  
  42.  
  43. using System;
  44. using System.IO;
  45. using System.Net.Sockets;
  46. using System.Text;
  47.  
  48. namespace whois
  49. {
  50.     class WhoisClient
  51.     {
  52.         #region privatedata
  53.         private const int    WHOIS_PORT = 43;
  54.         private const string BASE_WHOIS = "whois.iana.org";
  55.         private const string TAG_SINGLE = "to single out one record";
  56.         private const string TAG_DOMAIN = "domain name: {0}";
  57.         private const string TAG_REDIR  = "whois server:";
  58.         private const string TAG_HINT   = "whois:";
  59.  
  60.         private string      _lastErrMsg = null;
  61.         private string      _lastServer = null;
  62.         #endregion
  63.  
  64.         #region instance
  65.         /// <summary>
  66.         /// instance the class
  67.         /// </summary>
  68.         public WhoisClient()
  69.         {
  70.             _lastErrMsg = null;
  71.             _lastServer = null;
  72.         }
  73.         #endregion
  74.  
  75.         #region properties
  76.         /// <summary>
  77.         /// returns last error message
  78.         /// </summary>
  79.         public string errMsg
  80.         {
  81.             get { return _lastErrMsg; }
  82.         }
  83.  
  84.         /// <summary>
  85.         /// returns last whois server name
  86.         /// </summary>
  87.         public string srvName
  88.         {
  89.             get { return _lastServer; }
  90.         }
  91.  
  92.         #endregion
  93.  
  94.         #region methods
  95.         /// <summary>
  96.         /// run a whois query
  97.         /// </summary>
  98.         /// <param name="query">query string (IP or domain name)</param>
  99.         /// <param name="server">optional: server name or address</param>
  100.         /// <param name="port">optional: server port</param>
  101.         /// <returns>the whois lookup result or null</returns>
  102.         public string Whois(string query, string server = "", int port = WHOIS_PORT)
  103.         {
  104.             string srv = server;
  105.  
  106.             if (string.IsNullOrEmpty(srv))
  107.             {
  108.                 srv = getServer(query);
  109.                 if (string.IsNullOrEmpty(srv)) return null;
  110.             }
  111.  
  112.             string result = QuerySrv(query, srv, port);
  113.             string single = refineQuery(result, query);
  114.             if (!string.IsNullOrEmpty(single))
  115.             {
  116.                 result = QuerySrv(single, srv, port);
  117.                 if (!string.IsNullOrEmpty(result))
  118.                 {
  119.                     string patt = string.Format(TAG_DOMAIN, query.ToLower());
  120.                     int pos = result.ToLower().IndexOf(patt);
  121.                     if (pos > 0)
  122.                     {
  123.                         result = result.Substring(pos);
  124.                     }
  125.                 }
  126.             }
  127.             if (!string.IsNullOrEmpty(result))
  128.             {
  129.                 string hint = getHint(result, TAG_REDIR);
  130.                 if (!string.IsNullOrEmpty(hint))
  131.                 {
  132.                     result = QuerySrv(query, hint, port);
  133.                 }
  134.             }
  135.             return result;
  136.         }
  137.         #endregion
  138.  
  139.         #region privatecode
  140.         /// <summary>
  141.         /// retrieves the whois server to use for a given query
  142.         /// </summary>
  143.         /// <param name="query">query string</param>
  144.         /// <returns>server name or null</returns>
  145.         private string getServer(string query)
  146.         {
  147.             string buff = QuerySrv(query, BASE_WHOIS, WHOIS_PORT);
  148.             if (string.IsNullOrEmpty(buff)) return null;
  149.             string srv = getHint(buff, TAG_HINT);
  150.             return srv;
  151.         }
  152.  
  153.         /// <summary>
  154.         /// returns the hint/redirection for a query
  155.         /// </summary>
  156.         /// <param name="data">query result data</param>
  157.         /// <param name="tag">redirect tag to search</param>
  158.         /// <returns>redirect server name or null</returns>
  159.         private string getHint(string data, string tag)
  160.         {
  161.             string buf = data.ToLower();
  162.             int pos = buf.IndexOf(tag);
  163.             if (pos < 1) return null;
  164.             buf = buf.Substring(pos + tag.Length);
  165.             pos = buf.IndexOf("\n");
  166.             if (pos < 1) return null;
  167.             buf = buf.Substring(0, pos).Trim();
  168.             return buf;
  169.         }
  170.  
  171.         /// <summary>
  172.         /// checks if query result contains multiple entries
  173.         /// </summary>
  174.         /// <param name="data">query result</param>
  175.         /// <param name="query">query string</param>
  176.         /// <returns>query refinement string or null</returns>
  177.         private string refineQuery(string data, string query)
  178.         {
  179.             string buf = data.ToLower();
  180.             int pos = buf.IndexOf(TAG_SINGLE);
  181.             if (pos < 1) return null;
  182.             return "=" + query.ToUpper();
  183.         }
  184.  
  185.         /// <summary>
  186.         /// issues a whois query to the given server
  187.         /// </summary>
  188.         /// <param name="query">query string</param>
  189.         /// <param name="server">server name or ip</param>
  190.         /// <param name="port">server port</param>
  191.         /// <returns>query result or null</returns>
  192.         private string QuerySrv(string query, string server, int port = 43)
  193.         {
  194.             string result = null;
  195.  
  196.             //  Console.WriteLine(string.Format("# querying {0}:{1} for {2}", server, port, query));
  197.             _lastServer = server;
  198.  
  199.             try
  200.             {
  201.                 Encoding enc = Encoding.ASCII;
  202.                 TcpClient sock = new TcpClient(server, port);
  203.                 using (Stream s = sock.GetStream())
  204.                 {
  205.                     byte[] qry = enc.GetBytes(query + "\r\n");
  206.                     byte[] buf = new byte[8192];
  207.                     StringBuilder resp = new StringBuilder();
  208.                     s.Write(qry, 0, qry.Length);
  209.                     int nbytes = 0;
  210.                     do
  211.                     {
  212.                         nbytes = s.Read(buf, 0, buf.Length);
  213.                         if (nbytes > 0)
  214.                         {
  215.                             resp.Append(enc.GetString(buf, 0, nbytes));
  216.                         }
  217.                     } while (nbytes > 0);
  218.                     result = resp.ToString();
  219.                 }
  220.                 sock.Close();
  221.             }
  222.             catch (Exception ex)
  223.             {
  224.                 _lastErrMsg = string.Format("QuerySrv(\"{0}\",\"{1}\",{2}) :: {3}", query, server, port, ex.Message);
  225.                 result = null;
  226.             }
  227.  
  228.             return result;
  229.         }
  230.         #endregion
  231.  
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement