📄 lookupservice.java
字号:
file.seek(file.length() - 3); for (i = 0; i < STRUCTURE_INFO_MAX_SIZE; i++) { file.read(delim); if (delim[0] == -1 && delim[1] == -1 && delim[2] == -1) { databaseType = file.readByte(); if (databaseType >= 106) { // Backward compatibility with databases from April 2003 and earlier databaseType -= 105; } // Determine the database type. if (databaseType == DatabaseInfo.REGION_EDITION_REV0) { databaseSegments = new int[1]; databaseSegments[0] = STATE_BEGIN_REV0; recordLength = STANDARD_RECORD_LENGTH; }else if (databaseType == DatabaseInfo.REGION_EDITION_REV1){ databaseSegments = new int[1]; databaseSegments[0] = STATE_BEGIN_REV1; recordLength = STANDARD_RECORD_LENGTH; } else if (databaseType == DatabaseInfo.CITY_EDITION_REV0 || databaseType == DatabaseInfo.CITY_EDITION_REV1 || databaseType == DatabaseInfo.ORG_EDITION || databaseType == DatabaseInfo.ISP_EDITION || databaseType == DatabaseInfo.ASNUM_EDITION) { databaseSegments = new int[1]; databaseSegments[0] = 0; if (databaseType == DatabaseInfo.CITY_EDITION_REV0 || databaseType == DatabaseInfo.CITY_EDITION_REV1 || databaseType == DatabaseInfo.ASNUM_EDITION) { recordLength = STANDARD_RECORD_LENGTH; } else { recordLength = ORG_RECORD_LENGTH; } file.read(buf); for (j = 0; j < SEGMENT_RECORD_LENGTH; j++) { databaseSegments[0] += (unsignedByteToInt(buf[j]) << (j * 8)); } } break; } else { file.seek(file.getFilePointer() - 4); } } if ((databaseType == DatabaseInfo.COUNTRY_EDITION) | (databaseType == DatabaseInfo.PROXY_EDITION) | (databaseType == DatabaseInfo.NETSPEED_EDITION)) { databaseSegments = new int[1]; databaseSegments[0] = COUNTRY_BEGIN; recordLength = STANDARD_RECORD_LENGTH; } if ((dboptions & GEOIP_MEMORY_CACHE) == 1) { int l = (int) file.length(); dbbuffer = new byte[l]; file.seek(0); file.read(dbbuffer,0,l); databaseInfo = this.getDatabaseInfo(); file.close(); } if ((dboptions & GEOIP_INDEX_CACHE) != 0) { int l = databaseSegments[0] * recordLength * 2; index_cache = new byte[l]; if (index_cache != null){ file.seek(0); file.read(index_cache,0,l); } } else { index_cache = null; } } /** * Closes the lookup service. */ public void close() { try { if (file != null){ file.close(); } file = null; } catch (Exception e) { } } /** * Returns the country the IP address is in. * * @param ipAddress String version of an IP address, i.e. "127.0.0.1" * @return the country the IP address is from. */ public Country getCountry(String ipAddress) { InetAddress addr; try { addr = InetAddress.getByName(ipAddress); } catch (UnknownHostException e) { return UNKNOWN_COUNTRY; } return getCountry(bytesToLong(addr.getAddress())); } /** * Returns the country the IP address is in. * * @param ipAddress the IP address. * @return the country the IP address is from. */ public Country getCountry(InetAddress ipAddress) { return getCountry(bytesToLong(ipAddress.getAddress())); } /** * Returns the country the IP address is in. * * @param ipAddress the IP address in long format. * @return the country the IP address is from. */ public Country getCountry(long ipAddress) { if (file == null && (dboptions & GEOIP_MEMORY_CACHE) == 0) { throw new IllegalStateException("Database has been closed."); } int ret = seekCountry(ipAddress) - COUNTRY_BEGIN; if (ret == 0) { return UNKNOWN_COUNTRY; } else { return new Country(countryCode[ret], countryName[ret]); } } public int getID(String ipAddress) { InetAddress addr; try { addr = InetAddress.getByName(ipAddress); } catch (UnknownHostException e) { return 0; } return getID(bytesToLong(addr.getAddress())); } public int getID(InetAddress ipAddress) { return getID(bytesToLong(ipAddress.getAddress())); } public int getID(long ipAddress) { if (file == null && (dboptions & GEOIP_MEMORY_CACHE) == 0) { throw new IllegalStateException("Database has been closed."); } int ret = seekCountry(ipAddress) - databaseSegments[0]; return ret; } /** * Returns information about the database. * * @return database info. */ public DatabaseInfo getDatabaseInfo() { if (databaseInfo != null) { return databaseInfo; } try { // Synchronize since we're accessing the database file. synchronized (this) { _check_mtime(); boolean hasStructureInfo = false; byte [] delim = new byte[3]; // Advance to part of file where database info is stored. file.seek(file.length() - 3); for (int i=0; i<STRUCTURE_INFO_MAX_SIZE; i++) { file.read(delim); if (delim[0] == 255 && delim[1] == 255 && delim[2] == 255) { hasStructureInfo = true; break; } } if (hasStructureInfo) { file.seek(file.getFilePointer() - 3); } else { // No structure info, must be pre Sep 2002 database, go back to end. file.seek(file.length() - 3); } // Find the database info string. for (int i=0; i<DATABASE_INFO_MAX_SIZE; i++) { file.read(delim); if (delim[0]==0 && delim[1]==0 && delim[2]==0) { byte[] dbInfo = new byte[i]; file.read(dbInfo); // Create the database info object using the string. this.databaseInfo = new DatabaseInfo(new String(dbInfo)); return databaseInfo; } file.seek(file.getFilePointer() -4); } } } catch (Exception e) { e.printStackTrace(); } return new DatabaseInfo(""); } synchronized void _check_mtime(){ try { if ((dboptions & GEOIP_CHECK_CACHE) != 0){ long t = databaseFile.lastModified(); if (t != mtime){ /* GeoIP Database file updated */ /* refresh filehandle */ file.close(); file = new RandomAccessFile(databaseFile,"r"); databaseInfo = null; init(); } } } catch (IOException e) { System.out.println("file not found"); } } // for GeoIP City only public Location getLocation(InetAddress addr) { return getLocation(bytesToLong(addr.getAddress())); } // for GeoIP City only public Location getLocation(String str) { if (dnsService == 0) { InetAddress addr; try { addr = InetAddress.getByName(str); } catch (UnknownHostException e) { return null; } return getLocation(addr); } else { String str2 = getDnsAttributes(str); return getLocationwithdnsservice(str2); // TODO if DNS is not available, go to local file as backup } } String getDnsAttributes(String ip) { try { Hashtable env = new Hashtable(); env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); // TODO don't specify ws1, instead use ns servers for s.maxmind.com env.put("java.naming.provider.url","dns://ws1.maxmind.com/"); DirContext ictx = new InitialDirContext(env); Attributes attrs = ictx.getAttributes(licenseKey + "." + ip + ".s.maxmind.com", new String[] {"txt"}); //System.out.println(attrs.get("txt").get()); String str = attrs.get("txt").get().toString(); return str; } catch(NamingException e) { // TODO fix this to handle exceptions System.out.println("DNS error"); return null; } } public Location getLocationwithdnsservice(String str) { Location record = new Location(); String key; String value; StringTokenizer st = new StringTokenizer(str,";=\""); while (st.hasMoreTokens()) { key = st.nextToken(); if (st.hasMoreTokens()) { value = st.nextToken(); } else { value = "";} if (key.equals("co")) { Integer i = (Integer)hashmapcountryCodetoindex.get(value); record.countryCode = value; record.countryName = countryName[i.intValue()]; } if (key.equals("ci")) { record.city = value; } if (key.equals("re")) { record.region = value; } if (key.equals("zi")) { record.postalCode = value; } // TODO, ISP and Organization //if (key.equals("or")) { //record.org = value; //} //if (key.equals("is")) { //record.isp = value; //} if (key.equals("la")) { try{ record.latitude = Float.parseFloat(value); } catch(NumberFormatException e) { record.latitude = 0; } } if (key.equals("lo")) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -