package IPSeeker;
import java.io.File;
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.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Level;
public class IPSeeker {
//纯真IP数据库名
private String IP_FILE="QQWry.Dat";
//保存的文件夹
private String INSTALL_DIR="f:/qqwry";
// 一些固定常量,比如记录长度等等
private static final int IP_RECORD_LENGTH = 7;
private static final byte REDIRECT_MODE_1 = 0x01;
private static final byte REDIRECT_MODE_2 = 0x02;
// 用来做为cache,查询一个ip时首先查看cache,以减少不必要的重复查找
private Map<String, IPLocation> ipCache;
// 随机文件访问类
private RandomAccessFile ipFile;
// 内存映射文件
private MappedByteBuffer mbb;
// 起始地区的开始和结束的绝对偏移
private long ipBegin, ipEnd;
// 为提高效率而采用的临时变量
private IPLocation loc;
private byte[] buf;
private byte[] b4;
private byte[] b3;
public IPSeeker(String fileName,String dir) {
this.INSTALL_DIR=dir;
this.IP_FILE=fileName;
ipCache = new HashMap<String, IPLocation>();
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) {
// 如果找不到这个文件,再尝试再当前目录下搜索,这次全部改用小写文件名
// 因为有些系统可能区分大小写导致找不到ip地址信息文件
String filename = new File(IP_FILE).getName().toLowerCase();
File[] files = new File(INSTALL_DIR).listFiles();
for(int i = 0; i < files.length; i++) {
if(files[i].isFile()) {
if(files[i].getName().toLowerCase().equals(filename)) {
try {
ipFile = new RandomAccessFile(files[i], "r");
} catch (FileNotFoundException e1) {
LogFactory.log("IP地址信息文件没有找到,IP显示功能将无法使用",Level.ERROR,e1);
ipFile = null;
}
break;
}
}
}
}
// 如果打开文件成功,读取文件头信息
if(ipFile != null) {
try {
ipBegin = readLong4(0);
ipEnd = readLong4(4);
if(ipBegin == -1 || ipEnd == -1) {
ipFile.close();
ipFile = null;
}
} catch (IOException e) {
LogFactory.log("IP地址信息文件格式有错误,IP显示功能将无法使用",Level.ERROR,e);
ipFile = null;
}
}
}
/**
* 给定一个地点的不完全名字,得到一系列包含s子串的IP范围记录
* @param s 地点子串
* @return 包含IPEntry类型的List
*/
public List getIPEntriesDebug(String s) {
List<IPEntry> ret = new ArrayList<IPEntry>();
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 ipLoc = getIPLocation(temp);
// 判断是否这个地点里面包含了s子串,如果包含了,添加这个记录到List中,如果没有,继续
if(ipLoc.getCountry().indexOf(s) != -1 || ipLoc.getArea().indexOf(s) != -1) {
IPEntry entry = new IPEntry();
entry.country = ipLoc.getCountry();
entry.area = ipLoc.getArea();
// 得到起始IP
readIP(offset - 4, b4);
entry.beginIp = Util.getIpStringFromBytes(b4);
// 得到结束IP
readIP(temp, b4);
entry.endIp = Util.getIpStringFromBytes(b4);
// 添加该记录
ret.add(entry);
}
}
}
return ret;
}
public IPLocation getIPLocation(String ip){
IPLocation location=new IPLocation();
location.setArea(this.getArea(ip));
location.setCountry(this.getCountry(ip));
return location;
}
/**
* 给定一个地点的不完全名字,得到一系列包含s子串的IP范围记录
* @param s 地点子串
* @return 包含IPEntry类型的List
*/
public List<IPEntry> getIPEntries(String s) {
List<IPEntry> ret = new ArrayList<IPEntry>();
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 ipLoc = getIPLocation(temp);
// 判断是否这个地点里面包含了s子串,如果包含了,添加这个记录到List中,如果没有,继续
if(ipLoc.getCountry().indexOf(s) != -1 || ipLoc.getArea().indexOf(s) != -1) {
IPEntry entry = new IPEntry();
entry.country = ipLoc.getCountry();
entry.area = ipLoc.getArea();
// 得到起始IP
readIP(offset - 4, b4);
entry.beginIp = Util.getIpStringFromBytes(b4);
// 得到结束IP
readIP(temp, b4);
entry.endIp = Util.getIpStringFromBytes(b4);
// 添加该记录
ret.add(entry);
}
}
}
} catch (IOException e) {
LogFactory.log("",Level.ERROR,e);
}
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 M
- 1
- 2
- 3
- 4
前往页