summaryrefslogtreecommitdiffstats
path: root/EssentialsServerlist/src/net/mcserverlist/bukkit/Whitelist.java
blob: 96a96ecbcee88b9725b5addb904b3fc7e6ad0d39 (plain)
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
package net.mcserverlist.bukkit;

import com.earth2me.essentials.Essentials;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Server;


public class Whitelist
{
	private static final Logger logger = Logger.getLogger("Minecraft");
	private List<String> allowed = new ArrayList<String>();
	private final Object allowedLock = new Object();
	private Server server;

	public Whitelist(Server server)
	{
		this.server = server;
	}

	public void update()
	{
		Thread thread = new Thread(new UpdateRunnable());
		thread.setDaemon(true);
		thread.start();
	}

	public boolean isAllowed(String player)
	{
		String p = player.toLowerCase();
		synchronized (allowedLock)
		{
			return allowed.contains(p);
		}
	}

	private class UpdateRunnable implements Runnable
	{
		@SuppressWarnings("CallToThreadDumpStack")
		public void run()
		{
			// Check that we aren't receiving an event inappropriately
			if (Essentials.getSettings().getMcslKey() == null || Essentials.getSettings().getMcslKey().equals("")) return;

			try
			{
				OutputStreamWriter tx = null;
				BufferedReader rx = null;
				try
				{
					// Send GET request
					URL url = new URL("http://mcserverlist.net/api/whitelist");
					// Swap line for testing purposes
					//URL url = new URL("http://localhost/mcsl/whitelist.php");
					HttpURLConnection http = (HttpURLConnection)url.openConnection();
					http.setRequestMethod("POST");
					http.setUseCaches(false);
					http.setConnectTimeout(1000);
					http.setAllowUserInteraction(false);
					http.setInstanceFollowRedirects(true);
					http.setRequestProperty("User-Agent", "Java;Mcsl");
					http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
					http.setRequestProperty("X-Mcsl-Key", Essentials.getSettings().getMcslKey());
					http.setRequestProperty("X-Minecraft-Name", URLEncoder.encode(server.getName(), "UTF-8"));
					http.setRequestProperty("X-Minecraft-Version", server.getVersion());
					http.setDoInput(true);
					http.setDoOutput(false);

					// Get the HTTP response
					rx = new BufferedReader(new InputStreamReader(http.getInputStream()));
					List<String> allowed = new ArrayList<String>();
					for (String l = ""; rx.ready(); l = rx.readLine())
					{
						if ("".equals(l)) continue;
						else if (l.startsWith("i:")) logger.info(l.substring(2));
						else if (l.startsWith("w:")) logger.warning(l.substring(2));
						else allowed.add(l.toLowerCase()); // Add to whitelist
					}
					
					synchronized (Whitelist.this.allowedLock)
					{
						Whitelist.this.allowed = allowed;
						allowed = null; // Remove our reference so that we don't accidentally use it
					}
				}
				finally
				{
					if (tx != null) tx.close();
					if (rx != null) rx.close();
				}
			}
			catch (Exception ex)
			{
				logger.log(Level.WARNING, "Error communication with MCServerlist.", ex);
				ex.printStackTrace();
			}
		}
	}
}