package com.epsoft.eiss.base.utils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
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 java.util.Properties;
/**
* IP操作类
*
* @author bianzr
*
*/
public class IPSeeker {
private static final String IP_DATA_PATH = "ip.data.path";
// 一些固定常量,比如记录长度等等
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 transient long ipBegin, ipEnd;
// 为提高效率而采用的临时变量
private IPLocation loc;
private byte[] buf;
private byte[] b4;
private byte[] b3;
public IPSeeker() {
InputStream is = IPSeeker.class.getResourceAsStream("/system-globals.properties");
Properties properties = new Properties();
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
String path = properties.getProperty(IP_DATA_PATH);
ipCache = new HashMap<String, IPLocation>();
loc = new IPLocation();
buf = new byte[100000];
b4 = new byte[4];
b3 = new byte[3];
try {
ipFile = new RandomAccessFile(path, "r");
} catch (FileNotFoundException e) {
ipFile = null;
}
// 如果打开文件成功,读取文件头信息
if (ipFile != null) {
try {
ipBegin = readLong4(0);
ipEnd = readLong4(4);
if (ipBegin == -1 || ipEnd == -1) {
ipFile.close();
ipFile = null;
}
} catch (IOException e) {
ipFile = null;
}
}
}
/**
* 给定一个地点的不完全名字,得到一系列包含s子串的IP范围记录
*
* @param s
* 地点子串
* @return 包含IPEntry类型的List
*/
public List<IPEntry> 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 = IpUtil.getIpStringFromBytes(b4);
// 得到结束IP
readIP(temp, b4);
entry.endIp = IpUtil.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 = IpUtil.getIpStringFromBytes(b4);
// 得到结束IP
readIP(temp, b4);
entry.endIp = IpUtil.getIpStringFromBytes(b4);
// 添加该记录
ret.add(entry);
}
}
}
} catch (IOException 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 null;
// 保存ip,转换ip字节数组为字符串形式
String ipStr = IpUtil.getIpStringFromBytes(ip);
// 先检查cache中是否已经包含有这个ip的结果,没有再搜索文件
if (ipCache.containsKey(ipStr)) {
IPLocation ipLoc = ipCache.get(ipStr);
return ipLoc.getCountry();
} else {
IPLocation ipLoc = getIPLocation(ip);
ipCache.put(ipStr, ipLoc.getCopy());
return ipLoc.getCountry();
}
}
/**
* 根据IP得到省市名称
*
* @param ip
* IP的字符串形式
* @return 省市名称字符串
*/
public String getCountry(String ip) {
return getCountry(IpUtil.getIpByteArrayFromString(ip));
}
/**
* 根据IP得到运营商名称
*
* @param ip
* ip的字节数组形式
* @return 运营商名称字符串
*/
public String getArea(byte[] ip) {
// 检查ip地址文件是否正常
if (ipFile == null)
return null;
// 保存ip,转换ip字节数组为字符串形式
String ipStr = IpUtil.getIpStringFromBytes(ip);
// 先检查cache中是否已经包含有这个ip的结果,没有再搜索文件
if (ipCache.containsKey(ipStr)) {
IPLocation ipLoc = ipCache.get(ipStr);
return ipLoc.getArea();
} else {
IPLocation ipLoc = getIPLocation(ip);
ipCache.put(ipStr, ipLoc.getCopy());
return ipLoc.getArea();
}
}
/**
* 根据IP得到运营商名称
*
* @param ip
* IP的字符串形式
* @return 运营商名称字符串
*/
public String getArea(String ip) {
return getArea(IpUtil.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.setCountry(null);
info.setArea(null);
}
return info;
}
/**
* 从offset位置读取4个字节为一个long,因为java为big-endian格式,所以没办法 用了这么一个函数来做转换
*
* @param offset
* @return 读取的long值,返回-1表示读取文件失败
*/
private long r