Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BungeeAuth Config File

Use SQLite: false
Use SQLite: true
# Set this to true if you want to use a file based database (SQLite) rather than MySQL

Host: 127.0.0.1
Expand All @@ -18,17 +18,11 @@ Password: 'pass'
DBName: Bungee
#The name of the MySQL database where BungeeAuth's Tables shall be created.

Lobby: Lobby
# The name of the lobby server.

Fallback Lobby: Lobby2
# The name of the fallback lobby server in case the main lobby is down.

AuthLobby: AuthLobby
# The name of the lobby where players are pushed before they authenticate. leave it same as normal lobby if you don't have an auth lobby.

Fallback AuthLobby: AuthLobby2
# The name of the fallback AuthLobby server in case the main AuthLobby is down.
Strict AuthLobby: true
# Only allow being in AuthLobby for sake of authentication

Ask Email: false
# Set this to True if you want the plugin to prompt users to register their email when they login to the server for the first time.
Expand All @@ -52,7 +46,7 @@ Password Tries: 5
Wrong Password Timeout: 5
# Timeout (in minutes) for players who spam commands. The timeout occurs if a player tries to use a BungeeAuth command more than 10 times. Set this to 0 to disable this feature.

Guest Server Failsafe Check: true
Guest Server Failsafe Check: false
# A failsafe method that runs every 10 seconds to check if a guest player is in AuthLobby, and if not, moves them to AuthLobby.

#---------------------------- PHP API AREA ----------------------------#
Expand Down
170 changes: 111 additions & 59 deletions src/me/vik1395/BungeeAuth/ListenerClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.ServerPing;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.config.ServerInfo;
Expand All @@ -20,6 +22,7 @@
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.event.PreLoginEvent;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.scheduler.ScheduledTask;
import net.md_5.bungee.event.EventHandler;
Expand All @@ -45,7 +48,8 @@ public class ListenerClass implements Listener
private Tables ct = new Tables();
public static HashMap<String, ScheduledTask> prelogin = new HashMap<>();
public static List<String> guest = new ArrayList<String>();

public static HashMap<String, String> sendbackto = new HashMap<>();

@EventHandler(priority = EventPriority.HIGHEST)
public void onPreLogin(PreLoginEvent ple)
{
Expand All @@ -55,16 +59,51 @@ public void onPreLogin(PreLoginEvent ple)
ple.setCancelled(true);
}
}


@EventHandler(priority = EventPriority.HIGHEST)
public void onServerConnect(ServerConnectEvent sce)
{
String servername = sce.getTarget().getName();
String status = ct.getStatus(sce.getPlayer().getName());
if (!(sce.getTarget().getName().equals(Main.authlobby)))
{
sendbackto.put(sce.getPlayer().getName(), servername);
if (!(status.equals("online")))
{
movePlayer(sce, true);
kickPlayerIfServerDead(sce.getPlayer(), sce.getTarget());
}
// check restricted server (for session login)
if(!sce.getTarget().canAccess(sce.getPlayer()))
{
sce.getPlayer().sendMessage(new ComponentBuilder(Main.error_no_server).color(ChatColor.RED).create());
movePlayer(sce, true);
}
return;
}
// if no return server, send to default or kick player
else if (Main.strict_authlobby && (status.equals("online") || !(sendbackto.containsKey(sce.getPlayer().getName()))))
{
ServerInfo defaultServer = ProxyServer.getInstance().getServerInfo(sce.getPlayer().getPendingConnection().getListener().getDefaultServer());
if (defaultServer.canAccess(sce.getPlayer())) {
sce.setTarget(defaultServer);
kickPlayerIfServerDead(sce.getPlayer(), defaultServer);
} else {
sce.getPlayer().disconnect(new TextComponent(Main.error_no_server));
}
return;
}
}


