IPSearcher.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using TEAMModelOS.SDK.IP2Region;
  6. namespace TEAMModelOS.SDK
  7. {
  8. public class IPSearcher : IDisposable
  9. {
  10. const int BTREE_ALGORITHM = 1;
  11. const int BINARY_ALGORITHM = 2;
  12. const int MEMORY_ALGORITYM = 3;
  13. private IPConfig _dbConfig = null;
  14. /// <summary>
  15. /// db file access handler
  16. /// </summary>
  17. private Stream _raf = null;
  18. /// <summary>
  19. /// header blocks buffer
  20. /// </summary>
  21. private long[] _headerSip = null;
  22. private int[] _headerPtr = null;
  23. private int _headerLength;
  24. /// <summary>
  25. /// super blocks info
  26. /// </summary>
  27. private long _firstIndexPtr = 0;
  28. private long _lastIndexPtr = 0;
  29. private int _totalIndexBlocks = 0;
  30. /// <summary>
  31. /// for memory mode
  32. /// the original db binary string
  33. /// </summary>
  34. private byte[] _dbBinStr = null;
  35. /// <summary>
  36. /// Get by index ptr.
  37. /// </summary>
  38. private DataBlock GetByIndexPtr(long ptr)
  39. {
  40. _raf.Seek(ptr, SeekOrigin.Begin);
  41. byte[] buffer = new byte[12];
  42. _raf.Read(buffer, 0, buffer.Length);
  43. long extra = Utils.GetIntLong(buffer, 8);
  44. int dataLen = (int)((extra >> 24) & 0xFF);
  45. int dataPtr = (int)((extra & 0x00FFFFFF));
  46. _raf.Seek(dataPtr, SeekOrigin.Begin);
  47. byte[] data = new byte[dataLen];
  48. _raf.Read(data, 0, data.Length);
  49. int city_id = (int)Utils.GetIntLong(data, 0);
  50. string region = Encoding.UTF8.GetString(data, 4, data.Length - 4);
  51. return new DataBlock(city_id, region, dataPtr);
  52. }
  53. public IPSearcher(IPConfig dbConfig, string dbFile)
  54. {
  55. if (_dbConfig == null)
  56. {
  57. _dbConfig = dbConfig;
  58. }
  59. try {
  60. _raf = new FileStream(dbFile, FileMode.Open, FileAccess.Read, FileShare.Read);
  61. } catch { }
  62. }
  63. public IPSearcher(string dbFile) : this(null, dbFile) { }
  64. public IPSearcher(IPConfig dbConfig, Stream dbFileStream)
  65. {
  66. if (_dbConfig == null)
  67. {
  68. _dbConfig = dbConfig;
  69. }
  70. _raf = dbFileStream;
  71. }
  72. public IPSearcher(Stream dbFileStream) : this(null, dbFileStream) { }
  73. #region Sync Methods
  74. /// <summary>
  75. /// Get the region with a int ip address with memory binary search algorithm.
  76. /// </summary>
  77. private DataBlock MemorySearch(long ip)
  78. {
  79. int blen = IndexBlock.LENGTH;
  80. if (_dbBinStr == null)
  81. {
  82. if (_raf == null) { return null; }
  83. try { _dbBinStr = new byte[(int)_raf.Length]; } catch (Exception) { return null; }
  84. _raf.Seek(0L, SeekOrigin.Begin);
  85. _raf.Read(_dbBinStr, 0, _dbBinStr.Length);
  86. //initialize the global vars
  87. _firstIndexPtr = Utils.GetIntLong(_dbBinStr, 0);
  88. _lastIndexPtr = Utils.GetIntLong(_dbBinStr, 4);
  89. _totalIndexBlocks = (int)((_lastIndexPtr - _firstIndexPtr) / blen) + 1;
  90. }
  91. //search the index blocks to define the data
  92. int l = 0, h = _totalIndexBlocks;
  93. long sip = 0;
  94. while (l <= h)
  95. {
  96. int m = (l + h) >> 1;
  97. int p = (int)(_firstIndexPtr + m * blen);
  98. sip = Utils.GetIntLong(_dbBinStr, p);
  99. if (ip < sip)
  100. {
  101. h = m - 1;
  102. }
  103. else
  104. {
  105. sip = Utils.GetIntLong(_dbBinStr, p + 4);
  106. if (ip > sip)
  107. {
  108. l = m + 1;
  109. }
  110. else
  111. {
  112. sip = Utils.GetIntLong(_dbBinStr, p + 8);
  113. break;
  114. }
  115. }
  116. }
  117. //not matched
  118. if (sip == 0) return null;
  119. //get the data
  120. int dataLen = (int)((sip >> 24) & 0xFF);
  121. int dataPtr = (int)((sip & 0x00FFFFFF));
  122. int city_id = (int)Utils.GetIntLong(_dbBinStr, dataPtr);
  123. string region = Encoding.UTF8.GetString(_dbBinStr, dataPtr + 4, dataLen - 4);//new String(dbBinStr, dataPtr + 4, dataLen - 4, Encoding.UTF8);
  124. return new DataBlock(city_id, region, dataPtr);
  125. }
  126. /// <summary>
  127. /// Get the region throught the ip address with memory binary search algorithm.
  128. /// </summary>
  129. public DataBlock MemorySearch(string ip)
  130. {
  131. return MemorySearch(Utils.Ip2long(ip));
  132. }
  133. /// <summary>
  134. /// Get the region with a int ip address with b-tree algorithm.
  135. /// </summary>
  136. private DataBlock BtreeSearch(long ip)
  137. {
  138. //check and load the header
  139. if (_headerSip == null)
  140. {
  141. _raf.Seek(8L, SeekOrigin.Begin); //pass the super block
  142. byte[] b = new byte[4096];
  143. _raf.Read(b, 0, b.Length);
  144. //fill the header
  145. int len = b.Length >> 3, idx = 0; //b.lenght / 8
  146. _headerSip = new long[len];
  147. _headerPtr = new int[len];
  148. long startIp, dataPtrTemp;
  149. for (int i = 0; i < b.Length; i += 8)
  150. {
  151. startIp = Utils.GetIntLong(b, i);
  152. dataPtrTemp = Utils.GetIntLong(b, i + 4);
  153. if (dataPtrTemp == 0) break;
  154. _headerSip[idx] = startIp;
  155. _headerPtr[idx] = (int)dataPtrTemp;
  156. idx++;
  157. }
  158. _headerLength = idx;
  159. }
  160. //1. define the index block with the binary search
  161. if (ip == _headerSip[0])
  162. {
  163. return GetByIndexPtr(_headerPtr[0]);
  164. }
  165. else if (ip == _headerPtr[_headerLength - 1])
  166. {
  167. return GetByIndexPtr(_headerPtr[_headerLength - 1]);
  168. }
  169. int l = 0, h = _headerLength, sptr = 0, eptr = 0;
  170. int m = 0;
  171. while (l <= h)
  172. {
  173. m = (l + h) >> 1;
  174. //perfectly matched, just return it
  175. if (ip == _headerSip[m])
  176. {
  177. if (m > 0)
  178. {
  179. sptr = _headerPtr[m - 1];
  180. eptr = _headerPtr[m];
  181. }
  182. else
  183. {
  184. sptr = _headerPtr[m];
  185. eptr = _headerPtr[m + 1];
  186. }
  187. }
  188. //less then the middle value
  189. else if (ip < _headerSip[m])
  190. {
  191. if (m == 0)
  192. {
  193. sptr = _headerPtr[m];
  194. eptr = _headerPtr[m + 1];
  195. break;
  196. }
  197. else if (ip > _headerSip[m - 1])
  198. {
  199. sptr = _headerPtr[m - 1];
  200. eptr = _headerPtr[m];
  201. break;
  202. }
  203. h = m - 1;
  204. }
  205. else
  206. {
  207. if (m == _headerLength - 1)
  208. {
  209. sptr = _headerPtr[m - 1];
  210. eptr = _headerPtr[m];
  211. break;
  212. }
  213. else if (ip <= _headerSip[m + 1])
  214. {
  215. sptr = _headerPtr[m];
  216. eptr = _headerPtr[m + 1];
  217. break;
  218. }
  219. l = m + 1;
  220. }
  221. }
  222. //match nothing just stop it
  223. if (sptr == 0) return null;
  224. //2. search the index blocks to define the data
  225. int blockLen = eptr - sptr, blen = IndexBlock.LENGTH;
  226. byte[] iBuffer = new byte[blockLen + blen]; //include the right border block
  227. _raf.Seek(sptr, SeekOrigin.Begin);
  228. _raf.Read(iBuffer, 0, iBuffer.Length);
  229. l = 0; h = blockLen / blen;
  230. long sip = 0;
  231. int p = 0;
  232. while (l <= h)
  233. {
  234. m = (l + h) >> 1;
  235. p = m * blen;
  236. sip = Utils.GetIntLong(iBuffer, p);
  237. if (ip < sip)
  238. {
  239. h = m - 1;
  240. }
  241. else
  242. {
  243. sip = Utils.GetIntLong(iBuffer, p + 4);
  244. if (ip > sip)
  245. {
  246. l = m + 1;
  247. }
  248. else
  249. {
  250. sip = Utils.GetIntLong(iBuffer, p + 8);
  251. break;
  252. }
  253. }
  254. }
  255. //not matched
  256. if (sip == 0) return null;
  257. //3. get the data
  258. int dataLen = (int)((sip >> 24) & 0xFF);
  259. int dataPtr = (int)((sip & 0x00FFFFFF));
  260. _raf.Seek(dataPtr, SeekOrigin.Begin);
  261. byte[] data = new byte[dataLen];
  262. _raf.Read(data, 0, data.Length);
  263. int city_id = (int)Utils.GetIntLong(data, 0);
  264. String region = Encoding.UTF8.GetString(data, 4, data.Length - 4);// new String(data, 4, data.Length - 4, "UTF-8");
  265. return new DataBlock(city_id, region, dataPtr);
  266. }
  267. /// <summary>
  268. /// Get the region throught the ip address with b-tree search algorithm.
  269. /// </summary>
  270. public DataBlock BtreeSearch(string ip)
  271. {
  272. return BtreeSearch(Utils.Ip2long(ip));
  273. }
  274. /// <summary>
  275. /// Get the region with a int ip address with binary search algorithm.
  276. /// </summary>
  277. private DataBlock BinarySearch(long ip)
  278. {
  279. int blen = IndexBlock.LENGTH;
  280. if (_totalIndexBlocks == 0)
  281. {
  282. _raf.Seek(0L, SeekOrigin.Begin);
  283. byte[] superBytes = new byte[8];
  284. _raf.Read(superBytes, 0, superBytes.Length);
  285. //initialize the global vars
  286. _firstIndexPtr = Utils.GetIntLong(superBytes, 0);
  287. _lastIndexPtr = Utils.GetIntLong(superBytes, 4);
  288. _totalIndexBlocks = (int)((_lastIndexPtr - _firstIndexPtr) / blen) + 1;
  289. }
  290. //search the index blocks to define the data
  291. int l = 0, h = _totalIndexBlocks;
  292. byte[] buffer = new byte[blen];
  293. long sip = 0;
  294. while (l <= h)
  295. {
  296. int m = (l + h) >> 1;
  297. _raf.Seek(_firstIndexPtr + m * blen, SeekOrigin.Begin); //set the file pointer
  298. _raf.Read(buffer, 0, buffer.Length);
  299. sip = Utils.GetIntLong(buffer, 0);
  300. if (ip < sip)
  301. {
  302. h = m - 1;
  303. }
  304. else
  305. {
  306. sip = Utils.GetIntLong(buffer, 4);
  307. if (ip > sip)
  308. {
  309. l = m + 1;
  310. }
  311. else
  312. {
  313. sip = Utils.GetIntLong(buffer, 8);
  314. break;
  315. }
  316. }
  317. }
  318. //not matched
  319. if (sip == 0) return null;
  320. //get the data
  321. int dataLen = (int)((sip >> 24) & 0xFF);
  322. int dataPtr = (int)((sip & 0x00FFFFFF));
  323. _raf.Seek(dataPtr, SeekOrigin.Begin);
  324. byte[] data = new byte[dataLen];
  325. _raf.Read(data, 0, data.Length);
  326. int city_id = (int)Utils.GetIntLong(data, 0);
  327. String region = Encoding.UTF8.GetString(data, 4, data.Length - 4);//new String(data, 4, data.Length - 4, "UTF-8");
  328. return new DataBlock(city_id, region, dataPtr);
  329. }
  330. /// <summary>
  331. /// Get the region throught the ip address with binary search algorithm.
  332. /// </summary>
  333. public DataBlock BinarySearch(String ip)
  334. {
  335. return BinarySearch(Utils.Ip2long(ip));
  336. }
  337. #endregion
  338. #region Async Methods
  339. /// <summary>
  340. /// Get the region throught the ip address with memory binary search algorithm.
  341. /// </summary>
  342. public Task<DataBlock> MemorySearchAsync(string ip)
  343. {
  344. return Task.FromResult(MemorySearch(ip));
  345. }
  346. public async Task<string> SearchIpAsync( string ip)
  347. {
  348. try
  349. {
  350. DataBlock block = await MemorySearchAsync(ip);
  351. if (block != null)
  352. {
  353. string region = block.Region.Replace("0|0|0|0|", "").Replace("0|0|0|", "").Replace("|0|0|0|0", "").Replace("|0|0|0|", "").Replace("|0|0|0", "").Replace("|0|0|", "").Replace("|0|0", "").Replace("|0|", "·").Replace("|0", "").Replace("|", "·");
  354. if (!string.IsNullOrWhiteSpace(region))
  355. {
  356. region = region.Replace("中国·", "").Replace("中国", "").Replace("台湾省", "台湾");
  357. }
  358. return region;
  359. }
  360. else { return null; }
  361. }
  362. catch (IPInValidException)
  363. {
  364. return "IP Illigel.";
  365. }
  366. catch (Exception) {
  367. return null;
  368. }
  369. }
  370. /// <summary>
  371. /// Get the region throught the ip address with b-tree search algorithm.
  372. /// </summary>
  373. public Task<DataBlock> BtreeSearchAsync(string ip)
  374. {
  375. return Task.FromResult(BtreeSearch(ip));
  376. }
  377. /// <summary>
  378. /// Get the region throught the ip address with binary search algorithm.
  379. /// </summary>
  380. public Task<DataBlock> BinarySearchAsync(string ip)
  381. {
  382. return Task.FromResult(BinarySearch(ip));
  383. }
  384. #endregion
  385. /// <summary>
  386. /// Close the db.
  387. /// </summary>
  388. public void Close()
  389. {
  390. _headerSip = null;
  391. _headerPtr = null;
  392. _dbBinStr = null;
  393. _raf.Close();
  394. }
  395. public void Dispose()
  396. {
  397. Close();
  398. }
  399. }
  400. }