mirror of
https://github.com/octoleo/plantuml-server.git
synced 2024-12-22 08:48:54 +00:00
[FEATURE] Adding GeoIP package needed for stats
This commit is contained in:
parent
7c25eb2586
commit
8799670f1d
63
src/main/java/com/maxmind/geoip/Country.java
Normal file
63
src/main/java/com/maxmind/geoip/Country.java
Normal file
@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Country.java
|
||||
*
|
||||
* Copyright (C) 2003 MaxMind LLC. All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
package com.maxmind.geoip;
|
||||
|
||||
/**
|
||||
* Represents a country.
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class Country {
|
||||
|
||||
private String code;
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Creates a new Country.
|
||||
*
|
||||
* @param code
|
||||
* the country code.
|
||||
* @param name
|
||||
* the country name.
|
||||
*/
|
||||
public Country(String code, String name) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ISO two-letter country code of this country.
|
||||
*
|
||||
* @return the country code.
|
||||
*/
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of this country.
|
||||
*
|
||||
* @return the country name.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
37
src/main/java/com/maxmind/geoip/CountryLookup.java
Normal file
37
src/main/java/com/maxmind/geoip/CountryLookup.java
Normal file
@ -0,0 +1,37 @@
|
||||
package com.maxmind.geoip;
|
||||
/* Only works with GeoIP Country Edition */
|
||||
/* For Geoip City Edition, use CityLookup.java */
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
class CountryLookup {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
String sep = System.getProperty("file.separator");
|
||||
|
||||
// Uncomment for windows
|
||||
// String dir = System.getProperty("user.dir");
|
||||
|
||||
// Uncomment for Linux
|
||||
String dir = "C:/eclipse";
|
||||
|
||||
String dbfile = dir + sep + "GeoIP.dat";
|
||||
// You should only call LookupService once, especially if you use
|
||||
// GEOIP_MEMORY_CACHE mode, since the LookupService constructor takes up
|
||||
// resources to load the GeoIP.dat file into memory
|
||||
//LookupService cl = new LookupService(dbfile,LookupService.GEOIP_STANDARD);
|
||||
LookupService cl = new LookupService(dbfile,LookupService.GEOIP_MEMORY_CACHE);
|
||||
|
||||
System.out.println(cl.getCountry("151.38.39.114").getCode());
|
||||
System.out.println(cl.getCountry("151.38.39.114").getName());
|
||||
System.out.println(cl.getCountry("12.25.205.51").getName());
|
||||
System.out.println(cl.getCountry("64.81.104.131").getName());
|
||||
System.out.println(cl.getCountry("200.21.225.82").getName());
|
||||
|
||||
cl.close();
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.out.println("IO Exception");
|
||||
}
|
||||
}
|
||||
}
|
128
src/main/java/com/maxmind/geoip/DatabaseInfo.java
Normal file
128
src/main/java/com/maxmind/geoip/DatabaseInfo.java
Normal file
@ -0,0 +1,128 @@
|
||||
/**
|
||||
* DatabaseInfo.java
|
||||
*
|
||||
* Copyright (C) 2003 MaxMind LLC. All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
package com.maxmind.geoip;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Encapsulates metadata about the GeoIP database. The database has a date, is a
|
||||
* premium or standard version, and is one of the following types:
|
||||
*
|
||||
* <ul>
|
||||
* <li>Country edition -- this is the most common version of the database. It
|
||||
* includes the name of the country and it's ISO country code given an IP
|
||||
* address.
|
||||
* <li>Region edition -- includes the country information as well as what U.S.
|
||||
* state or Canadian province the IP address is from if the IP address is from
|
||||
* the U.S. or Canada.
|
||||
* <li>City edition -- includes country, region, city, postal code, latitude,
|
||||
* and longitude information.
|
||||
* <li>Org edition -- includes netblock owner.
|
||||
* <li>ISP edition -- ISP information.
|
||||
* </ul>
|
||||
*
|
||||
* @see com.maxmind.geoip.LookupService#getDatabaseInfo()
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class DatabaseInfo {
|
||||
|
||||
public final static int COUNTRY_EDITION = 1;
|
||||
public final static int REGION_EDITION_REV0 = 7;
|
||||
public final static int REGION_EDITION_REV1 = 3;
|
||||
public final static int CITY_EDITION_REV0 = 6;
|
||||
public final static int CITY_EDITION_REV1 = 2;
|
||||
public final static int ORG_EDITION = 5;
|
||||
public final static int ISP_EDITION = 4;
|
||||
public final static int PROXY_EDITION = 8;
|
||||
public final static int ASNUM_EDITION = 9;
|
||||
public final static int NETSPEED_EDITION = 10;
|
||||
public final static int DOMAIN_EDITION = 11;
|
||||
public final static int COUNTRY_EDITION_V6 = 12;
|
||||
public final static int ASNUM_EDITION_V6 = 21;
|
||||
public final static int ISP_EDITION_V6 = 22;
|
||||
public final static int ORG_EDITION_V6 = 23;
|
||||
public final static int DOMAIN_EDITION_V6 = 24;
|
||||
public final static int CITY_EDITION_REV1_V6 = 30;
|
||||
public final static int CITY_EDITION_REV0_V6 = 31;
|
||||
public final static int NETSPEED_EDITION_REV1 = 32;
|
||||
public final static int NETSPEED_EDITION_REV1_V6 = 33;
|
||||
|
||||
private static SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
|
||||
|
||||
private String info;
|
||||
|
||||
/**
|
||||
* Creates a new DatabaseInfo object given the database info String.
|
||||
*
|
||||
* @param info
|
||||
*/
|
||||
public DatabaseInfo(String info) {
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
if (info == null || info.equals("")) {
|
||||
return COUNTRY_EDITION;
|
||||
} else {
|
||||
// Get the type code from the database info string and then
|
||||
// subtract 105 from the value to preserve compatability with
|
||||
// databases from April 2003 and earlier.
|
||||
return Integer.parseInt(info.substring(4, 7)) - 105;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the database is the premium version.
|
||||
*
|
||||
* @return true if the premium version of the database.
|
||||
*/
|
||||
public boolean isPremium() {
|
||||
return info.indexOf("FREE") < 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the date of the database.
|
||||
*
|
||||
* @return the date of the database.
|
||||
*/
|
||||
public Date getDate() {
|
||||
for (int i = 0; i < info.length() - 9; i++) {
|
||||
if (Character.isWhitespace(info.charAt(i))) {
|
||||
String dateString = info.substring(i + 1, i + 9);
|
||||
try {
|
||||
synchronized (formatter) {
|
||||
return formatter.parse(dateString);
|
||||
}
|
||||
} catch (ParseException pe) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return info;
|
||||
}
|
||||
}
|
62
src/main/java/com/maxmind/geoip/Location.java
Normal file
62
src/main/java/com/maxmind/geoip/Location.java
Normal file
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Location.java
|
||||
*
|
||||
* Copyright (C) 2004 MaxMind LLC. All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Lesser Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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 Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
package com.maxmind.geoip;
|
||||
|
||||
public class Location {
|
||||
public String countryCode;
|
||||
public String countryName;
|
||||
public String region;
|
||||
public String city;
|
||||
public String postalCode;
|
||||
public float latitude;
|
||||
public float longitude;
|
||||
public int dma_code;
|
||||
public int area_code;
|
||||
public int metro_code;
|
||||
|
||||
private final static double EARTH_DIAMETER = 2 * 6378.2;
|
||||
private final static double PI = 3.14159265;
|
||||
private final static double RAD_CONVERT = PI / 180;
|
||||
|
||||
public double distance(Location loc) {
|
||||
double delta_lat, delta_lon;
|
||||
double temp;
|
||||
|
||||
float lat1 = latitude;
|
||||
float lon1 = longitude;
|
||||
float lat2 = loc.latitude;
|
||||
float lon2 = loc.longitude;
|
||||
|
||||
// convert degrees to radians
|
||||
lat1 *= RAD_CONVERT;
|
||||
lat2 *= RAD_CONVERT;
|
||||
|
||||
// find the deltas
|
||||
delta_lat = lat2 - lat1;
|
||||
delta_lon = (lon2 - lon1) * RAD_CONVERT;
|
||||
|
||||
// Find the great circle distance
|
||||
temp = Math.pow(Math.sin(delta_lat / 2), 2) + Math.cos(lat1)
|
||||
* Math.cos(lat2) * Math.pow(Math.sin(delta_lon / 2), 2);
|
||||
return EARTH_DIAMETER
|
||||
* Math.atan2(Math.sqrt(temp), Math.sqrt(1 - temp));
|
||||
}
|
||||
}
|
1288
src/main/java/com/maxmind/geoip/LookupService.java
Normal file
1288
src/main/java/com/maxmind/geoip/LookupService.java
Normal file
File diff suppressed because it is too large
Load Diff
7
src/main/java/com/maxmind/geoip/Region.java
Normal file
7
src/main/java/com/maxmind/geoip/Region.java
Normal file
@ -0,0 +1,7 @@
|
||||
package com.maxmind.geoip;
|
||||
|
||||
public class Region {
|
||||
public String countryCode;
|
||||
public String countryName;
|
||||
public String region;
|
||||
}
|
8726
src/main/java/com/maxmind/geoip/regionName.java
Normal file
8726
src/main/java/com/maxmind/geoip/regionName.java
Normal file
File diff suppressed because it is too large
Load Diff
1489
src/main/java/com/maxmind/geoip/timeZone.java
Normal file
1489
src/main/java/com/maxmind/geoip/timeZone.java
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user