org.lic.ip.ipseeker.IPSeeker.java Source code

Java tutorial

Introduction

Here is the source code for org.lic.ip.ipseeker.IPSeeker.java

Source

/*
 * 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
 */

/*
 * ??????
 * modified by liyalong      2012.07.31
 */
package org.lic.ip.ipseeker;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.StringTokenizer;

import org.apache.commons.lang.time.StopWatch;
import org.apache.log4j.Logger;

import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import org.lic.ip.iplocator.IPLocation;

/**
 * <pre>
 * IP???LumaQQIP???
 * </pre>
 * 
 * @author luma
 */
public class IPSeeker {

    private static final int IP_RECORD_LENGTH = 7;

    private static final byte REDIRECT_MODE_1 = 0x01;

    private static final byte REDIRECT_MODE_2 = 0x02;

    private static Logger logger = Logger.getLogger(IPSeeker.class);

    private static final ConcurrentLinkedHashMap<String, IPLocation> ipCacheMap = new ConcurrentLinkedHashMap.Builder<String, IPLocation>()
            .initialCapacity(100000).maximumWeightedCapacity(100000).build();

    private RandomAccessFile ipFile;

    private MappedByteBuffer mbb;

    private static ThreadLocal<IPSeeker> localInstance = new ThreadLocal<IPSeeker>();

    private int ipBegin;

    private int ipEnd;

    private byte[] tmpBuf;

    private byte[] tmpB4;

    private static final String ip_filename = "qqwry.dat";

