1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
package com.earth2me.essentials.geoip;
import com.earth2me.essentials.EssentialsConf;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.api.IReload;
import com.earth2me.essentials.api.IEssentials;
import com.earth2me.essentials.api.IUser;
import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName;
import java.io.*;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerListener;
public class EssentialsGeoIPPlayerListener extends PlayerListener implements IReload
{
LookupService ls = null;
private static final Logger logger = Logger.getLogger("Minecraft");
File databaseFile;
File dataFolder;
EssentialsConf config;
private final transient IEssentials ess;
public EssentialsGeoIPPlayerListener(File dataFolder, IEssentials ess)
{
this.ess = ess;
this.dataFolder = dataFolder;
this.config = new EssentialsConf(new File(dataFolder, "config.yml"));
config.setTemplateName("/config.yml", EssentialsGeoIP.class);
onReload();
}
@Override
public void onPlayerJoin(PlayerJoinEvent event)
{
IUser u = ess.getUser(event.getPlayer());
if (u.isAuthorized("essentials.geoip.hide"))
{
return;
}
InetAddress address = event.getPlayer().getAddress().getAddress();
StringBuilder sb = new StringBuilder();
if (config.getBoolean("database.show-cities", false))
{
Location loc = ls.getLocation(address);
if (loc == null)
{
return;
}
if (loc.city != null)
{
sb.append(loc.city).append(", ");
}
String region = regionName.regionNameByCode(loc.countryCode, loc.region);
if (region != null)
{
sb.append(region).append(", ");
}
sb.append(loc.countryName);
}
else
{
sb.append(ls.getCountry(address).getName());
}
if (config.getBoolean("show-on-whois", true))
{
u.acquireWriteLock();
try
{
u.getData().setGeolocation(sb.toString());
}
finally
{
u.unlock();
}
}
if (config.getBoolean("show-on-login", true) && !u.isHidden())
{
for (Player player : event.getPlayer().getServer().getOnlinePlayers())
{
IUser user = ess.getUser(player);
if (user.isAuthorized("essentials.geoip.show"))
{
user.sendMessage(_("geoipJoinFormat", u.getDisplayName(), sb.toString()));
}
}
}
}
@Override
public final void onReload()
{
config.load();
if (config.getBoolean("database.show-cities", false))
{
databaseFile = new File(dataFolder, "GeoIPCity.dat");
}
else
{
databaseFile = new File(dataFolder, "GeoIP.dat");
}
if (!databaseFile.exists())
{
if (config.getBoolean("database.download-if-missing", true))
{
downloadDatabase();
}
else
{
logger.log(Level.SEVERE, _("cantFindGeoIpDB"));
return;
}
}
try
{
ls = new LookupService(databaseFile);
}
catch (IOException ex)
{
logger.log(Level.SEVERE, _("cantReadGeoIpDB"), ex);
}
}
private void downloadDatabase()
{
try
{
String url;
if (config.getBoolean("database.show-cities", false))
{
url = config.getString("database.download-url-city");
}
else
{
url = config.getString("database.download-url");
}
if (url == null || url.isEmpty())
{
logger.log(Level.SEVERE, _("geoIpUrlEmpty"));
return;
}
logger.log(Level.INFO, _("downloadingGeoIp"));
URL downloadUrl = new URL(url);
URLConnection conn = downloadUrl.openConnection();
conn.setConnectTimeout(10000);
conn.connect();
InputStream input = conn.getInputStream();
if (url.endsWith(".gz"))
{
input = new GZIPInputStream(input);
}
OutputStream output = new FileOutputStream(databaseFile);
byte[] buffer = new byte[2048];
int length = input.read(buffer);
while (length >= 0)
{
output.write(buffer, 0, length);
length = input.read(buffer);
}
output.close();
input.close();
}
catch (MalformedURLException ex)
{
logger.log(Level.SEVERE, _("geoIpUrlInvalid"), ex);
return;
}
catch (IOException ex)
{
logger.log(Level.SEVERE, _("connectionFailed"), ex);
}
}
}
|