IPSeek.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.IO;
  6. using System.Web;
  7. namespace CP.Common
  8. {
  9. /// <summary>
  10. /// 纯真ip查询类
  11. /// 来自互联网
  12. /// </summary>
  13. public class IPSeek
  14. {
  15. private string dataPath;
  16. private string ip;
  17. private string country;
  18. private string local;
  19. private long firstStartIp = 0;
  20. private long lastStartIp = 0;
  21. private FileStream objfs = null;
  22. private long startIp = 0;
  23. private long endIp = 0;
  24. private int countryFlag = 0;
  25. private long endIpOff = 0;
  26. private string errMsg = null;
  27. public IPSeek()
  28. {
  29. }
  30. public string DataPath
  31. {
  32. set { dataPath = value; }
  33. }
  34. public string IP
  35. {
  36. set { ip = value; }
  37. }
  38. public string Country
  39. {
  40. get { return country; }
  41. }
  42. public string Local
  43. {
  44. get { return local; }
  45. }
  46. public string ErrMsg
  47. {
  48. get { return errMsg; }
  49. }
  50. private int QQwry()
  51. {
  52. string pattern = @"(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))";
  53. Regex objRe = new Regex(pattern);
  54. Match objMa = objRe.Match(ip);
  55. if (!objMa.Success)
  56. {
  57. this.errMsg = "IP格式错误";
  58. return 4;
  59. }
  60. long ip_Int = this.IpToInt(ip);
  61. int nRet = 0;
  62. if (ip_Int >= IpToInt("127.0.0.0") && ip_Int <= IpToInt("127.255.255.255"))
  63. {
  64. this.country = "本机内部环回地址";
  65. this.local = "";
  66. nRet = 1;
  67. }
  68. else if ((ip_Int >= IpToInt("0.0.0.0") && ip_Int <= IpToInt("2.255.255.255")) || (ip_Int >= IpToInt("64.0.0.0") && ip_Int <= IpToInt("126.255.255.255")) || (ip_Int >= IpToInt("58.0.0.0") && ip_Int <= IpToInt("60.255.255.255")))
  69. {
  70. this.country = "网络保留地址";
  71. this.local = "";
  72. nRet = 1;
  73. }
  74. objfs = new FileStream(this.dataPath, FileMode.Open, FileAccess.Read);
  75. try
  76. {
  77. //objfs.Seek(0,SeekOrigin.Begin);
  78. objfs.Position = 0;
  79. byte[] buff = new Byte[8];
  80. objfs.Read(buff, 0, 8);
  81. firstStartIp = buff[0] + buff[1] * 256 + buff[2] * 256 * 256 + buff[3] * 256 * 256 * 256;
  82. lastStartIp = buff[4] * 1 + buff[5] * 256 + buff[6] * 256 * 256 + buff[7] * 256 * 256 * 256;
  83. long recordCount = Convert.ToInt64((lastStartIp - firstStartIp) / 7.0);
  84. if (recordCount <= 1)
  85. {
  86. country = "FileDataError";
  87. objfs.Close();
  88. return 2;
  89. }
  90. long rangE = recordCount;
  91. long rangB = 0;
  92. long recNO = 0;
  93. while (rangB < rangE - 1)
  94. {
  95. recNO = (rangE + rangB) / 2;
  96. this.GetStartIp(recNO);
  97. if (ip_Int == this.startIp)
  98. {
  99. rangB = recNO;
  100. break;
  101. }
  102. if (ip_Int > this.startIp)
  103. rangB = recNO;
  104. else
  105. rangE = recNO;
  106. }
  107. this.GetStartIp(rangB);
  108. this.GetEndIp();
  109. if (this.startIp <= ip_Int && this.endIp >= ip_Int)
  110. {
  111. this.GetCountry();
  112. this.local = this.local.Replace("(我们一定要统一台湾!!!)", "");
  113. }
  114. else
  115. {
  116. nRet = 3;
  117. this.country = "未知";
  118. this.local = "";
  119. }
  120. objfs.Close();
  121. return nRet;
  122. }
  123. catch
  124. {
  125. return 1;
  126. }
  127. }
  128. private long IpToInt(string ip)
  129. {
  130. char[] dot = new char[] { '.' };
  131. string[] ipArr = ip.Split(dot);
  132. if (ipArr.Length == 3)
  133. ip = ip + ".0";
  134. ipArr = ip.Split(dot);
  135. long ip_Int = 0;
  136. long p1 = long.Parse(ipArr[0]) * 256 * 256 * 256;
  137. long p2 = long.Parse(ipArr[1]) * 256 * 256;
  138. long p3 = long.Parse(ipArr[2]) * 256;
  139. long p4 = long.Parse(ipArr[3]);
  140. ip_Int = p1 + p2 + p3 + p4;
  141. return ip_Int;
  142. }
  143. private string IntToIP(long ip_Int)
  144. {
  145. long seg1 = (ip_Int & 0xff000000) >> 24;
  146. if (seg1 < 0)
  147. seg1 += 0x100;
  148. long seg2 = (ip_Int & 0x00ff0000) >> 16;
  149. if (seg2 < 0)
  150. seg2 += 0x100;
  151. long seg3 = (ip_Int & 0x0000ff00) >> 8;
  152. if (seg3 < 0)
  153. seg3 += 0x100;
  154. long seg4 = (ip_Int & 0x000000ff);
  155. if (seg4 < 0)
  156. seg4 += 0x100;
  157. string ip = seg1.ToString() + "." + seg2.ToString() + "." + seg3.ToString() + "." + seg4.ToString();
  158. return ip;
  159. }
  160. private long GetStartIp(long recNO)
  161. {
  162. long offSet = firstStartIp + recNO * 7;
  163. //objfs.Seek(offSet,SeekOrigin.Begin);
  164. objfs.Position = offSet;
  165. byte[] buff = new Byte[7];
  166. objfs.Read(buff, 0, 7);
  167. endIpOff = Convert.ToInt64(buff[4].ToString()) + Convert.ToInt64(buff[5].ToString()) * 256 + Convert.ToInt64(buff[6].ToString()) * 256 * 256;
  168. startIp = Convert.ToInt64(buff[0].ToString()) + Convert.ToInt64(buff[1].ToString()) * 256 + Convert.ToInt64(buff[2].ToString()) * 256 * 256 + Convert.ToInt64(buff[3].ToString()) * 256 * 256 * 256;
  169. return startIp;
  170. }
  171. private long GetEndIp()
  172. {
  173. //objfs.Seek(endIpOff,SeekOrigin.Begin);
  174. objfs.Position = endIpOff;
  175. byte[] buff = new Byte[5];
  176. objfs.Read(buff, 0, 5);
  177. this.endIp = Convert.ToInt64(buff[0].ToString()) + Convert.ToInt64(buff[1].ToString()) * 256 + Convert.ToInt64(buff[2].ToString()) * 256 * 256 + Convert.ToInt64(buff[3].ToString()) * 256 * 256 * 256;
  178. this.countryFlag = buff[4];
  179. return this.endIp;
  180. }
  181. private string GetCountry()
  182. {
  183. switch (this.countryFlag)
  184. {
  185. case 1:
  186. case 2:
  187. this.country = GetFlagStr(this.endIpOff + 4);
  188. this.local = (1 == this.countryFlag) ? " " : this.GetFlagStr(this.endIpOff + 8);
  189. break;
  190. default:
  191. this.country = this.GetFlagStr(this.endIpOff + 4);
  192. this.local = this.GetFlagStr(objfs.Position);
  193. break;
  194. }
  195. return " ";
  196. }
  197. private string GetFlagStr(long offSet)
  198. {
  199. int flag = 0;
  200. byte[] buff = new Byte[3];
  201. while (1 == 1)
  202. {
  203. //objfs.Seek(offSet,SeekOrigin.Begin);
  204. objfs.Position = offSet;
  205. flag = objfs.ReadByte();
  206. if (flag == 1 || flag == 2)
  207. {
  208. objfs.Read(buff, 0, 3);
  209. if (flag == 2)
  210. {
  211. this.countryFlag = 2;
  212. this.endIpOff = offSet - 4;
  213. }
  214. offSet = Convert.ToInt64(buff[0].ToString()) + Convert.ToInt64(buff[1].ToString()) * 256 + Convert.ToInt64(buff[2].ToString()) * 256 * 256;
  215. }
  216. else
  217. {
  218. break;
  219. }
  220. }
  221. if (offSet < 12)
  222. return " ";
  223. objfs.Position = offSet;
  224. return GetStr();
  225. }
  226. private string GetStr()
  227. {
  228. byte lowC = 0;
  229. byte upC = 0;
  230. string str = "";
  231. byte[] buff = new byte[2];
  232. while (1 == 1)
  233. {
  234. lowC = (Byte)objfs.ReadByte();
  235. if (lowC == 0)
  236. break;
  237. if (lowC > 127)
  238. {
  239. upC = (byte)objfs.ReadByte();
  240. buff[0] = lowC;
  241. buff[1] = upC;
  242. System.Text.Encoding enc = System.Text.Encoding.GetEncoding("GB2312");
  243. str += enc.GetString(buff);
  244. }
  245. else
  246. {
  247. str += (char)lowC;
  248. }
  249. }
  250. return str;
  251. }
  252. /// <summary>
  253. /// 返回城市
  254. /// </summary>
  255. /// <returns></returns>
  256. public string IPCountry()
  257. {
  258. this.QQwry();
  259. return this.country;
  260. }
  261. /// <summary>
  262. /// 返回线路
  263. /// </summary>
  264. /// <returns></returns>
  265. public string IPLocal()
  266. {
  267. this.QQwry();
  268. return this.local;
  269. }
  270. public string IPLocation()
  271. {
  272. this.QQwry();
  273. return this.country + this.local;
  274. }
  275. public string IPLocation(string dataPath, string ip)
  276. {
  277. this.dataPath = dataPath;
  278. this.ip = ip;
  279. this.QQwry();
  280. return this.country;// + this.local;
  281. }
  282. /// <summary>
  283. /// 根据ip返回城市
  284. /// </summary>
  285. /// <param name="ip"></param>
  286. /// <returns></returns>
  287. public static string GetIPLocation(string ip)
  288. {
  289. if (string.IsNullOrEmpty(ip))
  290. return "";
  291. IPSeek ipseek = new IPSeek();
  292. string datapath = HttpContext.Current.Server.MapPath("~/Config/data/qqwry.dat");
  293. return ipseek.IPLocation(datapath, ip);
  294. }
  295. }
  296. }