    /**
     * ?
     */
    private IPSeeker() {
        tmpBuf = new byte[100];
        tmpB4 = new byte[4];

        try {
            String filepath = getClass().getClassLoader().getResource("qqwry.dat").getPath();
            ipFile = new RandomAccessFile(filepath, "r");
            // ipFile = new RandomAccessFile(ClassLoader.getSystemResource(
            // ip_filename).getPath(), "r");
        } catch (IOException e) {
            logger.error("IP??IP");
            return;
        }

        if (ipFile == null)
            return;

        // ??
        try {
            ipBegin = readInt4(0);
            ipEnd = readInt4(4);
            if (ipBegin == -1 || ipEnd == -1) {
                ipFile.close();
                ipFile = null;
                return;
            }
        } catch (IOException e) {
            logger.error("IP???IP");
            ipFile = null;
            return;
        }

        // IP?
        try {
            FileChannel fc = ipFile.getChannel();
            long ipFileLen = ipFile.length();
            mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, ipFileLen);
            mbb.order(ByteOrder.LITTLE_ENDIAN);
            ipFile.close();

            logger.info("read ip file to memory, len = " + ipFileLen + " bytes");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static IPSeeker getInstance() {
        IPSeeker instance = localInstance.get();
        if (instance == null) {
            instance = new IPSeeker();
            localInstance.set(instance);
        }
        return instance;
    }

    /**
     * ?ip
     */
    public IPLocation getIPLocation(String ip) {
        byte[] ipBytes = getIpByteArrayFromString(ip); // ??0??
        String ipSeg = getIpStringFromBytes(ipBytes); // 123.58.181.0/24

        IPLocation loc = ipCacheMap.get(ipSeg);
        if (loc == null) {
            loc = getIPLocation(ipBytes);
            ipCacheMap.put(ipSeg, loc);
        }
        return loc;
    }

    /**
     * ?ip
     */
    public List<IPEntry> getIPEntries(String s) {
        List<IPEntry> ret = new ArrayList<IPEntry>();

        int endOffset = ipEnd;
        for (int offset = ipBegin + 4; offset <= endOffset; offset += IP_RECORD_LENGTH) {
            int temp = readInt3(offset);
            if (temp != -1) {
                IPLocation ipLoc = getIPLocation(temp);
                // ???s??List
                if (ipLoc.country.indexOf(s) != -1 || ipLoc.area.indexOf(s) != -1) {
                    IPEntry entry = new IPEntry();
                    entry.country = ipLoc.country;
                    entry.area = ipLoc.area;
                    // IP
                    readIP(offset - 4, tmpB4);
                    entry.beginIp = getIpStringFromBytes(tmpB4);
                    // ?IP
                    readIP(temp, tmpB4);
                    entry.endIp = getIpStringFromBytes(tmpB4);
                    // 
                    ret.add(entry);
                }
            }
        }
        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?ip?IPLocation?ip??ip
     * 
     * @param ip
     *            ?IP
     * @return IPLocation
     */
    private IPLocation getIPLocation(byte[] ip) {
        IPLocation loc = null;
        int offset = locateIP(ip);
        if (offset != -1)
            loc = getIPLocation(offset);
        if (loc == null) {
            loc = new IPLocation();
            loc.country = IPLocation.UNKNOWN_COUNTRY;
            loc.area = IPLocation.UNKNOWN_AREA;
        }
        return loc;
    }

    /**
     * offset??4longjavabig-endian? ???
     * 
     * @param offset
     * @return ?long-1?
     */
    private int readInt4(int offset) {
        if (ipFile == null)
            return -1;

        int ret = 0;
        try {
            ipFile.seek(offset);
            ret |= (ipFile.readByte() & 0xFF);
            ret |= ((ipFile.readByte() << 8) & 0xFF00);
            ret |= ((ipFile.readByte() << 16) & 0xFF0000);
            ret |= ((ipFile.readByte() << 24) & 0xFF000000);
            return ret;
        } catch (IOException e) {
            return -1;
        }
    }

    /**
     * offset??ip?ip??ipbig-endian?
     * little-endian??
     * 
     * @param offset
     * @param ip
     */
    private void readIP(int offset, byte[] ip) {
        mbb.position(offset);
        mbb.get(ip);
        byte temp = ip[0];
        ip[0] = ip[3];
        ip[3] = temp;
        temp = ip[1];
        ip[1] = ip[2];
        ip[2] = temp;
    }

    /**
     * ?ipbeginIp?beginIpbig-endian
     * 
     * @param ip
     *            ?IP
     * @param beginIp
     *            IPIP
     * @return 0ipbeginIp1?-1
     */
    private int compareIP(byte[] ip, byte[] beginIp) {
        for (int i = 0; i < 4; i++) {
            int r = compareByte(ip[i], beginIp[i]);
            if (r != 0)
                return r;
        }
        return 0;
    }

    /**
     * byte?
     * 
     * @param b1
     * @param b2
     * @return b1b210?-1
     */
    private int compareByte(byte b1, byte b2) {
        if ((b1 & 0xFF) > (b2 & 0xFF)) // ?
            return 1;
        else if ((b1 ^ b2) == 0)// ?
            return 0;
        else
            return -1;
    }

    /**
     * ?ip??ip??? 
     * 
     * @param ip
     *            ?IP
     * @return ?IP??-1
     */
    private int locateIP(byte[] ip) {
        int m = 0;
        int r;
        // ip
        readIP(ipBegin, tmpB4);
        r = compareIP(ip, tmpB4);
        if (r == 0)
            return ipBegin;
        else if (r < 0)
            return -1;
        // ?
        for (int i = ipBegin, j = ipEnd; i < j;) {
            m = getMiddleOffset(i, j);
            readIP(m, tmpB4);
            r = compareIP(ip, tmpB4);
            // log.debug(Utils.getIpStringFromBytes(b));
            if (r > 0)
                i = m;
            else if (r < 0) {
                if (m == j) {
                    j -= IP_RECORD_LENGTH;
                    m = j;
                } else
                    j = m;
            } else
                return readInt3(m + 4);
        }
        // ?ij??
        // ??????
        m = readInt3(m + 4);
        readIP(m, tmpB4);
        r = compareIP(ip, tmpB4);
        if (r <= 0)
            return m;
        else
            return -1;
    }

    /**
     * begin??end?????
     * 
     * @param begin
     * @param end
     * @return
     */
    private int getMiddleOffset(int begin, int end) {
        int records = (end - begin) / IP_RECORD_LENGTH;
        records >>= 1;
        if (records == 0)
            records = 1;
        return begin + records * IP_RECORD_LENGTH;
    }

    /**
     * ip??IPLocation?
     * 
     * @param offset
     *            ??
     * @return IPLocation
     */
    private IPLocation getIPLocation(int offset) {
        IPLocation loc = new IPLocation();
        // 4ip
        mbb.position(offset + 4);
        // ??
        byte b = mbb.get();
        if (b == REDIRECT_MODE_1) {
            // ???
            int countryOffset = readInt3();
            // ??
            mbb.position(countryOffset);
            // ?????
            b = mbb.get();
            if (b == REDIRECT_MODE_2) {
                loc.country = readString(readInt3());
                mbb.position(countryOffset + 4);
            } else
                loc.country = readString(countryOffset);
            // ?
            loc.area = readArea(mbb.position());
        } else if (b == REDIRECT_MODE_2) {
            loc.country = readString(readInt3());
            loc.area = readArea(offset + 8);
        } else {
            loc.country = readString(mbb.position() - 1);
            loc.area = readArea(mbb.position());
        }
        return loc;
    }

    /**
     * @param offset
     *            ??
     * @return ??
     */
    private String readArea(int offset) {
        mbb.position(offset);
        byte b = mbb.get();
        if (b == REDIRECT_MODE_1 || b == REDIRECT_MODE_2) {
            int areaOffset = readInt3();
            if (areaOffset == 0)
                return IPLocation.UNKNOWN_AREA;
            else
                return readString(areaOffset);
        } else
            return readString(offset);
    }

    /**
     * offset?0
     * 
     * @param offset
     *            ??
     * @return ?
     */
    private String readString(int offset) {
        try {
            mbb.position(offset);
            int i;
            for (i = 0, tmpBuf[i] = mbb.get(); tmpBuf[i] != 0; tmpBuf[++i] = mbb.get())
                ;
            if (i != 0)
                return getString(tmpBuf, 0, i, "GBK");
        } catch (IllegalArgumentException e) {
            logger.error(e.getMessage());
        }
        return "";
    }

    /**
     * @param ip
     *            ip?
     * @return ?ip
     */
    private static String getIpStringFromBytes(byte[] ip) {
        StringBuilder sb = new StringBuilder();
        sb.append(ip[0] & 0xFF);
        sb.append('.');
        sb.append(ip[1] & 0xFF);
        sb.append('.');
        sb.append(ip[2] & 0xFF);
        sb.append('.');
        sb.append(ip[3] & 0xFF);
        return sb.toString();
    }

    /**
     * ip??
     * 
     * @param ip
     *            ?ip
     * @return ?ip
     */
    private static byte[] getIpByteArrayFromString(String ip) {
        byte[] ret = new byte[4];
        StringTokenizer st = new StringTokenizer(ip, ".");
        try {
            ret[0] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
            ret[1] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
            ret[2] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
            ret[3] = 0; // [!] ?0?/24ip???
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return ret;
    }

    /**
     * ???????
     * 
     * @param b
     *            
     * @param offset
     *            ???
     * @param len
     *            ??
     * @param encoding
     *            ??
     * @return encoding????
     */
    private static String getString(byte[] b, int offset, int len, String encoding) {
        try {
            return new String(b, offset, len, encoding);
        } catch (UnsupportedEncodingException e) {
            return new String(b, offset, len);
        }
    }

    private static void benchmark() throws Exception {
        String ip = "123.58.182.1";

        StopWatch sw = new StopWatch();
        int times = 100000000;
        Random rand = new Random();

        sw.start();
        for (int i = 0; i < times; i++) {
            IPSeeker ips = IPSeeker.getInstance();
            IPLocation ipl = ips.getIPLocation(String.valueOf(rand.nextInt()));
        }
        System.out.println(sw.getTime() + "ms");
        System.out.println(IPSeeker.ipCacheMap.size());

        while (true) {
            Thread.sleep(1);
        }
    }

    public static void main(String[] args) throws Exception {
        IPSeeker ips = IPSeeker.getInstance();

        String ipArray[] = { "123.58.181.1", /*
                                              * "115.236.97.158",
                                              * "182.140.134.24",
                                              * "115.236.153.148",
                                              * "114.113.197.131",
                                              */
                "115.236.153.148", "123.58.181.1", "115.236.153.148", "123.58.181.58" };
        for (String ip : ipArray) {
            IPLocation ipl = ips.getIPLocation(ip);
            System.out.println(ip + " [" + ipl.country + " " + ipl.area + "]");
        }

        System.out.println("\r\ncache: " + IPSeeker.ipCacheMap.size());
        for (Map.Entry<String, IPLocation> entry : IPSeeker.ipCacheMap.entrySet()) {
            System.out
                    .println(entry.getKey() + " [" + entry.getValue().country + " " + entry.getValue().area + "]");
        }

        // benchmark();
    }
}