@EventHandler(priority = EventPriority.HIGHEST)
public void onLogin(PostLoginEvent ple)
{
ProxiedPlayer pl = ple.getPlayer();
boolean check = ct.checkPlayerEntry(pl.getName());

if(!check)
{
movePlayer(pl, true);
String emailCh = "";
if(Main.email)
{
Expand All @@ -73,29 +112,30 @@ public void onLogin(PostLoginEvent ple)
pl.sendMessage(new ComponentBuilder(Main.welcome_register.replace("%player%", pl.getName()).replace("%email%", emailCh)).color(ChatColor.RED).create());
startTask(pl);
}

//Checks for player entry in Database
else if(check)
{
ct.setStatus(pl.getName(), "offline");
Date lastseen = ct.getLastSeen(pl.getName());
String lastip = ct.getLastIP(pl.getName());
String currip = pl.getAddress().getHostString();

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
String sdatenow = df.format(new Date());
Date datenow = null;
try
try
{
datenow = df.parse(sdatenow);
}
catch (ParseException e)
}
catch (ParseException e)
{
e.printStackTrace();
}

long difference = datenow.getTime() - lastseen.getTime();
long diffmin = (difference/1000)/60;

//checks if player's session is still available
if(currip.equals(lastip) && diffmin<=Main.seshlength)
{
Expand All @@ -113,33 +153,32 @@ else if(check)
{
Main.plonline.remove(pl.getName());
}

movePlayer(pl, true);
pl.sendMessage(new ComponentBuilder(Main.welcome_login).color(ChatColor.RED).create());
startTask(pl);
}
}
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onChatEvent(ChatEvent event) {
ProxiedPlayer p = (ProxiedPlayer) event.getSender();
String msg = event.getMessage();
String arr[] = msg.split(" ");
String cmd = arr[0];

if(!Main.plonline.contains(p.getName()) && !cmd.equalsIgnoreCase("/login") && !cmd.equalsIgnoreCase("/register"))
{
event.setCancelled(true);
p.sendMessage(new ComponentBuilder(Main.pre_login).color(ChatColor.GRAY).create());
return;
}
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerQuit(PlayerDisconnectEvent pde)
{
ProxiedPlayer pl = pde.getPlayer();
sendbackto.remove(pl.getName());
if(Main.plonline.contains(pl.getName()))
{
Main.plonline.remove(pl.getName());
Expand All @@ -157,55 +196,49 @@ public void onPlayerQuit(PlayerDisconnectEvent pde)
}
}
}

public static void movePlayer(ProxiedPlayer pl, boolean authlobby)

public static void movePlayer(ServerConnectEvent sce, boolean authlobby) {
movePlayer(null, authlobby, sce);
}
public static void movePlayer(ProxiedPlayer pl, boolean authlobby) {
movePlayer(pl, authlobby, null);
}
public static void movePlayer(ProxiedPlayer pl, boolean authlobby, ServerConnectEvent sce)
{
if (sce!=null) { pl = sce.getPlayer(); }
ProxyServer ps = Main.plugin.getProxy();

Tables ct = new Tables();
if(authlobby)
{
ServerInfo sinf = null;
if(!(ps.getServerInfo(Main.authlobby)==null))
{
if(!(ps.getServerInfo(Main.authlobby)==null)) {
sinf = ps.getServerInfo(Main.authlobby);
pl.connect(sinf);
}
else if(!(ps.getServerInfo(Main.authlobby2)==null))
{
sinf = ps.getServerInfo(Main.authlobby2);
pl.connect(sinf);
}
else
{
moveMethod(pl, sce, sinf);
} else {
pl.disconnect(new TextComponent(Main.error_authlobby));
System.err.println("[BungeeAuth] AuthLobby and Fallback AuthLobby not found!");
return;
}

}
else
{
ServerInfo sinf = null;
if(!(ps.getServerInfo(Main.lobby)==null))
{
sinf = ps.getServerInfo(Main.lobby);
pl.connect(sinf);
}
else if(!(ps.getServerInfo(Main.lobby2)==null))
{
sinf = ps.getServerInfo(Main.lobby2);
pl.connect(sinf);
System.err.println("[BungeeAuth] AuthLobby not found!");
}
else
{
pl.sendMessage(new ComponentBuilder(Main.error_lobby).color(ChatColor.DARK_RED).create());
System.err.println("[BungeeAuth] Lobby and Fallback Lobby not found!");
return;
} else if(sendbackto.containsKey(pl.getName()) && !(ps.getServerInfo(sendbackto.get(pl.getName()))==null)) {
ServerInfo sinf = ps.getServerInfo(sendbackto.get(pl.getName()));
sendbackto.remove(pl.getName());
if(sinf.canAccess(pl)) {
moveMethod(pl, sce, sinf);
} else {
pl.sendMessage(new ComponentBuilder(Main.error_no_server).color(ChatColor.RED).create());
movePlayer(pl, true, sce);
}
}
}



public static void moveMethod(ProxiedPlayer pl, ServerConnectEvent sce, ServerInfo sinf) {
if (sce!=null) {
sce.setTarget(sinf);
} else {
pl.connect(sinf);
}
}


protected static void startTask(final ProxiedPlayer pl)
{
guest.add(pl.getName());
Expand All @@ -218,7 +251,7 @@ protected static void startTask(final ProxiedPlayer pl)
prelogin.put(pl.getName(), Main.plugin.getProxy().getScheduler().schedule(Main.plugin, new Runnable() {

@Override
public void run()
public void run()
{
if(guest.contains(pl.getName()))
{
Expand All @@ -232,10 +265,29 @@ public void run()
}
pl.disconnect(new TextComponent(Main.nologin_kick));
}

}, (long) Main.gseshlength, TimeUnit.SECONDS));
}
}


}


protected static void kickPlayerIfServerDead(final ProxiedPlayer pl, final ServerInfo sinf)
{
try {
sinf.ping(new Callback<ServerPing>() {
@Override
public void done(ServerPing ping, Throwable throwable) {
if (ping==null)
{
pl.disconnect(new TextComponent(Main.error_no_server));
}
}
});
}
catch (Exception e)
{
pl.disconnect(new TextComponent(Main.error_no_server));
}
}

}
28 changes: 8 additions & 20 deletions src/me/vik1395/BungeeAuth/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public class Main extends Plugin
public static HashMap<String, Runnable> guestserverchecker;
public static Plugin plugin;
public static int seshlength, phpport, gseshlength, entperip, errlim, pwtimeout, pwtries;
public static boolean sqlite, email, phpapi, guestfailsafe;
public static String version, authlobby, authlobby2, lobby, lobby2, host, port, dbName, username, pass, register,
public static boolean sqlite, email, phpapi, guestfailsafe, strict_authlobby;
public static String version, authlobby, host, port, dbName, username, pass, register,
reg_success, already_reg, login_success, already_in, logout_success, already_out, reset_noreg, reset_success,
no_perm, pass_change_success, wrong_pass, welcome_resume, welcome_login, welcome_register, pre_login,
error_authlobby, error_lobby, phppass, reg_limit, illegal_name, nologin_kick, allowedun, spammed_password, force_register, force_login, force_logout;
error_authlobby, error_no_server, phppass, reg_limit, illegal_name, nologin_kick, allowedun, spammed_password, force_register, force_login, force_logout;

