?? ipseeker.java
字號:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma < stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.blogool.ip;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
/**
*
*
* 用來讀取QQwry.dat文件,以根據ip獲得好友位置,QQwry.dat的格式是 一. 文件頭,共8字節 1. 第一個起始IP的絕對偏移, 4字節 2.
* 最后一個起始IP的絕對偏移, 4字節 二. "結束地址/國家/區域"記錄區 四字節ip地址后跟的每一條記錄分成兩個部分 1. 國家記錄 2. 地區記錄
* 但是地區記錄是不一定有的。而且國家記錄和地區記錄都有兩種形式 1. 以0結束的字符串 2. 4個字節,一個字節可能為0x1或0x2 a.
* 為0x1時,表示在絕對偏移后還跟著一個區域的記錄,注意是絕對偏移之后,而不是這四個字節之后 b. 為0x2時,表示在絕對偏移后沒有區域記錄
* 不管為0x1還是0x2,后三個字節都是實際國家名的文件內絕對偏移
* 如果是地區記錄,0x1和0x2的含義不明,但是如果出現這兩個字節,也肯定是跟著3個字節偏移,如果不是 則為0結尾字符串 三.
* "起始地址/結束地址偏移"記錄區 1. 每條記錄7字節,按照起始地址從小到大排列 a. 起始IP地址,4字節 b. 結束ip地址的絕對偏移,3字節
*
* 注意,這個文件里的ip地址和所有的偏移量均采用little-endian格式,而java是采用 big-endian格式的,要注意轉換
*
*/
public class IPSeeker {
/**
*
*
* 用來封裝ip相關信息,目前只有兩個字段,ip所在的國家和地區
*
*
*
*
* @author 馬若劼
*/
public static void main(String[] args) {
IPSeeker ipseeker = IPSeeker.getInstance();
String sip[] = { "60.177.69.138", "124.91.62.239", "124.91.62.239",
"59.40.87.8", "221.222.73.88", "60.51.114.120",
"222.212.223.146", "60.51.114.120", "60.51.114.120",
"60.51.114.120", "60.51.114.120", "211.99.23.121",
"58.45.85.107", "58.45.85.107", "121.32.94.204",
"117.22.46.178", "218.19.2.195", "58.218.23.10",
"58.218.23.10", "202.173.10.2", "60.178.167.241",
"58.213.137.5", "58.38.165.221", "58.38.165.221",
"61.150.15.25", "58.45.82.76", "218.8.200.210", "59.33.24.233",
"60.176.240.196", "60.9.86.85", "60.51.114.120",
"60.51.114.120", "125.120.155.45", "219.140.61.180",
"219.140.61.180", "59.49.158.100", "59.49.158.100",
"58.218.23.10", "221.220.25.54", "222.71.176.119",
"220.168.115.124", "58.38.165.221", "218.3.251.246",
"58.213.137.5", "218.3.251.246", "121.33.219.235",
"222.130.195.225", "60.51.114.120", "218.3.251.246",
"219.140.61.180", "219.140.61.180", "125.120.155.45",
"60.9.97.186", "124.228.44.177", "218.19.2.195",
"222.70.225.180", "58.38.165.221", "58.38.165.221",
"60.168.67.99", "58.38.165.221", "58.38.165.221",
"61.29.214.76", "220.234.156.254", "60.51.114.120",
"60.51.114.120", "60.51.114.120", "222.168.148.111",
"117.24.28.13", "59.49.158.100", "60.209.253.61",
"61.150.15.83", "60.208.156.110", "124.227.192.131",
"125.120.155.90", "218.83.58.250", "60.191.89.102",
"222.71.176.119", "60.178.167.241", "60.208.111.203",
"222.74.231.66", "58.221.253.118", "122.89.146.55",
"60.51.114.120", "58.221.253.118", "59.49.158.100",
"218.10.158.162", "220.160.38.98", "124.227.192.131",
"219.245.44.214", "211.158.41.214", "219.245.44.185",
"218.0.113.43", "211.158.41.214", "218.68.82.73",
"125.120.144.14", "125.120.155.90", "60.171.169.251",
"124.78.165.8", "124.78.165.8", "218.22.186.125" };
for (int i = 0; i < sip.length; i++) {
System.out.println(ipseeker.getAddress(sip[i]));
}
}
private class IPLocation {
public String country;
public String area;
public IPLocation() {
country = area = "";
}
public IPLocation getCopy() {
IPLocation ret = new IPLocation();
ret.country = country;
ret.area = area;
return ret;
}
}
private static final String IP_FILE = IPSeeker.class.getResource(
"QQWry.dat").toString().substring(5);
// 一些固定常量,比如記錄長度等等
private static final int IP_RECORD_LENGTH = 7;
private static final byte AREA_FOLLOWED = 0x01;
private static final byte NO_AREA = 0x2;
// 用來做為cache,查詢一個ip時首先查看cache,以減少不必要的重復查找
private Hashtable ipCache;
// 隨機文件訪問類
private RandomAccessFile ipFile;
// 內存映射文件
private MappedByteBuffer mbb;
// 單一模式實例
private static IPSeeker instance = new IPSeeker();
// 起始地區的開始和結束的絕對偏移
private long ipBegin, ipEnd;
// 為提高效率而采用的臨時變量
private IPLocation loc;
private byte[] buf;
private byte[] b4;
private byte[] b3;
/**
* 私有構造函數
*/
private IPSeeker() {
ipCache = new Hashtable();
loc = new IPLocation();
buf = new byte[100];
b4 = new byte[4];
b3 = new byte[3];
try {
ipFile = new RandomAccessFile(IP_FILE, "r");
} catch (FileNotFoundException e) {
System.out.println(IPSeeker.class.getResource("QQWry.dat")
.toString());
System.out.println(IP_FILE);
System.out.println("IP地址信息文件沒有找到,IP顯示功能將無法使用");
ipFile = null;
}
// 如果打開文件成功,讀取文件頭信息
if (ipFile != null) {
try {
ipBegin = readLong4(0);
ipEnd = readLong4(4);
if (ipBegin == -1 || ipEnd == -1) {
ipFile.close();
ipFile = null;
}
} catch (IOException e) {
System.out.println("IP地址信息文件格式有錯誤,IP顯示功能將無法使用");
ipFile = null;
}
}
}
/**
* @return 單一實例
*/
public static IPSeeker getInstance() {
return instance;
}
/**
* 給定一個地點的不完全名字,得到一系列包含s子串的IP范圍記錄
*
* @param s
* 地點子串
* @return 包含IPEntry類型的List
*/
public List getIPEntriesDebug(String s) {
List ret = new ArrayList();
long endOffset = ipEnd + 4;
for (long offset = ipBegin + 4; offset <= endOffset; offset += IP_RECORD_LENGTH) {
// 讀取結束IP偏移
long temp = readLong3(offset);
// 如果temp不等于-1,讀取IP的地點信息
if (temp != -1) {
IPLocation loc = getIPLocation(temp);
// 判斷是否這個地點里面包含了s子串,如果包含了,添加這個記錄到List中,如果沒有,繼續
if (loc.country.indexOf(s) != -1 || loc.area.indexOf(s) != -1) {
IPEntry entry = new IPEntry();
entry.country = loc.country;
entry.area = loc.area;
// 得到起始IP
readIP(offset - 4, b4);
entry.beginIp = Utils.getIpStringFromBytes(b4);
// 得到結束IP
readIP(temp, b4);
entry.endIp = Utils.getIpStringFromBytes(b4);
// 添加該記錄
ret.add(entry);
}
}
}
return ret;
}
/**
* 給定一個地點的不完全名字,得到一系列包含s子串的IP范圍記錄
*
* @param s
* 地點子串
* @return 包含IPEntry類型的List
*/
public List getIPEntries(String s) {
List ret = new ArrayList();
try {
// 映射IP信息文件到內存中
if (mbb == null) {
FileChannel fc = ipFile.getChannel();
mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, ipFile.length());
mbb.order(ByteOrder.LITTLE_ENDIAN);
}
int endOffset = (int) ipEnd;
for (int offset = (int) ipBegin + 4; offset <= endOffset; offset += IP_RECORD_LENGTH) {
int temp = readInt3(offset);
if (temp != -1) {
IPLocation loc = getIPLocation(temp);
// 判斷是否這個地點里面包含了s子串,如果包含了,添加這個記錄到List中,如果沒有,繼續
if (loc.country.indexOf(s) != -1
|| loc.area.indexOf(s) != -1) {
IPEntry entry = new IPEntry();
entry.country = loc.country;
entry.area = loc.area;
// 得到起始IP
readIP(offset - 4, b4);
entry.beginIp = Utils.getIpStringFromBytes(b4);
// 得到結束IP
readIP(temp, b4);
entry.endIp = Utils.getIpStringFromBytes(b4);
// 添加該記錄
ret.add(entry);
}
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
return ret;
}
/**
* 從內存映射文件的offset位置開始的3個字節讀取一個int
*
* @param offset
* @return
*/
private int readInt3(int offset) {
mbb.position(offset);
return mbb.getInt() & 0x00FFFFFF;
}
/**
* 從內存映射文件的當前位置開始的3個字節讀取一個int
*
* @return
*/
private int readInt3() {
return mbb.getInt() & 0x00FFFFFF;
}
/**
* 根據IP得到國家名
*
* @param ip
* ip的字節數組形式
* @return 國家名字符串
*/
public String getCountry(byte[] ip) {
// 檢查ip地址文件是否正常
if (ipFile == null)
return "錯誤的IP數據庫文件";
// 保存ip,轉換ip字節數組為字符串形式
String ipStr = Utils.getIpStringFromBytes(ip);
// 先檢查cache中是否已經包含有這個ip的結果,沒有再搜索文件
if (ipCache.containsKey(ipStr)) {
IPLocation loc = (IPLocation) ipCache.get(ipStr);
return loc.country;
} else {
IPLocation loc = getIPLocation(ip);
ipCache.put(ipStr, loc.getCopy());
return loc.country;
}
}
/**
* 根據IP得到國家名
*
* @param ip
* IP的字符串形式
* @return 國家名字符串
*/
public String getCountry(String ip) {
return getCountry(Utils.getIpByteArrayFromString(ip));
}
/**
* 根據IP得到地區名
*
* @param ip
* ip的字節數組形式
* @return 地區名字符串
*/
public String getArea(byte[] ip) {
// 檢查ip地址文件是否正常
if (ipFile == null)
return "錯誤的IP數據庫文件";
// 保存ip,轉換ip字節數組為字符串形式
String ipStr = Utils.getIpStringFromBytes(ip);
// 先檢查cache中是否已經包含有這個ip的結果,沒有再搜索文件
if (ipCache.containsKey(ipStr)) {
IPLocation loc = (IPLocation) ipCache.get(ipStr);
return loc.area;
} else {
IPLocation loc = getIPLocation(ip);
ipCache.put(ipStr, loc.getCopy());
return loc.area;
}
}
/**
* 根據IP得到地區名
*
* @param ip
* IP的字符串形式
* @return 地區名字符串
*/
public String getArea(String ip) {
return getArea(Utils.getIpByteArrayFromString(ip));
}
/**
* 根據ip搜索ip信息文件,得到IPLocation結構,所搜索的ip參數從類成員ip中得到
*
* @param ip
* 要查詢的IP
* @return IPLocation結構
*/
private IPLocation getIPLocation(byte[] ip) {
IPLocation info = null;
long offset = locateIP(ip);
if (offset != -1)
info = getIPLocation(offset);
if (info == null) {
info = new IPLocation();
info.country = "未知國家";
info.area = "未知地區";
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -