Allow the URL to be configured via plugins/Caminus/config.yml

This commit is contained in:
Trever Fischer 2012-02-25 13:59:54 -05:00
parent 07278e50d7
commit 52fa187d91
2 changed files with 26 additions and 14 deletions

View File

@ -22,16 +22,24 @@ import java.io.InputStreamReader;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.HttpURLConnection;
import java.util.logging.Logger;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerListener; import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerLoginEvent;
public class JoinListener extends PlayerListener { public class JoinListener extends PlayerListener {
Logger log = Logger.getLogger("Caminus.Join");
private String m_url;
public JoinListener() { public JoinListener() {
} }
public void setURL(String url) {
m_url = url;
}
public static void main(String[] args) throws IOException, MalformedURLException { public static void main(String[] args) throws IOException, MalformedURLException {
JoinListener listener = new JoinListener(); JoinListener listener = new JoinListener();
if (listener.isUserAuthed(args[0])) if (listener.isUserAuthed(args[0]))
@ -43,8 +51,8 @@ public class JoinListener extends PlayerListener {
public void onPlayerLogin(PlayerLoginEvent event) { public void onPlayerLogin(PlayerLoginEvent event) {
Player p = event.getPlayer(); Player p = event.getPlayer();
try { try {
if (!isUserAuthed(p.getName())) if (!isUserAuthed(p.getName()))
event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "An active camin.us account is required."); event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "An active camin.us account is required.");
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "Auth URL is invalid!"); event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "Auth URL is invalid!");
} catch (IOException e) { } catch (IOException e) {
@ -53,14 +61,12 @@ public class JoinListener extends PlayerListener {
} }
public boolean isUserAuthed(String user) throws IOException, MalformedURLException { public boolean isUserAuthed(String user) throws IOException, MalformedURLException {
URL authServer = new URL("http://dev.camin.us/api/validate/"+user); URL authServer = new URL(m_url+user);
URLConnection conn = authServer.openConnection(); log.info("Authing "+user+" against "+authServer);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); HttpURLConnection conn = (HttpURLConnection)authServer.openConnection();
boolean ret; int code = conn.getResponseCode();
if (in.readLine().equals("false")) if (code >= 200 && code <= 300)
ret = false; return true;
ret = true; return false;
in.close();
return ret;
} }
} }

View File

@ -17,7 +17,7 @@ package us.camin;
along with Caminus. If not, see <http://www.gnu.org/licenses/>. along with Caminus. If not, see <http://www.gnu.org/licenses/>.
*/ */
import org.bukkit.Location; import org.bukkit.configuration.Configuration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
@ -28,6 +28,7 @@ import java.util.logging.Logger;
public class Plugin extends JavaPlugin { public class Plugin extends JavaPlugin {
Logger log = Logger.getLogger("Caminus");//Define your logger Logger log = Logger.getLogger("Caminus");//Define your logger
private JoinListener m_listener;
public void onDisable() { public void onDisable() {
log.info("[Caminus] Plugin disabled"); log.info("[Caminus] Plugin disabled");
@ -37,6 +38,11 @@ public class Plugin extends JavaPlugin {
log.info("[Caminus] Plugin enabled"); log.info("[Caminus] Plugin enabled");
PluginManager pm = this.getServer().getPluginManager(); PluginManager pm = this.getServer().getPluginManager();
pm.registerEvent(Event.Type.PLAYER_LOGIN, new JoinListener(), Event.Priority.Normal, this); m_listener = new JoinListener();
Configuration conf = getConfig();
conf.addDefault("url", "http://camin.us/api/validate/");
String url = conf.getString("url");
m_listener.setURL(url);
pm.registerEvent(Event.Type.PLAYER_LOGIN, m_listener, Event.Priority.Normal, this);
} }
} }