public void onEnable()
{
Expand Down Expand Up @@ -115,10 +115,8 @@ private void loadYaml()
dbName = YamlGenerator.config.getString("DBName");
username = YamlGenerator.config.getString("Username");
pass = YamlGenerator.config.getString("Password");
lobby = YamlGenerator.config.getString("Lobby");
lobby2 = YamlGenerator.config.getString("Fallback Lobby");
authlobby = YamlGenerator.config.getString("AuthLobby");
authlobby2 = YamlGenerator.config.getString("Fallback AuthLobby");
authlobby = YamlGenerator.config.getString("AuthLobby");
strict_authlobby = YamlGenerator.config.getBoolean("Strict AuthLobby");
email = YamlGenerator.config.getBoolean("Ask Email");
seshlength = YamlGenerator.config.getInt("Session Length");
gseshlength = YamlGenerator.config.getInt("Guest Session Length");
Expand Down Expand Up @@ -152,7 +150,7 @@ private void loadYaml()
welcome_register = YamlGenerator.message.getString("welcome_register");
pre_login = YamlGenerator.message.getString("pre_login");
error_authlobby = YamlGenerator.message.getString("error_authlobby");
error_lobby = YamlGenerator.message.getString("error_lobby");
error_no_server = YamlGenerator.message.getString("error_no_server");
spammed_password = YamlGenerator.message.getString("spammed_password");
force_register = YamlGenerator.message.getString("force_register");
force_login = YamlGenerator.message.getString("force_login");
Expand All @@ -175,17 +173,7 @@ public void run()

private void checkGuestServer()
{
ServerInfo sinf = null;
if(Main.plugin.getProxy().getServerInfo(Main.authlobby)!=null)
{
sinf = Main.plugin.getProxy().getServerInfo(Main.authlobby);
}
else if(Main.plugin.getProxy().getServerInfo(Main.authlobby2)!=null)
{
sinf = Main.plugin.getProxy().getServerInfo(Main.authlobby2);
}

final ServerInfo sinfo = sinf;
final ServerInfo sinfo = Main.plugin.getProxy().getServerInfo(Main.authlobby);

Main.plugin.getProxy().getScheduler().schedule(Main.plugin, new Runnable() {

Expand All @@ -200,7 +188,7 @@ public void run()
if(sinfo==null)
{
p.disconnect(new TextComponent(Main.error_authlobby));
System.err.println("[BungeeAuth] AuthLobby and Fallback AuthLobby not found!");
System.err.println("[BungeeAuth] AuthLobby not found!");
}
else if(!p.getServer().getInfo().equals(sinfo))
{
Expand Down
Loading