IPScanerHelper.cs 9.9 KB

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